2b2af058b8
Only the player character's HP was tracked before, so a monster's death was never detected — only the DM's own (unenforced) narration of it. Add a new update_monster_hp tool the DM calls whenever an NPC/monster is introduced or takes damage, stored as DM-only bookkeeping on Game (never exposed via GameRead, since HP/AC of NPCs is meant to stay secret from players). The tool reports back "defeated": true once HP drops to 0 so the DM can react to it — but unlike a character dying, this does NOT auto-end the session; whether a monster's death should end the game is a separate decision left to the DM's own end_game call, or to a future rule. Building an isolated test for this caught a real bug along the way: the update mutated the matched monster's dict in place before reassigning the list, which made SQLAlchemy's old-vs-new JSONB comparison see identical content and silently skip writing the change. Fixed by building fresh dicts instead of mutating shared ones. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import ForeignKey, String, Text, UniqueConstraint, func
|
|
from sqlalchemy.dialects.postgresql import JSONB, UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.db import Base
|
|
|
|
|
|
class Game(Base):
|
|
__tablename__ = "games"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
name: Mapped[str] = mapped_column(String(length=200), nullable=False)
|
|
description: Mapped[str] = mapped_column(Text, nullable=False, default="")
|
|
creator_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("users.id"), nullable=False
|
|
)
|
|
participation_code: Mapped[str] = mapped_column(
|
|
String(length=16), unique=True, nullable=False, index=True
|
|
)
|
|
status: Mapped[str] = mapped_column(String(length=20), nullable=False, default="active")
|
|
ended_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
|
|
# DM-only bookkeeping (name/current_hp/max_hp per monster) — never serialized to any
|
|
# API response, since HP/AC of NPCs is meant to stay secret from players.
|
|
monsters: Mapped[list] = mapped_column(JSONB, nullable=False, default=list)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
server_default=func.now(), nullable=False, index=True
|
|
)
|
|
|
|
|
|
class GameParticipant(Base):
|
|
__tablename__ = "game_participants"
|
|
__table_args__ = (UniqueConstraint("game_id", "user_id", name="uq_game_participant"),)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
|
)
|
|
game_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("games.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
user_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("users.id"), nullable=False
|
|
)
|
|
character_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("characters.id"), nullable=True
|
|
)
|
|
joined_at: Mapped[datetime] = mapped_column(server_default=func.now(), nullable=False)
|