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>