summaryrefslogtreecommitdiffstats
path: root/do.php
diff options
context:
space:
mode:
authorraylu <raylu@cmu.edu>2011-04-07 00:27:27 -0400
committerraylu <raylu@cmu.edu>2011-04-07 00:27:27 -0400
commitf8c9eb5220afaf2f9a62f9a176a45913240f4081 (patch)
tree99775178acfcef6e86e39184be0ac96083042f1b /do.php
downloadpathery-f8c9eb5220afaf2f9a62f9a176a45913240f4081.tar.xz
Initial import from Dropbox
Diffstat (limited to 'do.php')
-rw-r--r--do.php255
1 files changed, 255 insertions, 0 deletions
diff --git a/do.php b/do.php
new file mode 100644
index 0000000..8ff2162
--- /dev/null
+++ b/do.php
@@ -0,0 +1,255 @@
+<?PHP
+session_start();
+
+include "includes/maps.php";
+include "includes/db.inc.php";
+
+include "includes/datas.php";
+
+// encode array $json to JSON string
+
+
+if ($_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";
+ }
+}
+
+if ($_GET['r'] == 'getscores') {
+ $mapID = $_GET['mapid'] + 0;
+ if (!is_int($mapID))
+ return;
+ $topscores = topScores($mapID, 30);
+ $json['scores'] = $topscores;
+ $encoded = json_encode($json);
+ die($encoded);
+}
+
+
+if ($_GET['r'] == 'getsol') {
+ $solid = $_GET['solutionid'] + 0;
+ //echo "working...";
+ if (!is_int($solid)) return;
+
+ $sql = "SELECT `solution`, `mapID`
+ FROM `solutions`
+ WHERE
+ `ID` = '$solid'
+ LIMIT 1";
+
+ $result = mysql_query($sql);
+ if (mysql_num_rows($result) > 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);
+
+
+
+?> \ No newline at end of file