summaryrefslogtreecommitdiffstats
path: root/ajax/match.ajax.php
diff options
context:
space:
mode:
authorBlueRaja <BlueRaja.admin@gmail.com>2015-05-07 23:14:47 -0500
committerBlueRaja <BlueRaja.admin@gmail.com>2015-05-07 23:14:47 -0500
commit3dc3919ce1b5336861979cde56884842615c967b (patch)
treef0a2418290cecd15f20c834bb071ffa9f3694b09 /ajax/match.ajax.php
parent29e872fbc6c552ef02208fe9fa5416b69773aa38 (diff)
parentc517b645c8723b5f4d20cbb91cbc4b9f45579cbb (diff)
downloadpathery-3dc3919ce1b5336861979cde56884842615c967b.tar.xz
Merge branch 'master' of git.raylu.net:pathery
Diffstat (limited to 'ajax/match.ajax.php')
-rw-r--r--ajax/match.ajax.php263
1 files changed, 263 insertions, 0 deletions
diff --git a/ajax/match.ajax.php b/ajax/match.ajax.php
new file mode 100644
index 0000000..3a4bfae
--- /dev/null
+++ b/ajax/match.ajax.php
@@ -0,0 +1,263 @@
+<?
+session_start();
+//For testing purposes send a fake 404.
+// if (rand(1,3) == 2) {
+ // header("HTTP/1.0 404 Not Found");
+ // exit;
+// }
+
+//header_remove();
+
+require_once('../includes/sqlEmbedded.php');
+
+//Prep response
+$r = '';
+
+//Gamelist request?
+if (isset($_REQUEST['getGameList'])) {
+ $r['matches'] = getMatchList();
+ echo json_encode($r);
+ exit;
+}
+
+
+if (is_int($_GET['matchID'])) {
+ $matchID = $_GET['matchID'];
+}
+
+
+$matchID = 1;
+
+
+// Client sends isReady
+if (isset($_REQUEST['isReady'])) {
+
+ if ($_REQUEST['isReady'] == "true") {
+
+ $r['isReady'] = true;
+ } else {
+ $r['isReady'] = false;
+ }
+
+ setReady($userID, $matchID, $r['isReady']);
+}
+
+//echo "2";
+
+
+// Client sends StartGame
+if (isset($_REQUEST['startGame'])) {
+
+ // Can he start the game?
+
+ // Generate map
+
+ // Set map expires
+
+ // Return true
+ $r['startGameSuccess'] = true;
+}
+
+//echo "3";
+
+// Client sends updateRequest
+if (isset($_REQUEST['requestUpdate'])) {
+
+ //Return data on match
+ $r['gameStarted'] = true;
+ $r['gameStarted'] = true;
+ $r['secondsTillGameStarts'] = 8.6342;
+ $r['secondsTillGameExpires'] = 122.6342;
+ $r['mapID'] = 534;
+ $r['mapReady'] = true;
+ $r['players'] = getPlayersForMatch($matchID);
+
+
+ echo json_encode($r);
+}
+
+//echo "4";
+
+
+// Functions
+
+function setReady($userID, $matchID, $readyStatus) {
+ global $mysqli;
+
+ //Turn this to 0 to fill the bit
+ $intReadyStatus = intval($readyStatus);
+
+ $stmt = $mysqli->prepare("UPDATE `matchUsers`
+ SET `isReady` = ?
+ WHERE `userID` = ?
+ AND `matchID` = ?");
+ $stmt->bind_param('iii', $intReadyStatus, $userID, $matchID);
+ $stmt->execute();
+ return;
+}
+
+// !!
+function getPlayersForMatch($matchID) {
+ global $mysqli;
+
+ if (!is_numeric($matchID)) {
+ return false;
+ }
+ $res = $mysqli->query("
+ SELECT
+ matchUsers.userID,
+ TIME_TO_SEC(TIMEDIFF(NOW(), matchUsers.dateLastChecked)) as secFromLastChecked,
+ matchUsers.isReady,
+
+ users.displayName as 'display',
+ users.displayColor,
+ users.wallColor,
+ users.wallEmblem,
+ users.wallOrientation,
+ (matches.creatorUserID = users.ID) as `isCreator`
+
+ FROM `matches`
+ LEFT JOIN `matchUsers`
+ ON matchUsers.matchID = matches.ID
+ LEFT JOIN `users`
+ ON matchUsers.userID = users.ID
+ ");
+ $array = array();
+ if ($res->num_rows == 0) {
+ $res->close();
+ return false;
+ }
+ while ($response = $res->fetch_assoc()) {
+ $array['users'][] = $response;
+ }
+ $res->close();
+ $mysqli->close();
+
+ if (count($array) < 1) return false;
+ return $array;
+}
+
+function getMatchList() {
+ global $mysqli;
+
+ if (!is_object($mysqli)) die("mysqli is not an object");
+
+ if ($res = $mysqli->query("
+ SELECT
+ matches.ID,
+ matches.creatorUserID,
+ matches.mapID,
+ matches.isComplete,
+ matches.isStarted,
+
+ TIME_TO_SEC(TIMEDIFF(NOW(), matches.dateCreated)) as secondsSinceCreated,
+
+ matches.dateExpires,
+ matches.dateStarted,
+ matches.requiredPlayers,
+
+ COUNT(matchUsers.matchID) as currentPlayers,
+
+ matches.secondsGiven,
+ matches.useSmartTime,
+
+ users.displayName,
+ users.displayColor,
+ users.wallColor,
+ users.wallEmblem,
+ users.wallOrientation
+ FROM `matches`
+ JOIN `matchUsers`
+ ON matchUsers.matchID = matches.ID
+ JOIN `users`
+ ON matches.creatorUserID = users.ID
+ GROUP BY matches.ID
+ ")) {
+ $array = array();
+ if ($res->num_rows == 0) {
+ $res->close();
+ return false;
+ }
+ while ($response = $res->fetch_assoc()) {
+ $array[] = $response;
+ }
+ $res->close();
+ if (count($array) < 1) return false;
+ return $array;
+ } else {
+ printf("DB Error: %s\n", $mysqli->error);
+ return false;
+ }
+}
+
+
+function getMatchStatus($matchID) {
+ global $mysqli;
+
+ if (!is_numeric($matchID)) return false;
+
+ //Not sure why this happens; but just return nothing..
+ if (!is_object($mysqli)) die("mysqli is not an object");
+
+ if ($res = $mysqli->query("
+ SELECT
+ matches.creatorUserID,
+ matches.mapID,
+ matches.isComplete,
+ matches.isStarted,
+ matches.dateCreated,
+ matches.dateExpires,
+ matches.dateStarted,
+ matches.requiredPlayers,
+ matches.secondsGiven,
+ matches.useSmartTime,
+
+ users.displayName,
+ users.displayColor,
+ users.wallColor,
+ users.wallEmblem,
+ users.wallOrientation
+ FROM `matches`
+ LEFT JOIN `users`
+ ON matches.userID = users.ID
+ WHERE matches.ID = '$matchID'
+ ")) {
+ $array = array();
+ if ($res->num_rows == 0) {
+ $res->close();
+ return false;
+ }
+ while ($response = $res->fetch_assoc()) {
+ $array[] = $response;
+ }
+ $res->close();
+ if (count($array) < 1) return false;
+ return $array;
+ } else {
+ printf("DError: %s\n", $mysqli->error);
+ return false;
+ }
+}
+
+
+//Returns the matchID
+function createMatch($creatorID, $requiredPlayers, $secondsGiven, $useSmartTime = false) {
+ global $mysqli;
+
+ $useSmartTime = intval($useSmartTime);
+
+ $stmt = $mysqli->prepare("INSERT INTO `matches`
+ (`creatorUserID`, `requiredPlayers`, `secondsGiven`, `useSmartTime`)
+ VALUES (?, ?)");
+ $stmt->bind_param('iiii', $creatorID, $requiredPlayers, $secondsGiven, $useSmartTime);
+ $stmt->execute();
+
+ $ID = $stmt->insert_id;
+ $stmt->close();
+
+ return $ID;
+}
+
+
+
+?> \ No newline at end of file