示例#1
0
def test_kick(db_session):
    game = WurwolvesGame(GAME_ID)

    # Add three players
    player_ids = [uuid() for _ in range(3)]
    for p in player_ids:
        game.join(p)

    db_session = game._session

    # Set one to have joined ages ago
    timeout_player_id = player_ids[0]
    timeout_player = game.get_player(timeout_player_id)
    game.set_user_name(timeout_player_id, "timeout player")
    db_session.add(timeout_player)

    timeout_player.last_seen = datetime(1, 1, 1)

    db_session.commit()
    db_session.expire_all()

    # Get the game tag
    tag = game.get_game_model().update_tag

    timeout_player = game.get_player(timeout_player_id)
    db_session.add(timeout_player)

    assert timeout_player.state == PlayerState.SPECTATING

    # Keepalive one of the other players, which should kick the idler
    game.player_keepalive(player_ids[1])

    db_session.expire_all()

    # Check that the idler is gone
    assert not game.get_player(timeout_player_id)
    # And that they're not in the rendered state
    state = game.parse_game_to_state(player_ids[1])
    assert "timeout player" not in state.json()

    # Also check the tag changed
    assert tag != game.get_game_model().update_tag

    tag = game.get_game_model().update_tag

    # Bring the idler back
    game.player_keepalive(timeout_player_id)

    assert game.get_player(timeout_player_id)
    assert tag != game.get_game_model().update_tag
示例#2
0
def five_player_game(db_session) -> WurwolvesGame:

    g = WurwolvesGame(GAME_ID)

    # Make players
    user_ids = [uuid() for _ in range(5)]
    roles = [
        PlayerRole.WOLF,
        PlayerRole.MEDIC,
        PlayerRole.SEER,
        PlayerRole.VILLAGER,
        PlayerRole.VILLAGER,
    ]
    names = ["Wolf", "Medic", "Seer", "Villager 1", "Villager 2"]

    for i, id in enumerate(user_ids):
        g.join(id)

    g.start_game()

    # Override the roles
    for id, role, name in zip(user_ids, roles, names):
        u = g.get_player(id)
        u.role = role
        g.set_user_name(id, name)

    g._session.commit()

    roles_map = {name: user_id for name, user_id in zip(names, user_ids)}

    return g, roles_map
示例#3
0
def test_not_kicked_during_game(db_session):
    game = WurwolvesGame(GAME_ID)

    # Add four players
    player_ids = [uuid() for _ in range(4)]
    roles = [
        PlayerRole.MEDIC,
        PlayerRole.VILLAGER,
        PlayerRole.SPECTATOR,
        PlayerRole.WOLF,
    ]
    for p in player_ids:
        game.join(p)

    db_session = game._session

    game.start_game()

    for p, r in zip(player_ids, roles):
        game.set_player_role(game.get_player_id(p), r)

    timeout_player_id = player_ids[0]
    villager_id = player_ids[1]
    spectator_id = player_ids[2]
    wolf_id = player_ids[3]

    # Kill the medic with the wolf
    game.wolf_night_action(wolf_id, timeout_player_id)
    # Have the idler do something too
    game.medic_night_action(timeout_player_id, wolf_id)

    # Set medic to have joined ages ago
    timeout_player = game.get_player(timeout_player_id)
    db_session.add(timeout_player)
    timeout_player.last_seen = datetime(1, 1, 1)
    db_session.commit()
    db_session.expire_all()

    # Keepalive one of the other players, which should not kick the idler since it's a game
    game.player_keepalive(wolf_id)

    db_session.expire_all()
    assert game.get_player(timeout_player_id)
示例#4
0
def test_kick_with_actions(db_session):
    game = WurwolvesGame(GAME_ID)

    # Add four players
    player_ids = [uuid() for _ in range(4)]
    roles = [
        PlayerRole.MEDIC,
        PlayerRole.VILLAGER,
        PlayerRole.SPECTATOR,
        PlayerRole.WOLF,
    ]
    for p in player_ids:
        game.join(p)

    db_session = game._session

    game.start_game()

    for p, r in zip(player_ids, roles):
        game.set_player_role(game.get_player_id(p), r)

    timeout_player_id = player_ids[0]
    villager_id = player_ids[1]
    spectator_id = player_ids[2]
    wolf_id = player_ids[3]

    # Set one to have joined ages ago
    timeout_player = game.get_player(timeout_player_id)
    db_session.add(timeout_player)

    timeout_player.last_seen = datetime(1, 1, 1)

    db_session.commit()
    db_session.expire_all()

    timeout_player = game.get_player(timeout_player_id)
    db_session.add(timeout_player)

    assert timeout_player.state == PlayerState.ALIVE

    # Keepalive one of the other players, which should not kick the idler since it's a game
    game.player_keepalive(wolf_id)

    db_session.expire_all()
    assert game.get_player(timeout_player_id)

    # Kill the idler with the wolf
    game.wolf_night_action(wolf_id, timeout_player_id)
    # Have the idler do something too
    game.medic_night_action(timeout_player_id, wolf_id)

    db_session.expire_all()

    # Check game ended
    assert game.get_game_model().stage == GameStage.ENDED

    # Keepalive someone else
    game.player_keepalive(wolf_id)

    # Player should not yet be kicked
    db_session.expire_all()
    g = game.get_player(timeout_player_id)
    assert g

    # Put their timeout back into the past
    timeout_player.last_seen = datetime(1, 1, 1)
    db_session.commit()
    db_session.expire_all()

    # Keepalive someone else
    game.player_keepalive(wolf_id)

    # They still should be visible, but marked as inactive now
    db_session.expire_all()
    assert game.get_player(timeout_player_id)
    assert not game.get_player_model(timeout_player_id).active

    # Vote to start a new game
    game.wolf_ended_action(wolf_id)

    # Check a new game started
    db_session.expire_all()
    assert game.get_game_model().stage == GameStage.LOBBY

    # Check the idler was kicked after the next keepalive
    game.player_keepalive(wolf_id)
    assert not game.get_player(timeout_player_id)