fix: stop combatLoop goroutine and remove lobby room on session exit

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-24 10:56:23 +09:00
parent e8887cd69a
commit 46afd82060
3 changed files with 32 additions and 5 deletions

View File

@@ -67,6 +67,7 @@ type GameSession struct {
actions map[string]PlayerAction // playerName -> action
actionCh chan playerActionMsg
combatSignal chan struct{}
done chan struct{}
}
type playerActionMsg struct {
@@ -82,6 +83,16 @@ func NewGameSession() *GameSession {
actions: make(map[string]PlayerAction),
actionCh: make(chan playerActionMsg, 4),
combatSignal: make(chan struct{}, 1),
done: make(chan struct{}),
}
}
func (s *GameSession) Stop() {
select {
case <-s.done:
// already stopped
default:
close(s.done)
}
}
@@ -102,6 +113,12 @@ func (s *GameSession) StartGame() {
// combatLoop continuously runs turns while in combat phase
func (s *GameSession) combatLoop() {
for {
select {
case <-s.done:
return
default:
}
s.mu.Lock()
phase := s.state.Phase
gameOver := s.state.GameOver
@@ -112,13 +129,12 @@ func (s *GameSession) combatLoop() {
}
if phase == PhaseCombat {
s.RunTurn() // blocks until all actions collected or timeout
s.RunTurn()
} else {
// Not in combat, wait for an action signal to avoid busy-spinning
// We'll just sleep briefly and re-check
select {
case <-s.combatSignal:
// Room entered, combat may have started
case <-s.done:
return
}
}
}

View File

@@ -41,6 +41,9 @@ func (s *GameSession) RunTurn() {
collected++
case <-timer.C:
goto resolve
case <-s.done:
timer.Stop()
return
}
}
timer.Stop()