Skip to content

Commit

Permalink
Issue/#636 Refactor game validity checks (#652)
Browse files Browse the repository at this point in the history
This commit restructures the validity checks that are performed on
games. It starts taking advantage of the OOP setup of Game classes.
Child classes of Game can redefine the `validate_game_mode_settings`
method to set custom game validity rules.
  • Loading branch information
46bit authored Sep 9, 2020
1 parent 72d4774 commit 22a8e9c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
14 changes: 14 additions & 0 deletions server/games/coop.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ def __init__(self, *args, **kwargs):
"Difficulty": 3,
"Expansion": 1
})

async def validate_game_mode_settings(self):
"""
Checks which only apply to the coop mode
"""

valid_options = {
"Victory": (Victory.SANDBOX, ValidityState.WRONG_VICTORY_CONDITION),
"TeamSpawn": ("fixed", ValidityState.SPAWN_NOT_FIXED),
"RevealedCivilians": ("No", ValidityState.CIVILIANS_REVEALED),
"Difficulty": (3, ValidityState.WRONG_DIFFICULTY),
"Expansion": (1, ValidityState.EXPANSION_DISABLED),
}
await self._validate_game_options(valid_options)
47 changes: 17 additions & 30 deletions server/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,40 +584,16 @@ async def validate_game_settings(self):
if await self._validate_game_options(valid_options) is False:
return

if self.game_mode in (FeaturedModType.FAF, FeaturedModType.LADDER_1V1):
await self._validate_faf_game_settings()
elif self.game_mode == FeaturedModType.COOP:
await self._validate_coop_game_settings()
await self.validate_game_mode_settings()

async def _validate_game_options(
self, valid_options: Dict[str, Tuple[Any, ValidityState]]
) -> bool:
for key, value in self.gameOptions.items():
if key in valid_options:
(valid_value, validity_state) = valid_options[key]
if self.gameOptions[key] != valid_value:
await self.mark_invalid(validity_state)
return False
return True

async def _validate_coop_game_settings(self):
"""
Checks which only apply to the coop mode
"""

valid_options = {
"Victory": (Victory.SANDBOX, ValidityState.WRONG_VICTORY_CONDITION),
"TeamSpawn": ("fixed", ValidityState.SPAWN_NOT_FIXED),
"RevealedCivilians": ("No", ValidityState.CIVILIANS_REVEALED),
"Difficulty": (3, ValidityState.WRONG_DIFFICULTY),
"Expansion": (1, ValidityState.EXPANSION_DISABLED),
}
await self._validate_game_options(valid_options)

async def _validate_faf_game_settings(self):
async def validate_game_mode_settings(self):
"""
Checks which only apply to the faf or ladder1v1 mode
Override this in a child game class for custom checks
"""
if self.game_mode not in (FeaturedModType.FAF, FeaturedModType.LADDER_1V1):
return

if None in self.teams or not self.is_even:
await self.mark_invalid(ValidityState.UNEVEN_TEAMS_NOT_RANKED)
return
Expand All @@ -631,6 +607,17 @@ async def _validate_faf_game_settings(self):
}
await self._validate_game_options(valid_options)

async def _validate_game_options(
self, valid_options: Dict[str, Tuple[Any, ValidityState]]
) -> bool:
for key, value in self.gameOptions.items():
if key in valid_options:
(valid_value, validity_state) = valid_options[key]
if self.gameOptions[key] != valid_value:
await self.mark_invalid(validity_state)
return False
return True

async def launch(self):
"""
Mark the game as live.
Expand Down

0 comments on commit 22a8e9c

Please sign in to comment.