diff options
author | Patrick Davison <snapwilliam@gmail.com> | 2013-01-24 00:52:59 -0800 |
---|---|---|
committer | Patrick Davison <snapwilliam@gmail.com> | 2013-01-24 00:52:59 -0800 |
commit | daa1e4eeaaa1df43fd95bdb5aed59c602d2c14b7 (patch) | |
tree | 29d47f175d842402ff5a2d2741d1f16a45f0582f /api/app.php | |
parent | da05d696e7469d67d6df76d28aad711ee0e828e7 (diff) | |
download | pathery-daa1e4eeaaa1df43fd95bdb5aed59c602d2c14b7.tar.xz |
Some code relating to testing an API.
Diffstat (limited to 'api/app.php')
-rw-r--r-- | api/app.php | 591 |
1 files changed, 591 insertions, 0 deletions
diff --git a/api/app.php b/api/app.php new file mode 100644 index 0000000..806e4ae --- /dev/null +++ b/api/app.php @@ -0,0 +1,591 @@ +<?PHP
+
+include "../includes/maps.php";
+include "../includes/db.inc.php";
+include "../includes/mapoftheday.php";
+include "../includes/datas.php";
+
+include "../includes/mapclass.php";
+
+$myNewMap = new map("13x7.c1.r12.w10.t0..:0s.0r.0r.1a.7f.0s.11f.0s.7r.1r.1f.0s.1r.5r.1r.1f.0s.2r.5r.2f.0s.6r.4f.0s.0r.8r.1f.");
+
+//print_r($myNewMap->tiles);
+//echo "---<br>";
+
+//print_r(getJsonData($myNewMap));
+
+
+if ($_GET['r'] == 'scores') {
+
+ //echo 'getting...';
+ $scores = getScoresB(1156);
+ echo json_encode($scores);
+}
+exit;
+
+
+
+echo json_encode($myNewMap);
+
+//By Wrikken
+function getJsonData($obj){
+ $var = get_object_vars($obj);
+ foreach($var as &$value) {
+ if(is_object($value) && method_exists($value,'getJsonData')) {
+ $value = $value->getJsonData();
+ }
+ }
+ return $var;
+}
+
+
+exit;
+
+//Conditions for effectiveness.
+
+//Yes:
+echo ((20 & 4) == 4);
+echo "<br>";
+//Yes:
+echo ((4 & 20) == 4);
+echo "<br>";
+//No:
+echo ((4 & 20) == 20);
+echo "<br>";
+//No:
+echo ((16 & 4) == 4);
+echo "<br>";
+//Yes:
+echo ((37 & 4) == 4);
+echo "<br>";
+
+
+
+
+
+$format = '(%1$2d = %1$04b) = (%2$2d = %2$04b)'
+ . ' %3$s (%4$2d = %4$04b)' . "\n";
+
+echo <<<EOH
+ --------- --------- -- ---------
+ result value op test
+ --------- --------- -- ---------
+EOH;
+
+
+/*
+ * Here are the examples.
+ */
+
+$values = array(0, 1, 2, 4, 8);
+$test = 1 + 4;
+
+echo "\n Bitwise AND \n";
+foreach ($values as $value) {
+ $result = $value & $test;
+ printf($format, $result, $value, '&', $test);
+}
+
+echo "\n Bitwise Inclusive OR \n";
+foreach ($values as $value) {
+ $result = $value | $test;
+ printf($format, $result, $value, '|', $test);
+}
+
+echo "\n Bitwise Exclusive OR (XOR) \n";
+foreach ($values as $value) {
+ $result = $value ^ $test;
+ printf($format, $result, $value, '^', $test);
+}
+
+
+
+exit;
+
+
+
+
+
+//Select Stats/Scores.
+function getScoresB($mapID, $pageNumber = 1, $pageDivide = 10) {
+ $top = $pageNumber * $pageDivide;
+ $bottom = $top - $pageDivide;
+
+ //TODO: !! set to false before uploading.
+ $isBlindMap = true;
+
+ $pageRequest = 'all';
+
+ if ($pageRequest == 'all') {
+ $pageReqStart = 1;
+ $pageReqEnd = 1000;
+ } elseif (is_int($pageRequest)) {
+ $pageReqStart = $pageRequest;
+ $pageReqEnd = $pageRequest;
+ } else {
+ $tmp = explode($pageRequest, "-");
+ $pageReqStart = $tmp[0];
+ $pageReqEnd = $tmp[1];
+ }
+
+ $userID = $_SESSION['userID'];
+ $myBestMoves = 0;
+ if ($isBlindMap) {
+ $sql = "SELECT `moves` FROM `solutions` WHERE `mapID` = '$mapID' AND `userID` = '$userID'";
+ $result = mysql_query($sql) or die(mysql_error());
+ $row = mysql_fetch_assoc($result);
+ $myBestMoves = $row['moves'];
+ // echo "XX $myBestMoves XX ";
+ // echo "XX $userID XX ";
+ }
+
+ $sql = "
+ SELECT
+ timediff(solutions.dateModified, TIMESTAMP(CURDATE())) as diff,
+ users.displayName as display,
+ solutions.moves as moves,
+ users.ID as ID,
+ IfNull(userData.displayColor, '#cccccc') as displayColor,
+ IfNull(userData.wallColor, '#666666') as wallColor,
+ IfNull(userData.wallEmblem, 'blank.png') as wallEmblem,
+ solutions.dateModified as cdate
+ FROM
+ `users`
+ JOIN `solutions`
+ ON users.ID = solutions.userID
+ LEFT JOIN `userData`
+ ON users.ID = userData.userID
+ WHERE solutions.mapID = '$mapID'
+ ORDER BY solutions.moves DESC, solutions.dateModified ASC, solutions.ID DESC
+ ";
+ // ORDER BY solutions.moves ASC, solutions.dateModified DESC, solutions.ID ASC
+ $result = mysql_query($sql);
+ //$utime = date("g:i A (T)");
+
+ $output['updateTime'] = date("g:i A (T)");
+
+ $i = -1;
+ $foundUser = false;
+
+ while ($row = mysql_fetch_assoc($result)) {
+ $i++;
+
+ $moves = $row['moves'];
+
+ $requestedUserID = $row['ID'];
+ if ($i == 1) {
+ $bestMoves = $moves;
+ $output['bestMoves'] = $bestMoves;
+ $output['bestBy'] = $row['display'];
+ }
+
+ if ($_SESSION['userID'] == $requestedUserID) {
+ $userPosition = $i;
+ //$myBestMoves = $moves;
+ } else {
+ if ($i > $top)
+ continue;
+ if ($i <= $bottom)
+ continue;
+ }
+
+ if ($wallEmblem == '')
+ $wallEmblem = 'blank.png';
+
+ $cdate = $row['cdate'];
+ $cdate = date("g:i A (T)", strtotime($cdate));
+
+ $scoredDate = strtotime($row['cdate']);
+ $secondsSinceScored = strtotime("now") - $scoredDate ;
+
+ //Alternate background colors
+ $background = '#262631';
+ if ($i % 2 == 1)
+ $background = '#20202a';
+
+ if ($userPosition == $i) {
+ $background = '#343c57';
+ $foundUser = true;
+ }
+
+ $medal = 'none';
+ if ($moves == $bestMoves)
+ $medal = 'silver';
+ if ($i == 1)
+ $medal = 'gold';
+
+ // if is blind map.
+ if ($isBlindMap AND $myBestMoves < $moves)
+ $row['moves'] = '???';
+
+ $output['users'][$i] = $row;
+ $output['users'][$i]['rank'] = $i;
+ $output['users'][$i]['scoredDate'] = $cdate;
+ $output['users'][$i]['secondsSinceScored'] = $secondsSinceScored;
+ $output['users'][$i]['background'] = $background;
+ $output['users'][$i]['medal'] = $medal;
+ $output['users'][$i]['isUser'] = $userPosition == $i;
+ //$output['users'][$i]['debug'] = "Bestmoves: $bestMoves moves: $moves";
+ } // END WHILE
+
+ if ($foundUser) {
+ $userPage = ceil(($userPosition / $pageDivide));
+ $output['userPage'] = $userPage;
+ $output['userPosition'] = $userPosition;
+ }
+
+ $output['pageCount'] = ceil(($i / $pageDivide));
+
+ if ($prevPage > 0) {
+ $navi .= " <a href='javascript:scoresShowPage($mapID, 1);'><<</a> ";
+ $navi .= " <a href='javascript:scoresShowPage($mapID, $prevPage);'><</a> ";
+ } else {
+ $navi .= " << ";
+ $navi .= " <";
+ }
+ for ($x = 1; $x <= $pageCount; $x++) {
+ if ($x < $pageNumber - 3 OR $x > $pageNumber + 3)
+ continue;
+ if ($x == $pageNumber)
+ $navi .= "<strong> $x </strong>";
+ elseif ($userPage == $x)
+ $navi .= " <a href='javascript:scoresShowPage($mapID, $x)'><i>$x</i></a> ";
+ else
+ $navi .= " <a href='javascript:scoresShowPage($mapID, $x)'>$x</a> ";
+ }
+ if ($nextPage <= $pageCount) {
+ $navi .= " <a href='javascript:scoresShowPage($mapID, $nextPage);'>></a> ";
+ $navi .= " <a href='javascript:scoresShowPage($mapID, $pageCount);'>>></a> ";
+ }
+
+ $output['users'] = array_values($output['users']);
+
+ return $output;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+$prep['name'] = 'Active';
+for($i = 1; $i <= 4; $i++) {
+
+ $map = MapOfTheDay($i);
+ $mapid = $map['id'];
+
+ $data['name'] = $map['map'][0][6];
+ $data['id'] = $map['id'];
+ $data['tiles'] = convertMapTiles($map['map']);
+ $prep['levels'][] = $data;
+}
+
+// $past['name'] = 'Past Maps';
+// for($i = 2; $i <= 4; $i++) {
+
+ //$map = MapOfTheDay($i);
+
+ // $pastMap = pastMap(1, $i);
+ // $mapid = $pastMap;
+ // $mapcode = getMapCode($mapID);
+ // $map = GenerateMapByCode($mapcode);
+
+ // $mapid = $map['id'];
+
+ // $data['name'] = $map['map'][0][6];
+ // $data['id'] = $map['id'];
+ // $data['tiles'] = convertMapTiles($map['map']);
+ // $past['levels'][] = $data;
+// }
+
+
+$json['action'] = 'getLevelSets';
+$json['response'] = 'ok';
+$json['data']['levelSetsResponse']['sets'][] = $prep;
+$json['data']['levelSetsResponse']['sets'][] = $past;
+
+
+$encoded = json_encode($json);
+die($encoded);
+
+
+function convertMapTiles($map) {
+ $r = '';
+ for($i = 1; $i < count($map); $i++) {
+ if ($i !== 1)
+ $r .= ',';
+ for($j = 0; $j < count($map[$i]); $j++) {
+ switch($map[$i][$j]) {
+ case 'o': $r .= '00'; break;
+ case 's': $r .= '02'; break;
+ //Multipath:
+ case 'S': $r .= '99'; break;
+ //Path 1 and Path 2 allow-only rocks.
+ case 'X':
+ break;
+ case 'x':
+ break;
+
+ case 'f': $r .= '03';break;
+ case 't': $r .= '50';break;
+ case 'u': $r .= '60';break;
+ case 'm': $r .= '51';break;
+ case 'n': $r .= '61';break;
+ case 'g': $r .= '52';break;
+ case 'h': $r .= '63';break;
+ case 'i': $r .= '54';break;
+ case 'j': $r .= '64';break;
+ case 'k': $r .= '55';break;
+ case 'l': $r .= '65';break;
+
+ case 'a': $r .= '40';break;
+ case 'b': $r .= '41';break;
+ case 'c': $r .= '42';break;
+ case 'd': $r .= '43';break;
+ case 'e': $r .= '44';break;
+ case 'r': $r .= '00';break;
+ //Visually distinct rocks:
+ case 'R': $r .= '00';break;
+ case 'q': $r .= '00';break;
+
+ //Technically shouldn't ever be used to display a wall...
+ case 'w': $r .= '01';break;
+ }
+ }
+ }
+ return $r;
+}
+
+
+function translatemap($mapMatrix, $mapID) {
+
+
+ $mapdata['height'] = $mapMatrix[0][0];
+ $mapdata['width'] = $mapMatrix[0][1];
+ $waypoints = $mapMatrix[0][2];
+ $mapdata['rocks'] = $mapMatrix[0][3];
+ $walls = $mapMatrix[0][4];
+ $mapdata['teleports'] = $mapMatrix[0][5];
+
+ $r .= "<level>";
+ $r .= "\n <properties>";
+ $r .= "\n <mapID>$mapID</mapID>";
+ $r .= "\n <availableCost>$walls</availableCost>";
+ $r .= "\n <waypoints>$waypoints</waypoints>";
+ $r .= "\n </properties>";
+
+
+ $r .= "\n <tiles>";
+ $r .= "\n <rows>";
+ for ($i = 1; $i < count($mapMatrix); $i++) {
+
+ $r .= "\n <row>";
+ for ($j = 0; $j < count($mapMatrix[$i]); $j++) {
+
+ switch($mapMatrix[$i][$j]) {
+ case 's': $r .= "02 "; break;
+ case 'f': $r .= "03 "; break;
+
+ case 't': $r .= "50 "; break;
+ case 'u': $r .= "61 "; break;
+ //TP2
+ case 'm': $r .= "51 "; break;
+ case 'n': $r .= "62 "; break;
+ //TP3
+ case 'g': $r .= "52 "; break;
+ case 'h': $r .= "63 "; break;
+ //TP4
+ case 'i': $r .= "53 "; break;
+ case 'j': $r .= "64 "; break;
+ //TP5
+ case 'k': $r .= "54 "; break;
+ case 'l': $r .= "65 "; break;
+
+ case 'a': $r .= "40 "; break;
+ case 'b': $r .= "41 "; break;
+ case 'c': $r .= "42 "; break;
+ case 'd': $r .= "43 "; break;
+ case 'e': $r .= "44 "; break;
+
+ case 'r': $r .= "00 "; break; //rock
+ case 'w': $r .= "01 "; break; //wall
+ //default: $r .= "<td class='grid_td' id='$handle' onClick='grid_click(this)' >".$index."</td>";
+ default: $r .= "10 ";
+ //default: $r .= "<td class='grid_td' id='$handle' onClick='grid_click(this)' >".$mapMatrix[$i][$j]."</td>";
+ }
+ }
+ $r .= "</row>";
+ }
+ $r .= "\n </rows>";
+ $r .= "\n </tiles>";
+ $r .= "\n</level>";
+ return $r;
+}
+
+
+
+
+
+
+
+
+
+// encode array $json to JSON string
+
+//echo "working...";
+
+if ($_GET['act'] == "test") {
+
+//header('Content-Type: text/xml');
+header("Content-Type: text/plain");
+
+if ($_POST['XMLRequest']) {
+ $string = $_POST['XMLRequest'];
+} else {
+ $string = '<!-- request -->
+<BrainMazeAPI version="0.1">
+ <APIRequest>
+ <SubmitScore>
+ <MapID>5</MapID>
+ <Username>FiftyToo</Username>
+ <Score>50</Score>
+ </SubmitScore>
+ </APIRequest>
+</BrainMazeAPI>';
+}
+
+$xml = simplexml_load_string($string);
+
+//echo $xml->APIRequest->SubmitScore->MapID;
+//print_r ($xml);
+
+
+
+
+$score = $xml->APIRequest[0]->SubmitScore[0]->Score;
+$username = $xml->APIRequest[0]->SubmitScore[0]->Username;
+
+//echo $xml->getName() . "<br />";
+// foreach($xml->children() as $child) {
+// echo $child->getName() . ": " . $child . "<br />";
+// }
+
+
+//$score = 50;
+$previousScore = 49;
+$rank = 1;
+
+//Begin XML Response
+$w = new XMLWriter();
+$w->openMemory();
+$w->startDocument('1.0','UTF-8');
+$w->startElement("BrainMazeAPI");
+ $w->writeAttribute("version", "0.1");
+ $w->startElement("APIResponse");
+ $w->startElement("SubmitScore");
+ $w->writeAttribute("result", "SUCCESS");
+
+ $w->startElement("rank");
+ $w->text($rank);
+ $w->endElement();
+
+ $w->startElement("update");
+ $w->text('true');
+ $w->endElement();
+
+ $w->startElement("UsernameUsed");
+ $w->text($username);
+ $w->endElement();
+
+ $w->startElement("SubmittedScore");
+ $w->text($score);
+ $w->endElement();
+
+ $w->startElement("PreviousHighScore");
+ $w->text($previousScore);
+ $w->endElement();
+
+ $w->endElement();
+ $w->endElement();
+$w->endElement();
+echo $w->outputMemory(true);
+
+}
+
+
+
+if ($_GET['act'] == "submit") {
+header('Content-Type: text/xml');
+echo '<!-- response -->
+<BrainMazeAPI version="0.1">
+ <APIResponse>
+ <SubmitScore result="SUCCESS">
+ <Rank>5</Rank>
+ <SubmittedScore>50</SubmittedScore>
+ <PreviousHighScore>49</PreviousHighScore>
+ </SubmitScore>
+ </APIResponse>
+</BrainMazeAPI>';
+
+}
+
+
+$xml = '<!-- request -->
+<BrainMazeAPI version="0.1">
+ <APIRequest>
+ <SubmitScore>
+ <MapID>5</MapID>
+ <Username>FiftyToo</Username>
+ <Score>50</Score>
+ </SubmitScore>
+ </APIRequest>
+</BrainMazeAPI>
+
+
+<!-- response -->
+<BrainMazeAPI version="0.1">
+ <APIResponse>
+ <SubmitScore result="SUCCESS">
+ <Rank>5</Rank>
+ <SubmittedScore>50</SubmittedScore>
+ <PreviousHighScore>49</PreviousHighScore>
+ </SubmitScore>
+ </APIResponse>
+</BrainMazeAPI>';
+
+
+
+
+if ($_GET['act'] == "getmap") {
+
+ $maptype = $_GET['maptype'] + 0;
+ if (!is_int($maptype))
+ return;
+ if ($maptype > 4)
+ return;
+ if ($maptype < 1)
+ return;
+
+ $motd = MapOfTheDay($maptype);
+ $map = $motd['map'];
+ $mapID = $motd['id'];
+
+ //print_r($map);
+
+ echo translatemap($map, $mapID);
+
+}
+
+?>
|