function wsOnMessage($clientID, $message, $messageLength, $binary) { global $Server, $gameArray, $wsClientNames; $ip = long2ip($Server->wsClients[$clientID][6]); $jsonMessage = json_decode($message, true); $action = (string) $jsonMessage["action"]; $data = $jsonMessage["data"]; switch ($action) { case "new": $response = array("action" => "log", "message" => "Creating new game..."); sendJson($clientID, $response); addGame($clientID, $data); break; case "join": $response = array("action" => "log", "message" => "Joining a game"); $gameNumber = (int) $data["game"]; $teamNumber = (int) $data["team"]; sendJson($clientID, $response); joinGame($clientID, $gameNumber, $teamNumber); break; case "leave": $response = array("action" => "log", "message" => "Attempting to leave current game."); sendJson($clientID, $response); leaveGame($clientID); break; case "changename": $wsClientNames[$clientID] = (string) $data; foreach ($Server->wsClients as $id => $client) { if ($id != $clientID) { $response = array("action" => "chat", "message" => (string) $data . " has entered the lobby."); sendJson($id, $response); } } break; case "confirm": confirmClient($clientID); break; case "game": forwardCommand($clientID, $data); break; case "chat": chat($clientID, (string) $data); break; default: $response = array("action" => "log", "message" => "Didn't recognize that command."); sendJson($clientID, $response); } // check if message length is 0 if ($messageLength == 0) { $Server->wsClose($clientID); return; } //foreach ( $Server->wsClients as $id => $client ) //$Server->wsSend($id, "Visitor $clientID ($ip) said \"$message\""); //$Server->wsSend($id, "Server response: ($responseMessage)"); //$arrayCount = (string)count($gameArray); //$Server->wsSend($id, "number of open games: ($arrayCount)"); }
// ID is set to 1 for the player who creates the game $player = new Player($gameId, $playerName, $score, $id); // Save player object as JSON to database $playerJsonId = 'player_1'; $playerJson = json_encode($player, JSON_UNESCAPED_UNICODE); $sql = "UPDATE game SET {$playerJsonId} = '{$playerJson}' WHERE id = '{$gameId}'"; $db->query($sql); // Player JSON to be returned to js with JSONP $response = $playerJson; break; case 'join': // Join an already created game // Create new player object $player = new Player($gameId, $playerName); // joingame() returns player object as JSON to be returned to js $response = joinGame($player, $gameId); break; case 'u': // Update settings for player // Create new player object $player = new Player($gameId, $playerName, $score, $playerId); // The update() method returns the gameStatus. // If more than one player is still active, player object is returned as JSON. // If only one player has lives left, the game is over, and JSON with gameStatus: finished is returned $u = $player->update($gameId, $playerId, $score); if ($u == 'updated' || $u == 'ongoing') { $response = json_encode($player, JSON_UNESCAPED_UNICODE); } else { if ($u == 'finished') { $s = $player->gameStatus; $response = "{ 'gameStatus': {$s} }";
function handleMessage($tst_msg, $userSocket) { global $openGames; switch ($tst_msg->type) { case "message": $user_name = $tst_msg->name; //sender name $user_message = $tst_msg->message; //message text $user_color = $tst_msg->color; //color //prepare data to be sent to client $response_text = mask(json_encode(array('type' => 'usermsg', 'name' => $user_name, 'message' => $user_message, 'color' => $user_color))); send_message($response_text); //send data echo "User: "******"Send message " . $response_text . "<br>"; break; //exist this loop //exist this loop case "move": makeMove($tst_msg->gameID, $tst_msg->playerID, $tst_msg->oldSpot, $tst_msg->newSpot); echo "process a move from user " . $userSocket . "\n"; break; case "showOpenGames": echo "Show open games:\n"; print_r($openGames); $response_text = mask(json_encode(array('type' => 'script', 'message' => 'showGames([' . implode(",", $openGames) . '])'))); send_message($response_text); //send data break; case "startGame": startGame($tst_msg); //$response_text = mask(json_encode(array('type'=>'script', 'name'=>'nada', 'message'=>'console.log("recveive a script message")'))); //send_message($response_text); //send data break; case "join": joinGame($tst_msg, $userSocket); break; case "newGame": createGame($tst_msg->playerID, $userSocket); break; } }
$statement->execute(); $gameSession = $statement->fetchAll(PDO::FETCH_ASSOC)[0]; if (!empty($gameSession)) { return $gameSession['occupied'] !== '0' ? 'occupied' : 'unoccupied'; } return "not found"; } $value = "An error has occurred"; $action = getParamGET('action'); if ($action) { switch ($action) { case "creategame": $value = createGame($db); break; case "joingame": $value = joinGame($db); break; case "addevent": $value = addEvent($db); break; case "getevents": $value = getEvents($db); break; case "getoccupied": $value = getOccupied($db); break; default: $value = "unknown command"; } } echo $value;
break; case 'joinGame': if ($invalidArg === false) { $alias = arg('alias'); if ($alias === false) { $invalidArg = 'alias'; } } if ($invalidArg === false) { $tokens = arg('tokens'); if ($tokens === false) { $invalidArg = 'tokens'; } } if ($invalidArg === false) { joinGame($playerId, $alias, $tokens); } break; case 'reportAssassination': if ($invalidArg === false) { $gameId = arg('gameId'); if ($gameId === false) { $invalidArg = 'gameId'; } } if ($invalidArg === false) { $codeword = arg('codeword'); if ($codeword === false) { $invalidArg = 'codeword'; } }
function changeNPCLogin() { global $NPC_LOGIN, $actions, $NPC_LOGINS_USED, $underAttack, $previousContainer; if ($actions > 0) { debug('We have taken actions and now want to change NPC, let\'s exit and let next script choose a new NPC to reset execution time', getrusage()); exitNPC(); } $actions = -1; $GLOBALS['TRADE_ROUTE'] = null; $db = new SmrMySqlDatabase(); $db->query('UPDATE npc_logins SET working=' . $db->escapeBoolean(false) . ' WHERE login='******'Login'])); if ($db->getChangedRows() > 0) { debug('Unlocked NPC: ' . $NPC_LOGIN['Login']); } else { debug('Failed to unlock NPC: ' . $NPC_LOGIN['Login']); } $NPC_LOGIN = null; // We chose a new NPC, we don't care what we were doing beforehand. $previousContainer = null; debug('Choosing new NPC'); $db2 = new SmrMySqlDatabase(); $db->query('SELECT login, npc.player_name, alliance_name FROM npc_logins npc LEFT JOIN account a USING(login) LEFT JOIN player p ON a.account_id = p.account_id AND p.game_id = ' . $db->escapeNumber(NPC_GAME_ID) . ' WHERE active=' . $db->escapeBoolean(true) . ' AND working=' . $db->escapeBoolean(false) . ' AND login NOT IN (' . $db->escapeArray($NPC_LOGINS_USED) . ') ORDER BY (turns IS NOT NULL), turns DESC'); while ($db->nextRecord()) { $db2->query('UPDATE npc_logins SET working=' . $db2->escapeBoolean(true) . ' WHERE login='******'login')) . ' AND working=' . $db2->escapeBoolean(false)); if ($db2->getChangedRows() > 0) { $NPC_LOGIN = array('Login' => $db->getField('login'), 'PlayerName' => $db->getField('player_name'), 'AllianceName' => $db->getField('alliance_name')); break; } } $NPC_LOGINS_USED[] = $NPC_LOGIN['Login']; if ($NPC_LOGIN === null) { debug('No free NPCs'); exitNPC(); } debug('Chosen NPC: ' . $NPC_LOGIN['Login']); if (SmrAccount::getAccountByName($NPC_LOGIN['Login']) == null) { debug('Creating account for: ' . $NPC_LOGIN['Login']); $account =& SmrAccount::createAccount($NPC_LOGIN['Login'], '', '*****@*****.**', 'NPC', 'NPC', 'NPC', 'NPC', 'NPC', 'NPC', 'NPC', 0, 0); $account->setValidated(true); } else { $account =& SmrAccount::getAccountByName($NPC_LOGIN['Login']); } $GLOBALS['account'] =& $account; SmrSession::$account_id = $account->getAccountID(); $underAttack = false; //Auto-create player if need be. $db->query('SELECT 1 FROM player WHERE account_id = ' . $account->getAccountID() . ' AND game_id = ' . NPC_GAME_ID . ' LIMIT 1'); if (!$db->nextRecord()) { SmrSession::$game_id = 0; //Have to be out of game to join game. debug('Auto-creating player: ' . $account->getLogin()); processContainer(joinGame(SmrSession::$game_id, $NPC_LOGIN['PlayerName'])); } throw new Exception('Forward'); }
<?php include 'menu.php'; include 'data/attack.php'; include 'data/card.php'; include 'data/effect.php'; include 'data/hand.php'; $id = $_GET["id"]; echo '<meta http-equiv="refresh" content="30;URL=\'game.php?id=' . $id . '\'" />'; /********************************************************************************************************************** * Join Game **********************************************************************************************************************/ if (isset($_GET["join"])) { $deck = $_GET["deck"]; joinGame($id, getUserID(), $deck); } /********************************************************************************************************************** * Is the user player blue or player red? (or spectator)? (+Game Status) **********************************************************************************************************************/ $redplayer = false; if (getRedPlayer($id) == getUserID($id)) { $redplayer = true; } $blueplayer = false; if (getBluePlayer($id) == getUserID($id)) { $blueplayer = true; } $gamestatus = getGameStatus($id); $bluewin = ""; $redwin = ""; if ($gamestatus == 2) {