mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 21:56:50 -04:00
Update scoring panel for 2020.
This commit is contained in:
@@ -8,6 +8,7 @@ package web
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Team254/cheesy-arena/field"
|
||||
"github.com/Team254/cheesy-arena/game"
|
||||
"github.com/Team254/cheesy-arena/model"
|
||||
"github.com/Team254/cheesy-arena/websocket"
|
||||
"github.com/gorilla/mux"
|
||||
@@ -15,41 +16,9 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Maps a numbered bay on the scoring panel to the field that it represents in the Score model.
|
||||
type bayMapping struct {
|
||||
BayId int
|
||||
Shortcut string
|
||||
RedElement string
|
||||
RedIndex int
|
||||
BlueElement string
|
||||
BlueIndex int
|
||||
}
|
||||
|
||||
var bayMappings = []*bayMapping{
|
||||
{0, "q", "rocketNearRight", 2, "rocketFarRight", 2},
|
||||
{1, "a", "rocketNearRight", 1, "rocketFarRight", 1},
|
||||
{2, "z", "rocketNearRight", 0, "rocketFarRight", 0},
|
||||
{3, "w", "rocketNearLeft", 2, "rocketFarLeft", 2},
|
||||
{4, "s", "rocketNearLeft", 1, "rocketFarLeft", 1},
|
||||
{5, "x", "rocketNearLeft", 0, "rocketFarLeft", 0},
|
||||
{6, "e", "cargoShip", 0, "cargoShip", 7},
|
||||
{7, "d", "cargoShip", 1, "cargoShip", 6},
|
||||
{8, "c", "cargoShip", 2, "cargoShip", 5},
|
||||
{9, "v", "cargoShip", 3, "cargoShip", 4},
|
||||
{10, "b", "cargoShip", 4, "cargoShip", 3},
|
||||
{11, "n", "cargoShip", 5, "cargoShip", 2},
|
||||
{12, "j", "cargoShip", 6, "cargoShip", 1},
|
||||
{13, "i", "cargoShip", 7, "cargoShip", 0},
|
||||
{14, "o", "rocketFarRight", 2, "rocketNearRight", 2},
|
||||
{15, "k", "rocketFarRight", 1, "rocketNearRight", 1},
|
||||
{16, "m", "rocketFarRight", 0, "rocketNearRight", 0},
|
||||
{17, "p", "rocketFarLeft", 2, "rocketNearLeft", 2},
|
||||
{18, "l", "rocketFarLeft", 1, "rocketNearLeft", 1},
|
||||
{19, ",", "rocketFarLeft", 0, "rocketNearLeft", 0},
|
||||
}
|
||||
|
||||
// Renders the scoring interface which enables input of scores in real-time.
|
||||
func (web *Web) scoringPanelHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if !web.userIsAdmin(w, r) {
|
||||
@@ -70,9 +39,8 @@ func (web *Web) scoringPanelHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
data := struct {
|
||||
*model.EventSettings
|
||||
Alliance string
|
||||
BayMappings []*bayMapping
|
||||
}{web.arena.EventSettings, alliance, bayMappings}
|
||||
Alliance string
|
||||
}{web.arena.EventSettings, alliance}
|
||||
err = template.ExecuteTemplate(w, "base_no_navbar", data)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
@@ -92,15 +60,13 @@ func (web *Web) scoringPanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
|
||||
handleWebErr(w, fmt.Errorf("Invalid alliance '%s'.", alliance))
|
||||
return
|
||||
}
|
||||
// TODO(pat): Update for 2020.
|
||||
/*
|
||||
var realtimeScore **field.RealtimeScore
|
||||
if alliance == "red" {
|
||||
realtimeScore = &web.arena.RedRealtimeScore
|
||||
} else {
|
||||
realtimeScore = &web.arena.BlueRealtimeScore
|
||||
}
|
||||
*/
|
||||
|
||||
var realtimeScore **field.RealtimeScore
|
||||
if alliance == "red" {
|
||||
realtimeScore = &web.arena.RedRealtimeScore
|
||||
} else {
|
||||
realtimeScore = &web.arena.BlueRealtimeScore
|
||||
}
|
||||
|
||||
ws, err := websocket.NewWebsocket(w, r)
|
||||
if err != nil {
|
||||
@@ -129,8 +95,7 @@ func (web *Web) scoringPanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
|
||||
return
|
||||
}
|
||||
|
||||
// TODO(pat): Update for 2020.
|
||||
//score := &(*realtimeScore).CurrentScore
|
||||
score := &(*realtimeScore).CurrentScore
|
||||
scoreChanged := false
|
||||
|
||||
if command == "commitMatch" {
|
||||
@@ -141,62 +106,99 @@ func (web *Web) scoringPanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
web.arena.ScoringPanelRegistry.SetScoreCommitted(alliance, ws)
|
||||
web.arena.ScoringStatusNotifier.Notify()
|
||||
} else if number, err := strconv.Atoi(command); err == nil && number >= 1 && number <= 9 {
|
||||
// TODO(pat): Update for 2020.
|
||||
/*
|
||||
// Handle per-robot scoring fields.
|
||||
if number <= 3 && web.arena.MatchState == field.PreMatch {
|
||||
index := number - 1
|
||||
score.RobotStartLevels[index]++
|
||||
if score.RobotStartLevels[index] == 4 {
|
||||
score.RobotStartLevels[index] = 0
|
||||
}
|
||||
scoreChanged = true
|
||||
} else if number > 3 && number <= 6 && web.arena.MatchState != field.PreMatch {
|
||||
index := number - 4
|
||||
score.SandstormBonuses[index] =
|
||||
!score.SandstormBonuses[index]
|
||||
scoreChanged = true
|
||||
} else if number > 6 && web.arena.MatchState != field.PreMatch {
|
||||
index := number - 7
|
||||
score.RobotEndLevels[index]++
|
||||
if score.RobotEndLevels[index] == 4 {
|
||||
score.RobotEndLevels[index] = 0
|
||||
}
|
||||
scoreChanged = true
|
||||
}
|
||||
*/
|
||||
} else {
|
||||
// Handle cargo bays.
|
||||
var bayMapping *bayMapping
|
||||
for _, mapping := range bayMappings {
|
||||
if mapping.Shortcut == command {
|
||||
bayMapping = mapping
|
||||
break
|
||||
} else if number, err := strconv.Atoi(command); err == nil && number >= 1 && number <= 6 {
|
||||
// Handle per-robot scoring fields.
|
||||
if number <= 3 {
|
||||
index := number - 1
|
||||
score.ExitedInitiationLine[index] = !score.ExitedInitiationLine[index]
|
||||
scoreChanged = true
|
||||
} else {
|
||||
index := number - 4
|
||||
score.EndgameStatuses[index]++
|
||||
if score.EndgameStatuses[index] == 3 {
|
||||
score.EndgameStatuses[index] = 0
|
||||
}
|
||||
scoreChanged = true
|
||||
}
|
||||
if bayMapping != nil {
|
||||
// TODO(pat): Update for 2020.
|
||||
/*
|
||||
element := bayMapping.RedElement
|
||||
index := bayMapping.RedIndex
|
||||
if alliance == "blue" {
|
||||
element = bayMapping.BlueElement
|
||||
index = bayMapping.BlueIndex
|
||||
}
|
||||
switch element {
|
||||
case "cargoShip":
|
||||
scoreChanged = web.toggleCargoShipBay(&score.CargoBays[index], index)
|
||||
case "rocketNearLeft":
|
||||
scoreChanged = web.toggleRocketBay(&score.RocketNearLeftBays[index])
|
||||
case "rocketNearRight":
|
||||
scoreChanged = web.toggleRocketBay(&score.RocketNearRightBays[index])
|
||||
case "rocketFarLeft":
|
||||
scoreChanged = web.toggleRocketBay(&score.RocketFarLeftBays[index])
|
||||
case "rocketFarRight":
|
||||
scoreChanged = web.toggleRocketBay(&score.RocketFarRightBays[index])
|
||||
}
|
||||
*/
|
||||
} else {
|
||||
switch strings.ToUpper(command) {
|
||||
case "Q":
|
||||
if decrementGoal(score.AutoCellsInner[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "A":
|
||||
if decrementGoal(score.AutoCellsOuter[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "Z":
|
||||
if decrementGoal(score.AutoCellsBottom[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "W":
|
||||
if incrementGoal(score.AutoCellsInner[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "S":
|
||||
if incrementGoal(score.AutoCellsOuter[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "X":
|
||||
if incrementGoal(score.AutoCellsBottom[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "E":
|
||||
if decrementGoal(score.TeleopCellsInner[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "D":
|
||||
if decrementGoal(score.TeleopCellsOuter[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "C":
|
||||
if decrementGoal(score.TeleopCellsBottom[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "R":
|
||||
if incrementGoal(score.TeleopCellsInner[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "F":
|
||||
if incrementGoal(score.TeleopCellsOuter[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "V":
|
||||
if incrementGoal(score.TeleopCellsBottom[:],
|
||||
score.CellCountingStage(web.arena.MatchState >= field.TeleopPeriod)) {
|
||||
scoreChanged = true
|
||||
}
|
||||
case "O":
|
||||
if score.ControlPanelStatus >= game.ControlPanelRotation {
|
||||
score.ControlPanelStatus = game.ControlPanelNone
|
||||
} else if score.StageAtCapacity(game.Stage2, true) {
|
||||
score.ControlPanelStatus = game.ControlPanelRotation
|
||||
}
|
||||
scoreChanged = true
|
||||
case "P":
|
||||
if score.ControlPanelStatus == game.ControlPanelPosition {
|
||||
score.ControlPanelStatus = game.ControlPanelRotation
|
||||
} else if score.StageAtCapacity(game.Stage3, true) {
|
||||
score.ControlPanelStatus = game.ControlPanelPosition
|
||||
}
|
||||
scoreChanged = true
|
||||
case "L":
|
||||
score.RungIsLevel = !score.RungIsLevel
|
||||
scoreChanged = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,48 +208,20 @@ func (web *Web) scoringPanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(pat): Update for 2020.
|
||||
/*
|
||||
// Advances the given cargo ship bay through the states applicable to the current status of the field.
|
||||
func (web *Web) toggleCargoShipBay(bay *game.BayStatus, index int) bool {
|
||||
if (index == 3 || index == 4) && web.arena.MatchState == field.PreMatch {
|
||||
// Only the side bays can be preloaded.
|
||||
return false
|
||||
}
|
||||
|
||||
if web.arena.MatchState == field.PreMatch {
|
||||
*bay++
|
||||
if *bay == game.BayHatchCargo {
|
||||
// Skip the hatch+cargo state pre-match as it is invalid.
|
||||
*bay = game.BayCargo
|
||||
} else if *bay > game.BayCargo {
|
||||
*bay = game.BayEmpty
|
||||
}
|
||||
} else {
|
||||
if *bay == game.BayCargo {
|
||||
// If the bay was pre-loaded with cargo, go immediately to hatch+cargo during first toggle.
|
||||
*bay = game.BayHatchCargo
|
||||
} else {
|
||||
*bay++
|
||||
if *bay == game.BayCargo {
|
||||
// Skip the cargo-only state during the match as it can't stay in on its own.
|
||||
*bay = game.BayEmpty
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Advances the given rocket bay through the states applicable to the current status of the field.
|
||||
func (web *Web) toggleRocketBay(bay *game.BayStatus) bool {
|
||||
if web.arena.MatchState != field.PreMatch {
|
||||
*bay++
|
||||
if *bay == game.BayCargo {
|
||||
// Skip the cargo-only state as it's not applicable to rocket bays.
|
||||
*bay = game.BayEmpty
|
||||
}
|
||||
// Increments the power cell count for the given goal, if the preconditions are met.
|
||||
func incrementGoal(goal []int, currentStage game.Stage) bool {
|
||||
if int(currentStage) < len(goal) {
|
||||
goal[currentStage]++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Decrements the power cell count for the given goal, if the preconditions are met.
|
||||
func decrementGoal(goal []int, currentStage game.Stage) bool {
|
||||
if int(currentStage) < len(goal) && goal[currentStage] > 0 {
|
||||
goal[currentStage]--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -189,6 +189,7 @@ func (web *Web) newHandler() http.Handler {
|
||||
|
||||
// Writes the given error out as plain text with a status code of 500.
|
||||
func handleWebErr(w http.ResponseWriter, err error) {
|
||||
log.Printf("HTTP request error: %v", err)
|
||||
http.Error(w, "Internal server error: "+err.Error(), 500)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user