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.
This commit is contained in:
Remy Jette 2024-03-05 00:36:18 -08:00 committed by GitHub
parent 12cc930825
commit 26ee9fe05c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 6 additions and 5 deletions

View File

@ -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":