diff --git a/server/game_service.py b/server/game_service.py index 1e44bfa42..cdd3faf31 100644 --- a/server/game_service.py +++ b/server/game_service.py @@ -167,7 +167,7 @@ def create_game( "id_": game_id, "host": host, "name": name, - "map_": mapname, + "map_name": mapname, "game_mode": game_mode, "game_service": self, "game_stats_service": self.game_stats_service, diff --git a/server/gameconnection.py b/server/gameconnection.py index e447e9668..d9d3ed7c4 100644 --- a/server/gameconnection.py +++ b/server/gameconnection.py @@ -241,9 +241,8 @@ async def handle_game_option(self, key: str, value: Any): raw = repr(value) self.game.map_scenario_path = \ raw.replace("\\", "/").replace("//", "/").replace("'", "") - self.game.map_file_path = "maps/{}.zip".format( + self.game.map_name = \ self.game.map_scenario_path.split("/")[2].lower() - ) elif key == "Title": with contextlib.suppress(ValueError): self.game.name = value diff --git a/server/games/game.py b/server/games/game.py index a489005e6..9071efaf4 100644 --- a/server/games/game.py +++ b/server/games/game.py @@ -1,6 +1,7 @@ import asyncio import json import logging +import re import time from collections import defaultdict from typing import Any, Iterable, Optional @@ -43,6 +44,8 @@ VisibilityState ) +MAP_FILE_PATH_PATTERN = re.compile("maps/(.+).zip") + class GameError(Exception): pass @@ -63,7 +66,7 @@ def __init__( game_stats_service: "GameStatsService", host: Optional[Player] = None, name: str = "None", - map_: str = "SCMP_007", + map_name: str = "SCMP_007", game_mode: str = FeaturedModType.FAF, matchmaker_queue_id: Optional[int] = None, rating_type: Optional[str] = None, @@ -91,7 +94,7 @@ def __init__( self.host = host self.name = name self.map_id = None - self.map_file_path = f"maps/{map_}.zip" + self.map_name = map_name self.map_scenario_path = None self.password = None self._players_at_launch: list[Player] = [] @@ -155,6 +158,30 @@ def set_name_unchecked(self, value: str): max_len = game_stats.c.gameName.type.length self._name = value[:max_len] + @property + def map_name(self): + return self._map_name + + @map_name.setter + def map_name(self, name: str): + self._map_name = name + self._map_file_path = f"maps/{name}.zip" + + @property + def map_file_path(self): + return self._map_file_path + + @map_file_path.setter + def map_file_path(self, path: str): + m = re.match(MAP_FILE_PATH_PATTERN, path) + if m is None: + raise ValueError( + "Map path must start with 'maps/' and end with '.zip'" + ) + + self._map_name = m.group(1) + self._map_file_path = path + @property def armies(self) -> frozenset[int]: return frozenset( @@ -930,10 +957,7 @@ def map_folder_name(self) -> str: try: return str(self.map_scenario_path.split("/")[2]).lower() except (IndexError, AttributeError): - if self.map_file_path: - return self.map_file_path[5:-4].lower() - else: - return "scmp_009" + return self.map_name def __eq__(self, other): if not isinstance(other, Game): diff --git a/server/ladder_service/ladder_service.py b/server/ladder_service/ladder_service.py index e7046493d..cb47c4656 100644 --- a/server/ladder_service/ladder_service.py +++ b/server/ladder_service/ladder_service.py @@ -4,7 +4,6 @@ import asyncio import json import random -import re import statistics from collections import defaultdict from typing import Awaitable, Callable, Optional @@ -561,15 +560,11 @@ def get_displayed_rating(player: Player) -> float: if game_options: game.gameOptions.update(game_options) - mapname = re.match("maps/(.+).zip", map_path).group(1) - # FIXME: Database filenames contain the maps/ prefix and .zip suffix. - # Really in the future, just send a better description - self._logger.debug("Starting ladder game: %s", game) def make_game_options(player: Player) -> GameLaunchOptions: return GameLaunchOptions( - mapname=mapname, + mapname=game.map_name, expected_players=len(all_players), game_options=game_options, team=game.get_player_option(player.id, "Team"), diff --git a/tests/unit_tests/test_coop_game.py b/tests/unit_tests/test_coop_game.py index ce16aaf06..78753ebda 100644 --- a/tests/unit_tests/test_coop_game.py +++ b/tests/unit_tests/test_coop_game.py @@ -9,7 +9,7 @@ async def test_create_coop_game(database): database=database, host=mock.Mock(), name="Some game", - map_="some_map", + map_name="some_map", game_mode="coop", game_service=mock.Mock(), game_stats_service=mock.Mock() diff --git a/tests/unit_tests/test_gameconnection.py b/tests/unit_tests/test_gameconnection.py index 0780cdb5b..18aa0a38d 100644 --- a/tests/unit_tests/test_gameconnection.py +++ b/tests/unit_tests/test_gameconnection.py @@ -415,7 +415,7 @@ async def test_handle_action_GameOption( assert game.max_players == 7 # I don't know what these paths actually look like await game_connection.handle_action("GameOption", ["ScenarioFile", "C:\\Maps\\Some_Map"]) - assert game.map_file_path == "maps/some_map.zip" + assert game.map_name == "some_map" await game_connection.handle_action("GameOption", ["Title", "All welcome"]) assert game.name == "All welcome" await game_connection.handle_action("GameOption", ["ArbitraryKey", "ArbitraryValue"])