1) if (isset($_COOKIE['doLogin']) && $_COOKIE['doLogin'] == 'yes') CookieLogin(); // encode array $json to JSON string if (isset($_GET['act']) && $_GET['act'] == "getmap") { $map = GenerateMapByCode($_GET['mapcode']); $solution = formSolution($_GET['solution']); $solvedmap = MergeMapSolution($map, $solution); if ($solvedmap == -1) { $json['error'][] = "INVALID WALL POSITIONS"; } if ($solvedmap == -2) { $json['error'][] = "USED TOO MANY WALLS"; } } // !! Depreciate? if (isset($_GET['checkachieve']) && $_GET['checkachieve'] == 'true' && $_SESSION['accepted'] == 1) { $json['notification'] = true; $userID = $_SESSION['userID']; //Located in includes/datas.php $note = getNotified($userID); if ($note !== false) $json['notificationtext'] = $note; } if ($_GET['r'] == 'reqScorePage') { $mapID = $_GET['mapid'] + 0; $page = $_GET['reqPage'] + 0; if (!is_int($mapID) OR !is_int($page)) return; //Include the notification text $json = getScores($mapID, $page, 10); $json['mapid'] = $mapID; $json['page'] = $page; if ($_SESSION['accepted'] == 1) { $userID = $_SESSION['userID']; $note = getNotified($userID); if ($note !== false) $json['notificationtext'] = $note; } $encoded = json_encode($json); die($encoded); } if ($_GET['r'] == 'getscores') { $mapID = $_GET['mapid'] + 0; if (!is_int($mapID)) return; $topscores = topScores($mapID, 30); $json['scores'] = $topscores; $json['mapid'] = $mapID; $encoded = json_encode($json); die($encoded); } if ($_GET['r'] == 'getsol') { $mapID = $_GET['mapID'] + 0; //echo "working..."; if (!is_int($mapID)) return; $userID = $_SESSION['userID']; ///// !! FIX FIX FIX FIX FIX FIX ////// if ($_SESSION['accepted'] == 1) { $json = getSolution($userID, $mapID); if ($tmp['moves'] > $_SESSION[$mapID.'sol']) { $json = $tmp; } } if (isset($_SESSION[$mapID.'sol'])) { $json['solution'] = $_SESSION[$mapID.'sol']; $json['moves'] = $_SESSION[$mapID.'moves']; } $json['mapid'] = $mapID; $encoded = json_encode($json); die($encoded); } // ------------ MAIN; getpath. if ($_GET['r'] == 'getpath') { //Join the partial-solution from the map, and the solution sent. //Could be used to validate maps in for challenges where // getting the map code from the database is not an option //$tmp = GenerateMapByCode($_GET['mapcode']); //$tmp = seperateMapSolution($tmp); //$solution = formSolution($_GET['solution'].$tmp); // Enables the ability to discover an exact duplicate solution. $solution = formSolution($_GET['solution']); //valid mapID? $mapID = $_GET[mapid] + 0; if (!is_int($mapID)) return; //the first 10 ID's reserved for challenges & tutorial. if ($mapID > 10) $mapcode = getMapCode($mapID); else $mapcode = $_GET['mapcode']; //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; //What could go wrong? if ($json['blocked']) { //$json['error'][] = "blocked"; $encoded = json_encode($json); die($encoded); } if ($mygrid == -1) { $json['error'][] = "INVALID WALL POSITIONS"; $encoded = json_encode($json); die($encoded); } if ($mygrid[0][4] < 0) { $used = $mygrid[0][4] - $map[0][4]; $json['error'][] = "Too many walls used. ".$used." of ".$map[0][4]." walls used."; $encoded = json_encode($json); die($encoded); } //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, $myMoves) = mysql_fetch_row($result); $json['best'] = $bestMoves; $json['bestby'] = $byName; $json['mybest'] = $myMoves; } else { $json['best'] = 0; $json['bestby'] = 'no one'; } // --------- 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? 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; // --------- 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); } //Create a new record. } else { $sql = "INSERT INTO `solutions` (`userID`, `mapID`, `solution`, `moves`) VALUES ('$userID', '$mapID', '$solution', '$moves')"; mysql_query($sql); $checkcm = true; $checkcp = true; } // --------- APPLY ACHIEVEMENTS if ($checkcp) applyAchievements($userID, 1); if ($checkcm) { applyAchievements($userID, 2); applyAchievements($userID, 3); applyAchievements($userID, 4); } // no need w/ rush-sending. //$encoded = json_encode($json); //echo $encoded; // --------- END } function isCurrentMap($mapID) { include_once('./includes/db.inc.php'); $sql = "SELECT `ID` FROM `mapOfTheDay` WHERE `mapDate` = CURDATE() AND `mapID` = '$mapID' "; $result = mysql_query($sql) or die(mysql_error()); if (mysql_num_rows($result) == 0) return false; else return true; } //Very simple, confirm that all targets are reachable. function ValidateMap($mygrid) { $start = "0,1."; $target[] = 'a'; $target[] = 'b'; $target[] = 'c'; $target[] = 'f'; $blocked = false; foreach($target as $t) { $p = Findpath ($mygrid, $start, $t); if ($p['blocked']) return false; } return true; } $json['error'][] = 'Division by 912 trillion!'; $json['error'][] = 'Cant count that high.'; $json['error'][] = "I forgot what I was doing!"; $json['error'][] = "Lost track of time, and gaveup!"; $encoded = json_encode($json); die($encoded); ?>