Skip to content

Commit 27c38f7

Browse files
committed
Refactor websocket model to reduce duplicated code.
1 parent b49a86b commit 27c38f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1186
-1615
lines changed

coverage

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
go test -coverprofile=coverage.out && sleep 1 && go tool cover -html=coverage.out
1+
go test -coverprofile=coverage.out ./... && sleep 1 && go tool cover -html=coverage.out

field/arena.go

+66-111
Original file line numberDiff line numberDiff line change
@@ -46,58 +46,37 @@ type Arena struct {
4646
TbaClient *partner.TbaClient
4747
StemTvClient *partner.StemTvClient
4848
AllianceStations map[string]*AllianceStation
49-
CurrentMatch *model.Match
49+
ArenaNotifiers
5050
MatchState
51-
lastMatchState MatchState
52-
MatchStartTime time.Time
53-
LastMatchTimeSec float64
54-
RedRealtimeScore *RealtimeScore
55-
BlueRealtimeScore *RealtimeScore
56-
lastDsPacketTime time.Time
57-
FieldVolunteers bool
58-
FieldReset bool
59-
AudienceDisplayScreen string
60-
SavedMatch *model.Match
61-
SavedMatchResult *model.MatchResult
62-
AllianceStationDisplays map[string]string
63-
AllianceStationDisplayScreen string
64-
MuteMatchSounds bool
65-
matchAborted bool
66-
matchStateNotifier *Notifier
67-
MatchTimeNotifier *Notifier
68-
RobotStatusNotifier *Notifier
69-
MatchLoadTeamsNotifier *Notifier
70-
ScoringStatusNotifier *Notifier
71-
RealtimeScoreNotifier *Notifier
72-
ScorePostedNotifier *Notifier
73-
AudienceDisplayNotifier *Notifier
74-
PlaySoundNotifier *Notifier
75-
AllianceStationDisplayNotifier *Notifier
76-
AllianceSelectionNotifier *Notifier
77-
LowerThirdNotifier *Notifier
78-
ReloadDisplaysNotifier *Notifier
79-
Scale *game.Seesaw
80-
RedSwitch *game.Seesaw
81-
BlueSwitch *game.Seesaw
82-
RedVault *game.Vault
83-
BlueVault *game.Vault
84-
ScaleLeds led.Controller
85-
RedSwitchLeds led.Controller
86-
BlueSwitchLeds led.Controller
87-
RedVaultLeds vaultled.Controller
88-
BlueVaultLeds vaultled.Controller
89-
warmupLedMode led.Mode
90-
lastRedAllianceReady bool
91-
lastBlueAllianceReady bool
92-
}
93-
94-
type ArenaStatus struct {
95-
AllianceStations map[string]*AllianceStation
96-
MatchState
97-
CanStartMatch bool
98-
PlcIsHealthy bool
99-
FieldEstop bool
100-
GameSpecificData string
51+
lastMatchState MatchState
52+
CurrentMatch *model.Match
53+
MatchStartTime time.Time
54+
LastMatchTimeSec float64
55+
RedRealtimeScore *RealtimeScore
56+
BlueRealtimeScore *RealtimeScore
57+
lastDsPacketTime time.Time
58+
FieldVolunteers bool
59+
FieldReset bool
60+
AudienceDisplayMode string
61+
SavedMatch *model.Match
62+
SavedMatchResult *model.MatchResult
63+
AllianceStationDisplays map[string]string
64+
AllianceStationDisplayMode string
65+
MuteMatchSounds bool
66+
matchAborted bool
67+
Scale *game.Seesaw
68+
RedSwitch *game.Seesaw
69+
BlueSwitch *game.Seesaw
70+
RedVault *game.Vault
71+
BlueVault *game.Vault
72+
ScaleLeds led.Controller
73+
RedSwitchLeds led.Controller
74+
BlueSwitchLeds led.Controller
75+
RedVaultLeds vaultled.Controller
76+
BlueVaultLeds vaultled.Controller
77+
warmupLedMode led.Mode
78+
lastRedAllianceReady bool
79+
lastBlueAllianceReady bool
10180
}
10281

10382
type AllianceStation struct {
@@ -130,32 +109,20 @@ func NewArena(dbPath string) (*Arena, error) {
130109
arena.AllianceStations["B2"] = new(AllianceStation)
131110
arena.AllianceStations["B3"] = new(AllianceStation)
132111

133-
arena.matchStateNotifier = NewNotifier()
134-
arena.MatchTimeNotifier = NewNotifier()
135-
arena.RobotStatusNotifier = NewNotifier()
136-
arena.MatchLoadTeamsNotifier = NewNotifier()
137-
arena.ScoringStatusNotifier = NewNotifier()
138-
arena.RealtimeScoreNotifier = NewNotifier()
139-
arena.ScorePostedNotifier = NewNotifier()
140-
arena.AudienceDisplayNotifier = NewNotifier()
141-
arena.PlaySoundNotifier = NewNotifier()
142-
arena.AllianceStationDisplayNotifier = NewNotifier()
143-
arena.AllianceSelectionNotifier = NewNotifier()
144-
arena.LowerThirdNotifier = NewNotifier()
145-
arena.ReloadDisplaysNotifier = NewNotifier()
112+
arena.configureNotifiers()
146113

147114
// Load empty match as current.
148115
arena.MatchState = PreMatch
149116
arena.LoadTestMatch()
150-
arena.lastMatchState = -1
151117
arena.LastMatchTimeSec = 0
118+
arena.lastMatchState = -1
152119

153120
// Initialize display parameters.
154-
arena.AudienceDisplayScreen = "blank"
121+
arena.AudienceDisplayMode = "blank"
155122
arena.SavedMatch = &model.Match{}
156123
arena.SavedMatchResult = model.NewMatchResult()
157124
arena.AllianceStationDisplays = make(map[string]string)
158-
arena.AllianceStationDisplayScreen = "match"
125+
arena.AllianceStationDisplayMode = "match"
159126

160127
return arena, nil
161128
}
@@ -257,10 +224,10 @@ func (arena *Arena) LoadMatch(match *model.Match) error {
257224
arena.BlueSwitchLeds.SetSidedness(true)
258225

259226
// Notify any listeners about the new match.
260-
arena.MatchLoadTeamsNotifier.Notify(nil)
261-
arena.RealtimeScoreNotifier.Notify(nil)
262-
arena.AllianceStationDisplayScreen = "match"
263-
arena.AllianceStationDisplayNotifier.Notify(nil)
227+
arena.MatchLoadNotifier.Notify()
228+
arena.RealtimeScoreNotifier.Notify()
229+
arena.AllianceStationDisplayMode = "match"
230+
arena.AllianceStationDisplayModeNotifier.Notify()
264231

265232
// Set the initial state of the lights.
266233
arena.ScaleLeds.SetMode(led.OffMode, led.OffMode)
@@ -325,7 +292,7 @@ func (arena *Arena) SubstituteTeam(teamId int, station string) error {
325292
arena.CurrentMatch.Blue3 = teamId
326293
}
327294
arena.setupNetwork()
328-
arena.MatchLoadTeamsNotifier.Notify(nil)
295+
arena.MatchLoadNotifier.Notify()
329296

330297
if arena.CurrentMatch.Type != "test" {
331298
arena.Database.SaveMatch(arena.CurrentMatch)
@@ -379,12 +346,14 @@ func (arena *Arena) AbortMatch() error {
379346
return fmt.Errorf("Cannot abort match when it is not in progress.")
380347
}
381348
if !arena.MuteMatchSounds && arena.MatchState != WarmupPeriod {
382-
arena.PlaySoundNotifier.Notify("match-abort")
349+
arena.PlaySoundNotifier.NotifyWithMessage("match-abort")
383350
}
384351
arena.MatchState = PostMatch
385352
arena.matchAborted = true
386-
arena.AudienceDisplayScreen = "blank"
387-
arena.AudienceDisplayNotifier.Notify(nil)
353+
arena.AudienceDisplayMode = "blank"
354+
arena.AudienceDisplayModeNotifier.Notify()
355+
arena.AllianceStationDisplayMode = "logo"
356+
arena.AllianceStationDisplayModeNotifier.Notify()
388357
return nil
389358
}
390359

@@ -432,11 +401,13 @@ func (arena *Arena) Update() {
432401
arena.LastMatchTimeSec = -1
433402
auto = true
434403
enabled = false
435-
arena.AudienceDisplayScreen = "match"
436-
arena.AudienceDisplayNotifier.Notify(nil)
404+
arena.AudienceDisplayMode = "match"
405+
arena.AudienceDisplayModeNotifier.Notify()
406+
arena.AllianceStationDisplayMode = "match"
407+
arena.AllianceStationDisplayModeNotifier.Notify()
437408
arena.sendGameSpecificDataPacket()
438409
if !arena.MuteMatchSounds {
439-
arena.PlaySoundNotifier.Notify("match-warmup")
410+
arena.PlaySoundNotifier.NotifyWithMessage("match-warmup")
440411
}
441412
// Pick an LED warmup mode at random to keep things interesting.
442413
allWarmupModes := []led.Mode{led.WarmupMode, led.Warmup2Mode, led.Warmup3Mode, led.Warmup4Mode}
@@ -450,7 +421,7 @@ func (arena *Arena) Update() {
450421
enabled = true
451422
sendDsPacket = true
452423
if !arena.MuteMatchSounds {
453-
arena.PlaySoundNotifier.Notify("match-start")
424+
arena.PlaySoundNotifier.NotifyWithMessage("match-start")
454425
}
455426
}
456427
case AutoPeriod:
@@ -462,7 +433,7 @@ func (arena *Arena) Update() {
462433
enabled = false
463434
sendDsPacket = true
464435
if !arena.MuteMatchSounds {
465-
arena.PlaySoundNotifier.Notify("match-end")
436+
arena.PlaySoundNotifier.NotifyWithMessage("match-end")
466437
}
467438
}
468439
case PausePeriod:
@@ -475,7 +446,7 @@ func (arena *Arena) Update() {
475446
enabled = true
476447
sendDsPacket = true
477448
if !arena.MuteMatchSounds {
478-
arena.PlaySoundNotifier.Notify("match-resume")
449+
arena.PlaySoundNotifier.NotifyWithMessage("match-resume")
479450
}
480451
}
481452
case TeleopPeriod:
@@ -486,7 +457,7 @@ func (arena *Arena) Update() {
486457
arena.MatchState = EndgamePeriod
487458
sendDsPacket = false
488459
if !arena.MuteMatchSounds {
489-
arena.PlaySoundNotifier.Notify("match-endgame")
460+
arena.PlaySoundNotifier.NotifyWithMessage("match-endgame")
490461
}
491462
}
492463
case EndgamePeriod:
@@ -501,38 +472,32 @@ func (arena *Arena) Update() {
501472
go func() {
502473
// Leave the scores on the screen briefly at the end of the match.
503474
time.Sleep(time.Second * matchEndScoreDwellSec)
504-
arena.AudienceDisplayScreen = "blank"
505-
arena.AudienceDisplayNotifier.Notify(nil)
506-
arena.AllianceStationDisplayScreen = "logo"
507-
arena.AllianceStationDisplayNotifier.Notify(nil)
475+
arena.AudienceDisplayMode = "blank"
476+
arena.AudienceDisplayModeNotifier.Notify()
477+
arena.AllianceStationDisplayMode = "logo"
478+
arena.AllianceStationDisplayModeNotifier.Notify()
508479
}()
509480
if !arena.MuteMatchSounds {
510-
arena.PlaySoundNotifier.Notify("match-end")
481+
arena.PlaySoundNotifier.NotifyWithMessage("match-end")
511482
}
512483
}
513484
}
514485

515-
// Send a notification if the match state has changed.
516-
if arena.MatchState != arena.lastMatchState {
517-
arena.matchStateNotifier.Notify(arena.MatchState)
518-
}
519-
arena.lastMatchState = arena.MatchState
520-
521-
// Send a match tick notification if passing an integer second threshold.
522-
if int(matchTimeSec) != int(arena.LastMatchTimeSec) {
523-
arena.MatchTimeNotifier.Notify(int(matchTimeSec))
486+
// Send a match tick notification if passing an integer second threshold or if the match state changed.
487+
if int(matchTimeSec) != int(arena.LastMatchTimeSec) || arena.MatchState != arena.lastMatchState {
488+
arena.MatchTimeNotifier.Notify()
524489
}
525490
arena.LastMatchTimeSec = matchTimeSec
491+
arena.lastMatchState = arena.MatchState
526492

527493
// Send a packet if at a period transition point or if it's been long enough since the last one.
528494
if sendDsPacket || time.Since(arena.lastDsPacketTime).Seconds()*1000 >= dsPacketPeriodMs {
529495
arena.sendDsPacket(auto, enabled)
530-
arena.RobotStatusNotifier.Notify(nil)
496+
arena.ArenaStatusNotifier.Notify()
531497
}
532498

533499
// Handle field sensors/lights/motors.
534500
arena.handlePlcInput()
535-
arena.handlePlcOutput()
536501
arena.handleLeds()
537502
}
538503

@@ -560,11 +525,6 @@ func (arena *Arena) BlueScoreSummary() *game.ScoreSummary {
560525
return arena.BlueRealtimeScore.CurrentScore.Summarize(arena.RedRealtimeScore.CurrentScore.Fouls)
561526
}
562527

563-
func (arena *Arena) GetStatus() *ArenaStatus {
564-
return &ArenaStatus{arena.AllianceStations, arena.MatchState, arena.checkCanStartMatch() == nil,
565-
arena.Plc.IsHealthy, arena.Plc.GetFieldEstop(), arena.CurrentMatch.GameSpecificData}
566-
}
567-
568528
// Loads a team into an alliance station, cleaning up the previous team there if there is one.
569529
func (arena *Arena) assignTeam(teamId int, station string) error {
570530
// Reject invalid station values.
@@ -766,23 +726,18 @@ func (arena *Arena) handlePlcInput() {
766726
// Check if a power up has been newly played and trigger the accompanying sound effect if so.
767727
newRedPowerUp := arena.RedVault.CheckForNewlyPlayedPowerUp()
768728
if newRedPowerUp != "" && !arena.MuteMatchSounds {
769-
arena.PlaySoundNotifier.Notify("match-" + newRedPowerUp)
729+
arena.PlaySoundNotifier.NotifyWithMessage("match-" + newRedPowerUp)
770730
}
771731
newBluePowerUp := arena.BlueVault.CheckForNewlyPlayedPowerUp()
772732
if newBluePowerUp != "" && !arena.MuteMatchSounds {
773-
arena.PlaySoundNotifier.Notify("match-" + newBluePowerUp)
733+
arena.PlaySoundNotifier.NotifyWithMessage("match-" + newBluePowerUp)
774734
}
775735

776736
if !oldRedScore.Equals(redScore) || !oldBlueScore.Equals(blueScore) || ownershipChanged {
777-
arena.RealtimeScoreNotifier.Notify(nil)
737+
arena.RealtimeScoreNotifier.Notify()
778738
}
779739
}
780740

781-
// Writes light/motor commands to the field PLC.
782-
func (arena *Arena) handlePlcOutput() {
783-
// TODO(patrick): Update for 2018.
784-
}
785-
786741
func (arena *Arena) handleLeds() {
787742
switch arena.MatchState {
788743
case PreMatch:

0 commit comments

Comments
 (0)