-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
445 lines (385 loc) · 13.3 KB
/
index.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
<?
include('base.php');
?>
<html>
<head>
<?
if (!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) {
?>
<title>Jeopardy Game Generator</title>
<link rel="stylesheet" type="text/css" href="style/main.css"></link>
<?
}
elseif (!empty($_POST['username']) && !empty($_POST['password'])) {
?>
<title>Jeopardy Game Generator - Logging In...</title>
<link rel="stylesheet" type="text/css" href="style/login.css"></link>
<?
}
else {
?>
<title>Jeopardy Game Generator - Log In</title>
<link rel="stylesheet" type="text/css" href="style/login.css"></link>
<?
}
?>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<?
//TODO: Include a statistics panel on the side
include('simplehtmldom_1_5/simple_html_dom.php');
include('j-utils/clueRetrieval.php');
$NUM_CATEGORIES = 6;
$NUM_CLUES = 5;
$defaultGameID = "3752"; //Default game ID
$defaultRound = "J"; //Jeopardy round
//Get the game ID if specified
$gameID = $defaultGameID;
if (!(!isset($_GET["id"]) || $_GET["id"] === "" || $_GET["id"] === false || $_GET["id"] === null || empty($_GET["id"]))) { //note the short circuit
$gameID = htmlspecialchars($_GET["id"]);
}
//Get the round if specified (either "J" for Jeopardy or "DJ" for Double Jeopardy)
$round = $defaultRound;
if (!(!isset($_GET["round"]) || $_GET["round"] === "" || $_GET["round"] === false || $_GET["round"] === null || empty($_GET["round"]))) { //note the short circuit
$round = strtoupper(htmlspecialchars($_GET["round"]));
if (strcasecmp($round, "J") != 0 && strcasecmp($round, "DJ") != 0) {
$round = $defaultRound;
}
}
$html = file_get_html('http://www.j-archive.com/showgame.php?game_id='.$gameID);
//Determine if this is the error page (i.e., not in J!Archive database)
//If so, use default game
if (isErrorPage($html) || gameUnavailable($html, $round)) {
$round = $defaultRound;
$gameID = $defaultGameID;
$html = file_get_html('http://www.j-archive.com/showgame.php?game_id='.$defaultGameID);
}
//Get the answers (i.e., "questions" in Jeopardy terms)
$answersStr = getAnswersAsHtmlString($html, $round);
$answers = array();
//Get the questions (i.e., "answers" in Jeopardy terms) and categories
$questions = getQuestions($html, $round);
$categories = getCategories($html, $round);
$answersHtml = str_get_html($answersStr);
$indexSoFar = 0;
foreach($answersHtml->find('em') as $element) {
//Make sure the questions and answers are aligned
//Blank question should correspond to blank answer
while ($indexSoFar < $NUM_CLUES*$NUM_CATEGORIES && $questions[$indexSoFar] == "") {
array_push($answers, "");
$indexSoFar++;
}
//Strip any extraneous tags in the EM's inner text
$newText = $text = preg_replace('#</?[a-b]*[^>]*>#is', '', $element->innertext);
//echo stripslashes($newText) . '<br>';
//TODO: Design smarter way to accept answers
//Note: A conservative way to accept answers is to simply ensure the typed answer is a substring of the correct answer. You can try to look for other heuristics.
//Strip leading and trailing whitespace and double quote characters
$newText = trim($newText, " '\"");
$newText = addcslashes($newText, '"');
//Store all answers in an array
//Assumption: Questions and answers listed in same order in HTML code of J-Archive
array_push($answers, $newText);
$indexSoFar++;
}
//Pad the end of $answers array if necessary, with blank strings
//This code inherently assumes that since the foreach loop terminated at this point,
//the rest of the questions must have been unrevealed (i.e., the rest of the clues in the
//$questions array must be blank)
while ($indexSoFar < $NUM_CLUES*$NUM_CATEGORIES) {
array_push($answers, "");
$indexSoFar++;
}
?>
<script type="text/javascript" src="js/answerChecker.js"></script>
<script type="text/javascript" src="js/game.js"></script>
<script type="text/javascript">
var clues = new Array();
var NUM_CATEGORIES = <? echo $NUM_CATEGORIES ?>;
var NUM_CLUES = <? echo $NUM_CLUES ?>;
var ROUND = <? echo "'{$round}'" ?>;
var gameInProgress = false;
var valueChosen = 0;
var expectedAnswer = "";
var score = 0;
var numCluesRevealed = 0;
var actualNumberOfClues = 0;
var currentClue = -1;
var currentCat = -1;
function setUpClues() {
for (var i = 0; i < NUM_CLUES; i++) {
clues[i] = new Array();
<?
$qCounter = 0;
for ($i = 0; $i < $NUM_CLUES; $i++) {
if ($i != 0) {
echo "\t\t\t\t\t";
}
echo "if (i == ".$i.") {\n";
for ($j = 0; $j < $NUM_CATEGORIES; $j++) {
echo "\t\t\t\t\t\tclues[i][$j] = [\"".addcslashes(strtoupper($questions[$qCounter]), '"')."\", \"What is ".$answers[$qCounter]."\", null];\n";
$qCounter++;
}
echo "\t\t\t\t\t}\n";
}
?>
}
}
function centerPage() {
var pageWidth = window.innerWidth;
var allWrapperWidth = document.getElementById("allWrapper").offsetWidth;
if (pageWidth > allWrapperWidth) {
var marginWidth = (pageWidth - allWrapperWidth) / 2;
document.getElementById("allWrapper").style.marginLeft = marginWidth + "px";
document.getElementById("allWrapper").style.marginRight = marginWidth + "px";
}
}
function adjustClueBoardSize() {
var clueBoard = document.getElementById("clueBoard");
var widthOfClueBoxes = document.getElementById("board").offsetWidth;
clueBoard.style.width = widthOfClueBoxes + "px";
}
function fixStatsMenu() {
$(window).scroll(function(){
$('#mainUserStats').css('left', -$(window).scrollLeft());
});
}
function gotoGame() {
var gameId = document.getElementById("game-id-choice").value;
if (gameId == null || gameId == "" || isNaN(gameId)) {
alert("You must enter a valid, numerical game ID!");
return;
}
var roundElem = document.getElementById("game-round-choice");
var round = roundElem.options[roundElem.selectedIndex].value;
if (round == null || (round != "J" && round != "DJ")) {
alert("Round must be valid!");
return;
}
window.location.href = "index.php?id=" + gameId + "&round=" + round;
}
</script>
</head>
<?
if (!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) {
?>
<body onload="centerPage();setUpClues();adjustClueBoardSize();fixStatsMenu();">
<div id="mainUserStats">
<div id="greeting">Welcome, <? echo $_SESSION['Username']; ?>!</div>
<div id="game-identifier">
You are currently playing the
<?
if (strcasecmp($round, "DJ") == 0) {
echo "<span id=\"game-round\">Double Jeopardy</span>";
}
else {
echo "<span id=\"game-round\">Jeopardy</span>";
}
?>
round of Game #<span id="game-id"><? echo $gameID ?></span>,
</div>
<div id="game_selection">
<div id="choose-game-id">
ID:
<input type="text" id="game-id-choice"></input>
</div>
<div id="choose-game-round">
Round:
<select id="game-round-choice">
<option value="J">Jeopardy</option>
<option value="DJ">Double Jeopardy</option>
</select>
</div>
<button id="game-choice-btn" onclick="gotoGame();">Go to Game</button>
</div>
<div id="logout_wrapper">
<a href="logout.php">Logout</a>
</div>
</div>
<div id="allWrapper">
<div id="mainWrapper">
<div id="controls">
<button onclick="beginGame();" id="startButton">START</button>
<h3>SCORE:</h3>
<div id="scoreContainer">$<span id="score">0</span></div>
</div>
<div id="mainFrame">
<div id="mainBoard">
<table id="board">
<tr class="categories">
<?
for ($i = 0; $i < $NUM_CATEGORIES; $i++) {
echo "<td class=\"category\">";
echo $categories[$i];
echo "</td>";
}
?>
</tr>
<tr class="clue1">
<td class="clue clue1_cat1" onclick="showClue(1,1);">
$200
</td>
<td class="clue clue1_cat2" onclick="showClue(1,2);">
$200
</td>
<td class="clue clue1_cat3" onclick="showClue(1,3);">
$200
</td>
<td class="clue clue1_cat4" onclick="showClue(1,4);">
$200
</td>
<td class="clue clue1_cat5" onclick="showClue(1,5);">
$200
</td>
<td class="clue clue1_cat6" onclick="showClue(1,6);">
$200
</td>
</tr>
<tr class="clue2">
<td class="clue clue2_cat1" onclick="showClue(2,1);">
$400
</td>
<td class="clue clue2_cat2" onclick="showClue(2,2);">
$400
</td>
<td class="clue clue2_cat3" onclick="showClue(2,3);">
$400
</td>
<td class="clue clue2_cat4" onclick="showClue(2,4);">
$400
</td>
<td class="clue clue2_cat5" onclick="showClue(2,5);">
$400
</td>
<td class="clue clue2_cat6" onclick="showClue(2,6);">
$400
</td>
</tr>
<tr class="clue3">
<td class="clue clue3_cat1" onclick="showClue(3,1);">
$600
</td>
<td class="clue clue3_cat2" onclick="showClue(3,2);">
$600
</td>
<td class="clue clue3_cat3" onclick="showClue(3,3);">
$600
</td>
<td class="clue clue3_cat4" onclick="showClue(3,4);">
$600
</td>
<td class="clue clue3_cat5" onclick="showClue(3,5);">
$600
</td>
<td class="clue clue3_cat6" onclick="showClue(3,6);">
$600
</td>
</tr>
<tr class="clue4">
<td class="clue clue4_cat1" onclick="showClue(4,1);">
$800
</td>
<td class="clue clue4_cat2" onclick="showClue(4,2);">
$800
</td>
<td class="clue clue4_cat3" onclick="showClue(4,3);">
$800
</td>
<td class="clue clue4_cat4" onclick="showClue(4,4);">
$800
</td>
<td class="clue clue4_cat5" onclick="showClue(4,5);">
$800
</td>
<td class="clue clue4_cat6" onclick="showClue(4,6);">
$800
</td>
</tr>
<tr class="clue5">
<td class="clue clue5_cat1" onclick="showClue(5,1);">
$1000
</td>
<td class="clue clue5_cat2" onclick="showClue(5,2);">
$1000
</td>
<td class="clue clue5_cat3" onclick="showClue(5,3);">
$1000
</td>
<td class="clue clue5_cat4" onclick="showClue(5,4);">
$1000
</td>
<td class="clue clue5_cat5" onclick="showClue(5,5);">
$1000
</td>
<td class="clue clue5_cat6" onclick="showClue(5,6);">
$1000
</td>
</tr>
</table>
<div id="clueBoard">
<div id="clueBoardTextContainer">
<div id="clueBoardText">
How to Win:
<br />
Get as many points as you can!
<br /><br />
Press START to begin...
</div>
</div>
<div id="answerControls">
<span id="questionPhrase">What is</span>
<input id="response" type="text" />
<span id="questionMark">?</span>
<button onclick="checkAnswer();" id="submitAnswerButton">Submit</button>
<button onclick="pass();" id="passButton">Pass</button>
</div>
<div id="nextControl">
<button onclick="next();" id="nextButton">Next</button><br /><br />
<button onclick="adjustUp();" id="adjustUpButton">Judge My Answer as Correct</button>
<button onclick="adjustDown();" id="adjustDownButton">Judge My Answer as Incorrect</button>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<?
}
elseif (!empty($_POST['username']) && !empty($_POST['password'])) {
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['EmailAddress'];
$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['LoggedIn'] = 1;
echo "<h1>Success</h1>";
echo "<p>We are now redirecting you to the member area.</p>";
echo "<meta http-equiv='refresh' content='2;index.php' />";
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";
}
}
else {
?>
<body>
<h1>J! Archive Player</h1>
<p>Please either login below, or <a href="register.php">click here to register</a>.</p>
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
</body>
<?
}
?>
</html>