Add a persistent event log for shop and prep actions.

This commit is contained in:
Greyson Parrelli
2026-07-23 08:00:59 -04:00
parent f7a00f6c48
commit 506c6e5b55
7 changed files with 330 additions and 9 deletions
+19 -1
View File
@@ -93,6 +93,9 @@ type Game struct {
Battle *BattleResult `json:"battle,omitempty"` // most recent battle
NextCardID int `json:"nextCardId"`
WinnerSeat int `json:"winnerSeat"` // set at gameover; -1 = tie
// Log is the running, human-readable event log shown across every phase.
Log []LogEntry `json:"log,omitempty"`
LogSeq int `json:"logSeq"` // last assigned entry sequence number
// RollDie overrides the rock die (faces 0,0,1,1,2,2) for tests. Nil
// (including after loading from storage) means a fair random roll.
@@ -225,6 +228,7 @@ func (g *Game) startShopRound() {
}
// The priority-token holder shops first.
g.Turn = g.PrioritySeat
g.logf(-1, "🛒", "Round %d — shop opens (%s goes first).", g.Round, g.Players[g.PrioritySeat].Name)
}
// drawFromTier pops the top card of the given tier's deck (1-based tier).
@@ -273,6 +277,7 @@ func (g *Game) Buy(playerID string, rowIdx int) error {
p.Coins--
bought := g.ShopRow[rowIdx]
p.Deck = append(p.Deck, bought)
g.logf(p.Seat, "🛒", "%s bought %s %s.", p.Name, article(bought.Name), bought.Name)
g.ShopRow[rowIdx] = g.drawFromTier(g.Round)
g.applyShopTrigger(p, bought, TriggerBuy)
g.advanceShopTurn()
@@ -310,11 +315,15 @@ func (g *Game) applyShopTrigger(p *Player, c Card, trigger EffectTrigger) {
}
switch e.Action {
case ActionGainApple:
for range e.count() {
n := e.count()
for range n {
p.Deck = append(p.Deck, g.newApple())
}
g.addLog(LogEntry{Seat: p.Seat, Icon: "🍎", Source: c.ID, Spawn: "apple",
Text: fmt.Sprintf("%s adds %d apple%s to %s's deck.", c.Name, n, plural(n), p.Name)})
case ActionRefreshGold:
p.Coins = min(p.Coins+e.count(), CoinsPerRound)
g.logf(p.Seat, "🪙", "%s refreshes %s's coins.", c.Name, p.Name)
case ActionDoubleApples:
apples := 0
for _, dc := range p.Deck {
@@ -325,6 +334,10 @@ func (g *Game) applyShopTrigger(p *Player, c Card, trigger EffectTrigger) {
for range apples {
p.Deck = append(p.Deck, g.newApple())
}
if apples > 0 {
g.addLog(LogEntry{Seat: p.Seat, Icon: "🍎", Source: c.ID, Spawn: "apple",
Text: fmt.Sprintf("%s doubles %s's apples (+%d).", c.Name, p.Name, apples)})
}
}
}
}
@@ -349,6 +362,8 @@ func (g *Game) sellCards(p *Player, cardIDs []string) error {
}
for _, c := range sold {
p.Deck = append(p.Deck, g.newApple())
g.addLog(LogEntry{Seat: p.Seat, Icon: "🍎", Source: c.ID, Spawn: "apple",
Text: fmt.Sprintf("%s sold %s — it becomes an apple.", p.Name, c.Name)})
g.applyShopTrigger(p, c, TriggerSell)
}
return nil
@@ -397,6 +412,7 @@ func (g *Game) TradeStart(playerID string, cardIDs []string) error {
}
p.Coins--
p.TripledThisRound = true
g.logf(p.Seat, "🔄", "%s traded in 3 %s pets for a tier %d pick.", p.Name, suit, nextTier)
g.Pending = &PendingTrade{
PlayerID: playerID,
Tier: nextTier,
@@ -424,6 +440,7 @@ func (g *Game) TradeChoose(playerID string, pick int) error {
tierIdx := g.Pending.Tier - 1
g.ShopDecks[tierIdx] = append(g.ShopDecks[tierIdx], other)
g.Pending = nil
g.logf(p.Seat, "🔄", "%s picked %s from the trade.", p.Name, chosen.Name)
// Pets obtained via the Triple action trigger their Buy effects.
g.applyShopTrigger(p, chosen, TriggerBuy)
g.advanceShopTurn()
@@ -456,6 +473,7 @@ func (g *Game) Pass(playerID string) error {
return err
}
p.Coins = 0
g.logf(p.Seat, "✋", "%s passed.", p.Name)
g.advanceShopTurn()
return nil
}
+55
View File
@@ -0,0 +1,55 @@
package game
import (
"fmt"
"strings"
)
// LogEntry is one human-readable line in the game's running event log. The
// log is public — both players see the same entries across every phase — and
// explains not just what happened but why (e.g. which pet granted an apple).
type LogEntry struct {
Seq int `json:"seq"` // stable, monotonically increasing id
Round int `json:"round"` // round the entry belongs to
Phase Phase `json:"phase"`
Seat int `json:"seat"` // acting seat, or -1 when none
Icon string `json:"icon,omitempty"` // leading emoji
Text string `json:"text"` // the sentence itself
Source string `json:"source,omitempty"` // card id that caused a spawn
Spawn string `json:"spawn,omitempty"` // "apple" | "bee" for spawn entries
}
// addLog appends an entry, stamping it with the next sequence number and the
// current round/phase. Callers set Seat/Icon/Text (and Source/Spawn when the
// entry represents something spawning off a card).
func (g *Game) addLog(e LogEntry) {
g.LogSeq++
e.Seq = g.LogSeq
e.Round = g.Round
e.Phase = g.Phase
g.Log = append(g.Log, e)
}
// logf is the common case: an entry with just a seat, icon, and message.
func (g *Game) logf(seat int, icon, format string, args ...any) {
g.addLog(LogEntry{Seat: seat, Icon: icon, Text: fmt.Sprintf(format, args...)})
}
// article returns "a" or "an" to suit the following word.
func article(word string) string {
if word == "" {
return "a"
}
if strings.ContainsRune("aeiouAEIOU", rune(word[0])) {
return "an"
}
return "a"
}
// plural returns "s" when n is not 1, for simple "N apple(s)" phrasing.
func plural(n int) string {
if n == 1 {
return ""
}
return "s"
}
+3
View File
@@ -35,6 +35,8 @@ type View struct {
Pending *PendingTrade `json:"pending,omitempty"`
Battle *BattleResult `json:"battle,omitempty"`
WinnerSeat int `json:"winnerSeat"`
// Log is the shared, public event log shown across every phase.
Log []LogEntry `json:"log,omitempty"`
// Debug is set by the server when its DEBUG flag is on, unlocking the
// client's "buy any card" panel. Not part of the pure game state.
Debug bool `json:"debug,omitempty"`
@@ -54,6 +56,7 @@ func (g *Game) ViewFor(playerID string) View {
PrioritySeat: g.PrioritySeat,
ShopRow: g.ShopRow,
WinnerSeat: g.WinnerSeat,
Log: g.Log,
}
for _, deck := range g.ShopDecks {
v.DeckCounts = append(v.DeckCounts, len(deck))