@@ -9,93 +9,151 @@ import (
9
9
"time"
10
10
)
11
11
12
+ // CardType is one of yellow or red
12
13
type CardType string
13
- type CardOperation string
14
- type RefCommand string
15
- type ModifyType string
16
- type StageOperation string
17
- type TriggerType string
18
14
19
15
const (
16
+ // CardTypeYellow yellow card
20
17
CardTypeYellow CardType = "yellow"
21
- CardTypeRed CardType = "red"
18
+ // CardTypeRed red card
19
+ CardTypeRed CardType = "red"
20
+ )
21
+
22
+ // CardOperation on a card
23
+ type CardOperation string
22
24
23
- CardOperationAdd CardOperation = "add"
25
+ const (
26
+ // CardOperationAdd add a card
27
+ CardOperationAdd CardOperation = "add"
28
+ // CardOperationRevoke revoke a card
24
29
CardOperationRevoke CardOperation = "revoke"
30
+ // CardOperationModify modify a card
25
31
CardOperationModify CardOperation = "modify"
32
+ )
26
33
27
- CommandHalt RefCommand = "halt"
28
- CommandStop RefCommand = "stop"
29
- CommandNormalStart RefCommand = "normalStart"
30
- CommandForceStart RefCommand = "forceStart"
31
- CommandDirect RefCommand = "direct"
32
- CommandIndirect RefCommand = "indirect"
33
- CommandKickoff RefCommand = "kickoff"
34
- CommandPenalty RefCommand = "penalty"
35
- CommandTimeout RefCommand = "timeout"
34
+ // RefCommand is a command to be send to the teams
35
+ type RefCommand string
36
+
37
+ const (
38
+ // CommandHalt HALT
39
+ CommandHalt RefCommand = "halt"
40
+ // CommandStop STOP
41
+ CommandStop RefCommand = "stop"
42
+ // CommandNormalStart NORMAL_START
43
+ CommandNormalStart RefCommand = "normalStart"
44
+ // CommandForceStart FORCE_START
45
+ CommandForceStart RefCommand = "forceStart"
46
+ // CommandDirect DIRECT
47
+ CommandDirect RefCommand = "direct"
48
+ // CommandIndirect INDIRECT
49
+ CommandIndirect RefCommand = "indirect"
50
+ // CommandKickoff KICKOFF
51
+ CommandKickoff RefCommand = "kickoff"
52
+ // CommandPenalty PENALTY
53
+ CommandPenalty RefCommand = "penalty"
54
+ // CommandTimeout TIMEOUT
55
+ CommandTimeout RefCommand = "timeout"
56
+ // CommandBallPlacement BALL_PLACEMENT
36
57
CommandBallPlacement RefCommand = "ballPlacement"
37
- CommandGoal RefCommand = "goal"
38
-
39
- ModifyGoals ModifyType = "goals"
40
- ModifyGoalie ModifyType = "goalie"
41
- ModifyYellowCards ModifyType = "yellowCards"
42
- ModifyYellowCardTime ModifyType = "yellowCardTime"
43
- ModifyRedCards ModifyType = "redCards"
44
- ModifyTimeoutsLeft ModifyType = "timeoutsLeft"
58
+ // CommandGoal GOAL
59
+ CommandGoal RefCommand = "goal"
60
+ )
61
+
62
+ // ModifyType is something to be modified
63
+ type ModifyType string
64
+
65
+ const (
66
+ // ModifyGoals goals
67
+ ModifyGoals ModifyType = "goals"
68
+ // ModifyGoalie goalie
69
+ ModifyGoalie ModifyType = "goalie"
70
+ // ModifyYellowCards yellow cards
71
+ ModifyYellowCards ModifyType = "yellowCards"
72
+ // ModifyYellowCardTime yellow card time
73
+ ModifyYellowCardTime ModifyType = "yellowCardTime"
74
+ // ModifyRedCards red cards
75
+ ModifyRedCards ModifyType = "redCards"
76
+ // ModifyTimeoutsLeft number of timeouts left
77
+ ModifyTimeoutsLeft ModifyType = "timeoutsLeft"
78
+ // ModifyTimeoutTimeLeft timeout time left
45
79
ModifyTimeoutTimeLeft ModifyType = "timeoutTimeLeft"
46
- ModifyOnPositiveHalf ModifyType = "onPositiveHalf"
47
- ModifyTeamName ModifyType = "teamName"
80
+ // ModifyOnPositiveHalf on positive half?
81
+ ModifyOnPositiveHalf ModifyType = "onPositiveHalf"
82
+ // ModifyTeamName name of the team
83
+ ModifyTeamName ModifyType = "teamName"
84
+ )
48
85
49
- TriggerResetMatch TriggerType = "resetMatch"
50
- TriggerSwitchColor TriggerType = "switchColor"
51
- TriggerUndo TriggerType = "undo"
86
+ // StageOperation to apply on the current stage
87
+ type StageOperation string
52
88
53
- StageNext StageOperation = "next"
89
+ const (
90
+ // StageNext next stage
91
+ StageNext StageOperation = "next"
92
+ // StagePrevious previous stage
54
93
StagePrevious StageOperation = "previous"
55
94
)
56
95
96
+ // TriggerType is something that can be triggered
97
+ type TriggerType string
98
+
99
+ const (
100
+ // TriggerResetMatch reset match
101
+ TriggerResetMatch TriggerType = "resetMatch"
102
+ // TriggerSwitchColor switch color
103
+ TriggerSwitchColor TriggerType = "switchColor"
104
+ // TriggerUndo undo last action
105
+ TriggerUndo TriggerType = "undo"
106
+ )
107
+
108
+ // CardModification to apply to a card
57
109
type CardModification struct {
58
- CardId int `json:"cardId"`
110
+ CardID int `json:"cardId"`
59
111
TimeLeft time.Duration `json:"timeLeft"`
60
112
}
61
113
62
- type RefBoxEventCard struct {
114
+ // EventCard is an event that can be applied
115
+ type EventCard struct {
63
116
ForTeam Team `json:"forTeam"`
64
117
Type CardType `json:"cardType"`
65
118
Operation CardOperation `json:"operation"`
66
119
Modification CardModification `json:"modification"`
67
120
}
68
121
69
- type RefBoxEventCommand struct {
122
+ // EventCommand is an event that can be applied
123
+ type EventCommand struct {
70
124
ForTeam * Team `json:"forTeam"`
71
125
Type RefCommand `json:"commandType"`
72
126
}
73
127
74
- type RefBoxEventModifyValue struct {
128
+ // EventModifyValue is an event that can be applied
129
+ type EventModifyValue struct {
75
130
Type ModifyType `json:"modifyType"`
76
131
ForTeam Team `json:"forTeam"`
77
132
ValueStr * string `json:"valueStr"`
78
133
ValueInt * int `json:"valueInt"`
79
134
ValueBool * bool `json:"valueBool"`
80
135
}
81
136
82
- type RefBoxEventTrigger struct {
137
+ // EventTrigger is an event that can be applied
138
+ type EventTrigger struct {
83
139
Type TriggerType `json:"triggerType"`
84
140
}
85
141
86
- type RefBoxEventStage struct {
142
+ // EventStage is an event that can be applied
143
+ type EventStage struct {
87
144
StageOperation StageOperation `json:"stageOperation"`
88
145
}
89
146
90
- type RefBoxEvent struct {
91
- Card * RefBoxEventCard `json:"card"`
92
- Command * RefBoxEventCommand `json:"command"`
93
- Modify * RefBoxEventModifyValue `json:"modify"`
94
- Stage * RefBoxEventStage `json:"stage"`
95
- Trigger * RefBoxEventTrigger `json:"trigger"`
147
+ // Event holds all possible events. Only one at a time can be applied
148
+ type Event struct {
149
+ Card * EventCard `json:"card"`
150
+ Command * EventCommand `json:"command"`
151
+ Modify * EventModifyValue `json:"modify"`
152
+ Stage * EventStage `json:"stage"`
153
+ Trigger * EventTrigger `json:"trigger"`
96
154
}
97
155
98
- func processEvent (event * RefBoxEvent ) error {
156
+ func processEvent (event * Event ) error {
99
157
if event .Card != nil {
100
158
return processCard (event .Card )
101
159
} else if event .Command != nil {
@@ -107,10 +165,10 @@ func processEvent(event *RefBoxEvent) error {
107
165
} else if event .Trigger != nil {
108
166
return processTrigger (event .Trigger )
109
167
}
110
- return errors .New ("Unknown event. " )
168
+ return errors .New ("unknown event" )
111
169
}
112
170
113
- func processTrigger (t * RefBoxEventTrigger ) error {
171
+ func processTrigger (t * EventTrigger ) error {
114
172
if t .Type == TriggerResetMatch {
115
173
refBox .State = NewState (refBox .Config )
116
174
refBox .MatchTimeStart = time .Unix (0 , 0 )
@@ -127,12 +185,12 @@ func processTrigger(t *RefBoxEventTrigger) error {
127
185
return nil
128
186
}
129
187
130
- func processStage (s * RefBoxEventStage ) error {
188
+ func processStage (s * EventStage ) error {
131
189
if refBox .State .GameState != GameStateHalted && refBox .State .GameState != GameStateStopped {
132
190
return errors .New ("The game state must be halted or stopped to change the stage" )
133
191
}
134
192
135
- index , err := indexOfStage ( refBox .State .Stage )
193
+ index , err := refBox .State .Stage . index ( )
136
194
if err != nil {
137
195
return err
138
196
}
@@ -170,17 +228,8 @@ func processStage(s *RefBoxEventStage) error {
170
228
return nil
171
229
}
172
230
173
- func indexOfStage (stage Stage ) (int , error ) {
174
- for i , v := range Stages {
175
- if v == stage {
176
- return i , nil
177
- }
178
- }
179
- return 0 , errors .Errorf ("unknown stage: %v" , stage )
180
- }
181
-
182
- func processModify (m * RefBoxEventModifyValue ) error {
183
- if unknownTeam (m .ForTeam ) {
231
+ func processModify (m * EventModifyValue ) error {
232
+ if m .ForTeam .Unknown () {
184
233
return errors .Errorf ("Unknown team: %v" , m .ForTeam )
185
234
}
186
235
teamState := refBox .State .TeamState [m .ForTeam ]
@@ -238,7 +287,7 @@ func processModify(m *RefBoxEventModifyValue) error {
238
287
return nil
239
288
}
240
289
241
- func processCommand (c * RefBoxEventCommand ) error {
290
+ func processCommand (c * EventCommand ) error {
242
291
switch c .Type {
243
292
case CommandHalt :
244
293
refBox .State .GameState = GameStateHalted
@@ -287,7 +336,7 @@ func processCommand(c *RefBoxEventCommand) error {
287
336
return nil
288
337
}
289
338
290
- func processCard (card * RefBoxEventCard ) error {
339
+ func processCard (card * EventCard ) error {
291
340
if card .ForTeam != TeamYellow && card .ForTeam != TeamBlue {
292
341
return errors .Errorf ("Unknown team: %v" , card .ForTeam )
293
342
}
@@ -305,19 +354,19 @@ func processCard(card *RefBoxEventCard) error {
305
354
return errors .Errorf ("Unknown operation: %v" , card .Operation )
306
355
}
307
356
308
- func modifyCard (card * RefBoxEventCard , teamState * TeamInfo ) error {
357
+ func modifyCard (card * EventCard , teamState * TeamInfo ) error {
309
358
if card .Type == CardTypeRed {
310
359
return errors .New ("Red cards can not be modified" )
311
360
}
312
361
nCardTimes := len (teamState .YellowCardTimes )
313
- if card .Modification .CardId >= nCardTimes {
314
- return errors .Errorf ("Invalid card id %v. Only %v card times available" , card .Modification .CardId , nCardTimes )
362
+ if card .Modification .CardID >= nCardTimes {
363
+ return errors .Errorf ("Invalid card id %v. Only %v card times available" , card .Modification .CardID , nCardTimes )
315
364
}
316
- teamState .YellowCardTimes [card .Modification .CardId ] = card .Modification .TimeLeft
365
+ teamState .YellowCardTimes [card .Modification .CardID ] = card .Modification .TimeLeft
317
366
return nil
318
367
}
319
368
320
- func addCard (card * RefBoxEventCard , teamState * TeamInfo ) error {
369
+ func addCard (card * EventCard , teamState * TeamInfo ) error {
321
370
if card .Type == CardTypeYellow {
322
371
log .Printf ("Add yellow card for team %v" , card .ForTeam )
323
372
teamState .YellowCards ++
@@ -329,7 +378,7 @@ func addCard(card *RefBoxEventCard, teamState *TeamInfo) error {
329
378
return nil
330
379
}
331
380
332
- func revokeCard (card * RefBoxEventCard , teamState * TeamInfo ) error {
381
+ func revokeCard (card * EventCard , teamState * TeamInfo ) error {
333
382
if card .Type == CardTypeYellow {
334
383
if teamState .YellowCards > 0 {
335
384
log .Printf ("Revoke yellow card for team %v" , card .ForTeam )
@@ -353,8 +402,9 @@ func revokeCard(card *RefBoxEventCard, teamState *TeamInfo) error {
353
402
return nil
354
403
}
355
404
356
- func unknownTeam (team Team ) bool {
357
- return team != "Yellow" && team != "Blue"
405
+ // Unknown returns true if the team is not blue or yellow
406
+ func (t Team ) Unknown () bool {
407
+ return t != "Yellow" && t != "Blue"
358
408
}
359
409
360
410
func strToDuration (s string ) (duration time.Duration , err error ) {
@@ -389,14 +439,14 @@ func strToDuration(s string) (duration time.Duration, err error) {
389
439
return
390
440
}
391
441
392
- func (c RefBoxEventCommand ) String () string {
442
+ func (c EventCommand ) String () string {
393
443
if c .ForTeam == nil {
394
444
return string (c .Type )
395
445
}
396
446
return fmt .Sprintf ("%v for %v" , c .Type , * c .ForTeam )
397
447
}
398
448
399
- func (m RefBoxEventModifyValue ) String () string {
449
+ func (m EventModifyValue ) String () string {
400
450
str := fmt .Sprintf ("%v for %v" , m .Type , m .ForTeam )
401
451
if m .ValueBool != nil {
402
452
if * m .ValueBool {
@@ -413,3 +463,12 @@ func (m RefBoxEventModifyValue) String() string {
413
463
}
414
464
return str
415
465
}
466
+
467
+ func (s Stage ) index () (int , error ) {
468
+ for i , v := range Stages {
469
+ if v == s {
470
+ return i , nil
471
+ }
472
+ }
473
+ return 0 , errors .Errorf ("unknown stage: %v" , s )
474
+ }
0 commit comments