/** * Joins the game of another person */ public function join() { $players = Player::where('user_id', Auth::id())->get(); $gameIds = $players->lists('game_id')->toArray(); $game = Game::whereNotIn('id', $gameIds)->where(['state' => Game::STATE_OPEN])->first(); if (is_null($game)) { session()->flash('error', sprintf(self::ERROR_NO_GAMES_FOUND, Auth::user()->name)); return redirect()->route('welcome'); } $player = new Player(); $player->game_id = $game->id; $player->user_id = Auth::id(); $player->troops = 5; $player->save(); session()->flash('success', sprintf(self::MESSAGE_JOINED_GAME, Auth::user()->name)); return redirect()->route(self::ROUTE_SHOW_GAME, [$game->id]); }