summaryrefslogtreecommitdiffstats
path: root/includes/playerStats.php
blob: 5640dc7b6b9e8ba0943784e05e96c1dd0ca7bdaf (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
<?php

include_once('constants.php');

/**
 * Sets a flag on all solutions which are the high score (first to tie the best length)
 */
function setHighScoreFlags($mapExpireStartRange, $mapExpireEndRange = "NOW()")
{
	$mapExpireStartRange =  quoteSQLDateString($mapExpireStartRange);
	$mapExpireEndRange =  quoteSQLDateString($mapExpireEndRange);
	
	$sql1 = "
	UPDATE solutions
	INNER JOIN
	(
		SELECT a1.id FROM solutions AS a1
		INNER JOIN mapOfTheDay ON a1.mapID = mapOfTheDay.mapID
		WHERE mapOfTheDay.mapExpireTime >= $mapExpireStartRange
		AND mapOfTheDay.mapExpireTime <= $mapExpireEndRange
		AND NOT EXISTS
		(
			-- Only select rows which have highest score for map, or tied for highest with earliest date, or tied for date too with earliest ID
			SELECT 0 FROM solutions AS a2
			WHERE a2.mapID = a1.mapID AND 
				 (a2.moves > a1.moves OR (a2.moves = a1.moves AND (a2.dateModified < a1.dateModified OR (a2.dateModified = a1.dateModified AND a2.ID > a1.ID))))
		)
	) tmp ON tmp.id = solutions.id
	SET solutions.isHighScore = 1
	";
	
	$sql2 = "
	UPDATE solutions
	INNER JOIN
	(
		SELECT mapID, MAX(moves) AS moves
		FROM solutions AS s2
		WHERE isHighScore = 1
		GROUP BY mapID
	) AS maxMoves ON maxMoves.mapID = solutions.mapID AND solutions.moves = maxMoves.moves
	INNER JOIN mapOfTheDay ON mapOfTheDay.mapID = solutions.mapID
	SET solutions.isTiedForHighScore = 1
	WHERE mapOfTheDay.mapExpireTime >= $mapExpireStartRange
	AND mapOfTheDay.mapExpireTime <= $mapExpireEndRange
	";
	
	mysql_query($sql1);
	mysql_query($sql2);
}

/**
 * Calculates the worth of all maps' champion points from after the given date
 * @param $mapExpireTime The earliest mapExpireTime to count from.  Use SQL date-string format.
 *                  Pass in today's date, for example, for all maps that expired at midnight today
 */
function calculateMapsChampionPointWorth($mapExpireStartRange, $mapExpireEndRange = "NOW()")
{
	$mapExpireStartRange =  quoteSQLDateString($mapExpireStartRange);
	$mapExpireEndRange =  quoteSQLDateString($mapExpireEndRange);
	
	//We take the worth of the map, multiply by the number of people who tied the high score, then divide by the
	//total number of people who attempted the map (minus 1)
	$sql = "
	UPDATE mapOfTheDay
	SET championPointsWorth = GREATEST(".CP_MIN_WORTH.",
	(
		CASE mapType
		WHEN 1 THEN ".CP_MAX_WORTH_SIMPLE."
		WHEN 2 THEN ".CP_MAX_WORTH_NORMAL."
		WHEN 3 THEN ".CP_MAX_WORTH_COMPLEX."
		WHEN 4 THEN ".CP_MAX_WORTH_SPECIAL."
		WHEN 5 THEN ".CP_MAX_WORTH_WEEKLY."
		ELSE 0
		END
	) * (
		SELECT COUNT(*)
		FROM solutions
		WHERE solutions.mapID = mapOfTheDay.mapID
		AND solutions.isTiedForHighScore = 0
	) / (
		SELECT GREATEST(COUNT(*)-1, 1)
		FROM solutions
		WHERE solutions.mapID = mapOfTheDay.mapID
	))
	WHERE mapExpireTime >= $mapExpireStartRange
	AND mapExpireTime <= $mapExpireEndRange
	";
	
	mysql_query($sql);
}

/**
 * Adds the champion points for each player for every map that expired on or after the given date.
 * Thus, if you're using this to add points from yesterday's maps, this should be done
 * after the date has changed.
 * @param $mapExpireTime The earliest mapExpireTime to count from.  Use SQL date-string format.
 *                  Pass in today's date, for example, for all maps that expired at midnight today
 */
function addPlayerChampionPointsForMapsOfTheDay($mapExpireStartRange, $mapExpireEndRange = "NOW()")
{
	$mapExpireStartRange =  quoteSQLDateString($mapExpireStartRange);
	$mapExpireEndRange =  quoteSQLDateString($mapExpireEndRange);
	
	//Weird syntax of these queries is necessary due to a bug in MySQL with updating a table and selecting a table
	//in the same query.  See http://www.xaprb.com/blog/2006/06/23/how-to-select-from-an-update-target-in-mysql/
	$sql1 = "
	-- Add the points for tying the map
	UPDATE solutions
	INNER JOIN mapOfTheDay ON mapOfTheDay.mapID = solutions.mapID
	SET solutions.championPointsWorth = mapOfTheDay.championPointsWorth
	WHERE mapOfTheDay.mapExpireTime >= $mapExpireStartRange
	AND mapOfTheDay.mapExpireTime <= $mapExpireEndRange
	AND mapOfTheDay.mapType <> 5
	AND solutions.isTiedForHighScore
	";

	$sql2 = "
	-- Add the points for doing the weekly maps
	UPDATE solutions
	INNER JOIN mapOfTheDay ON mapOfTheDay.mapID = solutions.mapID
	SET solutions.championPointsWorth =
	(
		SELECT IFNULL(".CP_MAX_WORTH_WEEKLY." * 
			(
				SELECT COUNT(*)
				FROM (SELECT * FROM solutions) AS s2
				WHERE s2.mapID = mapOfTheDay.mapID
				AND s2.moves < usersSolutions.moves
			) / (
				SELECT GREATEST(COUNT(*)-1, 1)
				FROM (SELECT * FROM solutions) AS s2
				WHERE s2.mapID = mapOfTheDay.mapID
			) + ".CP_MIN_WORTH_WEEKLY.", 0)
		FROM (SELECT * FROM solutions) AS usersSolutions
		WHERE usersSolutions.ID = solutions.ID
	)
	WHERE mapOfTheDay.mapExpireTime >= $mapExpireStartRange
	AND mapOfTheDay.mapExpireTime <= $mapExpireEndRange
	AND mapOfTheDay.mapType = 5
	";
	
	$sql3 = "
	-- Add the points for attempting the map
	UPDATE solutions
	INNER JOIN mapOfTheDay ON mapOfTheDay.mapID = solutions.mapID
	SET solutions.championPointsWorth = ".CP_POINTS_FOR_ATTEMPTING."
	WHERE solutions.championPointsWorth = 0
	AND mapOfTheDay.mapExpireTime >= $mapExpireStartRange
	AND mapOfTheDay.mapExpireTime <= $mapExpireEndRange
	";
	
	$sql4 = "
	-- Add the extra for being the first to tie the map
	UPDATE solutions
	INNER JOIN mapOfTheDay ON mapOfTheDay.mapID = solutions.mapID
	SET solutions.championPointsWorth = solutions.championPointsWorth * (1 + ".CP_EXTRA_PERCENT_FOR_FIRST.")
	WHERE solutions.isHighScore = 1
	AND mapOfTheDay.mapExpireTime >= $mapExpireStartRange
	AND mapOfTheDay.mapExpireTime <= $mapExpireEndRange
	";
	
	//TODO: Points for doing challenges too
	//TODO: Is summing the entire table every time slow?  If so, we should just add the points instead
	$sql5 = "
	-- Simply re-sum all the points
	UPDATE users
	INNER JOIN
	(
		SELECT SUM(solutions.championPointsWorth) AS championPointsWorth, solutions.userID
		FROM solutions
		INNER JOIN mapOfTheDay ON solutions.mapID = mapOfTheDay.mapID
		WHERE mapOfTheDay.mapExpireTime >= $mapExpireStartRange
		AND mapOfTheDay.mapExpireTime <= $mapExpireEndRange
	    GROUP BY solutions.userID
	) AS newPointsEarned ON users.ID = newPointsEarned.userID
	SET users.championPoints = users.championPoints + newPointsEarned.championPointsWorth
	";

	mysql_query($sql1);
	mysql_query($sql2);
	mysql_query($sql3);
	mysql_query($sql4);
	mysql_query($sql5);
}

/**
 * Sets all players wins and draws to 0.  Should be done before a total recalculation
 */
function clearAllStatsBeforeRecalculation()
{
	$sql1 = "
	UPDATE users
	SET totalWins = 0, totalTies = 0, totalMazes = 0, totalMoves = 0;
	";
	
	$sql2 = "
	UPDATE solutions
	SET championPointsWorth = 0, isHighScore = 0, isTiedForHighScore = 0
	";
	
	$sql3 = "
	UPDATE mapOfTheDay
	SET challengePointsWorth = 0;
	";

	mysql_query($sql1);
	mysql_query($sql2);
	mysql_query($sql3);
}

/**
 * Calculates players' total wins, draws, moves, and mazes, adding from the given date
 */
function addOtherStats($mapExpireStartRange, $mapExpireEndRange = "NOW()")
{
	$mapExpireStartRange =  quoteSQLDateString($mapExpireStartRange);
	$mapExpireEndRange =  quoteSQLDateString($mapExpireEndRange);
	
	$sql = "
	-- Add wins, ties, mazes, and moves all at once
	UPDATE users
	INNER JOIN
	(
		SELECT solutions.userID,
			SUM(isHighScore) AS totalWins,
			SUM(isTiedForHighScore) AS totalTies,
			SUM(solutions.moves) AS totalMoves,
			COUNT(*) AS totalMazes
		FROM solutions
		INNER JOIN mapOfTheDay ON solutions.mapID = mapOfTheDay.mapID
		WHERE mapOfTheDay.mapExpireTime >= $mapExpireStartRange
		AND mapOfTheDay.mapExpireTime <= $mapExpireEndRange
		GROUP BY solutions.userID
	) AS newPoints ON users.ID = newPoints.userID
	SET users.totalWins = users.totalWins + newPoints.totalWins,
	    users.totalTies = users.totalTies + newPoints.totalTies,
		users.totalMoves = users.totalMoves + newPoints.totalMoves,
		users.totalMazes = users.totalMazes + newPoints.totalMazes
	";

	mysql_query($sql);
}

/**
 * Adds quotes around an SQL date string and escapes it, if necessary
 */
function quoteSQLDateString($dateString)
{
	if($dateString == "NOW()")
	{
		return $dateString;
	}
	return "'" . mysql_escape_string($dateString) . "'";
}

/**
 * Calculates the worth of the maps for yesterday, and distributes points for them.  Also adds wins and ties to
 * the correct players
 */
function addStatsForYesterdaysMaps()
{
	$today = strtotime('today');
	addStatsForDateTimeInternal($today);
}

/**
 * Adds stats for maps that end mid-day (namely, weekly maps)
 */
function addStatsForMiddayMaps()
{
	$noonToday = strtotime('noon');
	addStatsForDateTimeInternal($noonToday);
}

/**
 * Adds stats for maps that expire after the given date-time.  Should not be called outside of playerStats.php
 */
function addStatsForDateTimeInternal($dateTime)
{
	$dateTimeStr = date('Y-m-d H:i:s', $dateTime);
	setHighScoreFlags($dateTimeStr);
	calculateMapsChampionPointWorth($dateTimeStr);
	addPlayerChampionPointsForMapsOfTheDay($dateTimeStr);
	addOtherStats($dateTimeStr);
}

/**
 * Recalculates all player's total champion points, wins, and ties.
 * Will probably be extremely slow, so should not be called often!
 */
function recalculateStatsForAllPlayers()
{
	clearAllStatsBeforeRecalculation();
	
	$startDate = new DateTime(STATS_EARLIEST_DATE);
	$currentEnd = $startDate;
	$currentEndStr = "";
	$now = new DateTime("now");
	
	while($currentEndStr != "NOW()")
	{
		$currentStart = new DateTime($currentEnd->format("Y-m-d"));
		$currentStart->add(new DateInterval("PT1S")); //Add one second to start time, since it's inclusive
		
		$currentEnd = new DateTime($currentEnd->format("Y-m-d"));
		$currentEnd->add(new DateInterval("P1M")); //Run queries for one month at a time
		
		$currentStartStr = $currentStart->format("Y-m-d H:i:s");
		$currentEndStr = $currentEnd->format("Y-m-d H:i:s");
		
		if($currentEnd > $now)
		{
			$currentEndStr = "NOW()";
		}
		
		setHighScoreFlags($currentStartStr, $currentEndStr);
		calculateMapsChampionPointWorth($currentStartStr, $currentEndStr);
		addPlayerChampionPointsForMapsOfTheDay($currentStartStr, $currentEndStr);
		//TODO: Champion points for challenges!
		addOtherStats($currentStartStr, $currentEndStr);
	}
}
?>