// Package arcade is the per-session terminal interface for Thermal Snake. package arcade import ( "crypto/rand" "log/slog" random "math/rand/v2" "time" tea "charm.land/bubbletea/v2" "thermal-snake/internal/game" "thermal-snake/internal/scores" ) const ( minWidth = 50 minHeight = 23 wideWidth = 76 pageSize = 10 ) type screen uint8 const ( lobby screen = iota warmup playing paused debrief records ) type pulse struct{ generation uint64 } type Model struct { store *scores.Store fingerprint string pilot scores.Pilot width, height int screen screen generation uint64 countdown int phase int run *game.Run result scores.Run saveFailed bool personalBest bool returnScreen screen historyTab bool page int standings []scores.Standing history []scores.Run historyTotal int readFailed bool } func New(store *scores.Store, fingerprint string, pilot scores.Pilot, width, height int) *Model { return &Model{ store: store, fingerprint: fingerprint, pilot: pilot, width: width, height: height, } } func (m *Model) Init() tea.Cmd { return m.nextPulse(360 * time.Millisecond) } func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: m.width, m.height = msg.Width, msg.Height if m.tooSmall() && (m.screen == playing || m.screen == warmup) { m.screen = paused m.generation++ } case pulse: if msg.generation == m.generation { return m, m.onPulse() } case tea.KeyPressMsg: return m, m.onKey(msg.String()) } return m, nil } func (m *Model) tooSmall() bool { return m.width < minWidth || m.height < minHeight } func (m *Model) nextPulse(delay time.Duration) tea.Cmd { generation := m.generation return tea.Tick(delay, func(time.Time) tea.Msg { return pulse{generation} }) } func (m *Model) start() tea.Cmd { if m.tooSmall() { return nil } m.run = game.New(random.Uint64()) m.result = scores.Run{ID: rand.Text()} m.countdown, m.phase = 3, 0 m.personalBest, m.saveFailed = false, false m.screen = warmup m.generation++ return m.nextPulse(450 * time.Millisecond) } func (m *Model) onPulse() tea.Cmd { switch m.screen { case lobby: m.phase = (m.phase + 1) % (2 * game.Height) return m.nextPulse(360 * time.Millisecond) case warmup: m.countdown-- if m.countdown > 0 { return m.nextPulse(450 * time.Millisecond) } m.screen = playing return m.nextPulse(m.run.Interval()) case playing: m.phase = (m.phase + 1) % (2 * game.Height) m.run.Step() if m.run.Outcome() == "" { return m.nextPulse(m.run.Interval()) } m.screen = debrief m.result.Score, m.result.Heat = m.run.Score(), m.run.Heat() m.result.Outcome, m.result.Finished = m.run.Outcome(), time.Now().UTC() // Commit before the debrief is rendered, even if the client disconnects now. m.save() } return nil } func (m *Model) save() { pilot, best, err := m.store.Record(m.fingerprint, m.result) m.saveFailed = err != nil if err != nil { slog.Error("record run", "pilot", m.pilot.Callsign, "run", m.result.ID, "error", err) return } m.pilot, m.personalBest = pilot, best } func (m *Model) onKey(key string) tea.Cmd { switch key { case "q", "ctrl+c": return tea.Quit } if m.tooSmall() { return nil } switch m.screen { case lobby: switch key { case "enter": return m.start() case "l", "h": m.openRecords(key == "h") } case warmup, playing: switch key { case "p", "space", "esc": m.screen = paused m.generation++ case "up", "w": m.run.Steer(game.Up) case "right", "d": m.run.Steer(game.Right) case "down", "s": m.run.Steer(game.Down) case "left", "a": m.run.Steer(game.Left) } case paused: switch key { case "p", "space", "esc", "enter": if m.countdown > 0 { m.screen = warmup return m.nextPulse(450 * time.Millisecond) } m.screen = playing return m.nextPulse(m.run.Interval()) } case debrief: if m.saveFailed { if key == "r" || key == "enter" { m.save() } return nil } switch key { case "enter": return m.start() case "esc": return m.backToLobby() case "l", "h": m.openRecords(key == "h") } case records: return m.recordsKey(key) } return nil } func (m *Model) backToLobby() tea.Cmd { if pilot, err := m.store.Profile(m.fingerprint); err == nil { m.pilot = pilot } else { slog.Error("read pilot profile", "pilot", m.pilot.Callsign, "error", err) } m.screen = lobby m.run = nil return m.nextPulse(360 * time.Millisecond) } func (m *Model) openRecords(history bool) { m.returnScreen, m.screen = m.screen, records m.generation++ m.historyTab, m.page = history, 0 m.loadRecords() } func (m *Model) recordsKey(key string) tea.Cmd { switch key { case "esc": if m.returnScreen == lobby { return m.backToLobby() } m.screen = m.returnScreen case "tab", "l", "h": m.historyTab = key == "h" || (key == "tab" && !m.historyTab) m.page = 0 m.loadRecords() case "left", "pgup": if m.page > 0 { m.page-- m.loadRecords() } case "right", "pgdown": if m.page+1 < m.pages() { m.page++ m.loadRecords() } case "r": m.loadRecords() } return nil } func (m *Model) pages() int { total := len(m.standings) if m.historyTab { total = m.historyTotal } return max(1, (total+pageSize-1)/pageSize) } func (m *Model) loadRecords() { var err error if m.historyTab { m.history, m.historyTotal, err = m.store.History(m.fingerprint, m.page*pageSize, pageSize) } else { m.standings, err = m.store.Leaderboard() m.page = min(m.page, m.pages()-1) } m.readFailed = err != nil if err != nil { slog.Error("load records", "pilot", m.pilot.Callsign, "error", err) } }