add world get_type type checker

This commit is contained in:
Fabian Dill 2020-04-10 20:54:18 +02:00
parent 5254b5588e
commit 2ebc133cab
1 changed files with 56 additions and 0 deletions

View File

@ -11,6 +11,7 @@ from Utils import int16_as_bytes
from typing import Union from typing import Union
class World(object): class World(object):
debug_types = False
player_names: list player_names: list
_region_cache: dict _region_cache: dict
difficulty_requirements: dict difficulty_requirements: dict
@ -18,6 +19,15 @@ class World(object):
def __init__(self, players, shuffle, logic, mode, swords, difficulty, difficulty_adjustments, timer, progressive, def __init__(self, players, shuffle, logic, mode, swords, difficulty, difficulty_adjustments, timer, progressive,
goal, algorithm, accessibility, shuffle_ganon, retro, custom, customitemarray, hints): goal, algorithm, accessibility, shuffle_ganon, retro, custom, customitemarray, hints):
if self.debug_types:
import inspect
methods = inspect.getmembers(self, predicate=inspect.ismethod)
for name, method in methods:
if name.startswith("_debug_"):
setattr(self, name[7:], method)
logging.info(f"Set {self}.{name[7:]} to {method}")
self.get_location = self._debug_get_location
self.players = players self.players = players
self.teams = 1 self.teams = 1
self.shuffle = shuffle.copy() self.shuffle = shuffle.copy()
@ -127,6 +137,18 @@ class World(object):
return region return region
raise RuntimeError('No such region %s for player %d' % (regionname, player)) raise RuntimeError('No such region %s for player %d' % (regionname, player))
def _debug_get_region(self, regionname: str, player: int) -> Region:
if type(regionname) != str:
raise TypeError(f"expected str, got {type(regionname)} instead")
try:
return self._region_cache[player][regionname]
except KeyError:
for region in self.regions:
if region.name == regionname and region.player == player:
assert not region.world # this should only happen before initialization
return region
raise RuntimeError('No such region %s for player %d' % (regionname, player))
def get_entrance(self, entrance: str, player: int) -> Entrance: def get_entrance(self, entrance: str, player: int) -> Entrance:
try: try:
return self._entrance_cache[(entrance, player)] return self._entrance_cache[(entrance, player)]
@ -138,6 +160,19 @@ class World(object):
return exit return exit
raise RuntimeError('No such entrance %s for player %d' % (entrance, player)) raise RuntimeError('No such entrance %s for player %d' % (entrance, player))
def _debug_get_entrance(self, entrance: str, player: int) -> Entrance:
if type(entrance) != str:
raise TypeError(f"expected str, got {type(entrance)} instead")
try:
return self._entrance_cache[(entrance, player)]
except KeyError:
for region in self.regions:
for exit in region.exits:
if exit.name == entrance and exit.player == player:
self._entrance_cache[(entrance, player)] = exit
return exit
raise RuntimeError('No such entrance %s for player %d' % (entrance, player))
def get_location(self, location: str, player: int) -> Location: def get_location(self, location: str, player: int) -> Location:
try: try:
return self._location_cache[(location, player)] return self._location_cache[(location, player)]
@ -149,12 +184,33 @@ class World(object):
return r_location return r_location
raise RuntimeError('No such location %s for player %d' % (location, player)) raise RuntimeError('No such location %s for player %d' % (location, player))
def _debug_get_location(self, location: str, player: int) -> Location:
if type(location) != str:
raise TypeError(f"expected str, got {type(location)} instead")
try:
return self._location_cache[(location, player)]
except KeyError:
for region in self.regions:
for r_location in region.locations:
if r_location.name == location and r_location.player == player:
self._location_cache[(location, player)] = r_location
return r_location
raise RuntimeError('No such location %s for player %d' % (location, player))
def get_dungeon(self, dungeonname: str, player: int) -> Dungeon: def get_dungeon(self, dungeonname: str, player: int) -> Dungeon:
for dungeon in self.dungeons: for dungeon in self.dungeons:
if dungeon.name == dungeonname and dungeon.player == player: if dungeon.name == dungeonname and dungeon.player == player:
return dungeon return dungeon
raise RuntimeError('No such dungeon %s for player %d' % (dungeonname, player)) raise RuntimeError('No such dungeon %s for player %d' % (dungeonname, player))
def _debug_get_dungeon(self, dungeonname: str, player: int) -> Dungeon:
if type(dungeonname) != str:
raise TypeError(f"expected str, got {type(dungeonname)} instead")
for dungeon in self.dungeons:
if dungeon.name == dungeonname and dungeon.player == player:
return dungeon
raise RuntimeError('No such dungeon %s for player %d' % (dungeonname, player))
def get_all_state(self, keys=False) -> CollectionState: def get_all_state(self, keys=False) -> CollectionState:
ret = CollectionState(self) ret = CollectionState(self)