diff --git a/server/app/item_types.py b/server/app/item_types.py index 47b0efc..b6948ed 100644 --- a/server/app/item_types.py +++ b/server/app/item_types.py @@ -26,4 +26,4 @@ class ItemTypeHandler: validate_update: Callable[[WorldItem, dict], dict] use: Callable[[WorldItem, str, Callable[[dict], str]], ItemUseResult] secondary_use: Callable[[WorldItem, str, Callable[[dict], str]], ItemUseResult] | None = None - interact: Callable[[WorldItem, str, dict | None, str], ItemUseResult] | None = None + interact: Callable[[WorldItem, str, dict | None, str, str], ItemUseResult] | None = None diff --git a/server/app/items/types/card_table/actions.py b/server/app/items/types/card_table/actions.py index 605b882..6baeb4d 100644 --- a/server/app/items/types/card_table/actions.py +++ b/server/app/items/types/card_table/actions.py @@ -50,17 +50,14 @@ def use_item(item: WorldItem, nickname: str, _clock_formatter: Callable[[dict], discard_pile = [] if not isinstance(hands, dict): hands = {} - hand = hands.get(nickname, []) - if not isinstance(hand, list): - hand = [] draw_count = len(draw_pile) discard_count = len(discard_pile) - hand_count = len(hand) + dealt_count = sum(len(v) for v in hands.values() if isinstance(v, list)) return ItemUseResult( self_message=( f"{item.title}: {draw_count} in draw pile, " - f"{discard_count} in discard, {hand_count} in your hand." + f"{discard_count} in discard, {dealt_count} dealt out." ), others_message="", ) @@ -88,6 +85,7 @@ def interact_item( item: WorldItem, action: str, params: dict | None, + actor_id: str, nickname: str, ) -> ItemUseResult: """Handle a card table interact action on behalf of any user.""" @@ -103,9 +101,9 @@ def interact_item( if not draw_pile: raise ValueError("Draw pile is empty.") card = draw_pile.pop(0) - hand = list(hands.get(nickname, [])) + hand = list(hands.get(actor_id, [])) hand.append(card) - hands[nickname] = hand + hands[actor_id] = hand return ItemUseResult( self_message=f"You drew {_card_name(card)}. {len(draw_pile)} remaining in draw pile.", others_message=f"{nickname} draws a card.", @@ -121,9 +119,9 @@ def interact_item( if not isinstance(card_index, int) or card_index < 0 or card_index >= len(discard_pile): raise ValueError("Invalid card_index.") card = discard_pile.pop(card_index) - hand = list(hands.get(nickname, [])) + hand = list(hands.get(actor_id, [])) hand.append(card) - hands[nickname] = hand + hands[actor_id] = hand return ItemUseResult( self_message=f"You took {_card_name(card)} from the discard pile.", others_message=f"{nickname} takes a card from the discard pile.", @@ -133,13 +131,13 @@ def interact_item( if action == "discard": if not params or "card_index" not in params: raise ValueError("discard requires params.card_index.") - hand = list(hands.get(nickname, [])) + hand = list(hands.get(actor_id, [])) card_index = params["card_index"] if not isinstance(card_index, int) or card_index < 0 or card_index >= len(hand): raise ValueError("Invalid card_index.") card = hand.pop(card_index) discard_pile.insert(0, card) - hands[nickname] = hand + hands[actor_id] = hand return ItemUseResult( self_message=f"You discarded {_card_name(card)}.", others_message=f"{nickname} discards a card.", @@ -149,13 +147,13 @@ def interact_item( if action == "return_to_pile": if not params or "card_index" not in params: raise ValueError("return_to_pile requires params.card_index.") - hand = list(hands.get(nickname, [])) + hand = list(hands.get(actor_id, [])) card_index = params["card_index"] if not isinstance(card_index, int) or card_index < 0 or card_index >= len(hand): raise ValueError("Invalid card_index.") card = hand.pop(card_index) draw_pile.append(card) - hands[nickname] = hand + hands[actor_id] = hand return ItemUseResult( self_message=f"You returned {_card_name(card)} to the draw pile.", others_message=f"{nickname} returns a card to the draw pile.", diff --git a/server/app/server.py b/server/app/server.py index cde4d93..a492a83 100644 --- a/server/app/server.py +++ b/server/app/server.py @@ -2994,7 +2994,7 @@ class SignalingServer: ) return try: - interact_result = handler.interact(item, packet.action, packet.params, client.nickname) + interact_result = handler.interact(item, packet.action, packet.params, client.user_id or client.id, client.nickname) except ValueError as exc: await self._send_item_result(client, False, "interact", str(exc), item.id) return