13 Commits

3 changed files with 15 additions and 13 deletions

View File

@@ -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, str], ItemUseResult] | None = None
interact: Callable[[WorldItem, str, dict | None, str], ItemUseResult] | None = None

View File

@@ -50,14 +50,17 @@ 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)
dealt_count = sum(len(v) for v in hands.values() if isinstance(v, list))
hand_count = len(hand)
return ItemUseResult(
self_message=(
f"{item.title}: {draw_count} in draw pile, "
f"{discard_count} in discard, {dealt_count} dealt out."
f"{discard_count} in discard, {hand_count} in your hand."
),
others_message="",
)
@@ -85,7 +88,6 @@ 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."""
@@ -101,9 +103,9 @@ def interact_item(
if not draw_pile:
raise ValueError("Draw pile is empty.")
card = draw_pile.pop(0)
hand = list(hands.get(actor_id, []))
hand = list(hands.get(nickname, []))
hand.append(card)
hands[actor_id] = hand
hands[nickname] = 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.",
@@ -119,9 +121,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(actor_id, []))
hand = list(hands.get(nickname, []))
hand.append(card)
hands[actor_id] = hand
hands[nickname] = 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.",
@@ -131,13 +133,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(actor_id, []))
hand = list(hands.get(nickname, []))
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[actor_id] = hand
hands[nickname] = hand
return ItemUseResult(
self_message=f"You discarded {_card_name(card)}.",
others_message=f"{nickname} discards a card.",
@@ -147,13 +149,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(actor_id, []))
hand = list(hands.get(nickname, []))
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[actor_id] = hand
hands[nickname] = 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.",

View File

@@ -2994,7 +2994,7 @@ class SignalingServer:
)
return
try:
interact_result = handler.interact(item, packet.action, packet.params, client.user_id or client.id, client.nickname)
interact_result = handler.interact(item, packet.action, packet.params, client.nickname)
except ValueError as exc:
await self._send_item_result(client, False, "interact", str(exc), item.id)
return