diff options
author | BlueRaja <BlueRaja.admin@gmail.com> | 2012-09-30 18:04:57 -0500 |
---|---|---|
committer | BlueRaja <BlueRaja.admin@gmail.com> | 2012-09-30 18:04:57 -0500 |
commit | 60c8232de409d0cb6de37ef87a489049f060003d (patch) | |
tree | 46fdb1be91eb3a4db354d064363a46fbf27fdb8b /includes/datas.php | |
parent | 2ce5e3800ff764870c93038d0c5ed35181225a52 (diff) | |
download | pathery-60c8232de409d0cb6de37ef87a489049f060003d.tar.xz |
Did some more work on the challenges, to have them added to the DB when the user actually completes them. No idea if it actually works yet :)
Diffstat (limited to 'includes/datas.php')
-rw-r--r-- | includes/datas.php | 201 |
1 files changed, 167 insertions, 34 deletions
diff --git a/includes/datas.php b/includes/datas.php index 170f74e..676e68b 100644 --- a/includes/datas.php +++ b/includes/datas.php @@ -289,41 +289,173 @@ function applyCareerMazesAchievements($userID) { 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) +/** + * Called when a user completes a challenge + * @param type $userID User who did the challenge + * @param type $mapID Map to check for challenges + * @param type $solution The final placement of towers, as a string of .-separated x,y pairs. + * Begins and ends with "." as well + * @param type $moves The total number of moves in the solution, for all paths + * @param type $paths An array of path-objects, each as returned by routePath(). + */ +function checkForCompletedChallenges($userID, $mapID, $solution, $moves, $paths) { + //mapID 5 is the tutorial, and is treated specially + if($mapID == 5) { + if ($moves == 75) { + onCompletedTutorial($userID); + } + return; + } + + $challengeResultset = loadChallengesForMap($mapID, $userID); + while($challenge = mysql_fetch_array($challengeResultset)) + { + //Skip checking challenges which have already been completed + if($challenge["dateCompleted"] !== NULL) + continue; + else if(hasChallengeBeenCompleted($challenge, $solution, $moves, $paths)) + setChallengeCompleted($challenge, $solution, $userID, $moves); + } +} + +/** + * Called when a user completes the tutorial + */ +function onCompletedTutorial($userID) { + //have they previously completed the tutorial? + if(hasCompletedTutorial($userID)) + 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', 0, 'Blue', '#4444ff')"; + mysql_query($sql); + + return true; +} + +/** + * Checks if the given solution meets the requirements for the given challenge + */ +function hasChallengeBeenCompleted($challenge, $solution, $moves, $paths) { + //Check the maze-length + if($challenge['goal'] != 0) { + //Er, "greater than" should really be called "greater than or equal to" :\ + if(($challenge['inequality'] == "greater than" && $moves < $challenge['goal']) + || ($challenge['inequality'] == "less than" && $moves >= $challenge['goal']) + || ($challenge['inequality'] == "equal" && $moves != $challenge['goal'])) + { + return false; + } + } + + //Check wall-count restriction + if($challenge['restrictWallCount'] !== NULL) + { + //The number of walls is equal to the number of "," characters in the $solution string + $temp = count_chars($solution, 1); //$temp needed due to extreme PHP-stupidity + $numWalls = $temp[ord(",")]; + if($numWalls > $challenge['restrictWallCount']) { + return false; + } + } + + //Check wall-placement restriction + if($challenge['restrictWallPlacement'] !== NULL) + { + $invalidWalls = explode(".", $challenge['restrictWallPlacement']); + foreach($invalidWalls as $invalidWall) + { + if(strpos($solution,$invalidWall) !== false) + { 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', 0, 'Blue', '#4444ff')"; - mysql_query($sql); - return true; + } + } + } + + //TODO: This portion probably needs to be rewritten due to the weird way paths are saved + if($challenge['restrictTeleportCount'] !== NULL || $challenge['restrictTeleportsUsed'] !== NULL) + { + //Check teleport count + $usedTeleports = explode(".", getUsedTeleports($paths)); + if($challenge['restrictTeleportCount'] !== NULL) + { + if(count($usedTeleports) !== $challenge['restrictTeleportCount'] && + ($challenge['restrictTeleportCount'] != 0 || $usedTeleports[0] != "")) + { + return false; + } + } + + //Check teleports used + if($challenge['restrictTeleportsUsed'] !== NULL) + { + foreach($usedTeleports as $usedTeleport) + { + foreach($paths as $path) + { + //Check each path to see if it walks over that teleport + if(strpos($path['path'], $usedTeleport) !== false) + { + return false; + } + } + } } } + + //Check start points + if($challenge['restrictStartPoint'] !== NULL) + { + $usesCorrectStartPoint = false; + foreach($paths as $path) + { + if($path['start'] == $challenge['restrictStartPoint']) + $usesCorrectStartPoint = true; + } + + if(!$usesCorrectStartPoint) + return false; + } + + //Check end points + if($challenge['restrictEndPoint'] !== NULL) + { + $usesCorrectEndPoint = false; + foreach($paths as $path) + { + if($path['end'] == $challenge['restrictEndPoint']) + $usesCorrectEndPoint = true; + } + + if(!$usesCorrectEndPoint) + return false; + } + + //All of the restrictions were met, meaning the challenge was completed! + return true; +} + +/** + * Sets the given challenge as completed by the user in the database + */ +function setChallengeCompleted($challenge, $solution, $userID, $moves) { + $solution = mysql_escape_string($solution); + $userID = mysql_escape_string($userID); + $challengeID = $challenge["challengeID"]; + + $sql = "INSERT INTO `challengeSolutions` + (`userID`, `challengeID`, `solution`, `moves`, `dateSolved`) + VALUES ('$userID', '$challengeID', '$solution', $moves, NOW())"; + mysql_query($sql); } function applyAchievements($userID, $aType) { @@ -727,8 +859,9 @@ function getScores($mapid, $pageNumber = 1, $pageDivide = 10) { * Returns true if the given user has completed the tutorial, false otherwise */ function hasCompletedTutorial($userID) { - $sql = "SELECT * FROM `achievements` - WHERE `type` = 32 AND `userID` = '$userID'"; + $sql = "SELECT `level` FROM `achievements` + WHERE `type` = 32 AND `userID` = '$userID' + LIMIT 1"; $result = mysql_query($sql); return (mysql_num_rows($result) >= 1); } |