forked from fxaa/codenames.plus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
298 lines (254 loc) · 6.2 KB
/
game.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"strings"
"github.com/markbates/pkger"
)
var (
DefaultWords = []string{}
NsfwWords = []string{}
DuetWords = []string{}
UndercoverWords = []string{}
CustomWords = []string{}
BoardTypeToWordSet = map[BoardType]*[]string{}
)
func init() {
DefaultWords = readWords("/server/words.txt")
NsfwWords = readWords("/server/nsfw-words.txt")
DuetWords = readWords("/server/duet-words.txt")
UndercoverWords = readWords("/server/duet-words.txt")
CustomWords = readWords("/server/custom-words.txt")
BoardTypeToWordSet = map[BoardType]*[]string{
BoardTypeDefault: &DefaultWords,
BoardTypeDuet: &DuetWords,
BoardTypeCustom: &CustomWords,
BoardTypeNsfw: &NsfwWords,
BoardTypeUndercover: &UndercoverWords,
}
}
func readWords(file string) []string {
f, err := pkger.Open(file)
if err != nil {
panic(err)
}
defer f.Close()
txt, err := ioutil.ReadAll(f)
if err != nil {
fmt.Println("unable to read file", file)
panic(err)
}
return strings.Split(string(txt), "\n")
}
type Player struct {
ID string `json:"id"`
NameAvailable bool `json:"nameAvailable"`
TempName string `json:"tempName"`
Counter int `json:"counter"`
NickName string `json:"nickname"`
Room string `json:"room"`
Team string `json:"team"`
Role string `json:"role"`
GuessProposal *string `json:"guessProposal"`
Timeout int `json:"timeout"`
AfkTimer int `json:"afkTimer"`
}
var (
TileTypeBlue = "blue"
TileTypeRed = "red"
TileTypeBlack = "death"
TileTypeNeutral = "neutral"
)
type Tile struct {
Word string `json:"word"`
Flipped bool `json:"flipped"`
Type string `json:"type"`
}
var (
TeamBlue = "blue"
TeamRed = "red"
)
type Clue struct {
Word string `json:"word"`
Count int `json:"count"`
}
type GameLog struct {
Event string `json:"event,omitempty"`
Team string `json:"team,omitempty"`
Word string `json:"word,omitempty"`
Type string `json:"type,omitempty"`
Clue *Clue `json:"clue,omitempty"`
EndedTurn bool `json:"endedTurn"`
}
type Game struct {
TimerAmount float64 `json:"timerAmount"`
WordPool int `json:"wordPool"`
// Game types, these are kept here for the UI
// actual board types are stored in the room
Base bool `json:"base"`
Duet bool `json:"duet"`
Undercover bool `json:"undercover"`
Custom bool `json:"custom"`
Nsfw bool `json:"nsfw"`
// player count
Red int `json:"red"`
Blue int `json:"blue"`
// game state
Turn string `json:"turn"`
Over bool `json:"over"`
Winner *string `json:"winner"`
Timer float64 `json:"timer"`
Board [][]Tile `json:"board"`
Log []GameLog `json:"log"`
Clue *Clue `json:"clue"`
turnsTaken int
}
func NewGame(bt BoardType, timerAmount float64) *Game {
blueTiles := 9
redTiles := 8
turn := TeamBlue
if rand.Intn(100)%2 == 0 {
turn = TeamRed
blueTiles = 8
redTiles = 9
}
return &Game{
TimerAmount: timerAmount,
WordPool: wordpoolSize(bt),
Base: isSet(bt, BoardTypeDefault),
Duet: isSet(bt, BoardTypeDuet),
Undercover: isSet(bt, BoardTypeUndercover),
Custom: isSet(bt, BoardTypeCustom),
Nsfw: isSet(bt, BoardTypeNsfw),
Red: redTiles,
Blue: blueTiles,
Turn: turn,
Over: false,
Winner: nil,
Timer: timerAmount,
Board: generateBoard(bt, turn),
Log: []GameLog{},
Clue: nil,
}
}
func wordpoolSize(bt BoardType) int {
count := 0
visitBoardType(bt, func(bt BoardType) {
count += len(*BoardTypeToWordSet[bt])
})
return count
}
func generateBoard(bt BoardType, turn string) [][]Tile {
totalWords := 25
setsEnabled := getTotalSetsEnabled(bt)
if setsEnabled == 0 {
setsEnabled = 1
bt = BoardTypeDefault
}
wordsPerSet := (totalWords / setsEnabled) + 1
words := getWords(bt, wordsPerSet)
linearTiles := generateLinearTiles(words, turn)
rand.Shuffle(len(linearTiles), func(i, j int) { linearTiles[i], linearTiles[j] = linearTiles[j], linearTiles[i] })
result := [][]Tile{}
width := 5
for i := 0; i < 5; i++ {
x := i * width
y := x + width
result = append(result, linearTiles[x:y])
}
return result
}
func getTotalSetsEnabled(bt BoardType) int {
setsEnabled := 0
visitBoardType(bt, func(bt BoardType) {
setsEnabled++
})
return setsEnabled
}
func visitBoardType(bt BoardType, visitor func(bt BoardType)) {
if isSet(bt, BoardTypeDefault) {
visitor(BoardTypeDefault)
}
if isSet(bt, BoardTypeCustom) {
visitor(BoardTypeCustom)
}
if isSet(bt, BoardTypeDuet) {
visitor(BoardTypeDuet)
}
if isSet(bt, BoardTypeNsfw) {
visitor(BoardTypeNsfw)
}
if isSet(bt, BoardTypeUndercover) {
visitor(BoardTypeUndercover)
}
}
func getWords(bt BoardType, wordsPerSet int) []string {
words := map[string]struct{}{}
visitBoardType(bt, func(bt BoardType) {
selectWords(*BoardTypeToWordSet[bt], wordsPerSet, words)
})
result := []string{}
for k := range words {
result = append(result, k)
}
return result
}
func isSet(bt, typ BoardType) bool {
return (bt & typ) != 0
}
func selectWords(arr []string, count int, lookupTable map[string]struct{}) {
for i := 0; i < count; {
di := rand.Intn(len(arr))
if _, ok := lookupTable[arr[di]]; ok {
continue
}
lookupTable[arr[di]] = struct{}{}
i++
}
}
func generateLinearTiles(words []string, turn string) []Tile {
linearTiles := make([]Tile, 25)
wordIter := 0
// the game has 1 black time
// 9 for one team
// 8 for the other team
// rest are neutral
linearTiles[wordIter] = Tile{
Word: words[wordIter],
Type: TileTypeBlack,
Flipped: false,
}
wordIter++
firstColor := TileTypeBlue
secondColor := TileTypeRed
if turn == TeamRed {
firstColor = TileTypeRed
secondColor = TileTypeBlue
}
for i := 0; i < 9; i++ {
linearTiles[wordIter] = Tile{
Word: words[wordIter],
Type: firstColor,
Flipped: false,
}
wordIter++
}
for i := 0; i < 8; i++ {
linearTiles[wordIter] = Tile{
Word: words[wordIter],
Type: secondColor,
Flipped: false,
}
wordIter++
}
for i := 0; i < 7; i++ {
linearTiles[wordIter] = Tile{
Word: words[wordIter],
Type: TileTypeNeutral,
Flipped: false,
}
wordIter++
}
return linearTiles
}