-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
274 lines (235 loc) · 6.42 KB
/
types.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package quiz
import (
logger "log"
"math/rand"
"time"
)
type Game struct {
connection *Connection
Current int
Rounds []*Round
RoundDuration time.Duration
RoundTimer *time.Timer
StreamerVote int `json:"streamer_vote"`
ChatVote int `json:"chat_vote"`
ChatVoteCount [4]int `json:"chat_vote_count"`
voteHistory map[string]bool `json:"-"`
Summary *GameSummary
}
type GameSummary struct {
StreamerPoints int `json:"streamer_points"`
StreamerWon int `json:"streamer_won"`
ChatPoints int `json:"chat_points"`
ChatWon int `json:"chat_won"`
}
type CategoryGroupDefinition struct {
ID string `json:"id"`
Title string `json:"title"`
IsDev bool `json:"-"`
IsRelease bool `json:"-"`
Categories []CategoryDefinition `json:"categories"`
}
type CategoryGroup struct {
CategoryGroupDefinition
Categories []Category `json:"categories"`
}
func (cg CategoryGroup) GetDefinition() CategoryGroupDefinition {
cg.CategoryGroupDefinition.Categories = make([]CategoryDefinition, len(cg.Categories))
for i, c := range cg.Categories {
cg.CategoryGroupDefinition.Categories[i] = c.GetDefinition()
}
return cg.CategoryGroupDefinition
}
type CategoryDefinition struct {
ID string `json:"id"`
Title string `json:"title"`
Count int `json:"count,omitempty"`
}
type Category struct {
CategoryDefinition
Pool []*Question `json:"pool"`
}
func (c Category) GetDefinition() CategoryDefinition {
c.CategoryDefinition.Count = len(c.Pool)
return c.CategoryDefinition
}
type Question struct {
Question DisplayableContent `json:"question"`
Correct []DisplayableContent `json:"correct"`
Wrong []DisplayableContent `json:"wrong"`
}
type DisplayableContent struct {
Type ContentType
Text string
}
type ContentType uint8
const (
CONTENTTEXT ContentType = iota
CONTENTIMAGE
)
type Round struct {
Question string `json:"question"`
Answers []string `json:"answers"`
Correct int `json:"correct,omitempty"`
Current int `json:"current_round"`
Max int `json:"max_round"`
Category CategoryDefinition `json:"category"`
}
type RoundSummary struct {
*Round
StreamerPoints int `json:"streamer_points"`
StreamerVote int `json:"streamer_vote"`
ChatPoints int `json:"chat_points"`
ChatVote int `json:"chat_vote"`
ChatVoteCount [4]int `json:"chat_vote_count"`
}
type categoryGroups map[int]CategoryGroup
// Categories is the main list of all Categories and Groups
var Categories categoryGroups
var log = logger.New(logger.Writer(), "[WEB] ", logger.LstdFlags|logger.Lmsgprefix)
func (cg categoryGroups) GetCategoryByName(name string) Category {
for _, group := range cg {
for _, c := range group.Categories {
if c.ID == name {
return c
}
}
}
return Category{}
}
func (cg categoryGroups) GetDefinition() (definitions map[int]CategoryGroupDefinition) {
definitions = make(map[int]CategoryGroupDefinition, len(cg))
for color, group := range cg {
definitions[color] = group.GetDefinition()
}
return definitions
}
func (g Game) GetRoundSummary() RoundSummary {
sum := RoundSummary{
StreamerPoints: g.Summary.StreamerPoints,
StreamerVote: g.StreamerVote,
ChatPoints: g.Summary.ChatPoints,
ChatVote: g.ChatVote,
ChatVoteCount: g.ChatVoteCount,
}
if g.Current > 0 {
sum.Round = g.Rounds[g.Current-1]
}
return sum
}
// NextRound advances the game to the next round. That includes incrementing the counter and setting
// a new round timer.
func (g *Game) NextRound() {
g.Current++
g.StreamerVote = 0
g.voteHistory = make(map[string]bool)
g.ChatVoteCount = [4]int{}
g.RoundTimer = time.AfterFunc(g.RoundDuration, g.endRound)
}
func (g *Game) endRound() {
if g == nil || g.connection == nil {
return
}
if g.RoundTimer != nil && g.RoundTimer.Stop() {
// If the round timer was running, the call to Stop will run this function again. To prevent
// duplicates we exit here.
return
}
g.RoundTimer = nil
// determine winner
correct := g.Rounds[g.Current-1].Correct
const roundPoints = 5
if g.StreamerVote == correct {
g.Summary.StreamerPoints += roundPoints
g.Summary.StreamerWon++
}
chatCorrect := g.ChatVoteCount[correct-1]
g.ChatVote = correct
var totalVotes int
for i, votes := range g.ChatVoteCount {
totalVotes += votes
if votes > chatCorrect {
g.ChatVote = i + 1
}
}
if totalVotes > 0 && g.ChatVote == correct {
g.Summary.ChatPoints += roundPoints
g.Summary.ChatWon++
} else if totalVotes == 0 {
g.ChatVote = 0
}
// send to ws
if g.connection.WS == nil {
log.Println("tried to end round, but no websocket is not connected")
return
}
roundSummary := struct {
Type string `json:"type"`
RoundSummary
}{
Type: "ROUND_END",
RoundSummary: g.GetRoundSummary(),
}
g.connection.WS.WriteJSON(roundSummary)
}
// GetRounds tries to get n questions from c. If c contains less than n questions, GetRounds returns
// all questions of c.
//
// The returned questions are in a randomized order.
func (c Category) GetRounds(n int) []*Round {
if n == 0 {
return []*Round{}
}
rand.Shuffle(len(c.Pool), func(i, j int) {
c.Pool[i], c.Pool[j] = c.Pool[j], c.Pool[i]
})
var questions []*Question
if n >= len(c.Pool) {
questions = c.Pool
} else {
questions = c.Pool[:n]
}
var rounds = make([]*Round, 0, len(questions))
for _, q := range questions {
if q == nil {
continue
}
round := q.ToRound()
round.Category = c.GetDefinition()
rounds = append(rounds, &round)
}
return rounds
}
func (q Question) ToRound() Round {
var answers []string
// select one correct answer
if len(q.Correct) > 1 {
rand.Shuffle(len(q.Correct), func(i, j int) {
q.Correct[i], q.Correct[j] = q.Correct[j], q.Correct[i]
})
answers = append(answers, q.Correct[rand.Intn(len(q.Correct)-1)].Text)
} else {
answers = append(answers, q.Correct[0].Text)
}
// select up to 3 wrong answers
if len(q.Wrong) > 3 {
rand.Shuffle(len(q.Wrong), func(i, j int) {
q.Wrong[i], q.Wrong[j] = q.Wrong[j], q.Wrong[i]
})
}
for _, a := range q.Wrong[:3] {
answers = append(answers, a.Text)
}
var correct int
rand.Shuffle(len(answers), func(i, j int) {
answers[i], answers[j] = answers[j], answers[i]
if i == correct || j == correct {
correct = i + j - correct
}
})
return Round{
Question: q.Question.Text,
Answers: answers,
Correct: correct + 1,
}
}