def _refresh_game(self): """Use user location, is taken bait or not? If user is lobby owner then check is game end? Return other lobby users information(chat, users.id etc.) in game. """ self._add_chat() if self.lobby.is_bait_taken(self.request["location"]): self._points_inc_dec() bait_taken = True else: bait_taken = False location = UserLocation(session_id=self.session_id, location=self.request["location"], time=self.time_now, bait_location=self.lobby.bait_location, is_bait_taken=bait_taken) session.add(location) session.commit() # Check game is end or continue. self._is_game_end() return { "success": True, "greydRule": self.request["greydRule"], "greydId": self.request["greydId"], "lobbyId": self.lobby.id, "currentBaitLocation": self.lobby.bait_location, "lobbyStatus": self.lobby.status, "users": self._user_info_same_lobby() }
def _game_result(self): """Game Result. Find winner user.""" self.lobby.end_game() winner_session = session.query(UserToLobby).order_by( UserToLobby.collected_bait.desc()).first() winner_session.is_game_won = True session.commit()
def _create_lobby(self): """Lobby creator method.""" lobby = Lobby(creator_id=self.request["greydId"], center_location=self.request["lobbyCenterLocation"], name=self.request["lobbyName"], game_distance=self.request["lobbyDistance"], setup_time=self.time_now, life_number=self.request["gameMaxLife"], max_time=self.request["gameMaxTime"]) session.add(lobby) session.commit() self.logger.info("New lobby created. LobbyId: %s, Creator GreydId: %s", lobby.id, lobby.creator_id) # Add session for new lobby. self.request["lobbyId"] = lobby.id self._join_lobby() return { "success": True, "greydRule": self.request["greydRule"], "lobbyId": lobby.id, "lobbyStatus": lobby.status }
def _start_game(self): """Lobby creator start the game and first bait users will be notified. """ lobby = session.query(Lobby).filter( Lobby.id == self.request["lobbyId"]).first() # Add remaining life all lobby users. users = session.query(UserToLobby).filter( UserToLobby.lobby == lobby).all() for user in users: user.remaining_life = lobby.life_number session.commit() # Has user permission on the lobby? if self.request["greydId"] == lobby.creator_id: # noqa pylint: disable=no-else-return lobby.start_game() self.logger.info("Game Started LobbyId: %s", lobby.id) return { "success": True, "greydId": self.request["greydId"], "greydRule": self.request["greydRule"], "lobbyId": lobby.id, "lobbyStatus": lobby.status, "firstBaitLocation": lobby.bait_location } else: self.logger.warning( "Unauthorized start game request. GreydId: %s, LobbyId: %s", self.request["greydId"], lobby.id) return {"success": False, "errorType": "Unauthorized request"}
def _join_lobby(self): """User join the lobby proccess.""" lobby = session.query(Lobby).filter( Lobby.id == self.request["lobbyId"]).first() lobby_session = UserToLobby(lobby_id=self.request["lobbyId"], user_id=self.request["greydId"], entry_time=self.time_now, remaining_life=lobby.life_number) session.add(lobby_session) session.commit() self.logger.info("GreydId: %s joined the lobby. LobbyId: %s", self.request["greydId"], lobby.id) lobby_users = session.query(UserToLobby).join(UserToLobby.user).filter( UserToLobby.lobby_id == lobby_session.lobby_id).all() user_list = [] for lobby_user in lobby_users: user_list.append({ "userGreydId": lobby_user.user.id, "userFacebookId": lobby_user.user.facebook_id }) return { "success": True, "greydRule": self.request["greydRule"], "greydId": self.request["greydId"], "lobbyId": lobby.id, "users": user_list }
def _add_chat(self): """Add database chat content.""" if "lobbyChat" in self.request.keys(): for chat_content in self.request["lobbyChat"]: chat = Chat(session_id=self.session_id, content=chat_content, time=self.time_now) session.add(chat) session.commit()
def _points_inc_dec(self): """Add 1 point for bait taken user and other user lost 1 life.""" user = session.query(User).filter( User.id == self.request["greydId"]).first() user.add_point() users_session = session.query(UserToLobby).filter( UserToLobby.lobby_id == self.lobby.id).filter( UserToLobby.remaining_life > 0).all() for user_session in users_session: # User is won the bait then increase the collected bait # else then user lose the life. if user_session.id == self.session_id: user_session.collected_bait = user_session.collected_bait + 1 else: user_session.remaining_life = user_session.remaining_life - 1 session.commit()
def _facebook_login(self): user = session.query(User).filter( User.facebook_id == self.request["facebookId"]).first() if not user: # If user does not exist. user = User(facebook_id=self.request["facebookId"], full_name=self.request["fullName"], e_mail=self.request["eMail"]) session.add(user) session.commit() self.logger.info("New Facebook user GreydId: %s, FacebookId: %s", user.id, user.facebook_id) # Update location information this login. user.location = self.request["location"] user.city_update() session.commit() self.logger.info("User logged in GreydId: %s", user.id) return {"success": True, "greydRule": self.request["greydRule"], "greydId": user.id, "userStatus": user.status}
def _leave_lobby(self): """User quit the lobby""" lobby = session.query(Lobby).filter( Lobby.id == self.request["lobbyId"]).first() # Is lobby creator? if self.request["greydId"] == lobby.creator_id: lobby.end_game() # All session is terminating. users_session = session.query(UserToLobby).filter( UserToLobby.lobby_id == lobby.id).all() users_session.exit_time = self.time_now self.logger.info("The lobby owner ended the lobby. LobbyId: %s", lobby.id) winner_session = session.query(UserToLobby).order_by( UserToLobby.collected_bait.desc()).first() winner_session.is_game_won = True session.commit() else: lobby_session = session.query(UserToLobby).filter( UserToLobby.lobby_id == self.request["lobbyId"]).filter( UserToLobby.user_id == self.request["greydId"]).first() lobby_session.exit_time = self.time_now lobby_session.is_game_won = False self.logger.info("User left the lobby. GreydId: %s", self.request["greydId"]) session.commit() return { "success": True, "greydRule": self.request["greydRule"], "greydId": self.request["greydId"], "lobbyId": lobby.id }