0) { list($json['solution'], $json['mapid']) = mysql_fetch_row($result); $encoded = json_encode($json); die($encoded); } } if ($_GET['r'] == 'getpath') { $tmp = GenerateMapByCode($_GET['mapcode']); $tmp = seperateMapSolution($tmp); $mapID = $_GET[mapid] + 0; $solution = formSolution($_GET['solution'].$tmp); //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) { $mapcode = getMapCode($mapID); } $map = GenerateMapByCode($mapcode); $mygrid = MergeMapSolution($map, $solution); $json = routePath($mygrid, $start); $moves = $json['moves']; $topscores = topScores($mapID, 30); $json['scores'] = $topscores; 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); } 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"; $result = mysql_query($sql); if (mysql_num_rows($result) > 0) { list($bestMoves, $byName) = mysql_fetch_row($result); $json['best'] = $bestMoves; //$json['best'] = 3; $json['bestby'] = $byName; } 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; } //$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); } } else { //Store solution. $sql = "INSERT INTO `solutions` (`userID`, `mapID`, `solution`, `moves`) VALUES ('$userID', '$mapID', '$solution', '$moves') "; 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); } $json['mapid'] = $_GET[mapid]; //$json[error][] = "Start value: $start"; //$json['start'] = "hello jack"; //$json['map'] = DisplayMap($mygrid); $encoded = json_encode($json); die($encoded); //Die is the same as: //echo + exit } 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); ?>