MosswartOverlord/go-services/tracker-go/charstats.go
Erik a5d69ba88d feat(go-services): Phase 2 ingest — shared Ingestor + shadow consumer
Implements the plugin event handlers (the /ws/position write logic) as a shared
Ingestor, validated against real traffic by replaying Python's /ws/live firehose
into an isolated dereth_go DB (no production write, no plugin stolen).

- ingest.go: faithful ports of telemetry (kill-delta -> char_stats, server
  received_at stamp), rare (rare_stats/rare_stats_sessions/rare_events), portal
  (coord upsert), character_stats (stats_data JSONB subset + upsert), spawn, and
  the memory-only handlers (vitals/quest/equipment_cantrip/nearby/dungeon). In
  -memory live state + read-side overlay accessors.
- shadow.go: coder/websocket consumer of /ws/live -> Ingestor.dispatch (telemetry
  matched by shape since its broadcast has no type field).
- main.go/store.go: ingest mode (READ_ONLY=false + SHADOW_INGEST_WS) wires the
  ingestor; read handlers (/character-stats, /equipment-cantrip, /quest-status)
  now consult the live overlay first, like Python.
- compose: shadow instance ingests ws://dereth-tracker:8765/ws/live.

Validated live: dereth_go has 73 distinct telemetry chars; shadow /live online
set == production (73=73); character_stats 5/5 exact byte-match (0 mismatch);
char_stats kill-deltas + portals accumulating. compare/compare_ingest.py.

Deferred to next pass: combat_stats (delta/merge), share_*, the /ws/position +
/ws/live servers (for cutover).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 10:31:15 +02:00

94 lines
2.8 KiB
Go

package main
import (
"net/http"
"sort"
)
// GET /character-stats/{name} — latest full stats. Phase 1 reads the DB
// (character_stats is authoritative); the live_character_stats overlay is an
// ingest-only freshness layer we don't have yet. (main.py:4137)
func (s *Server) handleCharacterStats(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("name")
// Live overlay first (ingest mode), like Python's live_character_stats check.
if s.ingestor != nil {
if v, ok := s.ingestor.getCharacterStats(name); ok {
writeJSON(w, http.StatusOK, v)
return
}
}
ctx, cancel := reqCtx(r)
defer cancel()
row, err := queryRowAsMap(ctx, s.pool, `SELECT * FROM character_stats WHERE character_name = $1`, name)
if err != nil {
s.dbErr(w, "character-stats", err)
return
}
if row == nil {
writeJSON(w, http.StatusNotFound, map[string]any{"error": "No stats available for this character"})
return
}
// Merge stats_data JSONB up to the top level, matching the frontend contract.
sd := asJSONMap(row["stats_data"])
delete(row, "stats_data")
formatTimes([]map[string]any{row}, "timestamp")
for k, v := range sd {
row[k] = v
}
writeJSON(w, http.StatusOK, row)
}
// GET /combat-stats/{character_name} — lifetime combat blob. Phase 1: DB only,
// so session is always null. (main.py:1819)
func (s *Server) handleCombatStatsOne(w http.ResponseWriter, r *http.Request) {
cn := r.PathValue("character_name")
ctx, cancel := reqCtx(r)
defer cancel()
row, err := queryRowAsMap(ctx, s.pool, `SELECT stats_data FROM combat_stats WHERE character_name = $1`, cn)
if err != nil {
s.dbErr(w, "combat-stats/one", err)
return
}
if row == nil {
writeJSON(w, http.StatusOK, map[string]any{"character_name": cn, "session": nil, "lifetime": nil})
return
}
writeJSON(w, http.StatusOK, map[string]any{
"character_name": cn,
"session": nil,
"lifetime": decodeJSONValue(row["stats_data"]),
})
}
// GET /combat-stats — all characters' lifetime combat blobs. Phase 1: DB only. (main.py:1850)
func (s *Server) handleCombatStatsAll(w http.ResponseWriter, r *http.Request) {
ctx, cancel := reqCtx(r)
defer cancel()
rows, err := queryRowsAsMaps(ctx, s.pool, `SELECT character_name, stats_data FROM combat_stats`)
if err != nil {
s.dbErr(w, "combat-stats/all", err)
return
}
results := make([]map[string]any, 0, len(rows))
for _, row := range rows {
results = append(results, map[string]any{
"character_name": row["character_name"],
"session": nil,
"lifetime": decodeJSONValue(row["stats_data"]),
})
}
sort.Slice(results, func(i, j int) bool {
return toStr(results[i]["character_name"]) < toStr(results[j]["character_name"])
})
writeJSON(w, http.StatusOK, map[string]any{"stats": results})
}
func toStr(v any) string {
if s, ok := v.(string); ok {
return s
}
return ""
}