summaryrefslogtreecommitdiffstats
path: root/pages/challenge.php
blob: 4e5c50ceaf802f627d8f8daf74d433099cb3039a (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
<?php
htmlHeader(
	array('tutorial', 'challenge'), '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');

//Get custom wall colors;
$wallColor = '';
$wallEmblem = '';
$noteScript = '';
if ($accepted) {
	$userID = $_SESSION['userID'];

	$sql = "
	SELECT 
		userData.wallColor,
		userData.wallEmblem
	FROM `userData` 
	WHERE userID = '$userID' ";
	$result = mysql_query($sql);
	if ($result)
		list($wallColor, $wallEmblem) = mysql_fetch_row($result);
		
	//$note = getNotified($userID);
	$note = false;
	if ($note !== false) {
		//$note = str_replace("'", "\'", $note);
		//Double your slashes double your fun (Required)
		$note = addslashes($note);
		$note = addslashes($note);
		$noteScript = "
<script type='text/javascript'>
setTimeout(\"showNotification('".$note."');\", 1000);
if (getCookie('pref_mute') != 'true') {
	setTimeout(\"soundManager.setVolume('achieve', 40);\", 1340);
	setTimeout(\"soundManager.play('achieve');\", 1350);
}
</script>
		";
	}
}

?>

<body>
<script type="text/javascript">
var isChallenge = true;
playerWallColor = '<?PHP echo isset($wallColor) ? $wallColor : ''; ?>';
playerWallEmblem = '<?PHP echo isset($wallEmblem) ? $wallEmblem : ''; ?>';

//Legacy functions
function challengeLoad() {}
function challengeWall() {}
function challengeGo() {}
</script>

<?php
topbar($Links);
?>

<div id="challenge_wrapper" class="wrapper">

<?

//Check that the user is allowed to do the challenges
if (!$accepted) {
	echo "<center>Please <a href='login'>login</a> to do the challenges!</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;
//}

if(!isset($_GET["challengeMapID"]) OR !is_int($_GET["challengeMapID"] + 0))
{
	redirectToChallengeListing();
	return;
}

$mapCode = loadChallengeMapCode($_GET["challengeMapID"]);
if($mapCode === NULL)
{
	redirectToChallengeListing();
	return;
}

$challengeMapID = $_GET["challengeMapID"] + 0;
if (!is_int($challengeMapID))
	return;

$mapContent = displayMap(GenerateMapByCode($mapCode), $_GET["challengeMapID"]);
$challengeResultset = loadChallengesForMap($challengeMapID, $userID);
if($challengeResultset === NULL)
{
	redirectToChallengeListing();
	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 viewed in <a href='http://www.google.com/chrome'>Google Chrome</a>
	</noscript>
	<?
	echo $mapContent;
	displayChallenges($challengeResultset);
	?>
</div>


<script src="sounds/script/soundmanager.js"></script>
<script type="text/javascript">soundManagerInit();</script>
<?PHP echo $noteScript; ?>

<?php
htmlFooter();
?>

<?php
/**
 * Outputs the given list of challenges on the page
 */
function displayChallenges($challengeResultset)
{
	echo '<div id="challenges">';
	echo '<div id="challenges_title">Challenges</div>';
	echo '<div id="challenges_listing"><ul class="challenge_ulist">';
	while($challenge = mysql_fetch_array($challengeResultset))
	{
		$challengeMapID = $_GET["challengeMapID"];
		$challengeId = $challenge["challengeID"];
		if($challenge["dateSolved"] !== NULL)
			$cssClass = "challenge_complete";
		else
			$cssClass = "challenge_incomplete";
		$loadSolutionString = "<a href='javascript:requestChallengeSolution(\"$challengeMapID\", \"$challengeId\");'> Load this solution</a>";
		echo "<li class='$cssClass' id='challenge_id_$challengeId'>" . getChallengeDisplayString($challenge) . " $loadSolutionString </li>";
	}
	echo "</ul></div></div>";
}

/**
 * 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)
{
	$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'];
	
	$restrictions = array();
	$addUsingToText = false; //Grammar hack - specifies whether we want to add the word "using" to $returnMe

	//Wall-count restriction
	if($challenge['restrictWallCount'] !== NULL)
	{
		$restrictions[] = "only " . $challenge['restrictWallCount'] . " wall" . ($challenge['restrictWallCount'] == 1 ? "" : "s");
		$addUsingToText = true;
	}
	
	//Wall-placement restriction
	if($challenge['restrictWallPlacement'] !== NULL)
	{
		$invalidWalls = explode(".", $challenge['restrictWallPlacement']);
		$invalidWallsString = implode(") or (", $invalidWalls);
		$restrictions[] = "no walls at (" . $invalidWallsString . ")";
		$addUsingToText = true;
	}
	
	//Teleport-count
	if($challenge['restrictTeleportCount'] !== NULL)
	{
		if($challenge['restrictTeleportCount'] == 0)
			$restrictions[] = "no teleports";
		else
			$restrictions[] = "exactly " . $challenge['restrictTeleportCount'] . " teleports";
		$addUsingToText = true;
	}
	
	//Teleport points
	if($challenge['restrictTeleportsUsed'] !== NULL)
	{
		$invalidTeleports = explode(".", $challenge['restrictTeleportsUsed']);
		$invalidTeleportsString = implode(") or (", $invalidTeleports);
		$restrictions[] = "without using the teleport" . (count($invalidTeleports) == 1 ? "" : "s") . " at (" . $invalidTeleportsString . ")";
	}
	
	//Start point
	if($challenge['restrictStartPoint'] !== NULL)
	{
		$restrictions[] = "starting from (" . $challenge['restrictStartPoint'] . ")";
	}
	
	//End point
	if($challenge['restrictEndPoint'] !== NULL)
	{
		$restrictions[] = "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 ".
	$restrictionCount = count($restrictions);
	if($restrictionCount > 0)
	{
		if($restrictionCount == 1)
		{
			$restrictionString = $restrictions[0];
		}
		else
		{
			$lastRestriction = $restrictions[$restrictionCount - 1];
			unset($restrictions[$restrictionCount - 1]);
			$restrictionString = implode(", ", $restrictions) . " and " . $lastRestriction;
		}
		
		$returnMe .= ", " . ($addUsingToText ? "using " : "") . $restrictionString;
	}
	
	$returnMe .= ".";
	return $returnMe;
}

function redirectToChallengeListing()
{
	header("Location: /challengelist");
	die();
}
?>