Skip to content

Commit

Permalink
Refactor setting map name and map file path to properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Askaholic committed Dec 25, 2023
1 parent ea2b7d3 commit 6d97003
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
2 changes: 1 addition & 1 deletion server/game_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions server/gameconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 30 additions & 6 deletions server/games/game.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import json
import logging
import re
import time
from collections import defaultdict
from typing import Any, Iterable, Optional
Expand Down Expand Up @@ -43,6 +44,8 @@
VisibilityState
)

MAP_FILE_PATH_PATTERN = re.compile("maps/(.+).zip")


class GameError(Exception):
pass
Expand All @@ -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,
Expand Down Expand Up @@ -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] = []
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 1 addition & 6 deletions server/ladder_service/ladder_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import asyncio
import json
import random
import re
import statistics
from collections import defaultdict
from typing import Awaitable, Callable, Optional
Expand Down Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_coop_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_gameconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down

0 comments on commit 6d97003

Please sign in to comment.