Add game end status with an end_game DM tool and automatic 0-HP detection
Games now have a status/ended_reason pair. The DM can call the new end_game tool when the story reaches a real conclusion (victory, defeat, or a resolved one-shot), and the backend independently ends the game whenever a character's HP drops to 0 or below, regardless of whether the DM narrates it. The frontend shows a banner and a "Beendet" badge once a game ends. HP moves from the freeform combat_stats bag into dedicated current_hp/max_hp columns on Character, since reliably detecting 0 HP requires a real integer rather than parsing strings like "3/10" out of an LLM-authored key/value dict. Also fixed combat_stats to fully replace on each upsert instead of merging, matching its documented contract — the merge was leaving stale keys (old HP/TP text) behind after the model stopped sending them. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -32,12 +32,24 @@ TOOL_SCHEMA = {
|
||||
"type": "object",
|
||||
"description": "Ability scores, e.g. {\"STR\": 15, \"DEX\": 12, \"CON\": 14, \"INT\": 10, \"WIS\": 8, \"CHA\": 13}",
|
||||
},
|
||||
"current_hp": {
|
||||
"type": "integer",
|
||||
"description": (
|
||||
"Current hit points. Always send this together with max_hp whenever HP changes "
|
||||
"(damage, healing, character creation) — the server uses it to detect when a "
|
||||
"character goes down. Do NOT also put HP/TP in combat_stats, this is the only place for it."
|
||||
),
|
||||
},
|
||||
"max_hp": {
|
||||
"type": "integer",
|
||||
"description": "Maximum hit points. Send together with current_hp.",
|
||||
},
|
||||
"combat_stats": {
|
||||
"type": "object",
|
||||
"description": (
|
||||
"Key/value readouts, e.g. {\"HP\": \"13/13\", \"RK\": \"19\", \"Initiative\": \"+1\", "
|
||||
"\"Tempo\": \"7,5 m\", \"Übungsbonus\": \"+2\"}. Send the full current set each time — "
|
||||
"this replaces whatever was stored before."
|
||||
"Key/value readouts EXCLUDING hit points (use current_hp/max_hp for those), e.g. "
|
||||
"{\"RK\": \"19\", \"Initiative\": \"+1\", \"Tempo\": \"7,5 m\", \"Übungsbonus\": \"+2\"}. "
|
||||
"Send the full current set each time — this replaces whatever was stored before."
|
||||
),
|
||||
},
|
||||
"abilities": {
|
||||
@@ -108,7 +120,11 @@ async def upsert(session: AsyncSession, game_id: uuid.UUID, tool_input: dict) ->
|
||||
if "stats" in tool_input and tool_input["stats"] is not None:
|
||||
character.stats = {**(character.stats or {}), **tool_input["stats"]}
|
||||
if "combat_stats" in tool_input and tool_input["combat_stats"] is not None:
|
||||
character.combat_stats = {**(character.combat_stats or {}), **tool_input["combat_stats"]}
|
||||
character.combat_stats = tool_input["combat_stats"]
|
||||
if "current_hp" in tool_input and tool_input["current_hp"] is not None:
|
||||
character.current_hp = tool_input["current_hp"]
|
||||
if "max_hp" in tool_input and tool_input["max_hp"] is not None:
|
||||
character.max_hp = tool_input["max_hp"]
|
||||
if "abilities" in tool_input and tool_input["abilities"] is not None:
|
||||
character.abilities = tool_input["abilities"]
|
||||
if "equipment" in tool_input and tool_input["equipment"] is not None:
|
||||
@@ -122,4 +138,9 @@ async def upsert(session: AsyncSession, game_id: uuid.UUID, tool_input: dict) ->
|
||||
await session.commit()
|
||||
await session.refresh(character)
|
||||
|
||||
return {"character_id": str(character.id), "name": character.name}
|
||||
return {
|
||||
"character_id": str(character.id),
|
||||
"name": character.name,
|
||||
"current_hp": character.current_hp,
|
||||
"max_hp": character.max_hp,
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import uuid
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.models.game import Game
|
||||
|
||||
TOOL_SCHEMA = {
|
||||
"name": "end_game",
|
||||
"description": (
|
||||
"Call when the story reaches a real conclusion — the party wins, loses, or a one-shot's "
|
||||
"plot is resolved. Marks the game as ended so players see a clear ending. Do not call this "
|
||||
"just because a scene ends; only for a genuine story conclusion."
|
||||
),
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"reason": {
|
||||
"type": "string",
|
||||
"description": (
|
||||
"A short narrative summary of how/why the story ended (victory, defeat, or "
|
||||
"resolution), shown to the players, e.g. 'Das Dorf ist gerettet, die Kerze brennt wieder.'"
|
||||
),
|
||||
},
|
||||
},
|
||||
"required": ["reason"],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
async def end(session: AsyncSession, game_id: uuid.UUID, tool_input: dict) -> dict:
|
||||
game = await session.get(Game, game_id)
|
||||
if game is None:
|
||||
raise ValueError(f"Game {game_id} not found")
|
||||
|
||||
if game.status != "ended":
|
||||
game.status = "ended"
|
||||
game.ended_reason = tool_input["reason"]
|
||||
await session.commit()
|
||||
|
||||
return {"status": "ended", "reason": game.ended_reason}
|
||||
Reference in New Issue
Block a user