package game import ( "fmt" "math/rand" "sirlab.de/go/knyt/quote" "time" ) func (gm *Game) setupRound() error { gm.round = newRound() err := gm.selectQuote() return err } func newRound() Round { return Round{ selections: make(map[string]string, 0), revelation: Revelation{ votes: make(map[string][]string, 0), sources: make(map[string]bool, 0), }, } } func (gm *Game) selectQuote() error { allQuotes := gm.getAllQuotes() r := rand.New(rand.NewSource(time.Now().UnixNano())) var quote *quote.Quote sources := make([]Source, 0) for _, quoteIdx := range r.Perm(len(allQuotes)) { q := allQuotes[quoteIdx] if quote == nil && !q.IsPlayed() { quote = q } sourceId := q.GetSourceId() if len(sources) == 12 { if quote != nil { break } } else { if !gm.isSourceIdInList(sources, sourceId) { sources = append(sources, gm.getSourceById(sourceId)) } } } if quote == nil { return fmt.Errorf("no more quotes found") } gm.mu.Lock() defer gm.mu.Unlock() gm.round.quoteId = quote.GetId() gm.round.sources = make([]Source, len(sources)) for idx, idxPerm := range r.Perm(len(sources)) { gm.round.sources[idx] = sources[idxPerm] } return nil } func (gm *Game) getSourceById(id string) Source { plInfo, err := gm.getPlayerById(id) if err != nil { fmt.Println(err) return Source{} } return Source{ id: id, name: plInfo.name, } } func (gm *Game) isSourceIdInList(sources []Source, sourceId string) bool { for _, source := range sources { if source.id == sourceId { return true } } return false }