From 26ee9fe05cefea24c907a9a71941846dcf6db6bd Mon Sep 17 00:00:00 2001 From: Remy Jette Date: Tue, 5 Mar 2024 00:36:18 -0800 Subject: [PATCH] Pokemon RB: Fix exceptions raised by /bank (#2836) * If the user tried to run `/bank` with no arguments to see the current value while disconnected, previously it threw an exception `KeyError: 'EnergyLinkNone'`. Now it informs the user that they must be connected and in-game, like `/bank deposit` and `/bank withdraw` do. I'm also open to adding another `if` branch to make `/bank` only check for `ctx.server` instead of combining it with the other bank commands, to allow connecting to check the bank before the game save is loaded. If that's preferred let me know. * If the user tried to run `/bank` or `/bank deposit` when the EnergyLink hadn't been used yet, they would get a `TypeError` exception. Trying `/bank withdraw` would give no output and would crash the lua connector script. Now it treats a `None` EnergyLink as `0` and works properly. --- worlds/pokemon_rb/client.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/worlds/pokemon_rb/client.py b/worlds/pokemon_rb/client.py index 9e2689bc..8ed21443 100644 --- a/worlds/pokemon_rb/client.py +++ b/worlds/pokemon_rb/client.py @@ -206,7 +206,7 @@ class PokemonRBClient(BizHawkClient): money = int(original_money.hex()) if self.banking_command > money: logger.warning(f"You do not have ${self.banking_command} to deposit!") - elif (-self.banking_command * BANK_EXCHANGE_RATE) > ctx.stored_data[f"EnergyLink{ctx.team}"]: + elif (-self.banking_command * BANK_EXCHANGE_RATE) > (ctx.stored_data[f"EnergyLink{ctx.team}"] or 0): logger.warning("Not enough money in the EnergyLink storage!") else: if self.banking_command + money > 999999: @@ -258,11 +258,12 @@ def cmd_bank(self, cmd: str = "", amount: str = ""): if self.ctx.game != "Pokemon Red and Blue": logger.warning("This command can only be used while playing Pokémon Red and Blue") return - if not cmd: - logger.info(f"Money available: {int(self.ctx.stored_data[f'EnergyLink{self.ctx.team}'] / BANK_EXCHANGE_RATE)}") - return - elif (not self.ctx.server) or self.ctx.server.socket.closed or not self.ctx.client_handler.game_state: + if (not self.ctx.server) or self.ctx.server.socket.closed or not self.ctx.client_handler.game_state: logger.info(f"Must be connected to server and in game.") + return + elif not cmd: + logger.info(f"Money available: {int((self.ctx.stored_data[f'EnergyLink{self.ctx.team}'] or 0) / BANK_EXCHANGE_RATE)}") + return elif not amount: logger.warning("You must specify an amount.") elif cmd == "withdraw":