def test_add_player(db_session): g = WurwolvesGame(GAME_ID) g.join(USER_ID) assert len(db_session.query(Game).all()) == 1 assert len(db_session.query(User).all()) == 1 db_game = db_session.query(Game).first() assert len( db_session.query(Player).filter(Player.game == db_game).all()) == 1 game_hash = g.get_game_model().update_tag g.join(uuid()) assert len(db_session.query(Game).all()) == 1 assert len(db_session.query(User).all()) == 2 assert len( db_session.query(Player).filter(Player.game == db_game).all()) == 2 assert game_hash != g.get_game_model().update_tag game_hash = g.get_game_model().update_tag g.join(USER_ID) assert len(db_session.query(Game).all()) == 1 assert len(db_session.query(User).all()) == 2 assert len( db_session.query(Player).filter(Player.game == db_game).all()) == 2 assert game_hash == g.get_game_model().update_tag
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
def test_end_game(api_client, db_session): from uuid import uuid4 as uuid g = WurwolvesGame(GAME_ID) with api_client as s: r = s.post(f"/api/{GAME_ID}/join") assert r.ok for _ in range(4): g.join(uuid()) g.start_game() assert g.get_game_model().stage == GameStage.NIGHT r = s.post(f"/api/{GAME_ID}/end_game") assert r.ok assert g.get_game_model().stage == GameStage.ENDED
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)
def test_join(api_client, db_session): g = WurwolvesGame(GAME_ID) assert g.get_game_model() is None response = api_client.post("/api/{}/join".format(GAME_ID)) assert response.status_code == 200 assert len(g.get_game_model().players) == 1