forked from bdaloukas/moodle-mod_game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporthtml_snakes.php
541 lines (451 loc) · 15.9 KB
/
exporthtml_snakes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This page export the game snakes to html
*
* @author bdaloukas
* @version $Id: exporthtml_snakes.php,v 1.4 2011/07/23 12:34:08 bdaloukas Exp $
* @package game
**/
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $html->title;?></title>
<link href="css/game.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="css/subModal.css" />
<script type="text/javascript" src="js/common.js"></script>
<script type="text/javascript" src="js/subModal.js"></script>
<style type="text/css">
#pawn
{
position:absolute;
}
img
{
border:hidden
}
body
{
background: #999 url('images/backdropJungle.png') no-repeat fixed left top;
}
.score
{color: #FC3;
font-size: 40px;
}
</style>
</head>
<body>
<script language="JavaScript">
// Snakes for Moodle by Maria Rigkou.
var boards = 1;
var board_images = new Array( boards);
var board_names = new Array( boards);
var pawn_width = new Array(boards);
var pawn_height = new Array(boards);
var board_cols = new Array(boards);
var board_rows = new Array(boards);
var board_contents = new Array (boards);
var board_headerx = new Array(boards);
var board_headery = new Array(boards);
var board_footerx = new Array(boards);
var board_footery = new Array(boards);
var board_width = new Array(boards);
var board_height = new Array(boards);
var board_data = new Array(boards);
var pawn_width = new Array(boards);
var pawn_height = new Array(boards);
var current_board = 0;
var current_position=0;
var current_quest = 0;
var mchoice_count = 0;
var mchoice_positions =new Array( 1);
var quest_text = ""; //Question
var quest_resp = ""; // Answer
var quest_feedb = ""; // feedback
var quest_total = 25; // Count of questions
board_names[ 0] = "<?php echo $game->name; ?>";
board_images[ 0] = '<?php echo $board->fileboard; ?>';
board_cols [0] = <?php echo $board->cols; ?>;
board_rows [0] = <?php echo $board->rows; ?>;
board_contents [0] = '<?php echo $board->data; ?>';
board_headerx [0] = <?php echo $board->headerx; ?>;
board_headery [0] = <?php echo $board->headery; ?>;
board_footerx [0] = <?php echo $board->footerx; ?>;
board_footery [0] = <?php echo $board->footery; ?>;
board_width [0] = <?php echo $board->width; ?>;
board_height [0] = <?php echo $board->height; ?>;
pawn_width [0] = 40;
pawn_height [0]= 40;
<?php
echo "var countofquestionsM=$countofquestionsm;\r\n";
echo 'var countofquestionsS='.count($questionss).";\r\n";
foreach ($questionss as $line) {
$s = $line->question.'#'.str_replace( array( '"', '#'), array( "'", ' '), $line->answer);
if ($questionsm != '') {
$questionsm .= ",\r";
}
$questionsm .= '"'.base64_encode( game_upper( $s)).'"';
$s = '#'.str_replace( array( '"', '#'), array( "'", ' '), $line->feedback);
if ($retfeedback != '') {
$retfeedback .= ",\r";
}
$retfeedback .= '"'.base64_encode( $s).'"';
}
$rettimesasked = '';
for ($i = 0; $i < $countofquestionsm + count($questionss); $i++) {
$rettimesasked .= ',0';
}
$rettimesasked = substr( $rettimesasked, 1);
echo "var questions=new Array( $questionsm);\r\n";
echo "var feedbacks=new Array( $retfeedback);\r\n";
echo "var quest_times_asked=new Array( $rettimesasked); //How many times is used a question\r\n";
?>
var current_dice=0;
var feedb_correct_S = "<?php print_string( 'html_snakes_correct', 'game'); ?>";
var feedb_wrong_S = "<?php print_string( 'html_snakes_wrong', 'game'); ?>";
var str_score = "<?php print_string( 'score', 'game'); ?>";
var str_check = "<?php print_string( 'html_snakes_check', 'game'); ?>";
var str_no_selection = "<?php print_string( 'html_snakes_no_selection', 'game'); ?>";
var correct_ans = 0; //counter of correct answers
var all_ans = 0; //counter of all answers
var score = 0;
ShowMainForm();
display_quest();
function ShowMainForm()
{
var pawn_x =0;
var pawn_y=0;
var direction=0;
var cols = board_cols[current_board];
var rows = board_rows[current_board];
var col_width = (board_width[current_board]-board_headerx[current_board]-board_footerx[current_board])/cols;
var col_height = (board_height[current_board]-board_headery[current_board]-board_footery[current_board])/rows;
document.write('<img id="boardimage" src="images/meter.png">');
document.getElementById("boardimage").src = "images/" + board_images[ current_board];
document.write('<div id="dicecont"> </div>');
if( current_position >= 0)
{
direction=Math.floor((current_position /cols))%2;
if (direction==1)
axis_x=(cols-(current_position %cols)-1);
else
axis_x=current_position %cols;
axis_y=Math.floor((current_position /rows));
pawn_x=board_headerx[current_board]+(axis_x*col_width)+(col_width-pawn_width[current_board])/2;
pawn_y=board_footery[current_board]+pawn_height[current_board]+(axis_y*col_height)+(col_height-pawn_height[current_board])/2;
document.write('<div id="pawn1"><img id="pawn" alt="" src="images/player1.png"></div>');
move_pawn();
}
}
function selectBoard()
{
current_board = document.getElementById("boardtype").value;
document.getElementById("boardimage").src = "images/" + board_images[ current_board];
}
function select_quest()
{
var quest_total = countofquestionsM + countofquestionsS;
var quest_candidates= new Array();
var i, q;
for (i=0;i<3;i++) {
quest_candidates[i]=Math.floor((Math.random() * quest_total));
}
current_quest = quest_candidates[0];
for (i=1;i<3;i++) {
if (quest_times_asked[quest_candidates[i]]>quest_times_asked[current_quest])
current_quest=quest_candidates[i];
}
q=Base64decode( questions[ current_quest]);
quest_resp = decode_multiple_choice( q);
quest_text = quest_resp[ 0];
quest_feedb = Base64decode( feedbacks[ current_quest]);
}
function IsMultipleChoiceQuestion()
{
return (current_quest < countofquestionsM);
}
function check_answer()
{
all_ans=all_ans+1;
if( IsMultipleChoiceQuestion())
check_answer_M();
else
check_answer_S();
move_pawn();
}
function check_answer_M()
{
document.getElementById("check_btn").style.display = "none";
var useranswer;
var n=document.snakesform.radio_answer.length;
for(useranswer=0;useranswer < n;useranswer++)
{
if( document.snakesform.radio_answer[ useranswer].checked)
break;
}
if( useranswer >= n)
{
alert( str_no_selection);
document.getElementById("feedb").innerHTML= "";
document.getElementById("check_btn").style.display = "block";
return;
}
var feedbacks = decode_multiple_choice(quest_feedb);
var j;
for (j=0;j<n;j++) {
document.snakesform.radio_answer[ j].disabled = "true";
}
document.getElementById("feedb").innerHTML= feedbacks[ mchoice_positions[useranswer]];
document.getElementById("feedb").style.display = "block";
if ( mchoice_positions[ useranswer] == 1)
{
current_position += current_dice;
correct_ans =correct_ans+1; //calculate new score----
score = Math.round((correct_ans/all_ans)*100);
document.getElementById("show_score").innerHTML='<strong>'+str_score+': </strong><strong class="score">' +score+ '</strong>';
check_game_over();
check_exists_ladder();
}
else
{
score = Math.round((correct_ans/all_ans)*100);
document.getElementById("show_score").innerHTML='<strong>'+str_score+': </strong><strong class="score">' +score+ '</strong>';
check_exists_snake();
}
document.getElementById("OK_btn").style.display = "block";
}
function check_answer_S()
{
document.getElementById("answer").disabled = "true";
document.getElementById("check_btn").style.display = "none";
if (document.getElementById("answer").value.toUpperCase() == quest_resp[ 1].toUpperCase())
{
document.getElementById("feedb").style.display = "block";
current_position += current_dice;
correct_ans =correct_ans+1; //calculate new score
score = Math.round((correct_ans/all_ans)*100);
document.getElementById("show_score").innerHTML='<strong>'+str_score+': </strong><strong class="score">' +score+ '</strong>';
check_game_over();
check_exists_ladder();
}
else
{
document.getElementById("feedb_wrong").style.display = "block";
score = Math.round((correct_ans/all_ans)*100);
document.getElementById("show_score").innerHTML='<strong>'+str_score+': </strong><strong class="score">' +score+ '</strong>';
check_exists_snake();
}
document.getElementById("OK_btn").style.display = "block";
}
function check_game_over()
{
var out=(board_cols[current_board]*board_rows[current_board]);
if (current_position > out-1)
{
current_position=out-1;
showPopWin('modalContent.html', 350, 220, returnRefresh); // modal
}
}
function check_exists_ladder()
{
var find = "L" + (current_position+1) + "-";
var pos = board_contents[ current_board].indexOf( find);
if( pos < 0)
return;
var s = board_contents[ current_board].substr( pos+find.length)
pos = s.indexOf( ',');
if( pos >= 0)
s = s.substr( 0, pos);
current_position = s-1;
}
function check_exists_snake()
{
var find = "-" + (current_position+1) + ",";
var s= ',' +board_contents[ current_board] + ',';
for(;;)
{
var pos = s.indexOf( find);
if( pos < 0)
return;
var pos_start = s.lastIndexOf( ',', pos-1);
var kind = s.substr( pos_start+1, 1);
if( kind != "S")
{
s = s.substr( pos+1);
continue;
}
s = s.substr( pos_start+2);
pos = s.indexOf( '-');
current_position = s.substr( 0, pos)-1;
break;
}
}
function decode_multiple_choice(s)
{
var ret = new Array();
var i=0;
for(;;)
{
var pos=s.indexOf( '#');
if( pos < 0)
{
ret[ i++] = s;
return ret;
}
ret[ i++] = s.substr( 0, pos);
s = s.substr( pos+1);
}
}
function display_quest()
{
current_dice = Math.floor((Math.random() * 6)) + 1;
select_quest();
if( IsMultipleChoiceQuestion())
display_quest_M();
else
display_quest_S();
}
function display_quest_M()
{
s = '<table width="250px"><tr><td><div id="show_dice"> ';
s = s + '<img src = "images/dice' + current_dice + '.png"> </div> </td>';
s = s + '<td align=right><div id="show_score" style="color: #FFFFFF; font-weight:bold; font-size: 20px;">';
s = s + '<strong>'+str_score+': </strong>';
s = s + '<strong class="score">' +score+ '</strong></div></td></tr></table>';
s = s + '<div id="question_area">' + quest_text+'</div>';
s = s + '<form name="snakesform">';
mchoice_count = quest_resp.length-1;
mchoice_positions = new Array( mchoice_count);
for(i=0; i < mchoice_count ; i++)
mchoice_positions[ i] = i+1;
for(i=0; i < mchoice_count ; i++)
{
var j = Math.floor((Math.random() * mchoice_count));
var temp = mchoice_positions[ i];
mchoice_positions[ i] = mchoice_positions[ j];
mchoice_positions[ j] = temp;
}
for(i=0; i < mchoice_count;i++) {
s = s + '<input type="radio" name="radio_answer" id="radio_answer" value="';
s = s + i+'" />'+quest_resp[ mchoice_positions[ i]] + '<br />';
}
s = s + '<br /><input type="button" id="check_btn" value="'+str_check;
s = s + '" onclick="check_answer();"> <br/><div id="feedb_area"> <div id="feedb_wrong" style="display:none; color:yellow;"> ';
s = s + quest_feedb+' </div> <br /><div id="feedb" style="display:none; color:yellow;"> ';
s = s + quest_feedb+'. Θα προχωρήσεις ';
s = s + current_dice+' τετράγωνα μπροστά!</div><br /> <div id="OK_btn"';
s = s + 'style="display:none;"><input type="button" onclick="display_quest();" value="OK"/></div> </div></form>';
document.getElementById("dicecont").innerHTML = s;
document.getElementById("question_area").style.display = "block";
document.getElementById("check_btn").style.display = "block";
}
function display_quest_S()
{
var s = "";
s = '<table width="250px"><tr><td><div id="show_dice"> <img src = "images/dice';
s = s + current_dice + '.png"> </div> </td><td align=right>';
s = s + '<div id="show_score" style="color: #FFFFFF; font-weight:bold; font-size: 20px;"><strong>';
s = s + str_score+': </strong><strong class="score">' +score+ '</strong></div></td></tr></table><div id="question_area">';
s = s + quest_text+'</div> <br /><input type="text" id="answer"/><br /><br /> <input type="button" id="check_btn" value="';
s = s + str_check+'" onclick="check_answer();"> <br /> <div id="feedb_area">';
s = s + ' <div id="feedb_wrong" style="display:none; color:yellow;"> ';
s = s + feedb_wrong_S+' </div> <div id="feedb" style="display:none; color:yellow;"> ';
s = s + feedb_correct_S+'</div> <br /><div id="OK_btn" style="display:none;">';
s = s + '<input type="button" onclick="display_quest();" value="OK"/></div> </div>';
document.getElementById("dicecont").innerHTML = s;
document.getElementById("question_area").style.display = "block";
document.getElementById("check_btn").style.display = "block";
}
function move_pawn()
{
var pawn_x =0;
var pawn_y=0;
var direction=0;
var cols = board_cols[current_board];
var rows = board_rows[current_board];
var col_width = (board_width[current_board]-board_headerx[current_board]-board_footerx[current_board])/cols;
var col_height = (board_height[current_board]-board_headery[current_board]-board_footery[current_board])/rows;
if( current_position >= 0)
{
direction=Math.floor((current_position /cols))%2;
if (direction == 1) {
axis_x=(cols-(current_position %cols)-1);
}else
axis_x=current_position %cols;
axis_y=Math.floor((current_position /rows));
pawn_x=board_headerx[current_board]+(axis_x*col_width)+(col_width-pawn_width[current_board])/2;
pawn_y=board_footery[current_board]+pawn_height[current_board]+(axis_y*col_height)+(col_height-pawn_height[current_board])/2;
document.getElementById("pawn1").style.position='relative';
document.getElementById("pawn1").style.left=pawn_x+'px';
document.getElementById("pawn1").style.bottom=pawn_y+'px';
}
}
function Base64decode(input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var keyStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64_utf8_decode(output);
return output;
};
// private method for UTF-8 decoding
function Base64_utf8_decode(utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
</script>
</body>
</html>