Add run_encounters tables + backfill (roadmap #3 phase 1)#266
Open
ptrlrd wants to merge 1 commit into
Open
Conversation
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.
f6e4a23 to
f77dd05
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.