fix: 13 bugs found via systematic code review and testing

Multiplayer:
- Add WaitingScreen between class select and game start; previously
  selecting a class immediately started the game and locked the room,
  preventing other players from joining
- Add periodic lobby room list refresh (2s interval)
- Add LeaveRoom method for backing out of waiting room

Combat & mechanics:
- Mark invalid attack targets with TargetIdx=-1 to suppress misleading
  "0 dmg" combat log entries
- Make Freeze effect actually skip frozen player's action (was purely
  cosmetic before - expired during tick before action processing)
- Implement Life Siphon relic heal-on-damage effect (was defined but
  never applied in combat)
- Fix combo matching to track used actions and prevent reuse

Game modes:
- Wire up weekly mutations to GameSession via ApplyWeeklyMutation()
- Implement 3 mutation runtime effects: no_shop, glass_cannon, elite_flood
- Pass HardMode toggle from lobby UI through Context to GameSession
- Apply HardMode difficulty multipliers (1.5x monsters, 2x shop, 0.5x heal)

Polish:
- Set starting room (index 0) to always be Empty (safe start)
- Distinguish shop purchase errors: "Not enough gold" vs "Inventory full"
- Record random events in codex for discovery tracking

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-25 20:45:56 +09:00
parent 97aa4667a1
commit 1563091de1
20 changed files with 316 additions and 29 deletions

View File

@@ -36,6 +36,8 @@ func (s *ClassSelectScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd)
if room != nil {
if room.Session == nil {
room.Session = game.NewGameSession(ctx.Lobby.Cfg())
room.Session.HardMode = ctx.HardMode
room.Session.ApplyWeeklyMutation()
}
ctx.Session = room.Session
player := entity.NewPlayer(ctx.PlayerName, selectedClass)
@@ -44,11 +46,8 @@ func (s *ClassSelectScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd)
if ctx.Lobby != nil {
ctx.Lobby.RegisterSession(ctx.Fingerprint, ctx.RoomCode)
}
ctx.Session.StartGame()
ctx.Lobby.StartRoom(ctx.RoomCode)
gs := NewGameScreen()
gs.gameState = ctx.Session.GetState()
return gs, gs.pollState()
ws := NewWaitingScreen()
return ws, ws.pollWaiting()
}
}
}

View File

@@ -16,4 +16,5 @@ type Context struct {
Store *store.DB
Session *game.GameSession
RoomCode string
HardMode bool
}

View File

@@ -86,6 +86,15 @@ func (s *GameScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
}
}
// Record codex entries for events
if ctx.Store != nil && s.gameState.LastEventName != "" {
key := "event:" + s.gameState.LastEventName
if !s.codexRecorded[key] {
ctx.Store.RecordCodexEntry(ctx.Fingerprint, "event", s.gameState.LastEventName)
s.codexRecorded[key] = true
}
}
s.prevPhase = s.gameState.Phase
}

View File

@@ -41,6 +41,12 @@ func NewLobbyScreen() *LobbyScreen {
return &LobbyScreen{}
}
func (s *LobbyScreen) pollLobby() tea.Cmd {
return tea.Tick(time.Second*2, func(t time.Time) tea.Msg {
return tickMsg{}
})
}
func (s *LobbyScreen) refreshLobby(ctx *Context) {
if ctx.Lobby == nil {
return
@@ -71,6 +77,11 @@ func (s *LobbyScreen) refreshLobby(ctx *Context) {
}
func (s *LobbyScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
switch msg.(type) {
case tickMsg:
s.refreshLobby(ctx)
return s, s.pollLobby()
}
if key, ok := msg.(tea.KeyMsg); ok {
// Join-by-code input mode
if s.joining {
@@ -132,6 +143,7 @@ func (s *LobbyScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
room.Session = game.NewGameSession(ctx.Lobby.Cfg())
room.Session.DailyMode = true
room.Session.DailyDate = time.Now().Format("2006-01-02")
room.Session.ApplyWeeklyMutation()
ctx.Session = room.Session
}
return NewClassSelectScreen(), nil
@@ -139,6 +151,7 @@ func (s *LobbyScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
}
} else if isKey(key, "h") && s.hardUnlocked {
s.hardMode = !s.hardMode
ctx.HardMode = s.hardMode
} else if isKey(key, "q") {
if ctx.Lobby != nil {
ctx.Lobby.PlayerOffline(ctx.Fingerprint)

View File

@@ -110,6 +110,7 @@ const (
screenTitle screen = iota
screenLobby
screenClassSelect
screenWaiting
screenGame
screenShop
screenResult
@@ -129,6 +130,8 @@ func (m Model) screenType() screen {
return screenLobby
case *ClassSelectScreen:
return screenClassSelect
case *WaitingScreen:
return screenWaiting
case *GameScreen:
return screenGame
case *ShopScreen:

View File

@@ -111,14 +111,22 @@ func TestClassSelectToGame(t *testing.T) {
t.Fatalf("should be at class select, got %d", m3.screenType())
}
// Press Enter to select Warrior (default cursor=0)
// Press Enter to select Warrior (default cursor=0) → WaitingScreen
result, _ = m3.Update(tea.KeyMsg{Type: tea.KeyEnter})
m4 := result.(Model)
if m4.screenType() != screenGame {
t.Errorf("after class select Enter: screen=%d, want screenGame(3)", m4.screenType())
if m4.screenType() != screenWaiting {
t.Fatalf("after class select Enter: screen=%d, want screenWaiting(%d)", m4.screenType(), screenWaiting)
}
if m4.session() == nil {
// Press Enter to ready up (solo room → immediately starts game)
result, _ = m4.Update(tea.KeyMsg{Type: tea.KeyEnter})
m5 := result.(Model)
if m5.screenType() != screenGame {
t.Errorf("after ready Enter: screen=%d, want screenGame(%d)", m5.screenType(), screenGame)
}
if m5.session() == nil {
t.Error("session should be set")
}
}

View File

@@ -45,7 +45,7 @@ func (s *NicknameScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
}
ls := NewLobbyScreen()
ls.refreshLobby(ctx)
return ls, nil
return ls, ls.pollLobby()
} else if isKey(key, "esc") || key.Type == tea.KeyEsc {
s.input = ""
return NewTitleScreen(), nil

View File

@@ -35,7 +35,7 @@ func (s *ResultScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
ctx.RoomCode = ""
ls := NewLobbyScreen()
ls.refreshLobby(ctx)
return ls, nil
return ls, ls.pollLobby()
} else if isQuit(key) {
return s, tea.Quit
}

View File

@@ -25,10 +25,15 @@ func (s *ShopScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
case "1", "2", "3":
if ctx.Session != nil {
idx := int(key.String()[0] - '1')
if ctx.Session.BuyItem(ctx.Fingerprint, idx) {
switch ctx.Session.BuyItem(ctx.Fingerprint, idx) {
case game.BuyOK:
s.shopMsg = "Purchased!"
} else {
case game.BuyNoGold:
s.shopMsg = "Not enough gold!"
case game.BuyInventoryFull:
s.shopMsg = "Inventory full!"
default:
s.shopMsg = "Cannot buy that!"
}
s.gameState = ctx.Session.GetState()
}

View File

@@ -50,7 +50,7 @@ func (s *TitleScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
}
ls := NewLobbyScreen()
ls.refreshLobby(ctx)
return ls, nil
return ls, ls.pollLobby()
} else if isKey(key, "h") {
return NewHelpScreen(), nil
} else if isKey(key, "s") {

119
ui/waiting_view.go Normal file
View File

@@ -0,0 +1,119 @@
package ui
import (
"fmt"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// WaitingScreen shows room members and lets players ready up before starting.
type WaitingScreen struct {
ready bool
}
func NewWaitingScreen() *WaitingScreen {
return &WaitingScreen{}
}
func (s *WaitingScreen) pollWaiting() tea.Cmd {
return tea.Tick(time.Second, func(t time.Time) tea.Msg {
return tickMsg{}
})
}
func (s *WaitingScreen) startGame(ctx *Context) (Screen, tea.Cmd) {
room := ctx.Lobby.GetRoom(ctx.RoomCode)
if room != nil && room.Session != nil {
ctx.Session = room.Session
ctx.Session.StartGame()
ctx.Lobby.StartRoom(ctx.RoomCode)
gs := NewGameScreen()
gs.gameState = ctx.Session.GetState()
return gs, gs.pollState()
}
return s, s.pollWaiting()
}
func (s *WaitingScreen) Update(msg tea.Msg, ctx *Context) (Screen, tea.Cmd) {
switch msg.(type) {
case tickMsg:
// Check if all players are ready → start game
if ctx.Lobby != nil && ctx.Lobby.AllReady(ctx.RoomCode) {
return s.startGame(ctx)
}
return s, s.pollWaiting()
}
if key, ok := msg.(tea.KeyMsg); ok {
if isEnter(key) && !s.ready {
s.ready = true
if ctx.Lobby != nil {
ctx.Lobby.SetPlayerReady(ctx.RoomCode, ctx.Fingerprint, true)
// Solo: if only 1 player in room, start immediately
room := ctx.Lobby.GetRoom(ctx.RoomCode)
if room != nil && len(room.Players) == 1 {
return s.startGame(ctx)
}
}
} else if isKey(key, "esc") || key.Type == tea.KeyEsc {
// Leave room — unready and go back to lobby
if ctx.Lobby != nil {
ctx.Lobby.SetPlayerReady(ctx.RoomCode, ctx.Fingerprint, false)
ctx.Lobby.LeaveRoom(ctx.RoomCode, ctx.Fingerprint)
}
ctx.RoomCode = ""
ls := NewLobbyScreen()
ls.refreshLobby(ctx)
return ls, nil
}
}
return s, nil
}
func (s *WaitingScreen) View(ctx *Context) string {
headerStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("205")).
Bold(true)
readyStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("46"))
notReadyStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("240"))
header := headerStyle.Render(fmt.Sprintf("── Waiting Room [%s] ──", ctx.RoomCode))
playerList := ""
if ctx.Lobby != nil {
room := ctx.Lobby.GetRoom(ctx.RoomCode)
if room != nil {
for _, p := range room.Players {
status := notReadyStyle.Render("...")
if p.Ready {
status = readyStyle.Render("READY")
}
cls := p.Class
if cls == "" {
cls = "?"
}
playerList += fmt.Sprintf(" %s (%s) %s\n", p.Name, cls, status)
}
}
}
menu := "[Enter] Ready"
if s.ready {
menu = "Waiting for other players..."
}
menu += " [Esc] Leave"
return lipgloss.JoinVertical(lipgloss.Left,
header,
"",
playerList,
"",
menu,
)
}