From 8ef78cc32a90595f119aecec715887643d8dc015 Mon Sep 17 00:00:00 2001 From: CaitSith2 Date: Tue, 5 Jan 2021 09:53:52 -0800 Subject: [PATCH] Add options to allow silent failed plando placements. --- BaseClasses.py | 14 +++++++++++++- Fill.py | 41 ++++++++++++++--------------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/BaseClasses.py b/BaseClasses.py index 46f49521..8014605a 100644 --- a/BaseClasses.py +++ b/BaseClasses.py @@ -1477,7 +1477,19 @@ class PlandoItem(NamedTuple): location: str world: Union[bool, str] = False # False -> own world, True -> not own world from_pool: bool = True # if item should be removed from item pool - force: bool = False # False -> warns if item not successfully placed. True -> errors out on failure to place item. + force: Union[bool, str] = 'silent' # False -> warns if item not successfully placed. True -> errors out on failure to place item. + + def warn(self, warning: str): + if str(self.force).lower() in ['true', 'fail', 'failure', 'none', 'false', 'warn', 'warning']: + logging.warning(f'{warning}') + else: + logging.debug(f'{warning}') + + def failed(self, warning: str, exception=Exception): + if str(self.force).lower() in ['true', 'fail', 'failure']: + raise exception(warning) + else: + self.warn(warning) class PlandoConnection(NamedTuple): diff --git a/Fill.py b/Fill.py index efa9c95e..ba337bea 100644 --- a/Fill.py +++ b/Fill.py @@ -359,11 +359,8 @@ def distribute_planned(world): set(world.player_ids) - {player}) if location.item_rule(item) ) if not unfilled: - if placement.force: - raise FillError(f"Could not find a world with an unfilled location {placement.location}") - else: - logging.warning(f"Could not find a world with an unfilled location {placement.location}, skipping.") - continue + placement.failed(f"Could not find a world with an unfilled location {placement.location}", FillError) + continue target_world = world.random.choice(unfilled).player @@ -373,32 +370,25 @@ def distribute_planned(world): set(world.player_ids)) if location.item_rule(item) ) if not unfilled: - if placement.force: - raise FillError(f"Could not find a world with an unfilled location {placement.location}") - else: - logging.warning(f"Could not find a world with an unfilled location {placement.location}, skipping.") - continue + placement.failed(f"Could not find a world with an unfilled location {placement.location}", FillError) + continue target_world = world.random.choice(unfilled).player elif type(target_world) == int: # target world by player id - pass + if target_world not in range(1, world.players + 1): + placement.failed(f"Cannot place item in world {target_world} as it is not in range of (1, {world.players})", ValueError) + continue else: # find world by name if target_world not in world_name_lookup: - if placement.force: - raise Exception(f"Cannot place item to {target_world}'s world as that world does not exist.") - else: - logging.warning(f"Cannot place item to {target_world}'s world as that world does not exist. Skipping.") - continue + placement.failed(f"Cannot place item to {target_world}'s world as that world does not exist.", ValueError) + continue target_world = world_name_lookup[target_world] location = world.get_location(placement.location, target_world) if location.item: - if placement.force: - raise Exception(f"Cannot place item into already filled location {location}.") - else: - logging.warning(f"Cannot place item into already filled location {location}. Skipping.") - continue + placement.failed(f"Cannot place item into already filled location {location}.") + continue if location.can_fill(world.state, item, False): world.push_item(location, item, collect=False) @@ -406,14 +396,11 @@ def distribute_planned(world): location.locked = True logging.debug(f"Plando placed {item} at {location}") else: - if placement.force: - raise Exception(f"Can't place {item} at {location} due to fill condition not met.") - else: - logging.warning(f"Can't place {item} at {location} due to fill condition not met. Skipping.") - continue + placement.failed(f"Can't place {item} at {location} due to fill condition not met.") + continue if placement.from_pool: # Should happen AFTER the item is placed, in case it was allowed to skip failed placement. try: world.itempool.remove(item) except ValueError: - logging.warning(f"Could not remove {item} from pool as it's already missing from it.") + placement.warn(f"Could not remove {item} from pool as it's already missing from it.")