/** * Handle the event. * * @param \App\WS\Message $game * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { // TODO: Validation. // if (invalid_move) abort(); $game = $message->user()->startedGame(); $user = $game->users->find($message->user()->id); if (!$user || $game->player_turn != $user->id) { return $message->reply('Not your turn.', 422); } $piece = $message->get('piece'); $parent = $message->get('parent'); if (!$user->hasPiece($piece['name'])) { return $message->reply('You don\'t have this piece [' . $piece['name'] . '].', 422); } $user->removePiece($piece['name']); $game->addPiece($piece, $parent, $user->id); $game->player_turn = $this->nextPlayerTurn($game); $points = 0; if (!$user->hasPieces()) { $game->player_turn = null; $game->round += 1; $points = $this->calculatePoints($game); $user->addPoints($points); } $game->save(); foreach ($conn->gameClients($game) as $client) { $this->send($client, 'game.piece.added', ['piece' => $piece, 'parent' => $parent, 'user_id' => $user->id, 'player_turn' => $game->player_turn]); } if (!$user->hasPieces()) { $winner = $this->getWinner($game); if ($winner) { $game->winner = $winner->id; $game->status = 'finished'; $game->save(); } foreach ($conn->gameClients($game) as $client) { if ($winner) { $this->send($client, 'game.won', ['points' => $winner->getPoints(), 'user_id' => $user->id]); } else { $this->send($client, 'game.round.won', ['points' => $points, 'user_id' => $user->id]); } } } echo "User " . $message->user()->name . " added piece " . $piece['name'] . "\n"; }
/** * Handle the event. * * @param \App\Models\Game $game * @param \App\WS\Connection $conn * @return void */ public function handle($game, $user, $conn) { $game->load('countUsers'); foreach ($conn->clients() as $client) { $this->send($client, 'game.update', $game); } foreach ($conn->gameClients($game) as $client) { $this->send($client, 'game.left', $user); } }
/** * Handle the event. * * @param \App\WS\Message $game * @param \App\WS\Connection $conn * @return void */ public function handle($message, $conn) { $game = $message->user()->startedGame(); $user = $game->users->find($message->user()->id); if (!$user || $game->player_turn != $user->id) { return $message->reply('Not your turn.', 422); } // TODO: Validation. $piece = $game->randomPiece(); if ($piece) { $user->addPiece($piece); $message->reply($piece); foreach ($conn->gameClients($game) as $client) { $this->send($client, 'game.piece.drawn', ['user_id' => $user->id]); } } else { // Game has ended! } }
/** * Handle the event. * * @param \App\Models\Game $game * @param \App\WS\Connection $conn * @return void */ public function handle($game, $conn) { foreach ($conn->gameClients($game) as $client) { $this->send($client, 'game.start', $game); } }