Skip to content

Commit

Permalink
Make boolean game option checks more lenient
Browse files Browse the repository at this point in the history
  • Loading branch information
Askaholic committed Feb 20, 2021
1 parent c7410a4 commit 0790c99
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
7 changes: 4 additions & 3 deletions server/games/coop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from server.abc.base_game import InitMode

from .game import Game, GameType, ValidityState, Victory
from .game import Game
from .typedefs import FA, GameType, ValidityState, Victory


class CoopGame(Game):
Expand Down Expand Up @@ -29,9 +30,9 @@ async def validate_game_mode_settings(self):
valid_options = {
"Victory": (Victory.SANDBOX, ValidityState.WRONG_VICTORY_CONDITION),
"TeamSpawn": ("fixed", ValidityState.SPAWN_NOT_FIXED),
"RevealedCivilians": ("No", ValidityState.CIVILIANS_REVEALED),
"RevealedCivilians": (FA.FALSE, ValidityState.CIVILIANS_REVEALED),
"Difficulty": (3, ValidityState.WRONG_DIFFICULTY),
"Expansion": ("true", ValidityState.EXPANSION_DISABLED),
"Expansion": (FA.TRUE, ValidityState.EXPANSION_DISABLED),
}
await self._validate_game_options(valid_options)

Expand Down
9 changes: 5 additions & 4 deletions server/games/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ..abc.base_game import GameConnectionState, InitMode
from ..players import Player, PlayerState
from .typedefs import (
FA,
BasicGameInfo,
EndedGameInfo,
FeaturedModType,
Expand Down Expand Up @@ -617,11 +618,11 @@ async def validate_game_settings(self):
await self.mark_invalid(ValidityState.FFA_NOT_RANKED)
return
valid_options = {
"AIReplacement": ("Off", ValidityState.HAS_AI_PLAYERS),
"AIReplacement": (FA.FALSE, ValidityState.HAS_AI_PLAYERS),
"FogOfWar": ("explored", ValidityState.NO_FOG_OF_WAR),
"CheatsEnabled": ("false", ValidityState.CHEATS_ENABLED),
"PrebuiltUnits": ("Off", ValidityState.PREBUILT_ENABLED),
"NoRushOption": ("Off", ValidityState.NORUSH_ENABLED),
"CheatsEnabled": (FA.FALSE, ValidityState.CHEATS_ENABLED),
"PrebuiltUnits": (FA.FALSE, ValidityState.PREBUILT_ENABLED),
"NoRushOption": (FA.FALSE, ValidityState.NORUSH_ENABLED),
"RestrictedCategories": (0, ValidityState.BAD_UNIT_RESTRICTIONS),
"TeamLock": ("locked", ValidityState.UNLOCKED_TEAMS)
}
Expand Down
29 changes: 28 additions & 1 deletion server/games/typedefs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum, unique
from typing import Dict, List, NamedTuple, Optional, Set
from typing import Any, Dict, List, NamedTuple, Optional, Set

from server.games.game_results import GameOutcome
from server.players import Player
Expand Down Expand Up @@ -190,3 +190,30 @@ def to_dict(self):
for team_summary in self.team_summaries
],
}


class _FATrue(object):
__slots__ = ()

def __eq__(self, other: Any) -> bool:
if isinstance(other, str):
other = other.lower()

return other in (True, "true", "on", "yes", 1)


class _FAFalse(object):
__slots__ = ()

def __eq__(self, other: Any) -> bool:
if isinstance(other, str):
other = other.lower()

return other in (False, "false", "off", "no", 0)


class FA(object):
__slots__ = ()

TRUE = _FATrue()
FALSE = _FAFalse()

0 comments on commit 0790c99

Please sign in to comment.