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

include "includes/maps.php";
include "includes/db.inc.php";

include "includes/datas.php";

// encode array $json to JSON string

 
if ($_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";
	}
}

if ($_GET['checkachieve'] == 'true' AND $_SESSION['accepted'] == 1) {
	$json['notification'] = true;
	
	$userID = $_SESSION['userID'];
	
	$sql = "UPDATE `achievements` 
		SET `level` = '0'
		WHERE `userID` = '$userID'";
		//mysql_query($sql);
	//Located in includes/datas.php
	$note = getNotified($userID);
	
	if ($note !== false)
		$json['notificationtext'] = $note;
	
	// $achieved = checkAchievements($userID);
	
	// if ($achieved['result']) {
		// $json['notificationtext'] = $achieved['text'];
	// }
}

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


if ($_GET['r'] == 'getsol') {
	$solid = $_GET['solutionid'] + 0;
	//echo "working...";
	if (!is_int($solid)) return;
	
	$sql = "SELECT `solution`, `mapID`
	FROM `solutions`
	WHERE
	`ID` = '$solid'
	LIMIT 1";
	
	$result = mysql_query($sql);
	if (mysql_num_rows($result) > 0) {
		list($json['solution'], $json['mapid']) = mysql_fetch_row($result);
		$encoded = json_encode($json);
		die($encoded);
	}
}



if ($_GET['r'] == 'getpath') {

	//Stage 1; Get the path.
	
	//Generate the map based on the code.
	$mapcode = $_GET['mapcode'];
	//$tmp = GenerateMapByCode($_GET['mapcode']);
	//Seperate any existing solution from map  (?depreciated?)
	//$tmp = seperateMapSolution($tmp);
	
	//!!!!
	//Join the partial-solution from the map, and the solution sent  (?Again, depreciated?)
	//$solution = formSolution($_GET['solution'].$tmp);
	
	$solution = $_GET['solution'];

	//valid mapID? 
	$mapID = $_GET[mapid] + 0;
	if (!is_int($mapID)) return;
	
	if ($mapID > 10) {
		$mapcode = getMapCode($mapID);
	} 
	
	//mygrid will be the map, with the solution applied.
	$map = GenerateMapByCode($mapcode);
	$mygrid = MergeMapSolution($map, $solution);
	
	//Route the path
	$json = routePath($mygrid, $start);
	
	$moves = $json['moves'];

	$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);
	}

	//Get current score data. - to see if a pertinent score was beat.
	$userID = $_SESSION['userID'];
	$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
			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
	ignore_user_abort(true);
	$encoded = json_encode($json);
	header("Connection: close");
   header("Content-Length: " . mb_strlen($encoded));	
	echo $encoded;
	flush();
	
	// --------- CONTINUE EXECUTION
	
	
	// --------- USER NOT LOGGED IN
	if ($_SESSION['accepted'] !== 1) {
		if ($moves >= ($_SESSION[$mapID.'moves'] + 0)) {
			$_SESSION[$mapID.'moves'] = $moves;
			$_SESSION[$mapID.'sol'] = $solution;
		}
		return;
	}
	// --------- USER LOGGED IN
	
	
	//Challenge/Tutorial?
	//echo "pre";
	if ($mapID <= 10 AND $_GET['isChallenge'] = 'true') {
		//echo "running function".$_GET['challengeID'];
		applyChallengeAchievements($userID, $_GET['challengeID'], $mapID, $solution, $moves);
		return;
	}
	
	//Is the map still valid to score on?
	if (!isCurrentMap($mapID))
		return;

	// --------- UPDATE SCORES
	
	$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;
		//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);
			
		}
	//Create a new record.
	} else {
		$sql = "INSERT INTO `solutions` (`userID`, `mapID`, `solution`, `moves`)
		VALUES ('$userID', '$mapID', '$solution', '$moves')";
		mysql_query($sql);
		$checkcm = true;
	}
	
	// --------- APPLY ACHIEVEMENTS
	
	//while(applyCareerAchievement($userID)) {
	//	usleep(200);
	//}
	
	//if ($checkcm)
		applyCareerMazesAchievements($userID);
	//if ($checkcp)
		applyCareerPathAchievements($userID);
	
	//!! no need w/ rush-sending.
	$encoded = json_encode($json);
	echo $encoded;
	
	// --------- END
}


function isCurrentMap($mapID) {
	include_once('./includes/db.inc.php');
	$sql = "SELECT `ID` FROM `mapOfTheDay`
	WHERE `mapDate` = CURDATE() AND
		`mapID` = '$mapID'
	";
	$result = mysql_query($sql) or die(mysql_error());
	if (mysql_num_rows($result) == 0) 
		return false;
	else 
		return true;
}




//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;
}



$json['error'][] = 'Division by 912 trillion!';
$json['error'][] = 'Cant count that high.';
 
$json['error'][] = "I forgot what I was doing!";
$json['error'][] = "Lost track of time, and gaveup!";

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



?>