Beispiel #1
0
 /**
  * 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);
 }
Beispiel #2
0
 /**
  * Handle the event.
  *
  * @param  \App\WS\Message $message
  * @param  \App\WS\Connection $conn
  * @return void
  */
 public function handle($message, $conn)
 {
     $user = $message->user();
     $games = $user->finishedGames()->get();
     $total = $games->count();
     $won = $games->where('winner', $user->id)->count();
     $message->reply(['games' => $games, 'total' => $total, 'lost' => $total - $won, 'won' => $won]);
 }
Beispiel #3
0
 /**
  * Handle the event.
  *
  * @param  \App\WS\Message $message
  * @param  \App\WS\Connection $conn
  * @return void
  */
 public function handle($message, $conn)
 {
     $user = $message->user();
     $this->validate($message, ['name' => 'required|between:2,100', 'email' => 'required|email|max:200|unique:users,email,' . $user->id]);
     $user->name = e($message->get('name'));
     $user->email = e($message->get('email'));
     $user->save();
     $message->reply($user->setHidden([]));
 }
Beispiel #4
0
 /**
  * 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'));
 }
Beispiel #5
0
 /**
  * Handle the event.
  *
  * @param  \App\WS\Message $message
  * @param  \App\WS\Connection $conn
  * @return void
  */
 public function handle($message, $conn)
 {
     $user = $message->user();
     if ($game = $user->activeGame()) {
         $game->removeUser($user);
         event('game.left', [$game, $user, $conn]);
     }
     // If the game has no users left, fire event to delete it.
     if (!$game->users()->count()) {
         event('game.delete', [$game, $conn]);
     }
     $message->reply(null, 204);
 }
Beispiel #6
0
 /**
  * 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();
         }
     }
 }
Beispiel #7
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);
     }
 }
Beispiel #8
0
 /**
  * Handle the event.
  *
  * @param  \App\WS\Message $message
  * @return void
  */
 public function handle($message)
 {
     $game = $message->user()->activeGame();
     $message->reply($game);
 }
Beispiel #9
0
 /**
  * Determine if the user is authenticated.
  *
  * @param  \App\WS\Message $message
  * @return bool
  */
 protected function authenticated(Message $message)
 {
     if (in_array($message->event(), ['oauth.url', 'oauth.user', 'api_token'])) {
         return true;
     }
     return $message->user();
 }