Ejemplo n.º 1
0
def vote_turn(game_id: int, player_id: int, player_vote: bool):
    '''
    Submit a player's vote instance.
    If it is the first vote in the turn, create the Vote instance for the turn.
    '''

    turn = db_turn.get_current_turn_in_game(game_id)
    player = db_player.get_player_by_id(player_id)
    vote = Vote.get(lambda v: v.turn.turn_number == turn.turn_number \
                        and v.turn.game.id == game_id)

    Player_vote(player=player, vote=vote, is_lumos=player_vote)
    commit()

    # Check and take action if all players voted
    if len(vote.player_vote) == db_game.alive_players_count(game_id):
        lumos_counter = select(pv for pv in Player_vote if \
                                pv.vote.turn.turn_number == turn.turn_number \
                                and pv.vote.turn.game.id == game_id \
                                and pv.is_lumos
                              ).count()

        result = False
        if len(vote.player_vote) - lumos_counter < lumos_counter:
            result = True
            turn.current_minister = turn.candidate_minister
            turn.current_director = turn.candidate_director

        vote.result = result

    return len(vote.player_vote)
Ejemplo n.º 2
0
def execute_crucio(game_id, player_id):
    '''
    Execute crucio to a player in game
    '''

    board = Board[game_id]
    player = db_player.get_player_by_id(player_id)

    player.is_investigated = True
    board.spell_available = False

    return player.loyalty == "Fenix Order"
Ejemplo n.º 3
0
def select_DD_candidate(game_id, player_id):
    '''
    Set a player as director candidate in current turn.
    This function doesn't make checks, it should've been made privously
    '''

    turn = get_current_turn_in_game(game_id)

    director_candidate_player = db_player.get_player_by_id(player_id)

    turn.candidate_director = director_candidate_player

    return [turn.candidate_minister.id, director_candidate_player.id]
Ejemplo n.º 4
0
def execute_avada_kedavra(game_id: int, player_id: int):
    '''
    Execute avada kedavra to a player in game
    '''

    game = db_game.get_game_by_id(game_id)
    board = Board[game_id]

    player = db_player.get_player_by_id(player_id)
    player.is_alive = False
    player.chat_enabled = False

    board.spell_available = False

    return player.rol == "Voldemort"
Ejemplo n.º 5
0
def get_players_info(game_id):
    '''
    Get players ids, username and loyalty in current game
    '''

    players_id_list = get_all_players_id(game_id)

    players_info_list = []
    for id in players_id_list:
        player = db_player.get_player_by_id(id)
        loyalty = "Voldemort" if player.rol == "Voldemort" else player.loyalty
        players_info_list.append({
            "player_id": id,
            "username": player.user.username,
            "loyalty": loyalty,
            "is alive": player.is_alive
        })

    return players_info_list
Ejemplo n.º 6
0
def check_init_conditions(game_id: int, player_id: int):
    '''
    Check the conditions to start a game
    '''
    game = get_game_by_id(game_id=game_id)
    if not game:
        raise game_not_found_exception
    player = db_player.get_player_by_id(player_id=player_id)
    if not player:
        raise player_not_found_exception
    if player not in game.players:
        raise player_not_in_game_exception
    if game.state == 1:
        raise game_has_started_exception
    if game.state == 2:
        raise game_has_finished_exception
    if game.owner != player.user:
        raise not_the_owner_exception
    if game.min_players > game.players.count():
        raise min_player_not_reach_exception
Ejemplo n.º 7
0
def select_MM_candidate(game_id: int):
    '''
    Start a new turn and return the candidate for minister in mentioned turn.
    '''

    game = db_game.get_game_by_id(game_id)
    players_set = game.players
    next_candidate_minister = None

    # Last turn info
    game_turns = get_current_turn_number_in_game(game_id)
    last_turn = get_turn_in_game(game_id=game_id, turn_number=game_turns)

    last_candidate_minister = last_turn.candidate_minister

    # Get LAST minister candidate
    if game_turns > 1:
        # Second last turn info
        second_last_turn = get_turn_in_game(game_id=game_id,
                                            turn_number=game_turns - 1)
        if second_last_turn.imperius_player_id:
            last_candidate_minister = second_last_turn.candidate_minister

    # Get NEXT minister candidate
    if last_turn.imperius_player_id:
        next_candidate_minister = db_player.get_player_by_id(
            last_turn.imperius_player_id)
    else:
        next_candidate_minister = get_next_candidate(players_set,
                                                     last_candidate_minister)

    turn = generate_turn(game_instance=game,
                         turn_number=game_turns + 1,
                         candidate_minister=next_candidate_minister,
                         candidate_director=next_candidate_minister,
                         last_minister=last_turn.current_minister,
                         last_director=last_turn.current_director,
                         current_minister=next_candidate_minister,
                         current_director=next_candidate_minister)

    return next_candidate_minister.id