From 099e2d5ab8effe07b0e5d8c59e18c81f519fd833 Mon Sep 17 00:00:00 2001 From: BlueRaja Date: Sun, 13 Jan 2013 19:27:39 -0600 Subject: Added totalWins and totalTies columns to the users table. Also, removed all references to the 'statistics' table - it's not needed anymore. --- includes/championPoints.php | 204 --------------------------------- includes/datas.php | 82 +------------- includes/mapoftheday.php | 2 +- includes/playerStats.php | 271 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 277 insertions(+), 282 deletions(-) delete mode 100644 includes/championPoints.php create mode 100644 includes/playerStats.php (limited to 'includes') diff --git a/includes/championPoints.php b/includes/championPoints.php deleted file mode 100644 index 28b2b78..0000000 --- a/includes/championPoints.php +++ /dev/null @@ -1,204 +0,0 @@ -= '$fromDate' - ) - "; - - mysql_query($sql); -} - -/** - * Calculates the worth of all maps' champion points from after the given date - * @param $fromDate The earliest date to count from. Use SQL date-string format. - * Pass in yesterday's date, for example, to add champion points from yesterday's maps - */ -function calculateMapsChampionPointWorth($fromDate) -{ - $fromDate = mysql_escape_string($fromDate); - - //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.moves < - ( - SELECT MAX(moves) - FROM solutions AS s2 - WHERE s2.mapId = mapOfTheDay.mapID - ) - ) / ( - SELECT GREATEST(COUNT(*)-1, 1) - FROM solutions - WHERE solutions.mapID = mapOfTheDay.mapID - )) - WHERE mapDate >= '$fromDate' - "; - - mysql_query($sql); -} - -/** - * Adds the champion points for each player for every day after the given date, but before today. - * Thus, if you're using this to add points from yesterday's maps, this should be done - * after the date has changed. - * @param $fromDate The earliest date to count from. SQL date-string format. - * Pass in yesterday's date, for example, to add champion points from yesterday's maps - */ -function addPlayerChampionPointsForMapsOfTheDay($fromDate) -{ - $fromDate = mysql_escape_string($fromDate); - - //TODO: How do you know when an ultra-complex map has completed? - - //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 - ( - SELECT mapID, moves - FROM solutions AS s2 - WHERE isHighScore = 1 - ) AS maxMoves ON maxMoves.mapID = solutions.mapID - INNER JOIN mapOfTheDay ON mapOfTheDay.mapID = solutions.mapID - SET solutions.championPointsWorth = mapOfTheDay.championPointsWorth - WHERE mapOfTheDay.mapDate >= '$fromDate' - AND mapOfTheDay.mapType <> 5 - AND solutions.moves = maxMoves.moves - "; - - $sql2 = " - -- Add the extra for being the first to tie the map - UPDATE solutions - SET solutions.championPointsWorth = solutions.championPointsWorth * (1 + ".CP_EXTRA_PERCENT_FOR_FIRST.") - WHERE solutions.isHighScore = 1 - AND solutions.dateModified >= $fromDate - "; - - //TODO: This doesn't work right, weekly maps last more than a day... - $sql3 = " - -- Add the points for doing the weekly maps - UPDATE solutions - SET championPointsWorth = championPointsWorth + - ( - SELECT IFNULL(SUM(".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 mapOfTheDay - INNER JOIN (SELECT * FROM solutions) AS usersSolutions ON usersSolutions.mapID = mapOfTheDay.mapID - WHERE usersSolutions.ID = solutions.ID - AND mapOfTheDay.mapType = 5 - AND mapOfTheDay.mapDate >= '$fromDate' - ) - WHERE solutions.dateModified >= $fromDate; - "; - - $sql4 = " - -- Add the points for attempting the map - UPDATE solutions - SET championPointsWorth = ".CP_POINTS_FOR_ATTEMPTING." - WHERE championPointsWorth = 0 - AND dateModified >= $fromDate - AND dateModified < CURDATE() - "; - - //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 - SET championPoints = - ( - SELECT SUM(championPointsWorth) - FROM solutions - WHERE solutions.userID = users.ID - ) - "; - - mysql_query($sql1); - mysql_query($sql2); - mysql_query($sql3); - mysql_query($sql4); - mysql_query($sql5); -} - -/** - * Calculates the worth of the maps for yesterday, and distributes points for them - */ -function addChampionPointsForYesterdaysMaps() -{ - $yesterday = strtotime('-1 day', time()); - $yesterdayStr = date('Y-m-d', $yesterday); - setIsHighScoreFlag($yesterday); - calculateMapsChampionPointWorth($yesterdayStr); - addPlayerChampionPointsForMapsOfTheDay($yesterdayStr); -} - -/** - * Recalculates all player's total champion points. - * Will probably be extremely slow, so should not be called often! - */ -function recalculateAllPlayersChampionPoints() -{ - setIsHighScoreFlag(CP_EARLIEST_DATE); - calculateMapsChampionPointWorth(CP_EARLIEST_DATE); - addPlayerChampionPointsForMapsOfTheDay(CP_EARLIEST_DATE); - //TODO: Champion points for challenges! -} -?> diff --git a/includes/datas.php b/includes/datas.php index 6d73230..b29f1c9 100644 --- a/includes/datas.php +++ b/includes/datas.php @@ -572,11 +572,9 @@ function getAchievementCurrency($userID, $aType) { WHERE userID = '$userID'"; break; case 3: - $sql = "SELECT - SUM(`value`) as t - FROM `statistics` - WHERE `userID` = '$userID' - AND `type` IN (1, 2, 3, 4)"; + $sql = "SELECT totalTies + FROM `users` + WHERE `userID` = '$userID'"; break; case 4: $sql = "SELECT championPoints @@ -711,74 +709,6 @@ function getChampionPointsArray() { return $r; } -//Used to track statistics on who wins or ties maps at the end of a day. -function trackMOTDstats($mapType) { - // Our rather massive query to get the data we need. - $sql = "SELECT - users.ID as userID, - SUM(solutions.moves) as Moves, - timediff(MAX(dateModified), maps.dateCreated) as Timetaken - FROM `maps` - JOIN `solutions` - ON maps.ID = solutions.mapID - JOIN `users` - ON solutions.userID = users.ID - JOIN `mapOfTheDay` - ON maps.ID = mapOfTheDay.mapID - WHERE - DATE_ADD(CURDATE(), INTERVAL -1 DAY) = - DATE_FORMAT(solutions.dateModified,'%Y-%m-%d') - AND DATE_ADD(CURDATE(), INTERVAL -1 DAY) = - DATE_FORMAT(maps.dateCreated,'%Y-%m-%d') - AND `mapType` = '$mapType' - GROUP BY solutions.userID - ORDER BY Moves DESC, MAX(dateModified) ASC, solutions.ID DESC - "; - - $mainResult = mysql_query($sql); - if ($mainResult) { - $first = true; - while (list($userID, $uMoves, $uTimeTaken) = mysql_fetch_row($mainResult)) { - //echo "$first : userID: $userID uMoves: $uMoves uTimeTaken: $uTimeTaken mapType: $mapType
"; - do { - //If it's the rank #1 user, we need to repeat to also award him the tie stat. - $repeatThis = false; - $sType = $mapType; - if ($first) { - $bestMoves = $uMoves; - $sType = 31 + $mapType; - $repeatThis = true; - } - if ($uMoves == $bestMoves) { - $sql = "SELECT `ID` FROM `statistics` - WHERE `userID` = '$userID' AND `type` = '$sType'"; - $result = mysql_query($sql); - - if (mysql_num_rows($result) == 0) { - $sql = "INSERT INTO `statistics` (`userID`, `type`, `value`) - VALUES ('$userID', '$sType', 1)"; - $result = mysql_query($sql); - if (!$result) - return false; - } else { - $sql = "UPDATE `statistics` - SET `value` = `value` + 1 - WHERE `userID` = '$userID' AND `type` = '$sType'"; - $result = mysql_query($sql); - if (!$result) - return false; - } - - } else { - break 2; - } - $first = false; - } while ($repeatThis); - } - } - return true; -} - //Select Members function getMembers($order, $pageNumber = 1, $pageDivide = 50) { @@ -826,8 +756,8 @@ function getMembers($order, $pageNumber = 1, $pageDivide = 50) { WHERE solutions.userID = users.ID AND solutions.dateModified < CURRENT_DATE) , 0) AS totalMazes, - SUM(CASE WHEN statistics.type IN (32, 33, 34, 35) THEN statistics.value ELSE 0 END) as totalWins, - SUM(CASE WHEN statistics.type IN (1, 2, 3, 4) THEN statistics.value ELSE 0 END) as totalTies, + users.totalWins, + users.totalTies, IfNull(userData.wallColor, '#666666') as wallColor, IfNull(userData.displayColor, '#cccccc'), userData.wallEmblem as wallEmblem, @@ -835,8 +765,6 @@ function getMembers($order, $pageNumber = 1, $pageDivide = 50) { users.dateLogin as dateLogin FROM `users` LEFT JOIN `userData` ON users.ID = userData.userID - LEFT JOIN `statistics` ON users.ID = statistics.userID - GROUP BY users.ID $order LIMIT $limitTop, $pageDivide "; diff --git a/includes/mapoftheday.php b/includes/mapoftheday.php index 3a472ba..178dc64 100644 --- a/includes/mapoftheday.php +++ b/includes/mapoftheday.php @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/includes/playerStats.php b/includes/playerStats.php new file mode 100644 index 0000000..b302812 --- /dev/null +++ b/includes/playerStats.php @@ -0,0 +1,271 @@ += '$fromDate' + ) + "; + + mysql_query($sql); +} + +/** + * Calculates the worth of all maps' champion points from after the given date + * @param $fromDate The earliest date to count from. Use SQL date-string format. + * Pass in yesterday's date, for example, to add champion points from yesterday's maps + */ +function calculateMapsChampionPointWorth($fromDate) +{ + $fromDate = mysql_escape_string($fromDate); + + //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.moves < + ( + SELECT MAX(moves) + FROM solutions AS s2 + WHERE s2.mapId = mapOfTheDay.mapID + ) + ) / ( + SELECT GREATEST(COUNT(*)-1, 1) + FROM solutions + WHERE solutions.mapID = mapOfTheDay.mapID + )) + WHERE mapDate >= '$fromDate' + "; + + mysql_query($sql); +} + +/** + * Adds the champion points for each player for every day after the given date, but before today. + * Thus, if you're using this to add points from yesterday's maps, this should be done + * after the date has changed. + * @param $fromDate The earliest date to count from. SQL date-string format. + * Pass in yesterday's date, for example, to add champion points from yesterday's maps + */ +function addPlayerChampionPointsForMapsOfTheDay($fromDate) +{ + $fromDate = mysql_escape_string($fromDate); + + //TODO: How do you know when an ultra-complex map has completed? + + //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 + ( + SELECT mapID, moves + FROM solutions AS s2 + WHERE isHighScore = 1 + ) AS maxMoves ON maxMoves.mapID = solutions.mapID + INNER JOIN mapOfTheDay ON mapOfTheDay.mapID = solutions.mapID + SET solutions.championPointsWorth = mapOfTheDay.championPointsWorth + WHERE mapOfTheDay.mapDate >= '$fromDate' + AND mapOfTheDay.mapDate < CURDATE() + AND mapOfTheDay.mapType <> 5 + AND solutions.moves = maxMoves.moves + "; + + $sql2 = " + -- Add the extra for being the first to tie the map + UPDATE solutions + SET solutions.championPointsWorth = solutions.championPointsWorth * (1 + ".CP_EXTRA_PERCENT_FOR_FIRST.") + WHERE solutions.isHighScore = 1 + AND solutions.dateModified >= $fromDate + AND solutions.dateModified < CURDATE() + "; + + //TODO: This doesn't work right, weekly maps last more than a day... + $sql3 = " + -- Add the points for doing the weekly maps + UPDATE solutions + SET championPointsWorth = championPointsWorth + + ( + SELECT IFNULL(SUM(".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 mapOfTheDay + INNER JOIN (SELECT * FROM solutions) AS usersSolutions ON usersSolutions.mapID = mapOfTheDay.mapID + WHERE usersSolutions.ID = solutions.ID + AND mapOfTheDay.mapType = 5 + AND mapOfTheDay.mapDate >= '$fromDate' + AND mapOfTheDay.mapDate < CURDATE() + ) + WHERE solutions.dateModified >= $fromDate; + AND mapOfTheDay.mapDate < CURDATE() + "; + + $sql4 = " + -- Add the points for attempting the map + UPDATE solutions + SET championPointsWorth = ".CP_POINTS_FOR_ATTEMPTING." + WHERE championPointsWorth = 0 + AND dateModified >= $fromDate + AND dateModified < CURDATE() + "; + + //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 + SET championPoints = + ( + SELECT SUM(championPointsWorth) + FROM solutions + WHERE solutions.userID = users.ID + ) + "; + + 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 clearWinsAndTies() +{ + $sql = " + UPDATE users + SET totalWins = 0, totalTies = 0; + "; + + mysql_query($sql); +} + +/** + * Calculates players' total wins and draws, adding from the given date + */ +function addWinsAndTies($fromDate) +{ + $sql1 = " + -- Add the players' latest wins + UPDATE users + SET totalWins = totalWins + + ( + SELECT COUNT(*) + FROM solutions + INNER JOIN mapOfTheDay ON solutions.mapID = mapOfTheDay.mapID + WHERE solutions.isHighScore = 1 + AND mapOfTheDay.mapDate >= '$fromDate' + AND mapOfTheDay.mapDate < CURDATE() + AND solutions.userID = users.ID + ); + "; + + $sql2 = " + -- Add the players' latest ties + UPDATE users + SET totalTies = totalTies + + ( + SELECT COUNT(*) + FROM solutions + INNER JOIN mapOfTheDay ON solutions.mapID = mapOfTheDay.mapID + WHERE mapOfTheDay.mapDate >= '$fromDate' + AND mapOfTheDay.mapDate < CURDATE() + AND solutions.userID = users.ID + AND solutions.moves = + ( + SELECT moves + FROM solutions AS s2 + WHERE s2.mapID = mapOfTheDay.mapID + AND s2.isHighScore = 1 + LIMIT 1 + ) + ); + "; + + mysql_query($sql1); + mysql_query($sql2); +} + +/** + * Calculates the worth of the maps for yesterday, and distributes points for them. Also adds wins and ties to + * the correct players + */ +function addStatsForYesterdaysMaps() +{ + $yesterday = strtotime('-1 day', time()); + $yesterdayStr = date('Y-m-d', $yesterday); + setIsHighScoreFlag($yesterday); + calculateMapsChampionPointWorth($yesterdayStr); + addPlayerChampionPointsForMapsOfTheDay($yesterdayStr); + addWinsAndTies($yesterdayStr); +} + +/** + * Recalculates all player's total champion points, wins, and ties. + * Will probably be extremely slow, so should not be called often! + */ +function recalculateStatsForAllPlayers() +{ + setIsHighScoreFlag(CP_EARLIEST_DATE); + calculateMapsChampionPointWorth(CP_EARLIEST_DATE); + addPlayerChampionPointsForMapsOfTheDay(CP_EARLIEST_DATE); + //TODO: Champion points for challenges! + clearWinsAndTies(); + addWinsAndTies(CP_EARLIEST_DATE); +} +?> -- cgit v1.2.3