summaryrefslogtreecommitdiffstats
path: root/ajax/challenges.ajax.php
blob: 0caafd107b5971c44c643d99be4779faaf4a7531 (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
<?
ob_start("ob_gzhandler");

include('../includes/mapclass.php');
include('../includes/maps.php');
include_once('../includes/sqlEmbedded.php');

if ($_GET['getChallengeIDs'] == 'true') {
	echo getChallengesByTier($_GET['tier'], $_GET['userID']);
	exit;
}

if ($_GET['getChallengeMap'] == 'true') {
	echo getChallengeMapByID($_GET['challengeMapID']);
	exit;
}

if ($_GET['getChallenges'] == 'true') {
	echo json_encode(getChallenges($_GET['challengeMapID'], $_GET['userID']));
	exit;
}

function getChallengeMapByID($challengeMapID) {
	//62 days
	$expires = 62*24*60*60;
	// header("Cache-Control: public, maxage=".$expires);
	// header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
	// header("Content-type: text/javascript");

	//Remove the ending .js
	$tmp = explode(".", $challengeMapID);
	$challengeID = $tmp[0] * 1;
	if (!is_int($challengeID)) exit;

	$map = new map(getChallengeCode($challengeID), $challengeID);
	return json_encode($map);
}

function getChallengeCode($challengeID) {
	$sql = "SELECT `code`
		FROM `challengeMaps`
		WHERE `ID` = '$challengeID'
		";
	$result = mysql_query($sql);
	if (mysql_num_rows($result) > 0) {
		list($map) = mysql_fetch_row($result);
		return $map;
	}
}



function getChallengesByTier($tierUnsanitized, $userIdUnsanitized) {
	$userID = mysql_escape_string($userIdUnsanitized);
	$tier = mysql_escape_string($tierUnsanitized);
	
	$sql = "SELECT challengeMaps.ID AS challengeMapID, challengeMaps.challengeTier, challengeMaps.name AS name,
	challengeSolutions.dateSolved, challengeSolutions.challengeID AS challengeID,
	challengeMaps.challengeSuborder, challenges.ordering, challengeMaps.code as mapCode
	FROM challenges
	INNER JOIN challengeMaps ON challenges.mapID = challengeMaps.ID
	LEFT JOIN challengeSolutions ON challenges.ID = challengeSolutions.challengeID AND challengeSolutions.userID = '$userID'
	WHERE challenges.enabled = 1
	AND challengeMaps.challengeTier = '$tier'
	ORDER BY challengeMaps.challengeTier, challengeMaps.challengeSuborder, challenges.ordering";
	
	//AND challengeMaps.challengeTier <= (SELECT challengeTier FROM users WHERE ID = '$userID' LIMIT 1) OR 1
	
	$result = mysql_query($sql) OR die(mysql_error()."SQL: $sql");
	if (mysql_num_rows($result) >= 1) {
		$i = 0;
		//$layer['x'] = '';
		while($data = mysql_fetch_assoc($result)) {
			$i++;
			//echo $data['challengeMapID'];
			$tier = $data['challengeTier'];
			$challengeSuborder = $data['challengeSuborder'];
			$ordering = $data['ordering'];
			$challenges[$tier][$challengeSuborder][$ordering] = $data;
			//$data['mapObject'] = $challenge;
			$challengeMapID = $data['challengeMapID'];
			$challengeID = $data['challengeID'];
			
			$mapObj = new map($data['mapCode']);
			$mapObj->ID = $challengeMapID;
			$layer['CM'.$data['challengeMapID']][$challengeID] = $data;
			$layer['CM'.$data['challengeMapID']]['mapObject'] = $mapObj;
			$challenges = getChallenges($data['challengeMapID'], $userID);
			$layer['CM'.$data['challengeMapID']]['challenges'] = $challenges;
			//$layer[] = $data;
		}
		return json_encode($layer);
	}
	return NULL;
}


function getChallenges($mapIdUnsanitized, $userIdUnsanitized) {
	$result = loadChallengesForMap($mapIdUnsanitized, $userIdUnsanitized);
	while($challenge = mysql_fetch_assoc($result)) {
		$data[] = $challenge;
	}
	return $data;
}


/**
 * Returns a MySQL resultset for all challenges for the given mapID
 * @param $mapIdUnsanitized The mapID to load.  Assumed to be unsanitized.
 * @param $userID The userID.  Assumed to be unsanitized
 * @return Returns a MySQL resultset with the columns listed in the code, or NULL if nothing found
 */
function loadChallengesForMap($mapIdUnsanitized, $userIdUnsanitized)
{
	$mapID = mysql_escape_string($mapIdUnsanitized);
	$userID = mysql_escape_string($userIdUnsanitized);
	$sql = "
	SELECT challenges.ID AS challengeID, challenges.inequality, challenges.goal, challenges.hint,
		challenges.restrictWallCount, challenges.restrictWallPlacement, challenges.restrictTeleportCount,
		challenges.restrictTeleportsUsed, challenges.restrictStartPoint, challenges.restrictEndPoint,
		challengeSolutions.dateSolved,
		challenges.dialogStart, challenges.dialogFail, challenges.dialogSuccess
	FROM challenges
	LEFT JOIN challengeSolutions ON challenges.ID = challengeSolutions.challengeID AND challengeSolutions.userID = '$userID'
	WHERE challenges.mapID = '$mapID' AND challenges.enabled = 1
	ORDER BY challenges.ordering
	";
	
	$result = mysql_query($sql);
	echo mysql_error();
	if (mysql_num_rows($result) >= 1)
		return $result;
	return NULL;
}



?>