/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $user = $message->user(); $hash = $message->get('hash'); $game = Game::findByHash($hash); $game->load('users', 'allPieces'); $message->reply($game); }
/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $game = Game::findByHash($message->get('hash')); $started = $message->user()->startedGame(); if ($game->id != $started->id) { return $message->reply('Game not found.', 404); } if ($game->status != 'started') { return $message->reply('Game not started.', 422); } $game->load('users', 'pieces'); if (!$game->player_turn) { $this->generatePieces($game); } // Current player pieces. $pieces = json_decode($game->users->find($message->user()->id)->pivot->pieces, true); $player_turn = $game->player_turn; $message->reply(compact('game', 'pieces', 'player_turn')); }
/** * Handle the event. * * @param \App\WS\Message $message * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $game = Game::findByHash($message->get('hash')); $game->load('users', 'countUsers'); if ($started = $message->user()->activeGame()) { if ($game->id != $started->id) { return $message->reply('You\'ve already started a game.', 422); } $message->reply($game); } else { if ($game->users->count() == $game->players) { return $message->reply('Game already full.', 422); } $message->reply($game); $game->addUser($message->user()); event('game.joined', [$game, $message->user(), $conn]); if ($game->users->count() == $game->players) { $game->status = 'started'; $game->save(); } } }
/** * Resolve user token. * * @param \App\WS\Message $message * @return void */ protected function resolveToken(Message $message) { $conn = $message->from(); $hash = $this->clients->getHash($conn); if (!isset($this->tokens[$hash])) { if ($token = $message->get('api_token')) { $this->tokens[$hash] = $token; } else { $this->tokens[$hash] = null; } } $message->setApiToken($this->tokens[$hash]); }