Core: Add support for non dictionary iterables for `Region.add_exits` (#1698)
* Core: Add support for non dictionary iterables for `Region.add_exits` * some cleanup and duplicate code removal * add unit test for non dict iterable * use more consistent naming * sometimes i just make stuff harder on myself :)
This commit is contained in:
parent
fa3c132304
commit
9b15278de8
|
@ -836,20 +836,23 @@ class Region:
|
||||||
for location, address in locations.items():
|
for location, address in locations.items():
|
||||||
self.locations.append(location_type(self.player, location, address, self))
|
self.locations.append(location_type(self.player, location, address, self))
|
||||||
|
|
||||||
def add_exits(self, exits: Dict[str, Optional[str]], rules: Dict[str, Callable[[CollectionState], bool]] = None) -> None:
|
def add_exits(self, exits: Union[Iterable[str], Dict[str, Optional[str]]],
|
||||||
|
rules: Dict[str, Callable[[CollectionState], bool]] = None) -> None:
|
||||||
"""
|
"""
|
||||||
Connects current region to regions in exit dictionary. Passed region names must exist first.
|
Connects current region to regions in exit dictionary. Passed region names must exist first.
|
||||||
|
|
||||||
:param exits: exits from the region. format is {"connecting_region", "exit_name"}
|
:param exits: exits from the region. format is {"connecting_region": "exit_name"}. if a non dict is provided,
|
||||||
|
created entrances will be named "self.name -> connecting_region"
|
||||||
:param rules: rules for the exits from this region. format is {"connecting_region", rule}
|
:param rules: rules for the exits from this region. format is {"connecting_region", rule}
|
||||||
"""
|
"""
|
||||||
for exiting_region, name in exits.items():
|
if not isinstance(exits, Dict):
|
||||||
ret = Entrance(self.player, name, self) if name \
|
exits = dict.fromkeys(exits)
|
||||||
else Entrance(self.player, f"{self.name} -> {exiting_region}", self)
|
for connecting_region, name in exits.items():
|
||||||
if rules and exiting_region in rules:
|
entrance = Entrance(self.player, name if name else f"{self.name} -> {connecting_region}", self)
|
||||||
ret.access_rule = rules[exiting_region]
|
if rules and connecting_region in rules:
|
||||||
self.exits.append(ret)
|
entrance.access_rule = rules[connecting_region]
|
||||||
ret.connect(self.multiworld.get_region(exiting_region, self.player))
|
self.exits.append(entrance)
|
||||||
|
entrance.connect(self.multiworld.get_region(connecting_region, self.player))
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
|
@ -19,6 +19,7 @@ class TestHelpers(unittest.TestCase):
|
||||||
regions: Dict[str, str] = {
|
regions: Dict[str, str] = {
|
||||||
"TestRegion1": "I'm an apple",
|
"TestRegion1": "I'm an apple",
|
||||||
"TestRegion2": "I'm a banana",
|
"TestRegion2": "I'm a banana",
|
||||||
|
"TestRegion3": "Empty Region",
|
||||||
}
|
}
|
||||||
|
|
||||||
locations: Dict[str, Dict[str, Optional[int]]] = {
|
locations: Dict[str, Dict[str, Optional[int]]] = {
|
||||||
|
@ -38,6 +39,10 @@ class TestHelpers(unittest.TestCase):
|
||||||
"TestRegion2": {"TestRegion1": None},
|
"TestRegion2": {"TestRegion1": None},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
reg_exit_set: Dict[str, set[str]] = {
|
||||||
|
"TestRegion1": {"TestRegion3"}
|
||||||
|
}
|
||||||
|
|
||||||
exit_rules: Dict[str, Callable[[CollectionState], bool]] = {
|
exit_rules: Dict[str, Callable[[CollectionState], bool]] = {
|
||||||
"TestRegion1": lambda state: state.has("test_item", self.player)
|
"TestRegion1": lambda state: state.has("test_item", self.player)
|
||||||
}
|
}
|
||||||
|
@ -68,3 +73,10 @@ class TestHelpers(unittest.TestCase):
|
||||||
entrance_name = exit_name if exit_name else f"{parent} -> {exit_reg}"
|
entrance_name = exit_name if exit_name else f"{parent} -> {exit_reg}"
|
||||||
self.assertEqual(exit_rules[exit_reg],
|
self.assertEqual(exit_rules[exit_reg],
|
||||||
self.multiworld.get_entrance(entrance_name, self.player).access_rule)
|
self.multiworld.get_entrance(entrance_name, self.player).access_rule)
|
||||||
|
|
||||||
|
for region in reg_exit_set:
|
||||||
|
current_region = self.multiworld.get_region(region, self.player)
|
||||||
|
current_region.add_exits(reg_exit_set[region])
|
||||||
|
exit_names = {_exit.name for _exit in current_region.exits}
|
||||||
|
for reg_exit in reg_exit_set[region]:
|
||||||
|
self.assertTrue(f"{region} -> {reg_exit}" in exit_names, f"{region} -> {reg_exit} not in {exit_names}")
|
||||||
|
|
Loading…
Reference in New Issue