Ejemplo n.º 1
0
    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()
        }
Ejemplo n.º 2
0
    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
        }
Ejemplo n.º 3
0
    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
        }
Ejemplo n.º 4
0
 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()
Ejemplo n.º 5
0
    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}