don't crash on setupRound() when there're no more quotes

This commit is contained in:
Settel 2021-12-03 17:23:42 +01:00
parent f2e0166062
commit 32f9fa68b3
3 changed files with 16 additions and 5 deletions

View File

@ -6,7 +6,7 @@ import (
func (gm *Game) FinishGame() {
state, _ := gm.GetState()
if state != STATE_PLAY {
if state != STATE_IDLE && state != STATE_PLAY {
fmt.Printf("invalid state, can't finish game in %s state\n", state)
return
}

View File

@ -17,6 +17,10 @@ func (gm *Game) runRound() {
return
}
gm.setupRound()
errRound := gm.setupRound()
if errRound != nil {
gm.runFinal()
return
}
gm.notifyClients()
}

View File

@ -7,9 +7,10 @@ import (
"time"
)
func (gm *Game) setupRound() {
func (gm *Game) setupRound() error {
gm.round = newRound()
gm.selectQuote()
err := gm.selectQuote()
return err
}
func newRound() Round {
@ -22,7 +23,7 @@ func newRound() Round {
}
}
func (gm *Game) selectQuote() {
func (gm *Game) selectQuote() error {
allQuotes := gm.getAllQuotes()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
@ -46,6 +47,11 @@ func (gm *Game) selectQuote() {
}
}
}
if quote == nil {
return fmt.Errorf("no more quotes found")
}
gm.mu.Lock()
defer gm.mu.Unlock()
@ -54,6 +60,7 @@ func (gm *Game) selectQuote() {
for idx, idxPerm := range r.Perm(len(sources)) {
gm.round.sources[idx] = sources[idxPerm]
}
return nil
}
func (gm *Game) getSourceById(id string) Source {