From 59f0f8bd425573d0a33e643bb07477fa0339d4b0 Mon Sep 17 00:00:00 2001 From: Patrick Davison Date: Thu, 2 Feb 2012 23:14:17 -0800 Subject: Massive update to do.php Add: support for checking achievements. Cleaned: up code regarding getpath. Add: Rushed the sending of the path back, reduced SQL querys. More logical flow. --- do.php | 249 +++++++++++++++++++++++++++++------------------------ pages/tutorial.php | 3 +- 2 files changed, 140 insertions(+), 112 deletions(-) diff --git a/do.php b/do.php index b49fc29..d887be6 100644 --- a/do.php +++ b/do.php @@ -23,6 +23,28 @@ if ($_GET['act'] == "getmap") { } } +if ($_GET['checkachieve'] == 'true' AND $_SESSION['accepted'] == 1) { + $json['notification'] = true; + + $userID = $_SESSION['userID']; + + $sql = "UPDATE `achievements` + SET `level` = '0' + WHERE `userID` = '$userID'"; + //mysql_query($sql); + //Located in includes/datas.php + $note = getNotified($userID); + + if ($note !== false) + $json['notificationtext'] = $note; + + // $achieved = checkAchievements($userID); + + // if ($achieved['result']) { + // $json['notificationtext'] = $achieved['text']; + // } +} + if ($_GET['r'] == 'getscores') { $mapID = $_GET['mapid'] + 0; if (!is_int($mapID)) @@ -57,31 +79,40 @@ if ($_GET['r'] == 'getsol') { if ($_GET['r'] == 'getpath') { - $tmp = GenerateMapByCode($_GET['mapcode']); - $tmp = seperateMapSolution($tmp); + //Stage 1; Get the path. + //Generate the map based on the code. + $mapcode = $_GET['mapcode']; + //$tmp = GenerateMapByCode($_GET['mapcode']); + //Seperate any existing solution from map (?depreciated?) + //$tmp = seperateMapSolution($tmp); + + //!!!! + //Join the partial-solution from the map, and the solution sent (?Again, depreciated?) + //$solution = formSolution($_GET['solution'].$tmp); + + $solution = $_GET['solution']; + + //valid mapID? $mapID = $_GET[mapid] + 0; - $solution = formSolution($_GET['solution'].$tmp); + if (!is_int($mapID)) return; - //By rule, i try to avoid $var = $_GET as you - // cannot trust $_GET to be non-maliciously inputed data - $mapcode = $_GET['mapcode']; - if ($mapID > 5) { + if ($mapID > 10) { $mapcode = getMapCode($mapID); } + //mygrid will be the map, with the solution applied. $map = GenerateMapByCode($mapcode); - $mygrid = MergeMapSolution($map, $solution); - + + //Route the path $json = routePath($mygrid, $start); $moves = $json['moves']; + + $json['mapid'] = $mapID; - $topscores = topScores($mapID, 30); - $json['scores'] = $topscores; - - + //What could go wrong? if ($json['blocked']) { //$json['error'][] = "blocked"; $encoded = json_encode($json); @@ -99,118 +130,117 @@ if ($_GET['r'] == 'getpath') { die($encoded); } - for ($i = 1; $i <= $mygrid[0][1]; $i++) { - $start .= "0,$i."; - } - //$json['path'] = $pathmap1; - - $json['mapid'] = $_GET[mapid]; - - if (!is_int($mapID)) return; - $sql = "SELECT `moves`, `displayName` - FROM `solutions`, `users` - WHERE - `mapID` = '$mapID' AND - `userID` = users.ID - ORDER BY `moves` DESC, `dateModified` ASC - LIMIT 1"; - + //Get current score data. - to see if a pertinent score was beat. + $userID = $_SESSION['userID']; + $sql = "SELECT `moves` as bestmoves, `displayName`, IFNULL(q1.mymoves, 0) + FROM `solutions`, `users` + LEFT JOIN ( + SELECT `moves` as mymoves + FROM `solutions` + WHERE `userID` = '$userID' AND + `mapID` = '$mapID' + ) as q1 + ON 1 + WHERE + `mapID` = '$mapID' AND + `userID` = users.ID + ORDER BY `moves` DESC, `dateModified` ASC + LIMIT 1"; $result = mysql_query($sql); if (mysql_num_rows($result) > 0) { - list($bestMoves, $byName) = mysql_fetch_row($result); + list($bestMoves, $byName, $myMoves) = mysql_fetch_row($result); $json['best'] = $bestMoves; - //$json['best'] = 3; $json['bestby'] = $byName; + $json['mybest'] = $myMoves; } else { $json['best'] = 0; $json['bestby'] = 'no one'; } - - - //If there's a session, and this is a current map. - if ($_SESSION['accepted'] == 1 AND isCurrentMap($mapID)) { - $userID = $_SESSION['userID']; - - $sql = "SELECT * - FROM `maps` - WHERE `ID` = '$mapID' AND `code` = '$mapcode'"; - $result = mysql_query($sql); - if (mysql_num_rows($result) == 0) { - //$json['error'][] = "Error, mapID and code do not match."; - $encoded = json_encode($json); - die($encoded); - } - - //Personal best - $sql = "SELECT `moves` - FROM `solutions` - WHERE `userID` = '$userID' AND - `mapID` = '$mapID' - "; - $result = mysql_query($sql); - if (mysql_num_rows($result) > 0) { - list($myMoves) = mysql_fetch_row($result); - $json['mybest'] = $myMoves; + // --------- RUSH THE PATH BACK TO THE USER + ignore_user_abort(true); + $encoded = json_encode($json); + header("Connection: close"); + header("Content-Length: " . mb_strlen($encoded)); + echo $encoded; + flush(); + + // --------- CONTINUE EXECUTION + + + // --------- USER NOT LOGGED IN + if ($_SESSION['accepted'] !== 1) { + if ($moves >= ($_SESSION[$mapID.'moves'] + 0)) { + $_SESSION[$mapID.'moves'] = $moves; + $_SESSION[$mapID.'sol'] = $solution; } + return; + } + // --------- USER LOGGED IN + + //Challenge/Tutorial? + //echo "pre"; + if ($mapID <= 10 AND $_GET['isChallenge'] = 'true') { + //echo "running function".$_GET['challengeID']; + applyChallengeAchievements($userID, $_GET['challengeID'], $mapID, $solution, $moves); + return; + } + + //Is the map still valid to score on? + if (!isCurrentMap($mapID)) + return; - - //$result = mysql_query($sql); - //while (list($ListID, $modified) = mysql_fetch_row($result)) { - // $_SESSION['listsstate'][$ListID] = "$ListID$modified"; - // } - $sql = "SELECT `moves` - FROM `solutions` - WHERE `userID` = '$userID' - AND `mapID` = $mapID;"; - - $result = mysql_query($sql); - if (mysql_num_rows($result) > 0) { - list($reqMoves) = mysql_fetch_row($result); - if ($reqMoves < $moves) { - $sql = "UPDATE `solutions` - SET `moves` = '$moves' , - `dateModified` = NOW() , - `solution` = '$solution' - WHERE `userID` = '$userID' AND - `mapID` = '$mapID' - "; - mysql_query($sql); - //Update the solution, if it's the same score. - } elseif ($reqMoves == $moves) { - $sql = "UPDATE `solutions` - SET `moves` = '$moves' , - `solution` = '$solution' - WHERE `userID` = '$userID' AND - `mapID` = '$mapID' - "; - mysql_query($sql); - } - } else { - //Store solution. - $sql = "INSERT INTO `solutions` (`userID`, `mapID`, `solution`, `moves`) - VALUES ('$userID', '$mapID', '$solution', '$moves') - "; + // --------- UPDATE SCORES + + $checkcp = false; + $checkcm = false; + + //Is there an existing record? + if ($myMoves > 0) { + if ($myMoves < $moves) { + $sql = "UPDATE `solutions` + SET `moves` = '$moves' , + `dateModified` = NOW() , + `solution` = '$solution' + WHERE `userID` = '$userID' AND + `mapID` = '$mapID'"; + mysql_query($sql); + $checkcp = true; + //Update the solution only, if it's the same score. + } elseif ($myMoves == $moves) { + $sql = "UPDATE `solutions` + SET `moves` = '$moves' , + `solution` = '$solution' + WHERE `userID` = '$userID' AND + `mapID` = '$mapID'"; mysql_query($sql); + } - //==This still leaves a lot of solutions... - $sql = "DELETE FROM `solutions` WHERE - `userID` = '$userID' AND - `mapID` = '$mapID' AND - `moves` < '$moves' "; - //mysql_query($sql); + //Create a new record. + } else { + $sql = "INSERT INTO `solutions` (`userID`, `mapID`, `solution`, `moves`) + VALUES ('$userID', '$mapID', '$solution', '$moves')"; + mysql_query($sql); + $checkcm = true; } - $json['mapid'] = $_GET[mapid]; + // --------- APPLY ACHIEVEMENTS - //$json[error][] = "Start value: $start"; - //$json['start'] = "hello jack"; - //$json['map'] = DisplayMap($mygrid); + //while(applyCareerAchievement($userID)) { + // usleep(200); + //} + + //if ($checkcm) + applyCareerMazesAchievements($userID); + //if ($checkcp) + applyCareerPathAchievements($userID); + + //!! no need w/ rush-sending. $encoded = json_encode($json); - die($encoded); - //Die is the same as: - //echo + exit + echo $encoded; + + // --------- END } @@ -230,11 +260,8 @@ function isCurrentMap($mapID) { - - - //Very simple, confirm that all targets are reachable. -Function ValidateMap($mygrid) { +function ValidateMap($mygrid) { $start = "0,1."; $target[] = 'a'; $target[] = 'b'; diff --git a/pages/tutorial.php b/pages/tutorial.php index 782485f..b119412 100644 --- a/pages/tutorial.php +++ b/pages/tutorial.php @@ -1,5 +1,5 @@ @@ -12,6 +12,7 @@ topbar($Links);