-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.go
455 lines (399 loc) · 12.5 KB
/
common.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package game
import (
"math"
"github.com/dascr/dascr-board/player"
"github.com/dascr/dascr-board/throw"
"github.com/dascr/dascr-board/undo"
"github.com/dascr/dascr-board/utils"
"github.com/dascr/dascr-board/ws"
)
var (
soundNoOverwrite []string = []string{"reveal", "open", "close", "T17", "T18", "T19", "T20", "D25", "split"}
)
// This will check if an ongoing throw round
// is assosiated with the active player
// if not it will create one
func checkOngoingElseCreate(activePlayer *player.Player, base *BaseGame, sequence *undo.Sequence) {
// Check if ongoing round
ongoing := utils.CheckOngoingRound(activePlayer.ThrowRounds, base.ThrowRound)
if !ongoing {
// If there is no round associated with the current throw round of the game
// create one
newRound := &throw.Round{
Round: base.ThrowRound,
Throws: []throw.Throw{},
}
activePlayer.ThrowRounds = append(activePlayer.ThrowRounds, *newRound)
sequence.AddActionToSequence(undo.Action{
Action: "CREATETHROWROUND",
RoundNumber: newRound.Round,
Player: activePlayer,
})
}
}
// This will add a provided throw to the last round
// assosiated with the active player
// It returns the throw round for further processing withing game logic
func addThrowToCurrentRound(activePlayer *player.Player, base *BaseGame, sequence *undo.Sequence, number, modifier int) *throw.Round {
// Now add throw to that round or to the existing
throwRound := &activePlayer.ThrowRounds[base.ThrowRound-1]
newThrow := &throw.Throw{
Number: number,
Modifier: modifier,
}
throwRound.Throws = append(throwRound.Throws, *newThrow)
sequence.AddActionToSequence(undo.Action{
Action: "CREATETHROW",
Player: activePlayer,
RoundNumber: throwRound.Round,
})
return throwRound
}
// This will check if player threw 3 darst
// and then close the round if so
func closePlayerRound(base *BaseGame, activePlayer *player.Player, throwRound *throw.Round, sequence *undo.Sequence, actions []undo.Action, previousState, previousMessage string) {
throwRound.Done = true
base.GameState = "NEXTPLAYER"
base.Message = "Remove Darts!"
// Check if the sound can safely be overwritten
// Sounds which are not supposed to be overwritten
// go into the var at the top (soundNoOverwrite)
overwrite := true
for _, sound := range soundNoOverwrite {
if sound == base.SoundToPlay {
overwrite = false
break
}
}
if overwrite {
base.SoundToPlay = "rounddone"
}
for _, a := range actions {
sequence.AddActionToSequence(a)
}
sequence.AddActionToSequence(undo.Action{
Action: "CLOSEPLAYERTHROWROUND",
Player: activePlayer,
RoundNumber: throwRound.Round,
GameID: base.UID,
PreviousGameState: previousState,
PreviousMessage: previousMessage,
})
}
// This will be used by setFrontendAssets
// It calculates player average and total throw count
func calculateAverageAndTotalThrowCount(player *player.Player) *player.Player {
// Set average
allTotalThrowCount := 0
totalSumCount := 0
divider := float64(0)
for _, rnd := range player.ThrowRounds {
for _, thr := range rnd.Throws {
totalSumCount += thr.Number * thr.Modifier
divider += float64(1 / 3.0)
allTotalThrowCount++
}
}
avg := float64(totalSumCount) / divider
avg = math.Round(avg*100) / 100
if math.IsNaN(avg) {
avg = 0
}
player.Average = avg
player.TotalThrowCount = allTotalThrowCount
return player
}
// This will be used by setFrontendAssets
// It sets the players field LastThrows
// To the last 3 throws of current throw round
// Ommiting empty throws
func getLastThreeThrows(player *player.Player, base *BaseGame) *player.Player {
// Set last 3 throws
// and sum of it
player.LastThrows = make([]throw.Throw, 0)
lastThreeSum := 0
if len(player.ThrowRounds) > 0 {
if len(player.ThrowRounds[len(player.ThrowRounds)-1].Throws) != 0 {
for _, thr := range player.ThrowRounds[len(player.ThrowRounds)-1].Throws {
player.LastThrows = append(player.LastThrows, thr)
lastThreeSum += thr.Number * thr.Modifier
}
player.ThrowSum = lastThreeSum
}
} else {
player.LastThrows = make([]throw.Throw, 3)
}
return player
}
// This will take care of the stats to display at frontend
func setFrontendAssets(player *player.Player, base *BaseGame) {
player = calculateAverageAndTotalThrowCount(player)
getLastThreeThrows(player, base)
}
// This will set game state for podium case
// Placement of player on podium
func doPodium(base *BaseGame, activePlayer *player.Player, sequence *undo.Sequence) {
previousMessage := base.Message
previousState := base.GameState
playerLeft := len(base.Player) - base.Podium.GetPodiumLength()
// More than 2 player left to go to the podium
if playerLeft > 2 {
// Push active players uid to podium
base.Podium.AddPlayerToPodium(activePlayer)
base.GameState = "NEXTPLAYERWON"
base.Message = "Next Winner!"
base.SoundToPlay = "win"
if base.Podium.GetPodiumLength() == 1 {
base.Message = "Winner!"
}
sequence.AddActionToSequence(undo.Action{
Action: "DOPODIUM",
Player: activePlayer,
PreviousGameState: previousState,
PreviousMessage: previousMessage,
GameID: base.UID,
})
return
}
// Otherwise exactly 2 left
base.Podium.AddPlayerToPodium(activePlayer)
sequence.AddActionToSequence(undo.Action{
Action: "DOPODIUM",
Player: activePlayer,
PreviousGameState: previousState,
PreviousMessage: previousMessage,
GameID: base.UID,
})
for i := range base.Player {
var contained = false
// When player UID is not in base.Podium
podium := *base.Podium.GetPodium()
for j := range podium {
if base.Player[i].UID == podium[j].Player.UID {
contained = true
}
}
if !contained {
base.Podium.AddPlayerToPodium(&base.Player[i])
sequence.AddActionToSequence(undo.Action{
Action: "DOPODIUM",
Player: activePlayer,
PreviousGameState: previousState,
PreviousMessage: previousMessage,
GameID: base.UID,
})
doWin(base)
throwRound := &activePlayer.ThrowRounds[base.ThrowRound-1]
throwRound.Done = true
activePlayer.Score.Score = 0
activePlayer.Score.ParkScore = 0
previousScore := activePlayer.Score.Score
previousParkScore := activePlayer.Score.ParkScore
sequence.AddActionToSequence(undo.Action{
Action: "DOWIN",
GameID: base.UID,
PreviousGameState: previousState,
PreviousMessage: previousMessage,
Player: activePlayer,
PreviousScore: previousScore,
PreviousParkScore: previousParkScore,
})
return
}
}
}
// This will set game state for win case
func doWin(base *BaseGame) {
// Set game state
base.GameState = "WON"
base.Message = "Game shot!"
base.SoundToPlay = "win"
}
// This will strip response for Display function for FrontEnd
// To minimize traffic size
func stripDisplay(base *BaseGame) BaseGame {
var returnBase BaseGame
var returnPlayer = make([]player.Player, 0)
returnBase = *base
for _, p := range returnBase.Player {
var tr = make([]throw.Round, 0)
p.ThrowRounds = tr
returnPlayer = append(returnPlayer, p)
}
returnBase.UndoLog = nil
returnBase.Player = returnPlayer
return returnBase
}
// This will fill the players current throw round with 0s
// for the case someone pressed nextPlayer before
// entering all 3 throws
func fillWithZeros(base *BaseGame, sequence *undo.Sequence, previousState, PreviousMessage string) {
// fill empty throws of active player with 0
// before switching to next player
activePlayer := &base.Player[base.ActivePlayer]
// Check if ongoing round
checkOngoingElseCreate(activePlayer, base, sequence)
currentThrowRound := &activePlayer.ThrowRounds[base.ThrowRound-1]
count := 3 - len(currentThrowRound.Throws)
if count != 0 {
for i := 0; i < count; i++ {
newThrow := &throw.Throw{
Number: 0,
Modifier: 1,
}
currentThrowRound.Throws = append(currentThrowRound.Throws, *newThrow)
sequence.AddActionToSequence(undo.Action{
Action: "CREATETHROW",
Player: activePlayer,
RoundNumber: currentThrowRound.Round,
})
}
currentThrowRound.Done = true
// When X01 we need to set new parkScore
if base.Game == "x01" {
activePlayer.Score.ParkScore = activePlayer.Score.Score
previousParkScore := activePlayer.Score.ParkScore
previousScore := activePlayer.Score.Score
previousAverage := activePlayer.Average
previousThrowSum := activePlayer.ThrowSum
previousLastThree := activePlayer.LastThrows
sequence.AddActionToSequence(undo.Action{
Action: "UPDATESCOREANDPARK",
Player: activePlayer,
PreviousScore: previousScore,
PreviousParkScore: previousParkScore,
PreviousAverage: previousAverage,
PreviousThrowSum: previousThrowSum,
PreviousLastThree: previousLastThree,
})
}
sequence.AddActionToSequence(undo.Action{
Action: "CLOSEPLAYERTHROWROUND",
Player: activePlayer,
RoundNumber: currentThrowRound.Round,
GameID: base.UID,
PreviousGameState: previousState,
PreviousMessage: PreviousMessage,
})
}
}
// This will switch to next player
func switchToNextPlayer(base *BaseGame, h *ws.Hub) {
sequence := base.UndoLog.CreateSequence()
previousPlayerIndex := base.ActivePlayer
previousState := base.GameState
previousMessage := base.Message
// Switch to next player
if base.GameState != "WON" {
activePlayer := &base.Player[base.ActivePlayer]
fillWithZeros(base, sequence, previousState, previousMessage)
// Switch player
base.ActivePlayer = utils.ChooseNextPlayer(base.Player, base.ActivePlayer, base.Podium.GetPodium())
// Reset gamestate
base.GameState = "THROW"
base.Message = "-"
base.SoundToPlay = "nextplayer"
sequence.AddActionToSequence(undo.Action{
Action: "NEXTPLAYER",
PreviousPlayerIndex: previousPlayerIndex,
GameID: base.UID,
})
// Set assets for Frontend
setFrontendAssets(activePlayer, base)
// Update scoreboard
utils.WSSendUpdate(base.UID, h)
// Check if throw round done by all players and increase
checkRoundDone(base, sequence)
}
}
// This will check if the round is done and increase overall
// Throw round
func checkRoundDone(base *BaseGame, sequence *undo.Sequence) {
// Check if to increase game.ThrowRound
if roundDone := utils.CheckRoundDone(base.Player, base.ThrowRound, base.Podium.GetPodium()); roundDone {
base.ThrowRound++
sequence.AddActionToSequence(undo.Action{
Action: "INCREASETHROWROUND",
GameID: base.UID,
})
}
}
// This will trigger undo
func triggerUndo(base *BaseGame, h *ws.Hub) error {
// Check length and do not remove "CREATEGAME"
if len(*base.UndoLog) > 1 {
sequence, err := base.UndoLog.GetLastSequence()
if err != nil {
return err
}
for _, a := range sequence.Action {
if a.Action == "CREATETHROWROUND" {
// look if there is CREATETHROWROUND in sequence
// if so resort and put it last as otherwise
// undo will be rendered unusable
// park CREATETHROWROUND
parkAction := sequence.Action[0]
// remove CREATETHROWROUND from sequence
sequence.Action = sequence.Action[1:]
// add CREATETHROWROUND at the end
sequence.Action = append(sequence.Action, parkAction)
break
}
}
for _, a := range sequence.Action {
switch a.Action {
case "CREATETHROWROUND":
UndoCreateThrowRound(a)
case "CREATETHROW":
UndoCreateThrow(a)
case "DOWIN":
UndoWin(a, base)
case "DOPODIUM":
UndoDoPodium(a, base)
case "NEXTPLAYER":
UndoNextPlayer(a, base)
case "CLOSEPLAYERTHROWROUND":
UndoClosePlayerThrowRound(a, base)
case "INCREASETHROWROUND":
UndoIncreaseThrowRound(a, base)
// X01
case "UPDATESCORE":
UndoScore(a)
case "UPDATESCOREANDPARK":
UndoScoreAndPark(a)
case "X01BUST":
UndoBustAndWin(a, base)
// Cricket
case "REVEALNUMBER":
UndoRevealNumber(a, base)
case "INCREASEHITCOUNT":
UndoIncreaseHitCount(a)
case "CLOSEPLAYERNUMBER":
UndoClosePlayerNumber(a)
case "CLOSECONTROLLERNUMBER":
UndoCloseControllerNumber(a, base)
case "GAINPOINTS":
UndoGainPoints(a)
// ATC
case "ATCINCREASENUMBER":
UndoATCIncreaseNumber(a)
// Split
case "UPDATESPLITSCORE":
UndoUpdateSplitScore(a)
default:
break
}
}
// Remove sequence from undo log
base.UndoLog.RemoveLastSequence()
// reset assets
setFrontendAssets(&base.Player[base.ActivePlayer], base)
// Update trigger via hub
message := ws.Message{
Room: base.UID,
Data: []byte("update"),
}
h.Broadcast <- message
}
return nil
}