summaryrefslogtreecommitdiffstats
path: root/do.php
blob: d82254154b63439d03e07e748051e42005ab5768 (plain)
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
<?PHP
session_start();

include "includes/maps.php";
include "includes/sqlEmbedded.php";
include "includes/datas.php";

//FirePHP stuff - TODO: Delete
// require_once('includes/FirePHPCore/FirePHP.class.php');
// ob_start();
// $firephp = FirePHP::getInstance(true);
//FirePHP stuff - TODO: Delete

$note = false;

//Auto login;
include_once("globe.php");
if (!isset($_SESSION['accepted']) || $_SESSION['accepted'] <> 1)
	if (isset($_COOKIE['doLogin']) && $_COOKIE['doLogin'] == 'yes')
		CookieLogin();

if (!isset($_SESSION['accepted']) || $_SESSION['accepted'] <> 1)
	$accepted = false;
else
	$accepted = true;
	
		
// encode array $json to JSON string

$debug = false;

//$debug = true;


if (isset($_GET['act']) && $_GET['act'] == "getmap") {
	$map = GenerateMapByCode($_GET['mapcode']);

	$solution = formSolution($_GET['solution']);

	$solvedmap = MergeMapSolution($map, $solution);
	if ($solvedmap == -1) {
		$json['error'][] = "INVALID WALL POSITIONS";
	}
	if ($solvedmap == -2) {
		$json['error'][] = "USED TOO MANY WALLS";
	}
}

// TODO: Depreciate?
if (isset($_GET['checkachieve']) && $_GET['checkachieve'] == 'true' && $_SESSION['accepted'] == 1) {
	$json['notification'] = true;
	$userID = $_SESSION['userID'];
	//Located in includes/datas.php
	$note = getNotified($userID);
	if ($note !== false)
		$json['notificationtext'] = $note;
}

if ($_GET['r'] == 'reqMemberPage') {
	$order = $_GET['order'];
	$orderBy = $_GET['orderBy'];

	$page = $_GET['reqPage'] + 0;
	if (!is_int($page))
		return;
	$pageDivide = $_GET['membersPageDivide'] + 0;
	if (!is_int($pageDivide))
		return;
	$json = getMembers($page, $pageDivide, $order, $orderBy);
	$json['page'] = $page;
	$encoded = json_encode($json);
	die($encoded);
}

if ($_GET['r'] == 'reqScorePage') {
	$mapID = $_GET['mapid'] + 0;
	$page = $_GET['reqPage'] + 0;
	if (!is_int($mapID) OR !is_int($page))
		return;

	//Include the notification text
	$json = getScores($mapID, $page, 10);
	$json['mapid'] = $mapID;
	$json['page'] = $page;
	
	$note = false;
	if ($_SESSION['accepted'] == 1) {
		$userID = $_SESSION['userID'];
		$note = getNotified($userID);
	} else {
		if ($_SESSION['preCompletedTutorial'] == true && $_SESSION['preCompletedTutorialNotified'] == false) {
			$_SESSION['preCompletedTutorialNotified'] = true;
			$note = "<strong>Tutorial Completed!</strong>";
			$note .= "<center>You've unlocked: Blue Wall Color!";
			$note .= "<table><tr><td onclick='changeWallColor(\"#4444ff\")' style='background-color:#4444ff;' class='grid_td_rocks'></td></tr></table>";
			$note .= '<br /><a href="javascript:showSignin();"><strong>Sign in</strong></a> to save your progress!<br />';
			$note .= "</center>";
		}
	}
	if ($note !== false)
		$json['notificationtext'] = $note;
	
	$encoded = json_encode($json);
	die($encoded);
}

if ($_GET['r'] == 'getscores') {
	$mapID = $_GET['mapid'] + 0;
	if (!is_int($mapID))
		return;
	$topscores = topScores($mapID, 30);
	$json['scores'] = $topscores;
	$json['mapid'] = $mapID;
	$encoded = json_encode($json);
	die($encoded);
}

if ($_GET['r'] == 'getsol') {
	$mapID = $_GET['mapID'] + 0;
	//echo "working...";
	if (!is_int($mapID)) return;

	$userID = $_SESSION['userID'];

	$json['solution'] = '';
	$json['moves'] = 0;
	if ($accepted) {
		$sol = getSolution($userID, $mapID);
		$json['solution'] = $sol['solution'];
		$json['moves'] = $sol['moves'];
	}
	if (isset($_SESSION[$mapID.'sol']) && $_SESSION[$mapID.'moves'] > $json['moves']) {
		$json['solution'] = $_SESSION[$mapID.'sol'];
		$json['moves'] = $_SESSION[$mapID.'moves'];
	}
	
	$json['mapid'] = $mapID;

	$encoded = json_encode($json);
	die($encoded);
}

if ($_GET['r'] == 'getChallengeSolution') {
	$mapID = $_GET['mapID'] + 0;
	$challengeID = $_GET['challengeID'] + 0;
	//echo "working...";
	if (!is_int($mapID) OR !is_int($challengeID)) return;
	if (!$accepted) return;

	$userID = $_SESSION['userID'];

	$json = getChallengeSolution($userID, $challengeID);
	
	$json['mapid'] = $mapID;
	$encoded = json_encode($json);
	die($encoded);
}

// ------------ MAIN; getpath.
if ($_GET['r'] == 'getpath') {

	//Join the partial-solution from the map, and the solution sent.
	//Could be used to validate maps in for challenges where
	//		getting the map code from the database is not an option
	//$tmp = GenerateMapByCode($_GET['mapcode']);
	//$tmp = seperateMapSolution($tmp);
	//$solution = formSolution($_GET['solution'].$tmp);

	// Enables the ability to discover an exact duplicate solution.
	$solution = formSolution($_GET['solution']);
	$userID = $_SESSION['userID'];
	
	//valid mapID?
	$mapID = $_GET[mapid] + 0;
	if (!is_int($mapID)) return;
	//$firephp->log($mapID, "mapID");

	//the first 10 ID's reserved for challenges & tutorial.
	if ($mapID > 10)
		$mapcode = getMapCode($mapID);
	else
		$mapcode = $_GET['mapcode'];

	//mygrid will be the map, with the solution applied.
	$map = GenerateMapByCode($mapcode);
	$mygrid = MergeMapSolution($map, $solution);

	//Check both starting point groups for paths
	$json = routeMultiPath($mygrid);
	
	$moves = $json['totalMoves'];
	$json['mapid'] = $mapID;

	//What could go wrong?
	if ($json['blocked']) {
		//$json['error'][] = "blocked";
		$encoded = json_encode($json);
		die($encoded);
	}
	if ($mygrid == -1) {
		$json['error'][] = "INVALID WALL POSITIONS";
		$encoded = json_encode($json);
		die($encoded);
	}
	if ($mygrid[0][4] < 0) {
		$used = $mygrid[0][4] - $map[0][4];
		$json['error'][] = "Too many walls used. ".$used." of ".$map[0][4]." walls used.";
		$encoded = json_encode($json);
		die($encoded);
	}
	
	if ($_GET['isChallenge'] == 'true' AND isChallengeMap($mapID)) {
		// $firephp->log('Calling challenges');
		$json['info'][] = 'Executed as a challenge';
		$json['completedChallenges'] = getCompletedChallenges($userID, $mapID, $solution, $moves, $json['path']);
		// $firephp->log('Challenges called!');
		die(json_encode($json));
	}

	//Get current score data. - to see if a pertinent score was beat.
	$sql = "SELECT `moves` as bestmoves, `displayName`, IFNULL(q1.mymoves, 0)
			FROM `solutions`, `users`
			LEFT JOIN (
				SELECT `moves` as mymoves
				FROM `solutions`
				WHERE `userID` = '$userID' AND
				`mapID` = '$mapID'
			) as q1
			ON 1
			WHERE
			`mapID` = '$mapID' AND
			`userID` = users.ID
			ORDER BY `moves` DESC, `dateModified` ASC, solutions.ID DESC
			LIMIT 1";
	$result = mysql_query($sql);
	if (mysql_num_rows($result) > 0) {
		list($bestMoves, $byName, $myMoves) = mysql_fetch_row($result);
		$json['best'] = $bestMoves;
		$json['bestby'] = $byName;
		$json['mybest'] = $myMoves;
	} else {
		$json['best'] = 0;
		$json['bestby'] = 'no one';
	}
	
	// --------- ------------------------------ ---------
	// --------- RUSH THE PATH BACK TO THE USER ---------
	// --------- ------------------------------ ---------
	if (!$debug) {
		ignore_user_abort(true);
		$encoded = json_encode($json);
		header("Connection: close");
		header("Content-Length: " . mb_strlen($encoded));
		echo $encoded;
		flush();
		//The connection is now closed, no further communication to the client can be done!
	}
	// --------- CONTINUE EXECUTION ---------
	
	//TODO: !! Close the session with session_write_close(); as soon as possible.
	// This allows the session file to be opened by other requests.
	
	
	// Now we don't have to worry about how long we're taking.
	// All of this code will execute without the user waiting on the server.
	
	//$json['error'][] = "bestMoves, byName, myMoves: $bestMoves, $byName, $myMoves;";

	//TODO: Commented this out - temporarly.
   // $firephp->log('Got this far');
	// $firephp->log($_SESSION['accepted'], '$_SESSION["accepted"]');

	////Challenge/Tutorial?

	////TODO: Clean this up a bit
	// if ($_GET['isChallenge'] == 'true')
	// {
		// return;
	//} //x
	
	//TODO: I changed this; note these changes before re-applying the above.
	//Challenge/Tutorial?
	if ($mapID <= 10 AND $_GET['isChallenge'] = 'true') {
		$json['error'][] = 'executed as a challenge';
		
		//This will allow me to give insentive to logging in.
		// So that they don't have to do the tutorial twice.
		if ($mapID == 5 && $moves == 75 && $accepted == false) {
			$_SESSION['preCompletedTutorial'] = true;
			$_SESSION['preCompletedTutorialNotified'] = false;
		}
		if ($accepted == false)
			die(json_encode($json));
		getCompletedChallenges($userID, $mapID, $solution, $moves, $paths);
		die(json_encode($json));
	}
	

	// --------- USER NOT LOGGED IN?
	if ($_SESSION['accepted'] !== 1) {
		if ($moves >= ($_SESSION[$mapID.'moves'] + 0)) {
			$_SESSION[$mapID.'moves'] = $moves;
			$_SESSION[$mapID.'sol'] = $solution;
		}
		return;
	}
	
	// ---------- CLOSING THE SESSION $_SESSION WILL NO LONGER WORK FOR WRITING
	session_write_close();
	//Now that is done, we can easily do whatever we want - Like, send any emails that there might be.
	include_once('./includes/emails.php');
	SendQueuedEmail();
	
	$json['error'][] = 'user is logged in';
	// --------- USER LOGGED IN

	//Is the map still valid to score on?
	if (!isCurrentMap($mapID))
		return;

	// --------- UPDATE SCORES
	$json['error'][] = 'map is current';

	$checkcp = false;
	$checkcm = false;

	//Is there an existing record?
	if ($myMoves > 0) {
		if ($myMoves < $moves) {
			$sql = "UPDATE `solutions`
			SET `moves` = '$moves'  	,
			`dateModified` = NOW()		,
			`solution` = '$solution'
			WHERE `userID` = '$userID' AND
			`mapID` = '$mapID'";
			mysql_query($sql);
			$checkcp = true;
			$json['error'][] = 'Score was improved';
		//Update the solution only, if it's the same score.
		} elseif ($myMoves == $moves) {
			$sql = "UPDATE `solutions`
			SET `moves` = '$moves'  	,
			`solution` = '$solution'
			WHERE `userID` = '$userID' AND
			`mapID` = '$mapID'";
			mysql_query($sql);
			$json['error'][] = 'Score unchanged, but solution updated';
		}
	//Create a new record.
	} else {
		$sql = "INSERT INTO `solutions` (`userID`, `mapID`, `solution`, `moves`)
		VALUES ('$userID', '$mapID', '$solution', '$moves')";
		mysql_query($sql);
		$checkcm = true;
		$checkcp = true;
		$json['error'][] = 'A new record was created';
	}
	
	$json['error'][] = "Involved data: ('$userID', '$mapID', '$solution', '$moves')";

	// --------- APPLY ACHIEVEMENTS

	if ($checkcp)
		applyAchievements($userID, 1);
	if ($checkcm) {
		applyAchievements($userID, 2);
		applyAchievements($userID, 3);
		applyAchievements($userID, 4);
	}
	
	// --------- END
}

$json['error'][] = 'Rush-send failed';

//$encoded = json_encode($json);
die(json_encode($json));


function isCurrentMap($mapID) {
	include_once('./includes/sqlEmbedded.php');
	$sql = "SELECT mapID
	FROM `mapOfTheDay`
	WHERE `mapID` = '$mapID'
	AND mapExpireTime > NOW()
	LIMIT 1
	";
	
	$result = mysql_query($sql) or die(mysql_error());
	return (mysql_num_rows($result) != 0);
}

//Very simple, confirm that all targets are reachable.
function ValidateMap($mygrid) {
	$start = "0,1.";
	$target[] = 'a';
	$target[] = 'b';
	$target[] = 'c';
	$target[] = 'f';
	$blocked = false;
	foreach($target as $t) {
		$p = Findpath ($mygrid, $start, $t);
		if ($p['blocked']) return false;
	}
	return true;
}


?>