private function create_game($table = 0, $creator = null, $joiner = null) { $data = new stdClass(); $data->name = $this->format . ' ' . addslashes($this->name); $data->name .= ' : Round ' . $this->round . ' Table ' . $table; $data->creator_nick = $creator->nick; $data->creator_id = $creator->player_id; $data->creator_avatar = $creator->avatar; $data->creator_deck = $creator->get_deck()->summarize(); if ($joiner != null) { $data->joiner_nick = $joiner->nick; $data->joiner_id = $joiner->player_id; $data->joiner_avatar = $joiner->avatar; $data->joiner_deck = $joiner->get_deck()->summarize(); $data->status = 3; } else { $data->status = 7; $data->creator_score = 2; $data->joiner_nick = 'BYE'; $data->joiner_id = ''; $data->joiner_avatar = ''; $data->joiner_deck = ''; } $data->tournament = $this->id; $data->round = $this->round; $game = new Game($data, 'tournament', $this); $game->join($data); return $game; }
public function recieve(WebSocketTransportInterface $user, $data) { switch ($data->type) { // Shout case 'shout': global $db; $nick = $db->escape($user->nick); $message = $db->escape($data->message); $db->query("INSERT\n\t\t\t\t\tINTO `shout` (`sender_id`, `sender_nick`, `message`)\n\t\t\t\t\tVALUES ('{$user->player_id}', '{$nick}', '{$message}')"); $data->player_id = $user->player_id; $data->player_nick = $user->nick; $data->time = now(); $this->shouts[] = $data; if (count($this->shouts) > $this->nbshouts) { array_shift($this->shouts); } $this->broadcast(json_encode($data)); break; case 'blur': $user->inactive = true; $this->broadcast(json_encode($this->list_users())); break; case 'focus': $user->inactive = false; $this->broadcast(json_encode($this->list_users())); break; case 'keypress': $user->typing = true; $this->broadcast(json_encode($this->list_users())); break; case 'keyup': $user->typing = false; $this->broadcast(json_encode($this->list_users())); break; // Duels // Duels case 'pendingduel': $data->creator_id = $user->player_id; $duel = new Game($data); $this->observer->pending_duels[] = $duel; $this->broadcast(json_encode($duel)); break; case 'joineduel': $duel_index = $this->observer->pending_duel($data->id); if ($duel_index === false) { $this->say('Pending duel ' . $data->id . ' not found'); break; } $splduels = array_splice($this->observer->pending_duels, $duel_index, 1); $duel = $splduels[0]; if ($duel->creator_id == $user->player_id) { $this->broadcast('{"type": "duelcancel", "id": "' . $data->id . '"}'); } else { $data->joiner_id = $user->player_id; $duel = $duel->join($data); $duel->type = 'joineduel'; // Send first one time to both players $duel->redirect = true; $json = json_encode($duel); $this->send_first($json, $duel->creator_id); $this->send_first($json, $duel->joiner_id); // Then broadcast to inform other users game was joined $duel->redirect = false; $this->broadcast(json_encode($duel)); $this->observer->joined_duels[] = $duel; } break; case 'goldfish': $data->type = 'joineduel'; $data->name = 'Goldfish'; $data->creator_id = $user->player_id; $data->joiner_id = $user->player_id; $duel = new Game($data); $duel->join($data); $duel->redirect = true; $user->sendString(json_encode($duel)); $duel->redirect = false; $this->observer->joined_duels[] = $duel; break; // Tournament // Tournament case 'pending_tournament': foreach ($this->observer->pending_tournaments as $tournament) { if ($tournament->registered($user) !== false) { $tournament->unregister($user); } } $data->type = 'pending_tournament'; $tournament = Tournament::create($data, $user); if (is_string($tournament)) { $user->sendString('{"type": "msg", "msg": "' . $tournament . '"}'); } else { $tournament->type = $data->type; $this->observer->pending_tournaments[] = $tournament; $data->player_id = $user->player_id; $tournament->register($data, $user); } break; case 'tournament_register': $data->player_id = $user->player_id; foreach ($this->observer->pending_tournaments as $tournament) { if ($tournament->registered($user) !== false) { $tournament->unregister($user); } else { // Client not registered if ($tournament->id == $data->id) { // Wanted tournament $tournament->register($data, $user); } } } break; default: $this->say('Unknown type : ' . $data->type); print_r($data); } }
} } catch (MyException $e) { if (!defined('DEBUG') || !DEBUG) { Flash::store('Error Accessing Game !'); } else { call('ERROR ACCESSING GAME'); } } if (isset($_POST['join'])) { // make sure this user is not full if ($GLOBALS['Player']->max_games && $GLOBALS['Player']->max_games <= $GLOBALS['Player']->current_games) { Flash::store('You have reached your maximum allowed games !'); } test_token(); try { $Game->join(); Flash::store('Game Joined Successfully'); } catch (MyException $e) { if (214 == $e->getCode()) { Flash::store('That color is already in use, try again', true); } else { Flash::store('Game Join FAILED !'); } } } if (isset($_POST['invite'])) { test_token(); try { $player_ids = $Game->invite(); // send the messages $message = 'You have been invited to join the game "' . htmlentities($Game->name, ENT_QUOTES, 'UTF-8', false) . '".' . "\n\n" . 'If you wish to play in this game, please join it from the home page.';