-
Notifications
You must be signed in to change notification settings - Fork 1
/
apply.go
306 lines (302 loc) · 7.78 KB
/
apply.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
package sh
import (
"time"
)
//Apply mutates the game state by applying the given event.
func (g Game) Apply(e Event) (Game, Event, error) {
//Increment the event counter
g.EventID = g.EventID + 1
//Assign the event id to the event
switch e.GetType() {
//PLAYER EVENTS
case TypePlayerJoin:
ne := e.(PlayerEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
g.Players = append(g.Players, ne.Player)
case TypePlayerReady:
ne := e.(PlayerEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
for i, p := range g.Players {
if p.ID == ne.Player.ID {
g.Players[i].Ready = true
break
}
}
case TypePlayerAcknowledge:
ne := e.(PlayerEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
//Switch the given users ack attribute to true
for i, p := range g.Players {
if p.ID == ne.Player.ID {
g.Players[i].Ack = true
break
}
}
case TypePlayerNominate:
ne := e.(PlayerPlayerEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
//Add the chancelor to the round object
g.Round.ChancellorID = ne.OtherPlayerID
case TypePlayerVote:
ne := e.(PlayerVoteEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
//Add the given vote to the rounds vote array
g.Round.Votes = append(g.Round.Votes, Vote{ne.PlayerID, ne.Vote})
case TypePlayerLegislate:
ne := e.(PlayerLegislateEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
case TypePlayerInvestigate:
ne := e.(PlayerPlayerEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
for i, p := range g.Players {
if p.ID == ne.OtherPlayerID {
g.Players[i].InvestigatedBy = ne.PlayerID
}
}
case TypePlayerSpecialElection:
ne := e.(PlayerPlayerEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
g.SpecialElectionPresidentID = ne.OtherPlayerID
g.SpecialElectionRoundID = g.Round.ID + 1
case TypePlayerExecute:
ne := e.(PlayerPlayerEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
for i, p := range g.Players {
if p.ID == ne.OtherPlayerID {
g.Players[i].ExecutedBy = ne.PlayerID
}
}
case TypePlayerMessage:
ne := e.(MessageEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
for i, p := range g.Players {
if ne.PlayerID == p.ID {
g.Players[i].LastAction = ne.Moment
}
}
//ASSERT EVENTS
case TypeAssertPolicies:
fallthrough
case TypeAssertParty:
ne := e.(AssertEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
//REACT EVENTS
case TypeReactPlayer:
fallthrough
case TypeReactEventID:
fallthrough
case TypeReactStatus:
ne := e.(ReactEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
for i, p := range g.Players {
if ne.PlayerID == p.ID {
g.Players[i].LastAction = ne.Moment
}
}
//GUESS EVENTS
case TypeGuess:
ne := e.(GuessEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
for i, p := range g.Players {
if ne.PlayerID == p.ID {
g.Players[i].LastAction = ne.Moment
}
}
//REQUEST EVENTS
case TypeRequestAcknowledge:
ne := e.(RequestEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
case TypeRequestVote:
ne := e.(RequestEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
//Set the round state to voting
g.Round.State = RoundStateVoting
case TypeRequestNominate:
ne := e.(RequestEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
g.Round.State = RoundStateNominating
case TypeRequestLegislate:
ne := e.(RequestEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
g.Round.State = RoundStateLegislating
case TypeRequestExecutiveAction:
ne := e.(RequestEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
g.Round.State = RoundStateExecutiveAction
//GAME EVENTS
case TypeGameVoteResults:
ne := e.(VoteResultEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
case TypeGameInformation:
ne := e.(InformationEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
case TypeGameUpdate:
ne := e.(GameEvent)
ne.ID = g.EventID
ne.Moment = time.Now()
e = ne
//The event data, set the discard and draw pile accordingly
if ne.Game.ID == "-" {
g.ID = ""
} else if ne.Game.ID != "" {
g.ID = ne.Game.ID
}
if ne.Game.Secret == "-" {
g.Secret = ""
} else if ne.Game.Secret != "" {
g.Secret = ne.Game.Secret
}
if ne.Game.State == "-" {
g.State = ""
} else if ne.Game.State != "" {
g.State = ne.Game.State
}
if ne.Game.WinningParty == "-" {
g.WinningParty = ""
} else if ne.Game.WinningParty != "" {
g.WinningParty = ne.Game.WinningParty
}
if len(ne.Game.Draw) == 1 && ne.Game.Draw[0] == "-" {
g.Draw = []string{}
} else if len(ne.Game.Draw) > 0 {
g.Draw = ne.Game.Draw
}
if len(ne.Game.Discard) == 1 && ne.Game.Discard[0] == "-" {
g.Discard = []string{}
} else if len(ne.Game.Discard) > 0 {
g.Discard = ne.Game.Discard
}
if ne.Game.ElectionTracker > 0 {
g.ElectionTracker = ne.Game.ElectionTracker
} else if ne.Game.ElectionTracker == -1 {
g.ElectionTracker = 0
}
if ne.Game.Liberal > 0 {
g.Liberal = ne.Game.Liberal
} else if ne.Game.Liberal == -1 {
g.Liberal = 0
}
if ne.Game.Fascist > 0 {
g.Fascist = ne.Game.Fascist
} else if ne.Game.Fascist == -1 {
g.Fascist = 0
}
if ne.Game.NextPresidentID == "-" {
g.NextPresidentID = ""
} else if ne.Game.NextPresidentID != "" {
g.NextPresidentID = ne.Game.NextPresidentID
}
if len(ne.Game.Players) == 1 && ne.Game.Players[0].ID == "-" {
g.Players = []Player{}
} else if len(ne.Game.Players) > 0 {
g.Players = ne.Game.Players
}
if ne.Game.PreviousPresidentID == "-" {
g.PreviousPresidentID = ""
} else if ne.Game.PreviousPresidentID != "" {
g.PreviousPresidentID = ne.Game.PreviousPresidentID
}
if ne.Game.PreviousChancellorID == "-" {
g.PreviousChancellorID = ""
} else if ne.Game.PreviousChancellorID != "" {
g.PreviousChancellorID = ne.Game.PreviousChancellorID
}
if ne.Game.PreviousEnactedPolicy == "-" {
g.PreviousEnactedPolicy = ""
} else if ne.Game.PreviousEnactedPolicy != "" {
g.PreviousEnactedPolicy = ne.Game.PreviousEnactedPolicy
}
if ne.Game.SpecialElectionPresidentID == "-" {
g.SpecialElectionPresidentID = ""
} else if ne.Game.SpecialElectionPresidentID != "" {
g.SpecialElectionPresidentID = ne.Game.SpecialElectionPresidentID
}
if ne.Game.SpecialElectionRoundID > 0 {
g.SpecialElectionRoundID = ne.Game.SpecialElectionRoundID
} else if ne.Game.SpecialElectionRoundID == -1 {
g.SpecialElectionRoundID = 0
}
//Round Updates
if ne.Game.Round.ID > 0 {
g.Round.ID = ne.Game.Round.ID
} else if ne.Game.Round.ID == -1 {
g.Round.ID = 0
}
if ne.Game.Round.State == "-" {
g.Round.State = ""
} else if ne.Game.Round.State != "" {
g.Round.State = ne.Game.Round.State
}
if ne.Game.Round.PresidentID == "-" {
g.Round.PresidentID = ""
} else if ne.Game.Round.PresidentID != "" {
g.Round.PresidentID = ne.Game.Round.PresidentID
}
if ne.Game.Round.ChancellorID == "-" {
g.Round.ChancellorID = ""
} else if ne.Game.Round.ChancellorID != "" {
g.Round.ChancellorID = ne.Game.Round.ChancellorID
}
if ne.Game.Round.EnactedPolicy == "-" {
g.Round.EnactedPolicy = ""
} else if ne.Game.Round.EnactedPolicy != "" {
g.Round.EnactedPolicy = ne.Game.Round.EnactedPolicy
}
if ne.Game.Round.ExecutiveAction == "-" {
g.Round.ExecutiveAction = ""
} else if ne.Game.Round.ExecutiveAction != "" {
g.Round.ExecutiveAction = ne.Game.Round.ExecutiveAction
}
if len(ne.Game.Round.Votes) == 1 && ne.Game.Round.Votes[0].PlayerID == "-" {
g.Round.Votes = []Vote{}
} else if len(ne.Game.Round.Votes) > 0 {
g.Round.Votes = ne.Game.Round.Votes
}
if len(ne.Game.Round.Policies) == 1 && ne.Game.Round.Policies[0] == "-" {
g.Round.Policies = []string{}
} else if len(ne.Game.Round.Policies) > 0 {
g.Round.Policies = ne.Game.Round.Policies
}
}
return g, e, nil
}