adventure: keep the facts, not just the sentence we made of them

gogobee already sends boss/opponent/zone/outcome/level on every dispatch.
renderAdventure melted them into prose and only the prose was persisted, so
"how many bosses has she downed" was answerable only by parsing English back
out of a headline.

adventure_events keeps the fact as fact. It's the one adventure table that's a
log rather than a snapshot: the roster answers where Josie is now and is
replaced every tick; this answers what she has ever done, which no snapshot
can. INSERT OR IGNORE on the guid because gogobee retries a fact whose ack it
lost, and this is the only adventure store where a duplicate is permanently
wrong — the roster forgives one by replacing itself, a double-counted kill is
in the tally forever.

On top of it the who page grows two public sections, counted from dispatches
that were already public: the record (tallies, per-boss and per-zone, realm
firsts, milestones) and the trail (the last 40 facts, each linking to the
dispatch that told it). One read feeds both — the pool is MaxOpenConns(1), so
six COUNT queries would serialize for an answer that fits in memory.

Only the trail is capped. A limit on the read would truncate a *tally* rather
than a list, and a veteran's kill count frozen at 40 reads as a fact instead of
a missing page. Only the subject of a fact earns a trophy: a duel you lost
still names you, and it belongs on your trail but not in your record.

Trophies count forward only. Every adventurer's past is prose in the feed and
can't be counted back out, so they show no record until they next do something
— a clean absence rather than a wall of zeroes.

No treasures: there's no loot fact on the wire, and inventory is current-state
with no history, so counting the vault would score a bought sword as a trophy.
Needs a fact type upstream.
This commit is contained in:
prosolis
2026-07-16 23:57:23 -07:00
parent b814f936a8
commit cbe9e67b3e
7 changed files with 709 additions and 0 deletions

View File

@@ -132,6 +132,30 @@ func (s *Server) handleAdventureIngest(w http.ResponseWriter, r *http.Request) {
http.Error(w, "insert failed", http.StatusInternalServerError)
return
}
// Keep the fact itself, not just the sentence we made out of it. The story row
// above is what people read; this is what the trophy case and timeline can
// count. Best-effort on purpose: a dispatch that published is published, and
// losing its structured twin costs a tally, not the news. Failing the request
// here would make gogobee retry a fact we already have a story for.
if err := storage.InsertAdventureEvent(&storage.AdvEvent{
GUID: f.GUID,
EventType: f.EventType,
Tier: f.Tier,
Subject: f.Subject,
Opponent: f.Opponent,
Boss: f.Boss,
Zone: f.Zone,
Region: f.Region,
Level: f.Level,
Tally: f.Count,
Outcome: f.Outcome,
Milestone: f.Milestone,
Actors: f.Actors,
OccurredAt: occurredAt,
}); err != nil {
slog.Error("adventure ingest: event record failed", "guid", f.GUID, "err", err)
}
slog.Info("adventure ingest: published", "guid", f.GUID, "event_type", f.EventType, "tier", f.Tier)
// NoPush (cold-start backfill) means "never goes to Matrix". Suppressing only

View File

@@ -79,6 +79,107 @@
</section>
{{end}}
{{if .HasHistory}}
<!-- The record. Public, like the dispatches it's counted from — this is the
same information the /adventure feed already printed, only as numbers
instead of as forty separate sentences. Not a gogobee snapshot: Pete
counted these itself out of the facts it kept. -->
<section class="mt-8 rounded-3xl bg-[color:var(--card)] border-2 border-[color:var(--ink)]/10 p-6 shadow-pete">
<h2 class="font-display text-xl font-bold mb-4">The record</h2>
<div class="grid grid-cols-2 sm:grid-cols-4 gap-2">
<div class="rounded-2xl bg-[color:var(--ink)]/5 px-2 py-3 text-center">
<div class="font-display text-2xl font-bold leading-none">{{.Trophies.BossKills}}</div>
<div class="text-[10px] uppercase tracking-wider text-[color:var(--ink)]/50 mt-1.5">bosses down</div>
</div>
<div class="rounded-2xl bg-[color:var(--ink)]/5 px-2 py-3 text-center">
<div class="font-display text-2xl font-bold leading-none">{{.Trophies.ZoneClears}}</div>
<div class="text-[10px] uppercase tracking-wider text-[color:var(--ink)]/50 mt-1.5">zones cleared</div>
</div>
<div class="rounded-2xl bg-[color:var(--ink)]/5 px-2 py-3 text-center">
<div class="font-display text-2xl font-bold leading-none">{{.Trophies.Deaths}}</div>
<div class="text-[10px] uppercase tracking-wider text-[color:var(--ink)]/50 mt-1.5">death{{if ne .Trophies.Deaths 1}}s{{end}}</div>
</div>
<div class="rounded-2xl bg-[color:var(--ink)]/5 px-2 py-3 text-center">
<div class="font-display text-2xl font-bold leading-none">{{.Trophies.Retreats}}</div>
<div class="text-[10px] uppercase tracking-wider text-[color:var(--ink)]/50 mt-1.5">walked out</div>
</div>
</div>
{{if or .Trophies.BossFirsts .Trophies.ZoneFirsts}}
<p class="mt-3 text-sm text-theme-adventure font-semibold">
★ {{.Trophies.BossFirsts}} realm-first boss{{if ne .Trophies.BossFirsts 1}}es{{end}}{{if .Trophies.ZoneFirsts}}, {{.Trophies.ZoneFirsts}} first clear{{if ne .Trophies.ZoneFirsts 1}}s{{end}}{{end}} — nobody had done it before.
</p>
{{end}}
<div class="mt-6 grid gap-6 sm:grid-cols-2">
{{if .Trophies.Bosses}}
<div>
<h3 class="font-display text-lg font-bold mb-3">Bosses fought</h3>
<ul class="space-y-1.5 text-sm max-h-56 overflow-y-auto pr-1">
{{range .Trophies.Bosses}}
<li class="flex items-baseline justify-between gap-3">
<span class="flex-1">{{.Boss}}{{if .First}} <span class="text-theme-adventure" title="first in the realm to clear it"></span>{{end}}</span>
<span class="text-xs text-[color:var(--ink)]/50 shrink-0">×{{.Kills}}</span>
</li>
{{end}}
</ul>
</div>
{{end}}
{{if .Trophies.Zones}}
<div>
<h3 class="font-display text-lg font-bold mb-3">Ground covered</h3>
<ul class="space-y-1.5 text-sm max-h-56 overflow-y-auto pr-1">
{{range .Trophies.Zones}}
<li class="flex items-baseline justify-between gap-3">
<span class="flex-1">{{.Zone}}{{if .First}} <span class="text-theme-adventure" title="first clear in the realm"></span>{{end}}{{if .Region}} <span class="text-[color:var(--ink)]/45">{{.Region}}</span>{{end}}</span>
<span class="text-xs text-[color:var(--ink)]/50 shrink-0">×{{.Clears}}</span>
</li>
{{end}}
</ul>
</div>
{{end}}
</div>
{{if .Trophies.Milestones}}
<div class="mt-6 pt-4 border-t border-[color:var(--ink)]/10">
<h3 class="font-display text-lg font-bold mb-3">Milestones</h3>
<div class="flex flex-wrap gap-2">
{{range .Trophies.Milestones}}
<span class="rounded-full bg-theme-adventure/10 text-theme-adventure text-xs font-semibold px-3 py-1">🏅 {{.}}</span>
{{end}}
</div>
</div>
{{end}}
</section>
<!-- The trail: every fact we have, newest first, each linking to the dispatch
that told it. -->
<section class="mt-8 rounded-3xl bg-[color:var(--card)] border-2 border-[color:var(--ink)]/10 p-6 shadow-pete">
<h2 class="font-display text-xl font-bold mb-4">The trail</h2>
<ol class="relative border-l-2 border-[color:var(--ink)]/10 ml-3 space-y-4">
{{range .Timeline}}
<li class="relative pl-6">
<span class="absolute -left-[13px] top-0.5 flex h-6 w-6 items-center justify-center rounded-full bg-[color:var(--card)] border-2 {{if .Notable}}border-theme-adventure{{else}}border-[color:var(--ink)]/10{{end}} text-xs" aria-hidden="true">{{.Emoji}}</span>
<a href="{{.Permalink}}" class="group block">
<div class="flex items-baseline justify-between gap-3">
<span class="font-semibold group-hover:text-theme-adventure transition {{if .Notable}}text-theme-adventure{{end}}">{{.Label}}</span>
<span class="text-xs text-[color:var(--ink)]/45 shrink-0">{{.When}}</span>
</div>
{{if .Line}}<p class="text-sm text-[color:var(--ink)]/60 mt-0.5">{{.Line}}</p>{{end}}
</a>
</li>
{{end}}
</ol>
{{if .MoreHistory}}
<p class="mt-5 pt-4 border-t border-[color:var(--ink)]/10 text-xs text-[color:var(--ink)]/45">
Showing the most recent {{len .Timeline}}. The counts above cover everything.
</p>
{{end}}
</section>
{{end}}
{{if .HasSelf}}
<!-- Owner-only: this is you. Inventory, vault, house, pets — private, served
only because your signed-in localpart owns this page's token. -->

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"log/slog"
"net/http"
"time"
"pete/internal/storage"
)
@@ -59,8 +60,35 @@ type whoPage struct {
Abilities []abilityRow
HasSelf bool
Self storage.PlayerDetail
// History. Unlike everything above, these are not a gogobee snapshot — they
// are counted from the facts Pete has been keeping since adventure_events
// landed. HasHistory is false for an adventurer who hasn't done anything since
// then, which includes every veteran on the day this shipped: their past is in
// the story feed as prose and cannot be counted back out.
Trophies storage.TrophyCase
HasHistory bool
Timeline []timelineEntry
MoreHistory bool // the trail was capped; there is older history than this
}
// timelineEntry is one line of an adventurer's trail — a fact, rendered short,
// pointing at the dispatch that told it.
type timelineEntry struct {
Emoji string
Label string
Line string // "The Rotmother, in holymachina"
When string
Permalink string
Notable bool // a realm-first or a death: the lines worth an eye
}
// timelineCap bounds the trail. An adventurer accrues facts for as long as they
// play, and the page renders the whole list server-side — this is the point where
// "their whole history" stops being a page and starts being a scroll nobody
// reads. The count in the trophy case stays honest either way; only the trail is
// clipped.
const timelineCap = 40
// handleAdventureWho serves one adventurer's detail page. Public; 404s when the
// token isn't on the current board — the same liveness gate the storefront uses,
// so a stale or guessed token never resolves to a page.
@@ -96,6 +124,24 @@ func (s *Server) handleAdventureWho(w http.ResponseWriter, r *http.Request) {
page.Abilities = abil
}
// History: one read feeds both the trophy case and the trail. Keyed on the
// character *name* rather than the page token, because that is what a fact
// carries — see the adventure_events schema note on why there is no id to use.
// Best-effort: a page that can't count trophies is still a page.
// Unlimited on purpose: the trophy case counts these, and a limit would cap the
// tally rather than the list. The trail is clipped below, after the counting.
if events, err := storage.EventsBySubject(entry.Name, 0); err != nil {
slog.Error("who: history lookup failed", "token", token, "err", err)
} else if len(events) > 0 {
page.HasHistory = true
page.Trophies = storage.BuildTrophyCase(entry.Name, events)
page.MoreHistory = len(events) > timelineCap
if page.MoreHistory {
events = events[:timelineCap]
}
page.Timeline = buildTimeline(s, events)
}
// Owner enrichment: only when the signed-in user's localpart owns this exact
// page token. The ownership is proven by a row gogobee pushed, never by
// reversing the token, so no visitor can unlock another player's self extras.
@@ -144,6 +190,62 @@ func (s *Server) handleAdventureWhoAPI(w http.ResponseWriter, r *http.Request) {
})
}
// buildTimeline renders facts into trail lines.
//
// The line is built from the fact, not from the dispatch's headline, on purpose:
// the headline is a *news* sentence written for the moment it landed ("First
// ever: Josie brings down the Rotmother"), and forty of those stacked in a column
// read as a wall of shouting. The trail wants the noun, not the announcement. It
// also saves a join back to stories for every row.
func buildTimeline(s *Server, events []storage.AdvEvent) []timelineEntry {
out := make([]timelineEntry, 0, len(events))
for _, e := range events {
label, emoji := advEventMeta(e.EventType)
out = append(out, timelineEntry{
Emoji: emoji,
Label: label,
Line: timelineLine(e),
When: time.Unix(e.OccurredAt, 0).UTC().Format("Jan 2, 2006"),
Permalink: s.advPermalink(e.GUID),
Notable: e.EventType == "boss_first" || e.EventType == "zone_first" || e.EventType == "death",
})
}
return out
}
// timelineLine is the short "what" of a fact: the monster, the zone, the rival.
// Empty is fine — the label and emoji already carry the event, and inventing
// filler for a fact that carries no nouns would just be noise.
func timelineLine(e storage.AdvEvent) string {
inZone := func(s string) string {
if e.Zone == "" {
return s
}
if s == "" {
return e.Zone
}
return s + ", in " + e.Zone
}
switch e.EventType {
case "boss_first", "boss_kill", "siege_start", "siege_win", "siege_loss",
"mischief_survived", "mischief_downed":
return inZone(e.Boss)
case "zone_first", "zone_clear", "retreat", "departure", "death":
if e.Region != "" && e.Zone != "" {
return e.Zone + " — " + e.Region
}
return e.Zone
case "rival_result", "pete_duel_win", "pete_duel_loss":
if e.Opponent != "" {
return "vs " + e.Opponent
}
return ""
case "milestone":
return e.Milestone
}
return inZone("")
}
// decodeWhoDetail unpacks the public detail blob and builds the ability rows.
// Returns ok=false when there is no detail (a snapshot from before the detail
// push), so the page can fall back to the summary the board already has.

View File

@@ -81,6 +81,33 @@ func seedWho(t *testing.T, owner string) *Server {
return s
}
// seedHistory pushes facts about Josie through the real ingest path, so the
// history panels are driven by rows that arrived the way gogobee sends them
// rather than by a hand-built struct that could drift from the contract.
func seedHistory(t *testing.T, s *Server) {
t.Helper()
facts := []AdvFact{
{GUID: "boss_first:a:1", EventType: "boss_first", Tier: "priority", Actors: []string{"Josie"},
Subject: "Josie", Boss: "The Rotmother", Zone: "holymachina", Region: "the Reach", OccurredAt: 100},
{GUID: "boss_kill:b:2", EventType: "boss_kill", Tier: "bulletin", Actors: []string{"Josie"},
Subject: "Josie", Boss: "The Rotmother", Zone: "holymachina", OccurredAt: 200},
{GUID: "death:c:3", EventType: "death", Tier: "priority", Actors: []string{"Josie"},
Subject: "Josie", Zone: "holymachina", Level: 14, OccurredAt: 300},
{GUID: "milestone:d:4", EventType: "milestone", Tier: "bulletin", Actors: []string{"Josie"},
Subject: "Josie", Milestone: "level 14", OccurredAt: 400},
}
for _, f := range facts {
body, _ := json.Marshal(f)
req := httptest.NewRequest("POST", "/api/ingest/adventure", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer tok")
w := httptest.NewRecorder()
s.handleAdventureIngest(w, req)
if w.Code != 200 {
t.Fatalf("seed fact %s = %d: %s", f.GUID, w.Code, w.Body.String())
}
}
}
func getWho(t *testing.T, s *Server, token, asUser string) *httptest.ResponseRecorder {
t.Helper()
var req = httptest.NewRequest("GET", "/adventure/who/"+token, nil)
@@ -197,3 +224,45 @@ func TestDetailIngestReplacesAndAuth(t *testing.T) {
t.Error("public page vanished when only the private detail was dropped")
}
}
// TestWhoHistoryPanels: the record and the trail render for an adventurer with
// facts on file. Driven through the real template and the real ingest path, so a
// field slip in either 500s here rather than in prod.
func TestWhoHistoryPanels(t *testing.T) {
s := seedWho(t, "josie")
seedHistory(t, s)
w := getWho(t, s, "tok-josie", "")
if w.Code != 200 {
t.Fatalf("who = %d: %s", w.Code, w.Body.String())
}
body := w.Body.String()
for _, want := range []string{
"The record",
"The Rotmother", // the boss tally
"bosses down",
"realm-first", // the boss_first got flagged
"level 14", // the milestone chip
"The trail",
"/adventure/boss_first:a:1", // the trail links back to the dispatch
} {
if !strings.Contains(body, want) {
t.Errorf("history render missing %q", want)
}
}
}
// TestWhoHistoryAbsent: an adventurer with no facts on file gets no empty
// scaffolding. Every veteran looks like this the day this ships — their past is
// prose in the feed and was never counted — so the blank state has to be a clean
// absence, not a wall of zeroes.
func TestWhoHistoryAbsent(t *testing.T) {
s := seedWho(t, "josie")
body := getWho(t, s, "tok-josie", "").Body.String()
for _, leak := range []string{"The record", "The trail", "bosses down"} {
if strings.Contains(body, leak) {
t.Errorf("no-history page should not render %q", leak)
}
}
}