Ejemplo n.º 1
0
def check_and_reject_notify(game_id: int, player_id: int):
    if not db_game.get_game_by_id(game_id=game_id):
        raise game_not_found_exception

    vote_result = model_vote.check_and_get_vote_result(game_id)

    # If candidates were elected then don't notify
    if vote_result["result"]:
        return {"notified": False}

    return db_player.notify_with_player(game_id, player_id)
Ejemplo n.º 2
0
def check_and_get_available_spell(game_id: int):
    check_game_state(game_id)

    game = db_game.get_game_by_id(game_id)
    if db_turn.get_current_turn_number_in_game(game_id) == 0 or (
            not db_board.is_board_available_spell(game_id)) or game.chaos:
        spell = ""

    else:
        spell = db_spell.available_spell_in_game_conditions(game_id)

    return {"Spell": spell}
Ejemplo n.º 3
0
def generate_card(quantity: int, order_in_deck: int, game_id: int):
    '''
    Generate 'quantity' new cards for a game
    '''

    game = db_game.get_game_by_id(game_id=game_id)

    random.seed()

    for _ in range(quantity):
        card_type = random.randint(0, 1)
        Card(order=order_in_deck, type=card_type, game=game, discarded=False)
        order_in_deck += 1
Ejemplo n.º 4
0
def execute_guessing(game_id):
    '''
    Execute guessing
    '''

    game = db_game.get_game_by_id(game_id)
    board = Board[game_id]
    game_deck_cuantity = len(game.card)
    cards = Card.select(lambda c: c.game.id == game_id and c.order >
                        (game_deck_cuantity - 3)).order_by(Card.order)[:3]

    board.spell_available = False

    return [cards[0].type, cards[1].type, cards[2].type]
Ejemplo n.º 5
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.º 6
0
def leave_game_initialized(game_id: int, player_id: int):
    if not db_game.get_game_by_id(game_id=game_id):
        raise game_not_found_exception
    if not db_player.is_player_in_game_by_id(game_id=game_id,
                                             player_id=player_id):
        raise player_not_in_game_exception
    message = db_player.kill_player_leaving(player_id=player_id)
    if db_player.is_alive(game_id=game_id, player_id=player_id):
        raise player_not_killed_exception
    #if (db_turn.is_current_minister(game_id=game_id, player_id=player_id) or
    #    db_turn.is_current_director(game_id=game_id, player_id=player_id) or
    #    db_turn.is_minister_or_director_candidate(game_id=game_id, player_id=player_id)):
    # if the player leaving is M or D, a new turn starts
    db_turn.select_MM_candidate(game_id=game_id)
    return {"message": message}
Ejemplo n.º 7
0
def minister_set_expelliarmus_consent(game_id: int, consent: int):
    '''
    Minister accept/decline director's expelliarmus proposal
    '''

    game = db_game.get_game_by_id(game_id=game_id)
    game_deck_quantity = len(game.card)
    turn = get_current_turn_in_game(game_id)

    turn.minister_consent = consent

    if not consent:
        return turn.turn_number

    director_cards = Card.select(lambda c: (c.game.id == game_id) and (
        c.order > (game_deck_quantity - 6)) and (c.order <= (
            game_deck_quantity - 3)) and (not c.discarded)).order_by(
                Card.order)[:2]

    director_cards[0].discarded = True
    director_cards[1].discarded = True

    board = Board[game_id]
    # Case where chaos conditions are met because minister's consent
    if board.election_counter == 2:
        game_deck_cuantity = len(game.card)
        # Select next card, returns a singleton list
        card = Card.select(lambda c: c.game.id == game_id \
                            and c.order > (game_deck_cuantity - 1) \
                            and (not c.discarded)
                          ).order_by(Card.order)[:1]
        # Generate new card
        db_card.generate_card(1, game_deck_cuantity + 1, game_id)
        # This is not something necessary, just being consistent
        turn.taken_cards = True
        # With this inactive government, we have a sequence of three elections
        # where candidates were rejected
        db_board.promulgate(game_id, card[0].type)
        # Eliminate election constraints for director candidates
        game.chaos = True
    else:
        # board.election_counter < 2, and minister
        game.chaos = False
        board.election_counter += 1

    select_MM_candidate(game_id)

    return turn.turn_number
Ejemplo n.º 8
0
def leave_game_not_initialized(game_id: int, user_email: EmailParameter):
    if not db_game.get_game_by_id(game_id=game_id):
        raise game_not_found_exception
    if db_game.get_game_state(game_id=game_id):
        raise game_has_started_exception
    if not db_player.is_player_in_game_by_email(game_id=game_id,
                                                user_email=user_email.email):
        raise player_not_in_game_exception
    if db_player.is_player_the_owner(game_id=game_id,
                                     user_email=user_email.email):
        message = db_game.delete_game(game_id=game_id)
        return {"message": message}
    else:
        message = db_game.remove_player_from_game(game_id=game_id,
                                                  user_email=user_email.email)
        return {"message": message}
Ejemplo n.º 9
0
def player_voted(game_id: int, player_id: int):
    '''
    Assert if a player already voted
    '''

    game = db_game.get_game_by_id(game_id=game_id)
    player = get_player_by_id(player_id)

    turn = db_turn.get_current_turn_in_game(game_id)

    vote = Vote.get(lambda v: v.turn.turn_number == turn.turn_number and v.turn
                    .game.id == game_id)

    return Player_vote.get(lambda pv: pv.player.id == player_id \
                            and pv.vote.turn == turn \
                            and pv.vote.turn.game.id == game_id)
Ejemplo n.º 10
0
def get_not_discarded_cards(game_id: int):
    '''
    Get cards not discarded from the deck for the director
    '''

    game = db_game.get_game_by_id(game_id=game_id)
    game_deck_quantity = len(game.card)

    card_list = []
    cards = Card.select(lambda c: (c.game.id == game_id) and (c.order > (
        game_deck_quantity - 6)) and (c.order <= (game_deck_quantity - 3)) and
                        (not c.discarded)).order_by(Card.order)[:2]

    for card in cards:
        card_list.append(card.type)

    return card_list
Ejemplo n.º 11
0
def generate_3_cards(game_id: int):
    '''
    Return the three cards in order, and create three new cards for future turns
    '''

    game = db_game.get_game_by_id(game_id=game_id)
    turn = db_turn.get_current_turn_in_game(game_id)
    game_deck_cuantity = len(game.card)

    cards = Card.select(lambda c: c.game.id == game_id \
                            and c.order > (game_deck_cuantity - 3)
                        ).order_by(Card.order)[:3]

    generate_card(3, game_deck_cuantity + 1, game_id)

    turn.taken_cards = True
    return [cards[0].type, cards[1].type, cards[2].type]
Ejemplo n.º 12
0
def put_new_player_in_game(user: EmailStr, game_id: int):
    '''
    Create a new player and join a game
    '''
    game = db_game.get_game_by_id(game_id=game_id)
    creator = db_user.get_user_by_email(email=user)
    new_player = Player(user=creator,
                        is_alive=True,
                        game_in=game,
                        chat_enabled=True,
                        is_investigated=False,
                        turn=game.players.count() + 1)
    commit()
    creator.playing_in.add(Player[new_player.id])
    game.players.add(Player[new_player.id])

    return new_player.id
Ejemplo n.º 13
0
def create_first_turn(game_id: int):
    '''
    Generate first turn when game starts and therefore isnt a last minister or director
    '''

    game = db_game.get_game_by_id(game_id=game_id)
    players_set = db_game.get_player_set(game_id=game_id)
    next_minister = get_next_candidate(players_set)
    generate_turn(game_instance=game,
                  turn_number=1,
                  candidate_minister=next_minister,
                  candidate_director=next_minister,
                  last_minister=None,
                  last_director=None,
                  current_minister=next_minister,
                  current_director=next_minister)
    db_card.generate_card(3, 1, game_id)
    db_game.set_game_init(game_id)

    return next_minister.id
Ejemplo n.º 14
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
Ejemplo n.º 15
0
def check_if_game_started(game_id: int, player_id: int):
    if not db_game.get_game_by_id(game_id=game_id):
        return {"game_state": "The game has been deleted"}
    if not db_player.is_player_in_game_by_id(game_id=game_id,
                                             player_id=player_id):
        raise player_not_in_game_exception
    state = db_game.get_game_state(game_id)
    users = db_game.get_current_users_in_game(game_id=game_id)
    if not state or state == 2:
        return {"game_state": state, "users": users}
    else:
        minister_id = db_turn.get_current_minister(game_id=game_id)
        amount_players = db_game.players_in_game(game_id=game_id)
        rol_and_loyalty = db_player.get_player_rol_and_loyalty(
            player_id=player_id)
        users = db_game.get_current_users_in_game(game_id=game_id)
        return {
            "game_state": state,
            "minister_id": minister_id,
            "amount_of_players": amount_players,
            "rol": rol_and_loyalty.get("Rol"),
            "loyalty": rol_and_loyalty.get("Loyalty"),
            "users": users
        }
Ejemplo n.º 16
0
def get_vote_candidates(game_id: int):
    if not db_game.get_game_by_id(game_id=game_id):
        raise geme_not_found_exception
    candidates = db_turn.get_candidates(game_id=game_id)
    return TurnFormula(minister_id=candidates[0], director_id=candidates[1])