From be8e162a5a359f22d9f6762f893bce24fd4a2632 Mon Sep 17 00:00:00 2001 From: Sasha Vezhnevets Date: Wed, 9 Oct 2024 08:33:39 -0700 Subject: [PATCH] Fixes a bug in inventory component, which crashes if player_action_attempt has ': ' in its text. PiperOrigin-RevId: 684048494 Change-Id: I5ec782333684b1665e189f8a442d6c7afbac26cd --- .../game_master/triggered_function.py | 5 ++++- .../game_master/triggered_inventory_effect.py | 21 ++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/concordia/components/game_master/triggered_function.py b/concordia/components/game_master/triggered_function.py index 735850e4..22ccfb5c 100644 --- a/concordia/components/game_master/triggered_function.py +++ b/concordia/components/game_master/triggered_function.py @@ -121,7 +121,10 @@ def update(self) -> None: def update_before_event(self, player_action_attempt: str) -> None: if self._pre_event_fn is None: return - player_name, choice = player_action_attempt.split(': ') + # we assume that the player action attempt is in the format + # 'player_name: player_choice'. All other occurences of ':' will be treated + # as a part of the player choice. + player_name, choice = player_action_attempt.split(': ', 1) if player_name not in [player.name for player in self._players]: return current_scene_type = self._current_scene.state() diff --git a/concordia/components/game_master/triggered_inventory_effect.py b/concordia/components/game_master/triggered_inventory_effect.py index 11702420..aa8ae335 100644 --- a/concordia/components/game_master/triggered_inventory_effect.py +++ b/concordia/components/game_master/triggered_inventory_effect.py @@ -50,7 +50,7 @@ class PreEventFnArgsT: player: PlayerT -def _get_player_by_name(player_name: str, players: PlayersT) -> PlayerT | None: +def _get_player_by_name(player_name: str, players: PlayersT) -> PlayerT | None: """Get a player object by name. Assumes no duplicate names.""" for player in players: if player.name == player_name: @@ -63,9 +63,10 @@ class TriggeredInventoryEffect(component.Component): def __init__( self, - function: Callable[[PreEventFnArgsT, - inventory_gm_component.InventoryType], - inventory_gm_component.InventoryType], + function: Callable[ + [PreEventFnArgsT, inventory_gm_component.InventoryType], + inventory_gm_component.InventoryType, + ], inventory: inventory_gm_component.Inventory, memory: associative_memory.AssociativeMemory, players: PlayersT, @@ -76,8 +77,8 @@ def __init__( """Initialize a component to track how events change inventories. Args: - function: user-provided function that can modify the inventory based on - an action attempt. + function: user-provided function that can modify the inventory based on an + action attempt. inventory: the inventory component to use to get the inventory of players. memory: an associative memory players: sequence of players who can trigger an inventory event. @@ -115,7 +116,11 @@ def update(self) -> None: self._current_scene.update() def update_before_event(self, player_action_attempt: str) -> None: - player_name, choice = player_action_attempt.split(': ') + + # we assume that the player action attempt is in the format + # 'player_name: player_choice'. All other occurences of ':' will be treated + # as a part of the player choice. + player_name, choice = player_action_attempt.split(': ', 1) if player_name not in [player.name for player in self._players]: return current_scene_type = self._current_scene.state() @@ -128,7 +133,7 @@ def update_before_event(self, player_action_attempt: str) -> None: current_scene_type=current_scene_type, memory=self._memory, player=player, - ) + ), ) self._inventory.apply(update) self._latest_update_log = {