Esempio n. 1
0
def create_players(players: list[str]) -> list[Player]:
    """Create list of player objects with
    assigned Trail Objects and Supplies."""

    player_list: list[Player] = []

    for i in enumerate(players):
        supply_options: list[str] = []
        trail_options: list[Trail] = []
        calamities: dict[str, str] = []
        for j in range(5):
            choic: str = choice(trails)
            trail = Trail(choic[0], choic[1], i[1], choic[2], choic[3],
                          choic[4])

            trail_options.append(trail)

            choi: str = choice(supplies)
            supply_options.append(choi)
        player_num: int = i[0] + 1
        player = Player(player_num, players[i[0]], supply_options,
                        trail_options, calamities)

        player_list.append(player)
    return player_list
Esempio n. 2
0
def create_player(player_id):
    try:
        player = Player.get(player_id)
    except Player.DoesNotExist:
        logger.info("New player connecting, using defaults")
        player = Player(id=player_id)
    finally:
        return player
Esempio n. 3
0
async def join_game(game_id: int, user=Depends(manager)):
    with db_session:
        game = Game.get(id=game_id)
        if game is None:
            raise HTTPException(status_code=404, detail="Game not found")
        if game.players.count() == game.player_amount:
            raise HTTPException(status_code=403, detail="The game is full")
        new_player = Player(choosable=True,
                            current_position='',
                            role='',
                            is_voldemort=False,
                            alive=True,
                            user=User[user["id"]])
        new_player.game = game
        game.players.add(new_player)
    return {"message": 'joined successfully'}
Esempio n. 4
0
async def create_game(input_game: GameM, user=Depends(manager)):
    with db_session:
        new_game = Game(name=input_game.name,
                        created_by=user["id"],
                        started=False,
                        creation_date=datetime.datetime.now(),
                        player_amount=input_game.player_amount,
                        status={})
        new_player = Player(choosable=True,
                            current_position='',
                            role='',
                            is_voldemort=False,
                            alive=True,
                            user=User[user["id"]])
        new_game.players.add(new_player)
        new_player.game = new_game
        commit()
        status = {'id': new_game.id, 'message': 'Game created successfully'}
    return status
Esempio n. 5
0
def initialize_players(nr_players):
    players = []
    for i in range(1, nr_players + 1):
        player = Player("player_{}".format(i))
        players.append(player)
    return players
Esempio n. 6
0
def test_choosehm_game():
    with db_session:
        game = Game.get(id=pytest.info['game'])
        minister = game.status["minister"]
        game.status["phase"] = "x"
    headers = {
        'Authorization': 'Bearer ' + pytest.users[1]["token"],
        'Content-Type': 'text/plain'
    }
    response = client.post("/games/100/choosehm",
                           headers=headers,
                           json={'id': '2'})
    assert response.status_code == 404
    assert response.json() == {'detail': "Game not found"}
    response = client.post(f"/games/{pytest.info['game']}/choosehm",
                           headers=headers,
                           json={'id': '2'})
    assert response.status_code == 400
    assert response.json() == {
        'detail': "The headmaster only can be elected in the propose phase"
    }
    with db_session:
        game = Game.get(id=pytest.info['game'])
        game.status["phase"] = "propose"
    for i in pytest.users.keys():
        if pytest.users[i]["player_id"] != minister:
            acc = i
            break
    headers['Authorization'] = 'Bearer ' + pytest.users[acc]["token"]
    response = client.post(f"/games/{pytest.info['game']}/choosehm",
                           headers=headers,
                           json={'id': '2'})
    assert response.status_code == 400
    assert response.json() == {
        'detail': "Only the minister can propose a headmaster"
    }
    for i in pytest.users.keys():
        if pytest.users[i]["player_id"] == minister:
            user_minister = i
            break
    headers['Authorization'] = 'Bearer ' + pytest.users[user_minister]["token"]
    response = client.post(f"/games/{pytest.info['game']}/choosehm",
                           headers=headers,
                           json={"id": "300"})
    assert response.status_code == 400
    assert response.json() == {'detail': "The selected player does not exist"}
    response = client.post(f"/games/{pytest.info['game']}/choosehm",
                           headers=headers,
                           json={"id": str(minister)})
    assert response.status_code == 400
    assert response.json() == {
        'detail': "The minister can not be the headmaster"
    }
    with db_session:
        other_guy = Player.get(id=pytest.users[acc]["player_id"])
        other_guy.choosable = False
    response = client.post(f"/games/{pytest.info['game']}/choosehm",
                           headers=headers,
                           json={"id": str(pytest.users[acc]["player_id"])})
    assert response.status_code == 400
    assert response.json() == {
        'detail': "The player has been headmaster in the previous round"
    }
    with db_session:
        user = User.get(id=pytest.users[user_minister]["user_id"])
        new_game = Game(name="x",
                        created_by=pytest.users[acc]["user_id"],
                        started=False,
                        creation_date=datetime.datetime.now(),
                        player_amount=5,
                        status={})
        new_player = Player(choosable=True,
                            current_position='',
                            role='',
                            is_voldemort=False,
                            alive=True,
                            user=user)
        new_game.players.add(new_player)
        other_guy = Player.get(id=pytest.users[acc]["player_id"])
        other_guy.choosable = True
        other_guy.alive = False
        commit()
    pytest.info['other_game'] = new_game.id
    pytest.info['other_player'] = new_player.id
    response = client.post(f"/games/{pytest.info['game']}/choosehm",
                           headers=headers,
                           json={"id": str(new_player.id)})
    assert response.status_code == 400
    assert response.json() == {
        'detail': "The player does not belong to this game"
    }
    response = client.post(f"/games/{pytest.info['game']}/choosehm",
                           headers=headers,
                           json={"id": str(other_guy.id)})
    assert response.status_code == 400
    assert response.json() == {
        'detail': "The player cannot be headmaster because is dead"
    }
    with db_session:
        other_guy = Player.get(id=pytest.users[acc]["player_id"])
        other_guy.alive = True
        username = other_guy.user.username
    response = client.post(f"/games/{pytest.info['game']}/choosehm",
                           headers=headers,
                           json={"id": str(other_guy.id)})
    assert response.status_code == 200
    assert response.json() == {
        "message":
        f"The player number {other_guy.id}: {username} was proposed as headmaster"
    }