-
Notifications
You must be signed in to change notification settings - Fork 23
/
round.go
133 lines (115 loc) · 2.85 KB
/
round.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package fam100
import (
"math/rand"
"sort"
"time"
"github.com/yulrizka/fam100/model"
"github.com/yulrizka/fam100/qna"
)
// round represents with one question
type round struct {
id int64
q qna.Question
state State
correct []model.PlayerID // correct answer answered by a player, "" means not answered
players map[model.PlayerID]model.Player
highlight map[int]bool
endAt time.Time
}
func newRound(question qna.Question, players map[model.PlayerID]model.Player) (*round, error) {
return &round{
id: int64(rand.Int31()),
q: question,
correct: make([]model.PlayerID, len(question.Answers)),
state: Created,
players: players,
highlight: make(map[int]bool),
endAt: time.Now().Add(RoundDuration).Round(time.Second),
}, nil
}
func (r *round) timeLeft() time.Duration {
return r.endAt.Sub(time.Now().Round(time.Second))
}
// questionText construct QNAMessage which contains questions, answers and score
func (r *round) questionText(gameID string, showUnAnswered bool) QNAMessage {
ras := make([]roundAnswers, len(r.q.Answers))
for i, ans := range r.q.Answers {
ra := roundAnswers{
Text: ans.String(),
Score: ans.Score,
}
if pID := r.correct[i]; pID != "" {
ra.Answered = true
ra.PlayerName = r.players[pID].Name
}
if r.highlight[i] {
ra.Highlight = true
}
ras[i] = ra
}
msg := QNAMessage{
ChanID: gameID,
QuestionText: r.q.Text,
QuestionID: r.q.ID,
ShowUnanswered: showUnAnswered,
TimeLeft: r.timeLeft(),
Answers: ras,
}
return msg
}
func (r *round) finished() bool {
answered := 0
for _, pID := range r.correct {
if pID != "" {
answered++
}
}
return answered == len(r.q.Answers)
}
// ranking generates a rank for current round which contains player, answers and score
func (r *round) ranking() model.Rank {
var roundScores model.Rank
lookup := make(map[model.PlayerID]model.PlayerScore)
for i, pID := range r.correct {
if pID != "" {
score := r.q.Answers[i].Score
if ps, ok := lookup[pID]; !ok {
lookup[pID] = model.PlayerScore{
PlayerID: pID,
Name: r.players[pID].Name,
Score: score,
}
} else {
ps = lookup[pID]
ps.Score += score
lookup[pID] = ps
}
}
}
for _, ps := range lookup {
roundScores = append(roundScores, ps)
}
sort.Sort(roundScores)
for i := range roundScores {
roundScores[i].Position = i + 1
}
return roundScores
}
func (r *round) answer(p model.Player, text string) (correct, answered bool, index int) {
if r.state != RoundStarted {
return false, false, -1
}
if _, ok := r.players[p.ID]; !ok {
r.players[p.ID] = p
}
if correct, _, i := r.q.CheckAnswer(text); correct {
if r.correct[i] != "" {
// already answered
return correct, true, i
}
r.correct[i] = p.ID
r.highlight[i] = true
return correct, false, i
}
return false, false, -1
}