Skip to content

Commit

Permalink
add some better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouramie committed Dec 17, 2024
1 parent 90f920a commit 7af0458
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions worlds/stardew_valley/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ class StardewCommandProcessor(ClientCommandProcessor):
@mark_raw
def _cmd_explain(self, location: str = ""):
"""Explain the logic behind a location."""
if self.ctx.logic is None:
logger.warning("Internal logic was not able to load, check your yamls and relaunch.")
logic = self.ctx.get_logic()
if logic is None:
return

try:
rule = self.ctx.logic.region.can_reach_location(location)
rule = logic.region.can_reach_location(location)
expl = explain(rule, get_updated_state(self.ctx), expected=None, mode=ExplainMode.CLIENT)
except KeyError:

result, usable, response = Utils.get_intended_text(location, [loc.name for loc in self.ctx.multiworld.get_locations(1)])
if usable:
rule = self.ctx.logic.region.can_reach_location(result)
rule = logic.region.can_reach_location(result)
expl = explain(rule, get_updated_state(self.ctx), expected=None, mode=ExplainMode.CLIENT)
else:
logger.warning(response)
Expand All @@ -73,13 +73,13 @@ def _cmd_explain(self, location: str = ""):
@mark_raw
def _cmd_explain_item(self, item: str = ""):
"""Explain the logic behind a game item."""
if self.ctx.logic is None:
logger.warning("Internal logic was not able to load, check your yamls and relaunch.")
logic = self.ctx.get_logic()
if logic is None:
return

result, usable, response = Utils.get_intended_text(item, self.ctx.logic.registry.item_rules.keys())
result, usable, response = Utils.get_intended_text(item, logic.registry.item_rules.keys())
if usable:
rule = self.ctx.logic.has(result)
rule = logic.has(result)
expl = explain(rule, get_updated_state(self.ctx), expected=None, mode=ExplainMode.CLIENT)
else:
logger.warning(response)
Expand All @@ -91,20 +91,19 @@ def _cmd_explain_item(self, item: str = ""):
@mark_raw
def _cmd_explain_missing(self, location: str = ""):
"""Explain the logic behind a location, while skipping the rules that are already satisfied."""
if self.ctx.logic is None:
logger.warning("Internal logic was not able to load, check your yamls and relaunch.")
logic = self.ctx.get_logic()
if logic is None:
return

try:
rule = self.ctx.logic.region.can_reach_location(location)
rule = logic.region.can_reach_location(location)
state = get_updated_state(self.ctx)
simplified, _ = rule.evaluate_while_simplifying(state)
expl = explain(simplified, state, mode=ExplainMode.CLIENT)
except KeyError:

result, usable, response = Utils.get_intended_text(location, [loc.name for loc in self.ctx.multiworld.get_locations(1)])
if usable:
rule = self.ctx.logic.region.can_reach_location(result)
rule = logic.region.can_reach_location(result)
state = get_updated_state(self.ctx)
simplified, _ = rule.evaluate_while_simplifying(state)
expl = explain(simplified, state, mode=ExplainMode.CLIENT)
Expand Down Expand Up @@ -141,7 +140,6 @@ def _cmd_more(self, index: str = ""):
class StardewClientContext(TrackerGameContext):
game = "Stardew Valley"
command_processor = StardewCommandProcessor
logic: StardewLogic | None = None
previous_explanation: RuleExplanation | None = None

def make_gui(self):
Expand All @@ -160,9 +158,16 @@ def build(self):

return StardewManager

def setup_logic(self):
if self.multiworld is not None:
self.logic = self.multiworld.worlds[1].logic
def get_logic(self) -> StardewLogic | None:
if self.player_id is None:
logger.warning("Internal logic was not able to load, check your yamls and relaunch.")
return

if self.game != "Stardew Valley":
logger.warning(f"Please connect to a slot with explainable logic (not {self.game}).")
return

return self.multiworld.worlds[self.player_id].logic


def parse_explanation(explanation: RuleExplanation) -> list[JSONMessagePart]:
Expand Down Expand Up @@ -221,7 +226,6 @@ async def main(args):

if tracker_loaded:
ctx.run_generator()
ctx.setup_logic()
else:
logger.warning("Could not find Universal Tracker.")

Expand Down

0 comments on commit 7af0458

Please sign in to comment.