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:
Thorsten
2026-09-01 17:44:46 +02:00
parent 2b99451fdf
commit 8da834c5c1
17 changed files with 196 additions and 8 deletions
+26 -5
View File
@@ -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,
}