summaryrefslogtreecommitdiffstats
path: root/includes/datas.php
blob: c6f7666ae78110bc6991955cf527b98c224a307c (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
<?PHP
// For interaction with the SQL database.
//

include_once('db.inc.php');

//Select Stats/Scores.
function topScores($mapid, $top = 5) {
	$sql = "
	SELECT 
		timediff(solutions.dateModified, TIMESTAMP(CURDATE())) as diff,
		users.displayName as display,
		solutions.moves as m,
		users.ID as ID,
		userData.displayColor,
		userData.wallColor,
		userData.wallEmblem,
		solutions.dateModified as cdate
	FROM 
		`users`
		JOIN `solutions`
			ON users.ID = solutions.userID
		LEFT JOIN `userData`
			ON users.ID = userData.userID
	WHERE solutions.mapID = '$mapid'
	ORDER BY solutions.moves DESC, solutions.dateModified ASC
	LIMIT $top
	";
	$result = mysql_query($sql);

	if (mysql_num_rows($result) > 0) {
		$output .= "<table class='score'>";
		$output .= "<tr>";
		$output .= "<th>Rank</th>";
		$output .= "<th>Badge</th>";
		$output .= "<th>Name</th>";
		$output .= "<th>Moves</th>";
		$output .= "<th title='Time since map generation'>Time</th>";
		$output .= "</tr>";
		while (list($diff, $display, $moves, $userID, $displayColor, $wallColor, $wallEmblem, $cdate) = mysql_fetch_row($result)) {
			$i++;
			if (!isset($displayColor)) {
					$sql = "INSERT INTO `userData` (`userID`, `displayColor`, `wallColor`)
					VALUES ('$userID', '#cccccc', '#666666')";
					mysql_query($sql);
					$displayColor = '#cccccc';
					$wallColor = '#666666';
					$wallEmblem = '';
			}
			if ($_SESSION['userID'] == $userID)
				$output .= "<tr style='background-color: #356;'>";
			else
				$output .= "<tr>";
			
			$cdate = date("g:i A", strtotime($cdate));
			
			$output .= "<td>$i</td>";
			$output .= "<td><div class='grid_td' style='width:35px; height:35px; ";
			$output .= "background:$wallColor";
				if ($wallEmblem != '')
					$output .= " url(images/marks/$wallEmblem)";
			$output .= ";'><div style='background-color:transparent;' class='grid_td_inner grid_td_rocks'></div></td>";
			$output .= "<td><span title='UserID: $userID'><a href='achievements?id=$userID' style='color:$displayColor'>$display</a></span></td>";
			$output .= "<td>$moves</td>";
			$output .= "<td title='Improved $cdate (EST)'>$diff</td>";
			$output .= "</tr>";
		}
		$output .= "</table>";
	}
	return $output;
}


//Returns text refering to any notifications.
function getNotified($userID) {

	$sql = "SELECT `ID`, `type`, `level`
	FROM `achievements`
	WHERE `notified` = false
	AND userID = '$userID'
	LIMIT 1";
	
	$result = mysql_query($sql);
	if (mysql_num_rows($result) !== 1) 
		return false;
	
	// --------- We found an achievement they don't know they got!
	list($aID, $aType, $aLevel) = mysql_fetch_row($result);
	
	$aTypeNames[1] = "Path Career Level $aLevel!";
	$aTypeNames[2] = "Mazes Career Level $aLevel!";
	$aTypeNames[32] = 'Tutorial Complete!';
	
	$aName = $aTypeNames[$aType];
	
	// --------- Any unlocks for this achievement?
	$sql = "SELECT `type`, `name`, `value`
	FROM `unlocks`
	WHERE `achievementID` = '$aID'";
	$result = mysql_query($sql);
	$unlocked = '';
	if (mysql_num_rows($result) >= 1) {
		$unlocked = "You've unlocked: ";
		while (list($uType, $uName, $uValue) = mysql_fetch_row($result)) {
			switch ($uType) {
			case 1:
				$unlocked .= "$uName Wall Color!";
				$unlocked .= "<table><tr><td onclick='changeWallColor(\"$uValue\")' style='background-color:$uValue;' class='grid_td_rocks'></td></tr></table>";
				break;
			case 2:
				$unlocked .= "$uName Emblem!";
				$unlocked .= "<div class='grid_td_rocks' title='' onClick='emblemclick(this,\"$uValue\", \"0\")' style='background: #999 url(../images/marks/$uValue);' >";
				$unlocked .= "<div class='grid_inner grid_td_walls'></div></div>";
				break;
			case 3:
				$unlocked .= "<br /><span style='color:$uValue;'>$uName Username color!</span><br />";
				break;
			}
		}
	}

	// ---------- Prepare response
	
	$r = "<strong>$aName</strong>";
	if ($unlocked !== '') {
		$r .= "<center>$unlocked";
		$r .= "<a href='achievements'>";
		$r .= "Go select this now</a>";
		$r .= "</center>";
	}
	// "Next level at $nextLevelN";  // unavailable data?
	
	// ---------- Mark that we have notified the user
	$sql = "UPDATE `achievements` 
	SET `notified` = 1
	WHERE `ID` = '$aID'";
	mysql_query($sql);

	return $r;
	// ---------- Done!
}

//Returns true when an achievement is applied.
// Usage example: applyCareerAchievement($userID);
function applyCareerPathAchievements($userID) {
	$sql = "SELECT 
	SUM(solutions.moves) as totalMoves
	FROM `solutions`
	WHERE userID = '$userID'";
	$result = mysql_query($sql);
	
	// --------- User hasn't played yet.
	if (mysql_num_rows($result) != 1)
		return false;
	
	list($uTotalMoves) = mysql_fetch_row($result);

	// ------- Select the highest level they've achieved.
	$sql = "SELECT `level`
	FROM `achievements`
	WHERE `userID` = '$userID'
	AND `type` = 1
	ORDER BY `level` DESC
	LIMIT 1";
	$result = mysql_query($sql);
	// --------- No achievements yet?
	if (mysql_num_rows($result) == 1)
		list($aLevel) = mysql_fetch_row($result);
	else
		$aLevel = 0;
	
	$cp = getCareerPathArray();

	//Is there a next level for this?
	$aNextLevel = $aLevel + 1;
	if (!isset($cp[$aNextLevel]))
		return false;
	list($required, $unlockType, $unlockValue, $unlockName) = $cp[$aNextLevel];
	
	if ($uTotalMoves < $required)
		return false;

	$sql = "INSERT INTO `achievements`
	(`userID`, `type`, `level`)
	VALUES ('$userID', 1, '$aNextLevel')";
	mysql_query($sql);
	$aID = mysql_insert_id();
	
	// ------- User gets unlock.  (unlocks plural is possible here)
	$sql = "INSERT INTO `unlocks`
	(`userID`, `achievementID`, `type`, `subType`, `name`, `value`)
	VALUES ('$userID', '$aID', '$unlockType', NULL, '$unlockName', '$unlockValue')";
	mysql_query($sql);
	return true;
}

//Almost a duplicate of applyCareerPathAchievements
function applyCareerMazesAchievements($userID) {
	$sql = "SELECT 
	COUNT(*) as totalSolutions
	FROM `solutions`
	WHERE userID = '$userID'";
	$result = mysql_query($sql);
	
	// --------- User hasn't played yet.
	if (mysql_num_rows($result) != 1)
		return false;
	
	list($uTotalSolutions) = mysql_fetch_row($result);

	// ------- Select the highest level they've achieved.
	$sql = "SELECT `level`
	FROM `achievements`
	WHERE `userID` = '$userID'
	AND `type` = 2
	ORDER BY `level` DESC
	LIMIT 1";
	$result = mysql_query($sql);
	// --------- No achievements yet?
	if (mysql_num_rows($result) == 1)
		list($aLevel) = mysql_fetch_row($result);
	else
		$aLevel = 0;
	
	$cm = getCareerMazesArray();

	//Is there a next level for this?
	$aNextLevel = $aLevel + 1;
	if (!isset($cm[$aNextLevel]))
		return false;
	list($required, $unlockType, $unlockValue, $unlockName) = $cm[$aNextLevel];
	
	if ($uTotalSolutions < $required)
		return false;

	$sql = "INSERT INTO `achievements`
	(`userID`, `type`, `level`)
	VALUES ('$userID', 2, '$aNextLevel')";
	mysql_query($sql);
	$aID = mysql_insert_id();
	
	// ------- User gets unlock.  (unlocks plural is possible here)
	$sql = "INSERT INTO `unlocks`
	(`userID`, `achievementID`, `type`, `subType`, `name`, `value`)
	VALUES ('$userID', '$aID', '$unlockType', NULL, '$unlockName', '$unlockValue')";
	mysql_query($sql);
	return true;
}

//Challenges including the Tutorial.
function applyChallengeAchievements($userID, $challengeID, $mapID, $solution, $moves) {
	if ($challengeID == '1') {
		//echo 'challengeID'.$mapID;
		//echo 'moves'.$moves;
		if ($moves == 75 AND $mapID == 5) {
			//echo 'true story';
			// -------- Completed the tutorial!
			
			//have they already completed he tutorial?
			$sql = "SELECT `level`
			FROM `achievements`
			WHERE `userID` = '$userID'
			AND `type` = 32
			ORDER BY `level` DESC
			LIMIT 1";
			$result = mysql_query($sql);
			if (mysql_num_rows($result) == 1)
				return false;
				
			// -------- This is the first time they've completed the tutorial!
			$sql = "INSERT INTO `achievements`
			(`userID`, `type`, `level`)
			VALUES ('$userID', 32, '1')";
			mysql_query($sql);
			$aID = mysql_insert_id();
			
			// ------- User gets unlock.  (unlocks plural is possible here)
			$sql = "INSERT INTO `unlocks`
			(`userID`, `achievementID`, `type`, `subType`, `name`, `value`)
			VALUES ('$userID', '$aID', '1', NULL, 'Blue', '#4444ff')";
			mysql_query($sql);
			return true;
		}
	}
}


function getAchievementsArray($type) {
	if ($type == 1)
		return getCareerPathArray();
	if ($type == 2)
		return getCareerMazesArray();
	//Tutorial
	if ($type == 32) {
		$r[1] = array(1, 1, '#4444ff', 'Blue');
		return $r;
	}
	return array();
}

//TYPE 1
function getCareerPathArray() {
	//$cp[0] = array(requiredmoves, type, 'value', 'name');
	$cp[1] = array(100, 1, '#229922', 'Green');
	$cp[2] = array(400, 1, '#9922ff', 'Purple');
	$cp[3] = array(11, 2,'CircleSmall.png','Small White Circle');
	$cp[4] = array(11, 2,'StarsR_W.png','Starry White');
	$cp[5] = array(11, 2,'StarsR_B.png','Starry Black');
	$cp[6] = array(11, 1, '#a9b1f6', 'Steel Blue');
	$cp[7] = array(5000, 3, '#aafcbb', 'Green');
	$cp[8] = array(7500, 3, '#ffffaa', 'Yellow');
	
	return $cp;
}

//TYPE 2
function getCareerMazesArray() {
	//$cp[0] = array(requiredmazes, type, 'value', 'name');
	$cm[1] = array(1, 1, '#eeeeee', 'Silver');
	$cm[2] = array(5, 1, '#22aaaa', 'Teal');
	$cm[3] = array(20, 1, '#ff3344', 'Red');
	$cm[4] = array(50, 1, '#aaaa22', 'Chartreuse');
	$cm[5] = array(75, 1, '#cc22aa', 'Orange');
	$cm[6] = array(100, 1, '#ff9922', 'Magenta');

	return $cm;
}


function apply() {


}

?>