diff options
Diffstat (limited to 'ajax/match.ajax.php')
-rw-r--r-- | ajax/match.ajax.php | 322 |
1 files changed, 149 insertions, 173 deletions
diff --git a/ajax/match.ajax.php b/ajax/match.ajax.php index 7568115..391c69e 100644 --- a/ajax/match.ajax.php +++ b/ajax/match.ajax.php @@ -6,16 +6,40 @@ session_start(); // exit;
// }
-header_remove();
+//header_remove();
+
+
+require_once('../includes/sqlEmbedded.php');
+
+$r = '';
+
+if (is_int($_GET['matchID'])) {
+ $matchID = $_GET['matchID'];
+}
+
+
+$matchID = 1;
+
-
// Client sends isReady
+if (isset($_REQUEST['isReady'])) {
+
+ if ($_REQUEST['isReady'] == "true") {
- // Return true
+ $r['isReady'] = true;
+ } else {
+ $r['isReady'] = false;
+ }
+
+ setReady($userID, $matchID, $r['isReady']);
+}
+//echo "2";
-// Client sends StartGame
+// Client sends StartGame
+if (isset($_REQUEST['startGame'])) {
+
// Can he start the game?
// Generate map
@@ -23,204 +47,156 @@ header_remove(); // 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['mapID'] = false;
- $r['players'][0]['username'] = 'Bob!';
- $r['players'][0]['isCreator'] = true;
-
- echo json_encode($r);
- exit;
-
-
+ $r['mapReady'] = true;
+ $r['players'] = getPlayersForMatch($matchID);
-exit;
-//The below is just temporary reference material.
-//////////////////////////////////////////////////////////////////////////
-
-//ini_set('display_errors',1);
-//ini_set('display_startup_errors',1);
-//error_reporting(-1);
-
-if (isset($_REQUEST['pollChannelList'])) {
- require_once('../includes/chats.php');
- echo json_encode(getChannelList());
- exit;
+ echo json_encode($r);
}
-if (isset($_SESSION['chatLastReset'])) $chatLastReset = $_SESSION['chatLastReset'];
-else $chatLastReset = 0;
+//echo "4";
-if (strtotime('now') - $chatLastReset > 6) {
- $_SESSION['chatSendCount'] = 0;
- $_SESSION['chatLastReset'] = strtotime('now');
-}
-
-//T:
-if (rand(1,4) == 2 || true) {
- $r[0]['serverMessage'] = 'true';
- $r[0]['isJoinLeave'] = 'true';
- $r[0]['message'] = "Server Test";
- $r[0]['secondsSince'] = 0;
- $r[0]['displayName'] = 'SERVER';
- $r[0]['userID'] = null;
- //echo json_encode($r);
- //exit;
-}
+// Functions
-if (isset($_REQUEST['messages'])) {
- $_SESSION['chatSendCount']++;
- if ($_SESSION['chatSendCount'] > 4) {
- $r[0]['error'] = 'Spam';
- $r[0]['serverMessage'] = 'true';
- $r[0]['message'] = "You're talking too fast - or your internet is being too slow. *OR ARE YOU SPAMMING ON PURPOSE?! JERK!*";
- $r[0]['secondsSince'] = 0;
- $r[0]['displayName'] = 'SERVER';
- $r[0]['userID'] = null;
- echo json_encode($r);
- exit;
- }
+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;
}
-//Don't lockout requests by holding the session file open!
-
-$isLoggedIn = ($_SESSION['accepted'] == 1);
-
-session_write_close();
-$userID = $_SESSION['userID'];
-$chatLockDown = false;
-$use_psuedo_longpoll = true;
+// !!
+function getPlayersForMatch($matchID) {
+ global $mysqli;
-// STOP CHAT?
-//$chatLockDown = true;
-
-// if (true) {
- // if ($_SESSION['accepted'] !== 1) {
- // exit;
- // }
-// }
-
-require_once('../includes/chats.php');
-
-//exit;
-
-enterChannel($userID);
-$sent = false;
-
-//User is sending a message
-if (isset($_REQUEST['messages'])) {
- $date = isMuted($userID);
- if ($isLoggedIn !== true) {
- $r[0]['error'] = 'Logged out';
- $r[0]['serverMessage'] = 'true';
- $r[0]['message'] = "Whoops, your session has timed out. Please sign in again.";
- $r[0]['secondsSince'] = 0;
- $r[0]['displayName'] = 'SERVER';
- $r[0]['userID'] = null;
- echo json_encode($r);
- exit;
+ if (!is_numeric($matchID)) {
+ return false;
}
- if ($date !== false) {
- $r[0]['error'] = 'Muted';
- $r[0]['serverMessage'] = 'true';
- $r[0]['message'] = 'You have been muted; You are muted until '.$date." (The current time is: ".date("Y-m-d H:i:s").")";
- $r[0]['secondsSince'] = 0;
- $r[0]['displayName'] = 'SERVER';
- $r[0]['userID'] = null;
- echo json_encode($r);
- exit;
+ $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;
}
- if ($chatLockDown == true AND $_SESSION['isAdmin'] !== true) {
- $r[0]['error'] = 'Lockdown';
- $r[0]['serverMessage'] = 'true';
- $r[0]['message'] = 'Chat Lockdown has been activated. Messages are not being accepted';
- $r[0]['secondsSince'] = 0;
- $r[0]['displayName'] = 'SERVER';
- $r[0]['userID'] = null;
- echo json_encode($r);
- exit;
+ while ($response = $res->fetch_assoc()) {
+ $array['users'][] = $response;
}
+ $res->close();
+ $mysqli->close();
- $messages = $_REQUEST['messages'];
- $messages = explode("|:|", $messages);
- foreach ($messages as $message) {
- $message = filterStringForBadLanguage($message);
- if ($message[0] == "/") {
- $arguements = explode(" ", substr($message, 1));
- $command = $arguements[0];
-
- //Admin only commands
- if ($_SESSION['isAdmin'] == true) {
- if ($command == 'say') {
- $insertID = addchat(null, substr($message, 5));
- }
- }
- //javascript based commands:
- if ($command == 'spoiler' OR $command == 'me') {
- $insertID = addchat($userID, $message);
- }
- if ($command == 'help') {
- $r[0]['serverMessage'] = 'true';
- $r[0]['message'] = "Commands: /help /time /spoiler /me. Surround text with ** for bold * for italics and ~~ for strikethrough. Chat by Pathery.com";
- $r[0]['secondsSince'] = 0;
- $r[0]['userID'] = null;
- echo json_encode($r);
- exit;
- }
- if ($command == 'time') {
- $r[0]['serverMessage'] = 'true';
- $r[0]['message'] = "Server Time: ".date('l jS \of F Y h:i:s A');
- $r[0]['secondsSince'] = 0;
- $r[0]['userID'] = null;
- echo json_encode($r);
- exit;
- }
- } else {
- $insertID = addchat($userID, $message);
- }
- }
- $sent = true;
+ if (count($array) < 1) return false;
+ return $array;
}
-if ($_REQUEST['getChatFromID']) {
- $getChatID = $_REQUEST['getChatFromID'];
- if ($sent == true AND $use_psuedo_longpoll == true) {
- // PSUEDO LONG POLLING: - This loops to try to retrieve
- // the message you just sent, back from the server.
+
+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,
- for( $i = 1; $i < 3; $i++) {
- $data = getChat($getChatID);
- if ($data !== false) {
- echo prepareChatData($data);
- exit;
- }
- //Wait .01 seconds per loop
- usleep(10000);
+ 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;
}
- $json = prepareChatData(getChat($getChatID));
- echo $json;
}
-function prepareChatData($data) {
- if (!$data)
- {
- return false;
- }
- foreach($data as &$user) {
- //$user['message'] = $user['message'].'write-append';
- $user['secondsSince'] = strtotime($user['dateSent']) - strtotime("now");
- }
- return json_encode($data);
+
+//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 |