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
+91
View File
@@ -0,0 +1,91 @@
import { useEffect, useRef, useState } from 'react'
import type { LogEntry } from '../types'
// A single battle line, derived from the battle event stream and fed in by
// BattlePhase so the log stays in step with the replay controls.
export interface BattleLogLine {
key: string
icon?: string
text: string
seat: number // -1 = neutral/system
}
interface Props {
entries: LogEntry[]
youSeat: number
// During a battle, the lines revealed up to the current replay step. The
// last one is the "current" step and gets highlighted.
battleLines?: BattleLogLine[]
}
function seatClass(seat: number, youSeat: number): string {
if (seat < 0) return 'log-system'
return seat === youSeat ? 'log-you' : 'log-opp'
}
// EventLog is the persistent, human-readable narration of the game, shown in
// every phase. Shop/prep entries come from the server; battle entries are fed
// in step-by-step so stepping back through the replay walks back up the log.
export function EventLog({ entries, youSeat, battleLines }: Props) {
const [open, setOpen] = useState(true)
const bodyRef = useRef<HTMLDivElement>(null)
const lastSeq = entries.length ? entries[entries.length - 1].seq : 0
const battleLen = battleLines?.length ?? 0
// Keep the newest line in view as the log grows or the replay advances.
useEffect(() => {
const el = bodyRef.current
if (el) el.scrollTop = el.scrollHeight
}, [lastSeq, battleLen, open])
return (
<aside className={`event-log ${open ? '' : 'is-collapsed'}`}>
<div className="event-log-head">
<span>📜 Event Log</span>
<button
className="btn btn-ghost btn-sm"
onClick={() => setOpen((o) => !o)}
title={open ? 'Collapse log' : 'Expand log'}
aria-label={open ? 'Collapse log' : 'Expand log'}
>
{open ? '▸' : '◂'}
</button>
</div>
{open && (
<div className="event-log-body" ref={bodyRef}>
{entries.length === 0 && battleLen === 0 && (
<div className="log-empty muted">Nothing has happened yet.</div>
)}
{entries.map((e) => (
<div key={`e${e.seq}`} className={`log-line ${seatClass(e.seat, youSeat)}`}>
{e.icon && <span className="log-icon">{e.icon}</span>}
<span className="log-text">{e.text}</span>
</div>
))}
{battleLen > 0 && (
<>
<div className="log-line log-system log-divider">
<span className="log-text"> Battle</span>
</div>
{battleLines!.map((l, i) => (
<div
key={l.key}
className={[
'log-line',
seatClass(l.seat, youSeat),
i === battleLen - 1 ? 'log-current' : '',
]
.filter(Boolean)
.join(' ')}
>
{l.icon && <span className="log-icon">{l.icon}</span>}
<span className="log-text">{l.text}</span>
</div>
))}
</>
)}
</div>
)}
</aside>
)
}
+14 -8
View File
@@ -6,6 +6,7 @@ import { ArrangePhase } from './ArrangePhase'
import { BattlePhase } from './BattlePhase'
import { GameOver } from './GameOver'
import { DebugPanel } from './DebugPanel'
import { EventLog } from './EventLog'
// Table connects to the game and routes to the right phase screen.
export function Table({ session, onLeave }: { session: Session; onLeave: () => void }) {
@@ -56,15 +57,20 @@ export function Table({ session, onLeave }: { session: Session; onLeave: () => v
</button>
</header>
<main className="table-main">
{view.phase === 'lobby' && <Lobby view={view} />}
{(view.phase === 'shop' || view.phase === 'cleanup') && (
<ShopPhase view={view} you={you} send={send} />
<div className="table-body">
<main className="table-main">
{view.phase === 'lobby' && <Lobby view={view} />}
{(view.phase === 'shop' || view.phase === 'cleanup') && (
<ShopPhase view={view} you={you} send={send} />
)}
{view.phase === 'arrange' && <ArrangePhase view={view} you={you} send={send} />}
{view.phase === 'battle' && <BattlePhase view={view} send={send} />}
{view.phase === 'gameover' && <GameOver view={view} onLeave={onLeave} />}
</main>
{view.phase !== 'lobby' && (
<EventLog entries={view.log ?? []} youSeat={view.youSeat} />
)}
{view.phase === 'arrange' && <ArrangePhase view={view} you={you} send={send} />}
{view.phase === 'battle' && <BattlePhase view={view} send={send} />}
{view.phase === 'gameover' && <GameOver view={view} onLeave={onLeave} />}
</main>
</div>
{opponents.some((p) => !p.connected) && view.phase !== 'lobby' && (
<div className="banner banner-warn">An opponent is disconnected</div>
+136
View File
@@ -330,6 +330,13 @@ h3 {
padding: 4px 10px;
}
.table-body {
flex: 1;
display: flex;
align-items: flex-start;
min-height: 0;
}
.table-main {
flex: 1;
padding: 20px 16px 40px;
@@ -338,6 +345,135 @@ h3 {
margin: 0 auto;
}
/* ---------- event log ---------- */
.event-log {
width: 300px;
flex-shrink: 0;
align-self: stretch;
position: sticky;
top: 0;
max-height: calc(100vh - 58px);
display: flex;
flex-direction: column;
background: rgba(0, 0, 0, 0.28);
border-left: 3px solid rgba(0, 0, 0, 0.35);
}
.event-log.is-collapsed {
width: 42px;
}
.event-log-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 6px;
padding: 8px 10px;
font-family: var(--font-display);
font-size: 0.9rem;
color: var(--gold);
background: rgba(0, 0, 0, 0.25);
border-bottom: 2px solid rgba(0, 0, 0, 0.3);
white-space: nowrap;
}
.event-log.is-collapsed .event-log-head {
writing-mode: vertical-rl;
justify-content: flex-start;
gap: 10px;
padding: 10px 8px;
height: 100%;
}
.event-log.is-collapsed .event-log-head button {
writing-mode: horizontal-tb;
}
.event-log-body {
flex: 1;
overflow-y: auto;
padding: 8px 10px 12px;
display: flex;
flex-direction: column;
gap: 5px;
font-size: 0.82rem;
line-height: 1.25;
}
.log-empty {
padding: 8px 2px;
font-style: italic;
}
.log-line {
display: flex;
gap: 6px;
align-items: baseline;
padding: 3px 6px;
border-radius: 6px;
border-left: 3px solid transparent;
background: rgba(255, 255, 255, 0.03);
}
.log-icon {
flex-shrink: 0;
}
.log-text {
min-width: 0;
word-break: break-word;
}
.log-line.log-you {
border-left-color: var(--gold);
}
.log-line.log-opp {
border-left-color: var(--red);
}
.log-line.log-system {
background: none;
color: var(--cream-dark);
justify-content: center;
font-family: var(--font-display);
font-size: 0.8rem;
opacity: 0.85;
margin-top: 4px;
}
.log-line.log-divider {
border-top: 1px dashed rgba(253, 243, 220, 0.25);
padding-top: 8px;
color: var(--gold);
}
.log-line.log-current {
background: rgba(255, 207, 92, 0.18);
outline: 1px solid rgba(255, 207, 92, 0.4);
}
@media (max-width: 860px) {
.table-body {
flex-direction: column;
}
.event-log {
width: 100%;
position: static;
max-height: 220px;
border-left: none;
border-top: 3px solid rgba(0, 0, 0, 0.35);
}
.event-log.is-collapsed {
width: 100%;
}
.event-log.is-collapsed .event-log-head {
writing-mode: horizontal-tb;
height: auto;
}
}
/* ---------- lobby ---------- */
.lobby {
+12
View File
@@ -75,6 +75,17 @@ export interface BattleResult {
trophies: number
}
export interface LogEntry {
seq: number
round: number
phase: Phase
seat: number // acting seat, or -1
icon?: string
text: string
source?: string // card id that caused a spawn
spawn?: string // 'apple' | 'bee'
}
export interface GameView {
gameId: string
code: string
@@ -90,6 +101,7 @@ export interface GameView {
pending?: PendingTrade
battle?: BattleResult
winnerSeat: number
log?: LogEntry[]
debug?: boolean // server DEBUG mode: unlocks the buy-any-card panel
}