Add realtime scoring API

This commit is contained in:
Ken Schenke
2020-04-16 19:37:54 -05:00
parent 2b414bcc69
commit 6375862b4f
3 changed files with 94 additions and 1 deletions

View File

@@ -250,7 +250,25 @@ var handleMatchTime = function(data) {
var handleRealtimeScore = function(data) {
$("#redScore").text(data.Red.ScoreSummary.Score);
$("#blueScore").text(data.Blue.ScoreSummary.Score);
};
if (parseInt($("#redAutoScore").val()) != data.Red.Score.AutoPoints) {
$("#redAutoScore").val(data.Red.Score.AutoPoints);
}
if (parseInt($("#redTeleopScore").val()) != data.Red.Score.TeleopPoints) {
$("#redTeleopScore").val(data.Red.Score.TeleopPoints);
}
if (parseInt($("#redEndgameScore").val()) != data.Red.Score.EndgamePoints) {
$("#redEndgameScore").val(data.Red.Score.EndgamePoints);
}
if (parseInt($("#blueAutoScore").val()) != data.Blue.Score.AutoPoints) {
$("#blueAutoScore").val(data.Blue.Score.AutoPoints);
}
if (parseInt($("#blueTeleopScore").val()) != data.Blue.Score.TeleopPoints) {
$("#blueTeleopScore").val(data.Blue.Score.TeleopPoints);
}
if (parseInt($("#blueEndgameScore").val()) != data.Blue.Score.EndgamePoints) {
$("#blueEndgameScore").val(data.Blue.Score.EndgamePoints);
}
}
// Handles a websocket message to update the audience display screen selector.
var handleAudienceDisplayMode = function(data) {

73
web/scores.go Normal file
View File

@@ -0,0 +1,73 @@
// Copyright 2020 Team 254. All Rights Reserved.
// Author: kenschenke@gmail.com (Ken Schenke)
//
// Web handlers for handling realtime scores API.
package web
import (
"encoding/json"
"fmt"
"github.com/Team254/cheesy-arena/field"
"github.com/Team254/cheesy-arena/game"
"io/ioutil"
"net/http"
)
type jsonAllianceScore struct {
Auto int `json:"auto"`
Teleop int `json:"teleop"`
Endgame int `json:"endgame"`
}
type jsonScore struct {
Red jsonAllianceScore `json:"red"`
Blue jsonAllianceScore `json:"blue"`
}
func (web *Web) getScoresHandler(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(jsonScore{
Red: jsonAllianceScore{
Auto: web.arena.RedScore.AutoPoints,
Teleop: web.arena.RedScore.TeleopPoints,
Endgame: web.arena.RedScore.EndgamePoints,
},
Blue: jsonAllianceScore{
Auto: web.arena.BlueScore.AutoPoints,
Teleop: web.arena.BlueScore.TeleopPoints,
Endgame: web.arena.BlueScore.EndgamePoints,
},
})
}
func (web *Web) setScoresHandler(w http.ResponseWriter, r *http.Request) {
if web.arena.MatchState == field.PreMatch || web.arena.MatchState == field.TimeoutActive || web.arena.MatchState == field.PostTimeout {
fmt.Fprintf(w, "Score cannot be updated in this match state")
w.WriteHeader(http.StatusNotAcceptable)
return
}
var scores jsonScore
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Score data missing")
w.WriteHeader(http.StatusNoContent)
return
}
json.Unmarshal(reqBody, &scores)
if r.Method == "PUT" {
web.arena.RedScore = new(game.Score)
web.arena.BlueScore = new(game.Score)
}
web.arena.RedScore.AutoPoints += scores.Red.Auto
web.arena.RedScore.TeleopPoints += scores.Red.Teleop
web.arena.RedScore.EndgamePoints += scores.Red.Endgame
web.arena.BlueScore.AutoPoints += scores.Blue.Auto
web.arena.BlueScore.TeleopPoints += scores.Blue.Teleop
web.arena.BlueScore.EndgamePoints += scores.Blue.Endgame
web.arena.RealtimeScoreNotifier.Notify()
w.WriteHeader(http.StatusOK)
}

View File

@@ -117,6 +117,8 @@ func (web *Web) newHandler() http.Handler {
router.HandleFunc("/api/arena/websocket", web.arenaWebsocketApiHandler).Methods("GET")
router.HandleFunc("/api/matches/{type}", web.matchesApiHandler).Methods("GET")
router.HandleFunc("/api/rankings", web.rankingsApiHandler).Methods("GET")
router.HandleFunc("/api/scores", web.getScoresHandler).Methods("GET")
router.HandleFunc("/api/scores", web.setScoresHandler).Methods("PATCH", "PUT")
router.HandleFunc("/api/sponsor_slides", web.sponsorSlidesApiHandler).Methods("GET")
router.HandleFunc("/api/teams/{teamId}/avatar", web.teamAvatarsApiHandler).Methods("GET")
router.HandleFunc("/display", web.placeholderDisplayHandler).Methods("GET")