def add_accepted_player(self,
                            player_id=None,
                            player_email=None,
                            session=None,
                            account_id=None):
        """
        add host player id directly to accepted players and player_ids
        """

        if not session:
            session = get_new_db_session()
        if not player_id:
            player_id = register_pending_player(game_id=self.game_id,
                                                account_id=account_id)

        self.accepted_players.append(player_email)
        self.player_ids[player_email] = player_id

        session.query(Game).filter(Game.id == self.game_id).update({
            'accepted_players':
            json.dumps({'acceptedPlayers': self.accepted_players})
        })
        session.query(Game).filter(Game.id == self.game_id).update(
            {'player_ids': json.dumps({'playerIds': self.player_ids})})
        session.commit()
        session.close()
 def __init__(self, game_id):
     self.game_id = game_id
     session = get_new_db_session()
     game_state = session.query(GameState).filter(
         GameState.game_id == game_id).first()
     self.player_turn_order = json.loads(
         game_state.player_turn_order)['playerTurnOrder']
     self.active_player_id = game_state.active_player_id
 def update_active_player_id(self, active_player_id):
     """
     updated active player id to given value
     """
     self.active_player_id = active_player_id
     session = get_new_db_session()
     session.query(GameState).filter(
         GameState.game_id == self.game_id).update(
             {'active_player_id': self.active_player_id})
     session.commit()
     session.close()
    def player_declines_invite(self, player_email=None):
        """
        flow for rejecting game participation
        """
        if not verify_player_pending(player_email):
            raise ValueError('PlayerNotPending')

        session = get_new_db_session()
        self.remove_player_from_pending(player_email=player_email,
                                        session=session)
        session.close()
Esempio n. 5
0
def create_new_game_entry(players):
    """
    perform SQLAlchemy Footwork of creating new game entry
    """
    host_player = players['hostPlayer']
    host_player_id = players['hostPlayerId']
    invited_players = players['invitedPlayers']['invitedPlayers']
    game_state = 'pending'
    new_game = Game(game_state=game_state,
                    invited_players=invited_players,
                    host_player=host_player)
    session = get_new_db_session()
    session.add(new_game)
    session.commit()
    new_game_id = new_game.id
    session.close()
    return new_game_id
    def __init__(self, game_id):
        self.game_id = game_id
        session = get_new_db_session()
        game = session.query(Game).filter(Game.id == game_id).first()

        self.game_state = game.game_state
        self.host_player = game.host_player
        self.invited_players = json.loads(
            game.invited_players)['invitedPlayers']
        self.accepted_players = json.loads(
            game.accepted_players)['acceptedPlayers']
        self.declined_players = json.loads(
            game.declined_players)['declinedPlayers']
        self.pending_players = json.loads(
            game.pending_players)['pendingPlayers']
        self.player_ids = json.loads(game.player_ids)['playerIds']
        session.close()
    def player_accepts_invite(self,
                              player_email=None,
                              game_id=None,
                              account_id=None):
        """
        flow for removing given host_email from pending and adding to accepted
        """
        if not self.verify_player_pending(player_email):
            raise ValueError('PlayerNotPending')

        player_id = register_pending_player(game_id=game_id,
                                            account_id=account_id)

        session = get_new_db_session()

        self.remove_player_from_pending(player_email=player_email,
                                        session=session)
        self.add_accepted_player(player_id=player_id,
                                 player_email=player_email,
                                 session=session)
Esempio n. 8
0
def get_game_info(gameId):
    """
    retrieve data for existing game
    """

    session = get_new_db_session()
    game = session.query(Game).filter(Game.id == gameId).first()

    game_players = GameInfoPlayers(
        host_player=game.host_player,
        invited_players=json.loads(game.invited_players),
        accepted_players=json.loads(game.accepted_players),
        declined_players=json.loads(game.declined_players),
        pending_players=json.loads(game.pending_players))

    game_info = GameInfo(game_id=game.id, players=game_players)

    session.close()

    return game_info.to_dict(), status.HTTP_200_OK
Esempio n. 9
0
def start_game(game_id):
    """
    Handles initial work of getting game state ready for play
    """

    game = GameProxy(game_id)
    turn_order = [player for player in game.get_accepted_players()]

    player_one_id = game.get_player_id(turn_order[0])
    player_one_email = turn_order[0]

    session = get_new_db_session()
    game_state = GameState(game_id=game_id,
                           player_turn_order=turn_order,
                           active_player_id=player_one_id)
    session.add(game_state)
    session.commit()
    session.close()

    for player in turn_order:
        phase = 'action' if player == player_one_email else 'inactive'
        player_id = game.get_player_id(player)
        activate_pending_player(player_id=player_id, starting_phase=phase)