Core: use assert correctly (#345)

Core: add some more types to State and add count() method
This commit is contained in:
Fabian Dill 2022-03-26 01:12:54 +01:00 committed by GitHub
parent 902472be32
commit 64ac619b46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 17 deletions

View File

@ -644,15 +644,18 @@ class CollectionState():
self.events.add(event)
self.collect(event.item, True, event)
def has(self, item: str, player: int, count: int = 1):
def has(self, item: str, player: int, count: int = 1) -> bool:
return self.prog_items[item, player] >= count
def has_all(self, items: Set[str], player: int):
def has_all(self, items: Set[str], player: int) -> bool:
return all(self.prog_items[item, player] for item in items)
def has_any(self, items: Set[str], player: int):
def has_any(self, items: Set[str], player: int) -> bool:
return any(self.prog_items[item, player] for item in items)
def count(self, item: str, player: int) -> int:
return self.prog_items[item, player]
def has_group(self, item_name_group: str, player: int, count: int = 1):
found: int = 0
for item_name in self.world.worlds[player].item_name_groups[item_name_group]:

View File

@ -360,8 +360,8 @@ def main(args, seed=None, baked_server_options: Optional[Dict[str, object]] = No
locations_data: Dict[int, Dict[int, Tuple[int, int, int]]] = {player: {} for player in world.player_ids}
for location in world.get_filled_locations():
if type(location.address) == int:
# item code None should be event, location.address should then also be None
assert location.item.code is not None
assert location.item.code is not None, "item code None should be event, " \
"location.address should then also be None"
locations_data[location.player][location.address] = \
location.item.code, location.item.player, location.item.flags
if location.name in world.start_location_hints[location.player]:

View File

@ -17,8 +17,8 @@ class AssembleOptions(type):
new_options = {name[7:].lower(): option_id for name, option_id in attrs.items() if
name.startswith("option_")}
assert "random" not in new_options # Choice option 'random' cannot be manually assigned.
assert len(new_options) == len(set(new_options.values())) # same ID cannot be used twice. Try alias?
assert "random" not in new_options, "Choice option 'random' cannot be manually assigned."
assert len(new_options) == len(set(new_options.values())), "same ID cannot be used twice. Try alias?"
attrs["name_lookup"].update({option_id: name for name, option_id in new_options.items()})
options.update(new_options)
@ -104,7 +104,7 @@ class Toggle(Option):
default = 0
def __init__(self, value: int):
assert value == 0 or value == 1
assert value == 0 or value == 1, "value of Toggle can only be 0 or 1"
self.value = value
@classmethod
@ -178,10 +178,10 @@ class Choice(Option):
if isinstance(other, self.__class__):
return other.value == self.value
elif isinstance(other, str):
assert other in self.options
assert other in self.options, "compared against a str that could never be equal."
return other == self.current_key
elif isinstance(other, int):
assert other in self.name_lookup
assert other in self.name_lookup, "compared against an int that could never be equal."
return other == self.value
elif isinstance(other, bool):
return other == bool(self.value)
@ -192,10 +192,10 @@ class Choice(Option):
if isinstance(other, self.__class__):
return other.value != self.value
elif isinstance(other, str):
assert other in self.options
assert other in self.options , "compared against a str that could never be equal."
return other != self.current_key
elif isinstance(other, int):
assert other in self.name_lookup
assert other in self.name_lookup, "compared against am int that could never be equal."
return other != self.value
elif isinstance(other, bool):
return other != bool(self.value)

View File

@ -2242,7 +2242,7 @@ def crossed_shuffle_dungeons(world, player: int):
connect_caves(world, dungeon_entrances, [], [('Hyrule Castle Exit (West)', 'Hyrule Castle Exit (East)')], player)
connect_caves(world, dungeon_entrances, [], dungeon_exits, player)
assert not dungeon_exits # make sure all exits are accounted for
assert not dungeon_exits , "make sure all exits are accounted for"
def inverted_crossed_shuffle_dungeons(world, player: int):
@ -2293,7 +2293,7 @@ def inverted_crossed_shuffle_dungeons(world, player: int):
connect_mandatory_exits(world, dungeon_entrances, dungeon_exits, lw_dungeon_entrances_must_exit, player)
connect_caves(world, dungeon_entrances, [], dungeon_exits, player)
assert not dungeon_exits # make sure all exits are accounted for
assert not dungeon_exits, "make sure all exits are accounted for"
def unbias_some_entrances(world, Dungeon_Exits, Cave_Exits, Old_Man_House, Cave_Three_Exits):
def shuffle_lists_in_list(ls):

View File

@ -545,7 +545,7 @@ def get_pool_core(world, player: int):
pool.extend(diff.alwaysitems)
def place_item(loc, item):
assert loc not in placed_items
assert loc not in placed_items, "cannot place item twice"
placed_items[loc] = item
# provide boots to major glitch dependent seeds
@ -681,7 +681,7 @@ def make_custom_item_pool(world, player):
treasure_hunt_icon = None
def place_item(loc, item):
assert loc not in placed_items
assert loc not in placed_items, "cannot place item twice"
placed_items[loc] = item
# Correct for insanely oversized item counts and take initial steps to handle undersized pools.

View File

@ -419,7 +419,7 @@ progressive_technology_table: Dict[str, Technology] = {}
for root in sorted_rows:
progressive = progressive_rows[root]
assert all(tech in tech_table for tech in progressive)
assert all(tech in tech_table for tech in progressive), "declared a progressive technology without base technology"
factorio_id += 1
progressive_technology = Technology(root, technology_table[progressive_rows[root][0]].ingredients, factorio_id,
progressive,