示例#1
0
 /**
  * Handle the event.
  *
  * @param  \App\Models\Game $game
  * @param  \App\WS\Connection $conn
  * @return void
  */
 public function handle($game, $conn)
 {
     $game->delete();
     foreach ($conn->clients() as $client) {
         $this->send($client, 'game.delete', $game);
     }
 }
示例#2
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     $address = $this->option('address');
     $port = intval($this->option('port'));
     $this->info('Starting web socket server on port ' . $port);
     $server = IoServer::factory(new HttpServer(new WsServer($this->connection)), $port, $address);
     $this->connection->setServer($server);
     $server->run();
 }
示例#3
0
 /**
  * Handle the event.
  *
  * @param  \App\WS\Message $message
  * @param  \App\WS\Connection $conn
  * @return void
  */
 public function handle($message, $conn)
 {
     $clients = $conn->clients();
     foreach ($clients as $client) {
         if ($message->from() !== $client) {
             $this->send($client, $message->event(), $message->data());
         }
     }
 }
示例#4
0
 /**
  * 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);
     }
 }
示例#5
0
 /**
  * Handle the event.
  *
  * @param  \App\WS\Message $message
  * @param  \App\WS\Connection $conn
  * @return void
  */
 public function handle($message, $conn)
 {
     if ($message->user()->activeGame()) {
         return $message->reply(['You\'ve already started a game.'], 422);
     }
     $this->validate($message, ['name' => 'required|between:2,100', 'players' => 'required|numeric|min:2|max:4', 'matches' => 'required|numeric|min:1|max:4', 'points' => 'required|numeric|min:10']);
     $game = Game::create(['hash' => Str::random(32), 'name' => $message->get('name'), 'players' => $message->get('players'), 'matches' => $message->get('matches'), 'points' => $message->get('points'), 'user_id' => $message->user()->id]);
     // Add the current user to the game.
     $game->addUser($message->user());
     $game->load('user', 'countUsers');
     // Send response the the user that created the game.
     $message->reply($game, 201);
     // Notify all users of a new game.
     foreach ($conn->clients() as $client) {
         $this->send($client, 'game.new', $game, 201);
     }
 }
示例#6
0
 /**
  * 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";
 }
示例#7
0
 /**
  * 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!
     }
 }
示例#8
0
 /**
  * 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);
     }
 }