From 36907fa848c9f0f0f269901e1ec8dfa56c8110b0 Mon Sep 17 00:00:00 2001 From: Askaholic Date: Sat, 9 Apr 2022 16:38:19 -0800 Subject: [PATCH] Make sure game_info messages are sent when GameOptions change --- server/gameconnection.py | 3 +- tests/integration_tests/test_game.py | 67 ++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/server/gameconnection.py b/server/gameconnection.py index 3e6a5c3b3..57ad01073 100644 --- a/server/gameconnection.py +++ b/server/gameconnection.py @@ -248,7 +248,8 @@ async def handle_game_option(self, key: str, value: Any): elif key == "Title": with contextlib.suppress(ValueError): self.game.name = value - self._mark_dirty() + + self._mark_dirty() async def handle_game_mods(self, mode: Any, args: list[Any]): if not self.is_host(): diff --git a/tests/integration_tests/test_game.py b/tests/integration_tests/test_game.py index b9c72fc71..0469590bb 100644 --- a/tests/integration_tests/test_game.py +++ b/tests/integration_tests/test_game.py @@ -264,6 +264,52 @@ async def send_player_options(proto, *options): }) +async def send_game_options(proto, *options): + for option in options: + await proto.send_message({ + "target": "game", + "command": "GameOption", + "args": list(option) + }) + + +@fast_forward(20) +async def test_game_info_message_updates(lobby_server): + _, _, proto = await connect_and_sign_in( + ("test", "test_password"), lobby_server + ) + game_id = await host_game(proto) + await read_until_command(proto, "game_info", uid=game_id, timeout=5) + + await send_game_options(proto, ["Title", "Test GameOptions"]) + msg = await read_until_command(proto, "game_info", uid=game_id, timeout=5) + assert msg["title"] == "Test GameOptions" + + await send_game_options(proto, ["Slots", "7"]) + msg = await read_until_command(proto, "game_info", uid=game_id, timeout=5) + assert msg["max_players"] == 7 + + await send_game_options(proto, ["ScenarioFile", "/maps/foobar/scenario.lua"]) + msg = await read_until_command(proto, "game_info", uid=game_id, timeout=5) + assert msg["mapname"] == "foobar" + assert msg["map_file_path"] == "maps/foobar.zip" + + await send_game_options(proto, ["RestrictedCategories", "1"]) + msg = await read_until_command(proto, "game_info", uid=game_id, timeout=5) + assert "bad_unit_restrictions" in msg["validity"] + + await send_game_options(proto, ["Unranked", "Yes"]) + msg = await read_until_command(proto, "game_info", uid=game_id, timeout=5) + assert "host_set_unranked" in msg["validity"] + + await send_game_options(proto, ["Victory", "eradication"]) + msg = await read_until_command(proto, "game_info", uid=game_id, timeout=5) + assert "wrong_victory_condition" in msg["validity"] + + # TODO: Some options don't need to trigger game info updates. Pruning these + # could help with performance and reduce network bandwidth + + @fast_forward(20) async def test_game_validity_states(lobby_server): host_id, _, host_proto = await connect_and_sign_in( @@ -283,14 +329,10 @@ async def test_game_validity_states(lobby_server): assert msg["validity"] == ["uneven_teams_not_ranked", "single_player"] # Change the map to an unranked map - await host_proto.send_message({ - "target": "game", - "command": "GameOption", - "args": [ - "ScenarioFile", - "/maps/neroxis_map_generator_sneaky_map/sneaky_map_scenario.lua" - ] - }) + await send_game_options(host_proto, [ + "ScenarioFile", + "/maps/neroxis_map_generator_sneaky_map/sneaky_map_scenario.lua" + ]) msg = await read_until_command(host_proto, "game_info", uid=game_id, timeout=5) assert msg["validity"] == [ "bad_map", @@ -305,11 +347,10 @@ async def test_game_validity_states(lobby_server): assert msg["validity"] == ["bad_map"] # Change the map to a ranked map - await host_proto.send_message({ - "target": "game", - "command": "GameOption", - "args": ["ScenarioFile", "/maps/scmp_001/scmp_001_scenario.lua"] - }) + await send_game_options(host_proto, [ + "ScenarioFile", + "/maps/scmp_001/scmp_001_scenario.lua" + ]) msg = await read_until_command(host_proto, "game_info", uid=game_id, timeout=5) assert msg["validity"] == ["valid"]