Skip to content

Add run_encounters tables + backfill (roadmap #3 phase 1)#266

Open
ptrlrd wants to merge 1 commit into
mainfrom
feat/run-encounters-schema
Open

Add run_encounters tables + backfill (roadmap #3 phase 1)#266
ptrlrd wants to merge 1 commit into
mainfrom
feat/run-encounters-schema

Conversation

@ptrlrd
Copy link
Copy Markdown
Owner

@ptrlrd ptrlrd commented May 14, 2026

Re-created after the 2026-05-14 12:42 UTC repo rollback closed the original #258.

Roadmap #3 phase 1 of 2 (schema + backfill, UI in follow-up). Adds run_encounters (id, run_id, encounter_id, act_id, room_type, floor, damage_taken, turns_taken, won_fight) and run_encounter_monsters (encounter_row_id, monster_id) tables to init_db. Submit-time extract_run_encounters walks map_point_history. Backfill script tools/backfill_run_encounters.py replays archived JSONs through the same helper, idempotent. Verified locally: 44 runs → 523 encounter rows + 696 monster joins. Unlocks the per-monster Stats tab in the follow-up PR.

Roadmap #3 phase 1 of 2 (schema + backfill, then UI in a follow-up).
Unlocks monster-level community stats (encounter rate, player win
rate vs monster, avg HP lost when fought, run-killer rank) which the
current schema couldn't answer — only `runs.killed_by` exists today
and it captures the boss that ended a losing run, nothing else.

### Schema (init_db, both auto-created on first run)

run_encounters (
  id, run_id, encounter_id, act_id, room_type, floor,
  damage_taken, turns_taken, won_fight
)

run_encounter_monsters (
  encounter_row_id, monster_id           -- composite PK collapses dups
)

Normalized split (instead of stuffing monster_ids as JSON in
run_encounters) so "win rate vs MONSTER" stays a regular indexed JOIN
without json_each(). Indexes on encounter_id, run_id, monster_id.

### Submit-time ingest

extract_run_encounters(data, player_id, is_win, is_abandoned) walks
map_point_history and yields one row per combat room scoped to the
target player. Won-fight heuristic: every combat is a win except the
last combat room of a non-win, non-abandoned run whose encounter_id
matches killed_by_encounter. Abandoned-early runs leave the final
encounter as won=1 (quit, not died).

_submit_player_run() calls the extractor and inserts rows after the
existing cards/relics/potions writes. Wrapped in try/except so a
parse failure doesn't roll back the primary run row — the encounter
table is an analytics surface, not a record of truth, and the
backfill picks up anything that misses live.

### Backfill — tools/backfill_run_encounters.py

Walks data/runs/*.json, looks up the row in `runs` by hash, replays
extract_run_encounters() via the same shared helper. Idempotent
(skips runs already represented in run_encounters). Reports counts.

Verified locally against 44 archived runs:
  - 523 encounter rows inserted (~12 / run, matches typical run length)
  - 696 monster join rows
  - Top encounters: NIBBITS_WEAK, SHRINKER_BEETLE_WEAK, CORPSE_SLUGS_WEAK
  - Second run with no new data: skipped 43, no-op on the 44th

Run on prod after this lands:
  docker exec spire-codex-backend \
    python3 /app/tools/backfill_run_encounters.py

### Follow-up

Next PR adds GET /api/runs/monster-stats/{monster_id} + a Stats tab on
/monsters/[id] that consumes it. No schema change required there —
this PR carries the load-bearing piece.
@ptrlrd ptrlrd force-pushed the feat/run-encounters-schema branch from f6e4a23 to f77dd05 Compare May 18, 2026 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant