summaryrefslogtreecommitdiffstats
path: root/pages/challengelist.php
blob: df04bdd03392c7b76d35977b092be9b7860f0f94 (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
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
<?php
htmlHeader(
	array('tutorial', 'challenge', 'stats'), 'Pathery Challenges', 
	'Challenges', array('scores', 'dateformat')
);

include_once ('./includes/maps.php');
include_once ('./includes/mapoftheday.php');
include_once ('./includes/sqlEmbedded.php');
include_once ('./includes/datas.php');
include_once ('./includes/mapclass.php');
include_once ('./includes/constants.php');

echo soundManager2();
topbar($Links);
?>

<script>
// -display sections labeled..
// -*click
// -display levels
// -*click
// -display map.
// -*finish map:
// -highlight 'Next'


function navShowChallengeTiers() {

}

function navShowLevels(tier) {
	$('#mainDisplay')
		.hide()
		.html("")
		.slideDown('fast');
	
	var urlString = 'ajax/challenges.ajax.php?getChallengeIDs=true';
	urlString += "&userID="+userObj.ID;
	$.ajax({
		type: "GET",  
		url: urlString,
		cache: true,
		data: '', 
		fail: function() { alert("ajax error - if this persists check your connection."); },
		complete: function(data) {showLevelsResponse(data.responseText);}
	});	
}

function showLevelsResponse(response) {
	var json = decryptJSON(response);
	$.each(json, function(key, challengeObj) {
		console.log("tests", key, challengeObj);
		//$("#thumb_"+mapID).html(formatMapThumbForNav(data.responseText)).show();
		$("#mainDisplay").append(formatChallengeThumbnail(challengeObj.mapObject)).show();
		mapdata[key] = challengeObj.mapObject;
	});
	$("#mainDisplay").append('<div style="float:left; margin:8px; width:200px; height:180px;" class="mapThumbnail">Grayed out Obscured Map Here.</div>').show();
	$("#mainDisplay").append('<div style="float:left; margin:8px; width:200px; height:180px;" class="mapThumbnail">Grayed out Obscured Map Here.</div>').show();
	$("#mainDisplay").append('<div style="float:left; margin:8px; width:200px; height:180px;" class="mapThumbnail">Grayed out Obscured Map Here.</div>').show();
	$("#mainDisplay").append('<div style="float:left; margin:8px; width:200px; height:180px;" class="mapThumbnail">Grayed out Obscured Map Here.</div>').show();
}

function formatChallengeThumbnail(map) {
	var r = '<div onclick="displayChallengeMap('+map.ID+')"';
	r+= 'style="float:left; margin:8px;width:200px;height:180px;" class="mapThumbnail">';
	r+= mapThumbnailHTML(map,200, 160)+'</div>';
	return r;
}

function displayChallengeMap(challengeMapID) {
	$("#mainDisplay").html(mapAsHTML(mapdata[challengeMapID]));
	getmapdata(challengeMapID);
	
	var urlString = 'ajax/challenges.ajax.php?getChallenges=true&challengeMapID='+challengeMapID;
	urlString += "&userID="+userObj.ID;
	$.ajax({
		type: "GET",  
		url: urlString,
		cache: true,
		data: '', 
		fail: function() { alert("ajax error - if this persists check your connection."); },
		complete: function(data) {
			$("#mainDisplay").append(challengesHTML(data.responseText, challengeMapID));
		}
	});
	$("#mainDisplay").append(challengeNextBtn(challengeMapID));
	$("#mainDisplay").append(challengePrevBtn(challengeMapID));
}

function challengeNextBtn(challengeMapID) {
	var next = (challengeMapID-0)+1;
	return '<a class="next" id="challengesNextBtn" href="javascript:displayChallengeMap('+next+')" >Next &gt;</a>';
}
function challengePrevBtn(challengeMapID) {
	var prev = challengeMapID-1;
	return '<a class="next" id="challengesPrevBtn" href="javascript:displayChallengeMap('+prev+')" >&lt; Prev</a>';
}


function challengesHTML(response, challengeMapID) {
	
	var json = decryptJSON(response);
	var r = '<div id="challenges">';
	r += '<div id="challenges_title">Challenges:</div>';
	r += '<div id="challenges_listing"><ul class="challenge_ulist">';
	
	console.log('CC', json, challengeMapID);
	
	var cssClass;
	$.each(json, function(key, challenge) {
		console.log('d', key, challenge);
		if (challenge.dateSolved) cssClass = 'challenge_complete';
		else cssClass = 'challenge_incomplete';
		var loadSolutionString = " <a href='javascript:requestChallengeSolution(\""+challengeMapID+"\", \""+challenge.challengeID+"\");'> Load this solution</a>";
		r += "<li class='"+cssClass+"' id='challenge_id_"+challenge.challengeID+"'>"
		r += getChallengeDisplayString(challenge) + loadSolutionString + "</li>";
	});
	r += "</ul></div></div>";
	return r;
}

/**
 * Returns a user-friendly string describing the challenge.
 * Example: "Get at least 245, using at most 10 walls and starting from (6,2)
 */
function getChallengeDisplayString(challenge)
{
	var returnMe = "";
	if(challenge.goal == 0)
		returnMe += "Complete the maze";
	else if (challenge.inequality == "greater than")
		returnMe += "Get " + challenge.goal + ' or more';
	else if (challenge.inequality == "less than")
		returnMe += "Get LESS THAN " . challenge.goal;
	else //inequality == "equal"
		returnMe += "Get EXACTLY " + challenge.goal;
	
	var restrictions = new Array();
	var addUsingToText = false; //Grammar hack - specifies whether we want to add the word "using" to $returnMe

	//Wall-count restriction
	if(challenge.restrictWallCount)
	{
		restrictions.push("only " + challenge.restrictWallCount + " wall" + (challenge.restrictWallCount == 1 ? "" : "s"));
		addUsingToText = true;
	}

	
	//Wall-placement restriction
	if(challenge.restrictWallPlacement)
	{
		invalidWalls = challenge.restrictWallPlacement.split(".");
		invalidWallsString = invalidWalls.join(") or (");
		restrictions.push("no walls at (" + invalidWallsString + ")");
		addUsingToText = true;
	}
	

	//Teleport-count
	if(challenge.restrictTeleportCount)
	{
		if(challenge.restrictTeleportCount == 0)
			restrictions.push("no teleports");
		else
			restrictions.push("exactly " + challenge.restrictTeleportCount + " teleports");
		addUsingToText = true;
	}
	
	//Teleport points
	if(challenge.restrictTeleportsUsed)
	{
		invalidTeleports = challenge.restrictTeleportsUsed.split(".");
		invalidTeleportsString = invalidTeleports.join(") or (");
		restrictions.push("without using the teleport" + (invalidTeleports.length == 1 ? "" : "s") + " at (" + invalidTeleportsString + ")");
	}
	
	//Start point
	if(challenge.restrictStartPoint)
	{
		restrictions.push("starting from (" + challenge.restrictStartPoint + ")");
	}
	
	//End point
	if(challenge.restrictEndPoint)
	{
		restrictions.push("ending at (" + challenge.restrictEndPoint + ")");
	}
	
	//Here comes the tricky part:  we want to join all the $restriction strings with commas, EXCEPT for the last
	//two, which should be separated by " and ".
	var restrictionCount = restrictions.length;
	if(restrictionCount > 0)
	{
		if(restrictionCount == 1)
		{
			restrictionString = restrictions[0];
		}
		else
		{
			lastRestriction = restrictions[restrictionCount - 1];
			restrictions[restrictionCount - 1] = '';
			restrictionString = restrictions.join(", ") + " and " + lastRestriction;
		}
		
		returnMe += ", " + (addUsingToText ? "using " : "") + restrictionString;
	}
	
	
	returnMe += ".";
	return returnMe;
}


function hideNav() {

}
function showNav() {

}



</script>

<div id="challengelist_wrapper" class="wrapper" style='overflow: auto;'>
<h3>Challenges - I'z working on this stuff.</h3>

<div id='backBtn'><a href='' class='next'>Back Btn</a></div>
<div id='mainDisplay' style='width:99%;overflow: hidden;'>
	<div onclick='navShowLevels(1);' style='float:left; margin:20px; border:20px solid white;padding:40px'>
		<h2>Easy</h2>
	</div>
	<div style='float:left; margin:10px; border:10px solid gray;padding:20px'>
		Normal
	</div>
	<div style='float:left; margin:10px; border:10px solid gray;padding:20px'>
		Hard
	</div>
</div>

<div style='clear:both'></div>



</div>
<?
htmlFooter();
exit;

//Check that the user is allowed to do the challenges
if (!$accepted) {
	echo "<center>Please <a href='javascript:showSignin();'>login</a> to do the challenges!</center>";
	echo "<br /><br /></div>";
	htmlFooter();
	return;
}

$userID = $_SESSION['userID'];

if (!hasCompletedTutorial($userID)) {
	echo "<center>Please <a href='tutorial'>complete the tutorial</a> to unlock Challenge mode!</div>";
	htmlFooter();
	return;
}

//TODO: Uncomment
//if (!hasCompletedTutorial($userID)) {
//	echo "<center>Please <a href='tutorial'>complete the tutorial</a> to unlock Challenge mode!</div>";
//	htmlFooter();
//	return;
//}

?>
	<noscript>Sorry, this game requires scripts to run. Please enable javascript and <a href='home'>Reload this site</a>
	<br />This game is best played in <a href='http://www.google.com/chrome'>Google Chrome</a>
	</noscript>
<?

//Display the actual challenge list
$challengeListResultset = loadChallengeListing($userID);
//displayChallengeList($challengeListResultset);
echo getChallengeListHtml($challengeListResultset);

?>

</div>

<?php
htmlFooter();
?>

<?php

function getChallengeListHtml($challengeListResultset) {
	//First gather data to a more useful form.
	while($data = mysql_fetch_array($challengeListResultset)) {
		$tier = $data['challengeTier'];
		$challengeSuborder = $data['challengeSuborder'];
		$ordering = $data['ordering'];
		$challenges[$tier][$challengeSuborder][$ordering] = $data;
	}
	
	$r = '';
	$numCompletedChallenges = 0;
	foreach ($challenges as $tier => $challengeMap) {
		$r .= "<b>Section $tier</b> levels:<div class='challengelist_tier' style='border: 1px solid yellow; overflow: auto;'>";
		foreach ($challengeMap as $challengeSuborder => $challenge) {
			//Hack to get the first element of the array:
			$firstChallenge = $challenge[key($challenge)];
			$challengeMapID = $firstChallenge['challengeMapID'];

			//$mapCode = getMapCode($mapid);
			//$mapCode = $firstChallenge['mapCode'];
			//$map = new map($mapCode);
			//$thumbnail = DisplayMapThumbnail($map);
			
			$r .= "<div class='challengelist_map' onclick='document.location.href=\"challenge?challengeMapID=$challengeMapID\"'>";
			//$r .= "$map->name";
			//$r .= $thumbnail;
			
			$r .= "<div id='innerChallengeMapDspThumb_$challengeMapID'>\n";
			$r .= "</div>\n";
			$r .= "<script>";
			$r .= "	displayMap($challengeMapID, 'innerChallengeMapDspThumb_$challengeMapID', 170, '', '', true, true);";
			$r .= "</script>";

			foreach ($challenge as $ordering => $content) {
				//Each challenge gets its own star
				if($content["dateSolved"] !== NULL)
				{
					$cssClass = "challengelist_complete";
					$numCompletedChallenges++;
				}
				else
				{
					$cssClass = "challengelist_incomplete";
				}
				$r .= "<div class='$cssClass'></div>";
			}
			$r .= "</div>";
			//$r .= "END MAP";
		}
		$r .= "</div>";
	}
	
	$r .= getNextChallengeTierHtml($numCompletedChallenges);
	return $r;
}

function getNextChallengeTierHtml($numCompletedChallenges)
{
	global $tierChallengeRequirements, $userID;
	
	for($nextTier = 0; $nextTier < count($tierChallengeRequirements); $nextTier++)
	{
		$numChallengesForNextTier = numChallengesRemainingForTier($nextTier, $numCompletedChallenges);
		if($numChallengesForNextTier > 0)
			break;
	}
	
	//We've found the tier they're at - verify this matches up with what's in the DB
	//This probably belongs somewhere else, but whatever
	$userCurrentTier = getUserChallengeTier($userID);
	if($userCurrentTier != $nextTier - 1)
	{
		//TODO: Display some sort of "tier XY unlocked" or something
		setUserChallengeTier($userID, $nextTier -1);
	}
	
	//Return the tiers-left html
	if($numChallengesForNextTier == 0)
		return "<div class='challengelist_nexttier>All tiers have been unlocked!</div>";
	
	return "<div class='challengelist_nexttier'>Next tier unlocked in <span class='challengelist_nexttier_requirement'>$numChallengesForNextTier</span>"
		 . "<span class='challengelist_complete'></span></div>";
}

function numChallengesRemainingForTier($tier, $numCompletedChallenges)
{
	global $tierChallengeRequirements;
	
	if($tier >= maxTier())
		return -1;
	if($tierChallengeRequirements[$tier] <= $numCompletedChallenges)
		return 0;
	return ($tierChallengeRequirements[$tier] - $numCompletedChallenges);
}

function maxTier()
{
	global $tierChallengeRequirements;
	
	return (count($tierChallengeRequirements) - 1);
}

// TODO: !! Depreciate
/**
 * Outputs the list of all challenges to the page
 */
function displayChallengeList($challengeListResultset)
{
	echo '<div id="challengelist">';
	$currentTier = -1;
	$currentMap = -1;
	
	while($challenge = mysql_fetch_array($challengeListResultset))
	{
		//Each challenge gets its own header/table
		// echo "<br>L: $currentTier<br>N: ";
		// echo $challenge["challengeTier"];
		// echo "<br>";
		if($challenge["challengeTier"] != $currentTier)
		{
			if($currentTier >= 0)
			{
				echo '</div>';
			}
			$currentTier = $challenge["challengeTier"];
			echo "<div class='challengelist_tier'>Tier $currentTier</div>";
			echo "<div class='challengelist_table'>\n";
		}
		
		//Each map gets it own row
		if($challenge["mapID"] != $currentMap)
		{
			if($currentMap >= 0)
			{
				echo '</div>';
			}
			$currentMap = $challenge["mapID"];
			
			$mapCode = getMapCode($currentMap);
			$map = new map($mapCode);
			$thumbnail = DisplayMapThumbnail($map);
			
			$mapName = $map->name;
			if($mapName == NULL || $mapName == "")
				$mapName = "(unknown)";
			
			echo "<div style='border:1px solid yellow; float:left; padding:5px; background-color: #222;'>
				<div class='challengelist_link' style='cursor:pointer' onclick='document.location.href=\"challenge?mapID=$currentMap\"'>
					
					<a href='challenge?mapID=$currentMap'>$mapName</a>
					$thumbnail
					
				</div>";
			echo "<div class='challengelist_stars'>";
		}
		
		//Each challenge gets its own star
		if($challenge["dateSolved"] !== NULL)
			$cssClass = "challengelist_complete";
		else
			$cssClass = "challengelist_incomplete";
		echo "<div class='$cssClass'></div>";
	}
	echo "</div></div>";
	echo "</div>";
}


?>