From 01cb30b61739780e680b501e7719fb23749ecaeb Mon Sep 17 00:00:00 2001 From: Snap Date: Fri, 1 May 2015 16:26:15 -0700 Subject: Versus Matches - Initial code and testing. --- ajax/match.ajax.php | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 226 insertions(+) create mode 100644 ajax/match.ajax.php (limited to 'ajax') diff --git a/ajax/match.ajax.php b/ajax/match.ajax.php new file mode 100644 index 0000000..7568115 --- /dev/null +++ b/ajax/match.ajax.php @@ -0,0 +1,226 @@ + 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; +} + + +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; + } +} +//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; + +// 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 ($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; + } + 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; + } + + $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 ($_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. + + for( $i = 1; $i < 3; $i++) { + $data = getChat($getChatID); + if ($data !== false) { + echo prepareChatData($data); + exit; + } + //Wait .01 seconds per loop + usleep(10000); + } + } + $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); +} +?> \ No newline at end of file -- cgit v1.2.3