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 May 6, 2021
1 parent 38cc8fe commit 01d4cde
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 18 deletions.
2 changes: 1 addition & 1 deletion server/game_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,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 @@ -239,9 +239,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, Dict, FrozenSet, Iterable, List, Optional, Set, Tuple
Expand Down Expand Up @@ -42,6 +43,8 @@
VisibilityState
)

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


class GameError(Exception):
pass
Expand All @@ -62,7 +65,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 All @@ -88,7 +91,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 = []
Expand Down Expand Up @@ -152,6 +155,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 @@ -890,10 +917,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.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import asyncio
import json
import random
import re
from collections import defaultdict
from typing import Dict, List, Optional, Set, Tuple

Expand Down Expand Up @@ -428,14 +427,10 @@ def get_player_mean(player):
game.set_player_option(player.id, "Army", slot)
game.set_player_option(player.id, "Color", slot)

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)
# Options shared by all players
options = GameLaunchOptions(
mapname=mapname,
mapname=game.map_name,
expected_players=len(all_players),
)

Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/test_coop_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@


async def test_create_coop_game(database):
game = CoopGame(
CoopGame(
id_=0,
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 @@ -367,7 +367,7 @@ async def test_handle_action_GameOption(game: Game, game_connection: GameConnect
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 01d4cde

Please sign in to comment.