Skip to content

Commit

Permalink
Fixes a bug in inventory component, which crashes if player_action_at…
Browse files Browse the repository at this point in the history
…tempt has ': ' in its text.

PiperOrigin-RevId: 684048494
Change-Id: I5ec782333684b1665e189f8a442d6c7afbac26cd
  • Loading branch information
vezhnick authored and copybara-github committed Oct 9, 2024
1 parent b50d13a commit be8e162
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
5 changes: 4 additions & 1 deletion concordia/components/game_master/triggered_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
21 changes: 13 additions & 8 deletions concordia/components/game_master/triggered_inventory_effect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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 = {
Expand Down

0 comments on commit be8e162

Please sign in to comment.