Move ControlPanel to within RealtimeScore to reduce complexity.

This commit is contained in:
Patrick Fairbank
2020-04-04 15:52:49 -07:00
parent a6109a978a
commit 0ed6f771d5
6 changed files with 12 additions and 46 deletions

View File

@@ -77,8 +77,6 @@ type Arena struct {
MuteMatchSounds bool MuteMatchSounds bool
matchAborted bool matchAborted bool
soundsPlayed map[*game.MatchSound]struct{} soundsPlayed map[*game.MatchSound]struct{}
RedControlPanel *game.ControlPanel
BlueControlPanel *game.ControlPanel
} }
type AllianceStation struct { type AllianceStation struct {
@@ -216,13 +214,10 @@ func (arena *Arena) LoadMatch(match *model.Match) error {
arena.FieldVolunteers = false arena.FieldVolunteers = false
arena.FieldReset = false arena.FieldReset = false
arena.ScoringPanelRegistry.resetScoreCommitted() arena.ScoringPanelRegistry.resetScoreCommitted()
arena.RedControlPanel = new(game.ControlPanel)
arena.BlueControlPanel = new(game.ControlPanel)
// Notify any listeners about the new match. // Notify any listeners about the new match.
arena.MatchLoadNotifier.Notify() arena.MatchLoadNotifier.Notify()
arena.RealtimeScoreNotifier.Notify() arena.RealtimeScoreNotifier.Notify()
arena.ControlPanelColorNotifier.Notify()
arena.AllianceStationDisplayMode = "match" arena.AllianceStationDisplayMode = "match"
arena.AllianceStationDisplayModeNotifier.Notify() arena.AllianceStationDisplayModeNotifier.Notify()
@@ -772,8 +767,8 @@ func (arena *Arena) handlePlcInput() {
blueScore.StageAtCapacity(game.Stage3, arena.MatchState >= TeleopPeriod) && blueScore.StageAtCapacity(game.Stage3, arena.MatchState >= TeleopPeriod) &&
blueScore.Stage3TargetColor == game.ColorUnknown { blueScore.Stage3TargetColor == game.ColorUnknown {
// Determine the position control target colors and send packets to inform the driver stations. // Determine the position control target colors and send packets to inform the driver stations.
redScore.Stage3TargetColor = arena.RedControlPanel.GetStage3TargetColor() redScore.Stage3TargetColor = arena.RedRealtimeScore.ControlPanel.GetStage3TargetColor()
blueScore.Stage3TargetColor = arena.BlueControlPanel.GetStage3TargetColor() blueScore.Stage3TargetColor = arena.BlueRealtimeScore.ControlPanel.GetStage3TargetColor()
arena.sendGameDataPacket(redScore.Stage3TargetColor, "R1", "R2", "R3") arena.sendGameDataPacket(redScore.Stage3TargetColor, "R1", "R2", "R3")
arena.sendGameDataPacket(blueScore.Stage3TargetColor, "B1", "B2", "B3") arena.sendGameDataPacket(blueScore.Stage3TargetColor, "B1", "B2", "B3")
} }

View File

@@ -30,7 +30,6 @@ type ArenaNotifiers struct {
ReloadDisplaysNotifier *websocket.Notifier ReloadDisplaysNotifier *websocket.Notifier
ScorePostedNotifier *websocket.Notifier ScorePostedNotifier *websocket.Notifier
ScoringStatusNotifier *websocket.Notifier ScoringStatusNotifier *websocket.Notifier
ControlPanelColorNotifier *websocket.Notifier
} }
type MatchTimeMessage struct { type MatchTimeMessage struct {
@@ -41,6 +40,7 @@ type MatchTimeMessage struct {
type audienceAllianceScoreFields struct { type audienceAllianceScoreFields struct {
Score *game.Score Score *game.Score
ScoreSummary *game.ScoreSummary ScoreSummary *game.ScoreSummary
ControlPanel *game.ControlPanel
} }
// Instantiates notifiers and configures their message producing methods. // Instantiates notifiers and configures their message producing methods.
@@ -63,7 +63,6 @@ func (arena *Arena) configureNotifiers() {
arena.ReloadDisplaysNotifier = websocket.NewNotifier("reload", nil) arena.ReloadDisplaysNotifier = websocket.NewNotifier("reload", nil)
arena.ScorePostedNotifier = websocket.NewNotifier("scorePosted", arena.generateScorePostedMessage) arena.ScorePostedNotifier = websocket.NewNotifier("scorePosted", arena.generateScorePostedMessage)
arena.ScoringStatusNotifier = websocket.NewNotifier("scoringStatus", arena.generateScoringStatusMessage) arena.ScoringStatusNotifier = websocket.NewNotifier("scoringStatus", arena.generateScoringStatusMessage)
arena.ControlPanelColorNotifier = websocket.NewNotifier("controlPanelColor", arena.generateControlPanelColorMessage)
} }
func (arena *Arena) generateAllianceSelectionMessage() interface{} { func (arena *Arena) generateAllianceSelectionMessage() interface{} {
@@ -236,19 +235,13 @@ func (arena *Arena) generateScoringStatusMessage() interface{} {
arena.ScoringPanelRegistry.GetNumPanels("blue"), arena.ScoringPanelRegistry.GetNumScoreCommitted("blue")} arena.ScoringPanelRegistry.GetNumPanels("blue"), arena.ScoringPanelRegistry.GetNumScoreCommitted("blue")}
} }
func (arena *Arena) generateControlPanelColorMessage() interface{} {
return &struct {
RedControlPanelColor game.ControlPanelColor
BlueControlPanelColor game.ControlPanelColor
}{arena.RedControlPanel.CurrentColor, arena.BlueControlPanel.CurrentColor}
}
// Constructs the data object for one alliance sent to the audience display for the realtime scoring overlay. // Constructs the data object for one alliance sent to the audience display for the realtime scoring overlay.
func getAudienceAllianceScoreFields(allianceScore *RealtimeScore, func getAudienceAllianceScoreFields(allianceScore *RealtimeScore,
allianceScoreSummary *game.ScoreSummary) *audienceAllianceScoreFields { allianceScoreSummary *game.ScoreSummary) *audienceAllianceScoreFields {
fields := new(audienceAllianceScoreFields) fields := new(audienceAllianceScoreFields)
fields.Score = &allianceScore.CurrentScore fields.Score = &allianceScore.CurrentScore
fields.ScoreSummary = allianceScoreSummary fields.ScoreSummary = allianceScoreSummary
fields.ControlPanel = &allianceScore.ControlPanel
return fields return fields
} }

View File

@@ -11,6 +11,7 @@ type RealtimeScore struct {
CurrentScore game.Score CurrentScore game.Score
Cards map[string]string Cards map[string]string
FoulsCommitted bool FoulsCommitted bool
ControlPanel game.ControlPanel
} }
func NewRealtimeScore() *RealtimeScore { func NewRealtimeScore() *RealtimeScore {

View File

@@ -85,22 +85,11 @@ var handleRealtimeScore = function(data) {
} }
$("#rungIsLevel>.value").text(score.RungIsLevel ? "Yes" : "No"); $("#rungIsLevel>.value").text(score.RungIsLevel ? "Yes" : "No");
$("#rungIsLevel").attr("data-value", score.RungIsLevel); $("#rungIsLevel").attr("data-value", score.RungIsLevel);
$("#controlPanelColor>.value").text(getControlPanelColorText(realtimeScore.ControlPanel.CurrentColor));
$("#controlPanelColor").attr("data-value", realtimeScore.ControlPanel.CurrentColor);
$("#controlPanelColor").attr("data-control-panel-status", score.ControlPanelStatus) $("#controlPanelColor").attr("data-control-panel-status", score.ControlPanelStatus)
}; };
// Handles a websocket message to update the Control Panel color.
var handleControlPanelColor = function(data) {
var color;
if (alliance === "red") {
color = data.RedControlPanelColor;
} else {
color = data.BlueControlPanelColor;
}
$("#controlPanelColor>.value").text(getControlPanelColorText(color));
$("#controlPanelColor").attr("data-value", color);
};
// Handles a keyboard event and sends the appropriate websocket message. // Handles a keyboard event and sends the appropriate websocket message.
var handleKeyPress = function(event) { var handleKeyPress = function(event) {
websocket.send(String.fromCharCode(event.keyCode)); websocket.send(String.fromCharCode(event.keyCode));
@@ -164,7 +153,6 @@ $(function() {
matchLoad: function(event) { handleMatchLoad(event.data); }, matchLoad: function(event) { handleMatchLoad(event.data); },
matchTime: function(event) { handleMatchTime(event.data); }, matchTime: function(event) { handleMatchTime(event.data); },
realtimeScore: function(event) { handleRealtimeScore(event.data); }, realtimeScore: function(event) { handleRealtimeScore(event.data); },
controlPanelColor: function(event) { handleControlPanelColor(event.data); }
}); });
$(document).keypress(handleKeyPress); $(document).keypress(handleKeyPress);

View File

@@ -82,7 +82,7 @@ func (web *Web) scoringPanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
// Subscribe the websocket to the notifiers whose messages will be passed on to the client, in a separate goroutine. // Subscribe the websocket to the notifiers whose messages will be passed on to the client, in a separate goroutine.
go ws.HandleNotifiers(web.arena.MatchLoadNotifier, web.arena.MatchTimeNotifier, web.arena.RealtimeScoreNotifier, go ws.HandleNotifiers(web.arena.MatchLoadNotifier, web.arena.MatchTimeNotifier, web.arena.RealtimeScoreNotifier,
web.arena.ControlPanelColorNotifier, web.arena.ReloadDisplaysNotifier) web.arena.ReloadDisplaysNotifier)
// Loop, waiting for commands and responding to them, until the client closes the connection. // Loop, waiting for commands and responding to them, until the client closes the connection.
for { for {
@@ -192,17 +192,12 @@ func (web *Web) scoringPanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
scoreChanged = true scoreChanged = true
case "K": case "K":
if score.ControlPanelStatus == game.ControlPanelRotation { if score.ControlPanelStatus == game.ControlPanelRotation {
var controlPanel *game.ControlPanel controlPanel := &(*realtimeScore).ControlPanel
if alliance == "red" {
controlPanel = web.arena.RedControlPanel
} else {
controlPanel = web.arena.BlueControlPanel
}
controlPanel.CurrentColor++ controlPanel.CurrentColor++
if controlPanel.CurrentColor == 5 { if controlPanel.CurrentColor == 5 {
controlPanel.CurrentColor = 1 controlPanel.CurrentColor = 1
} }
web.arena.ControlPanelColorNotifier.Notify() scoreChanged = true
} }
case "P": case "P":
if score.ControlPanelStatus == game.ControlPanelPosition { if score.ControlPanelStatus == game.ControlPanelPosition {

View File

@@ -50,11 +50,9 @@ func TestScoringPanelWebsocket(t *testing.T) {
readWebsocketType(t, redWs, "matchLoad") readWebsocketType(t, redWs, "matchLoad")
readWebsocketType(t, redWs, "matchTime") readWebsocketType(t, redWs, "matchTime")
readWebsocketType(t, redWs, "realtimeScore") readWebsocketType(t, redWs, "realtimeScore")
readWebsocketType(t, redWs, "controlPanelColor")
readWebsocketType(t, blueWs, "matchLoad") readWebsocketType(t, blueWs, "matchLoad")
readWebsocketType(t, blueWs, "matchTime") readWebsocketType(t, blueWs, "matchTime")
readWebsocketType(t, blueWs, "realtimeScore") readWebsocketType(t, blueWs, "realtimeScore")
readWebsocketType(t, blueWs, "controlPanelColor")
// Send some autonomous period scoring commands. // Send some autonomous period scoring commands.
web.arena.MatchState = field.AutoPeriod web.arena.MatchState = field.AutoPeriod
@@ -109,13 +107,9 @@ func TestScoringPanelWebsocket(t *testing.T) {
web.arena.ResetMatch() web.arena.ResetMatch()
web.arena.LoadTestMatch() web.arena.LoadTestMatch()
readWebsocketType(t, redWs, "matchLoad") readWebsocketType(t, redWs, "matchLoad")
messages := readWebsocketMultiple(t, redWs, 2) readWebsocketType(t, redWs, "realtimeScore")
assert.Contains(t, messages, "realtimeScore")
assert.Contains(t, messages, "controlPanelColor")
readWebsocketType(t, blueWs, "matchLoad") readWebsocketType(t, blueWs, "matchLoad")
messages = readWebsocketMultiple(t, blueWs, 2) readWebsocketType(t, blueWs, "realtimeScore")
assert.Contains(t, messages, "realtimeScore")
assert.Contains(t, messages, "controlPanelColor")
assert.Equal(t, field.NewRealtimeScore(), web.arena.RedRealtimeScore) assert.Equal(t, field.NewRealtimeScore(), web.arena.RedRealtimeScore)
assert.Equal(t, field.NewRealtimeScore(), web.arena.BlueRealtimeScore) assert.Equal(t, field.NewRealtimeScore(), web.arena.BlueRealtimeScore)
assert.Equal(t, 0, web.arena.ScoringPanelRegistry.GetNumScoreCommitted("red")) assert.Equal(t, 0, web.arena.ScoringPanelRegistry.GetNumScoreCommitted("red"))