Refactor Rules to have an ID that is referenced instead of copying details everywhere.

This commit is contained in:
Patrick Fairbank
2020-03-15 21:09:48 -07:00
parent 4c3850e2e4
commit 49758eaafd
18 changed files with 183 additions and 138 deletions

View File

@@ -178,7 +178,7 @@ func TestCommitEliminationTie(t *testing.T) {
MatchId: match.Id,
RedScore: &game.Score{
RocketFarRightBays: [3]game.BayStatus{game.BayHatchCargo, game.BayHatch, game.BayHatch},
Fouls: []game.Foul{{}, {}, {}}},
Fouls: []game.Foul{{RuleId: 1}, {RuleId: 2}, {RuleId: 4}}},
BlueScore: &game.Score{},
}
err := web.commitMatchScore(match, matchResult, false)

View File

@@ -7,6 +7,7 @@ package web
import (
"fmt"
"github.com/Team254/cheesy-arena/game"
"github.com/Team254/cheesy-arena/model"
"github.com/gorilla/mux"
"net/http"
@@ -91,7 +92,8 @@ func (web *Web) matchReviewEditGetHandler(w http.ResponseWriter, r *http.Request
*model.EventSettings
Match *model.Match
MatchResultJson *model.MatchResultDb
}{web.arena.EventSettings, match, matchResultJson}
Rules map[int]*game.Rule
}{web.arena.EventSettings, match, matchResultJson, game.GetAllRules()}
err = template.ExecuteTemplate(w, "base", data)
if err != nil {
handleWebErr(w, err)

View File

@@ -63,7 +63,7 @@ func TestMatchReviewEditExistingResult(t *testing.T) {
// Update the score to something else.
postBody := "redScoreJson={\"RobotEndLevels\":[0,3,0]}&blueScoreJson={\"CargoBays\":[0,2,1,2,2,0,1]," +
"\"Fouls\":[{\"TeamId\":973,\"Rule\":\"G22\"}]}&redCardsJson={\"105\":\"yellow\"}&blueCardsJson={}"
"\"Fouls\":[{\"TeamId\":973,\"RuleId\":1}]}&redCardsJson={\"105\":\"yellow\"}&blueCardsJson={}"
recorder = web.postHttpResponse(fmt.Sprintf("/match_review/%d/edit", match.Id), postBody)
assert.Equal(t, 303, recorder.Code)
@@ -95,7 +95,7 @@ func TestMatchReviewCreateNewResult(t *testing.T) {
// Update the score to something else.
postBody := "redScoreJson={\"RocketNearLeftBays\":[1,0,2]}&blueScoreJson={\"RocketFarRightBays\":[2,2,2]," +
"\"Fouls\":[{\"TeamId\":973,\"Rule\":\"G22\"}]}&redCardsJson={\"105\":\"yellow\"}&blueCardsJson={}"
"\"Fouls\":[{\"TeamId\":973,\"RuleId\":1}]}&redCardsJson={\"105\":\"yellow\"}&blueCardsJson={}"
recorder = web.postHttpResponse(fmt.Sprintf("/match_review/%d/edit", match.Id), postBody)
assert.Equal(t, 303, recorder.Code)

View File

@@ -70,11 +70,11 @@ func (web *Web) refereePanelHandler(w http.ResponseWriter, r *http.Request) {
BlueFouls []game.Foul
RedCards map[string]string
BlueCards map[string]string
Rules []game.Rule
Rules map[int]*game.Rule
EntryEnabled bool
}{web.arena.EventSettings, matchType, match.DisplayName, red1, red2, red3, blue1, blue2, blue3,
web.arena.RedRealtimeScore.CurrentScore.Fouls, web.arena.BlueRealtimeScore.CurrentScore.Fouls,
web.arena.RedRealtimeScore.Cards, web.arena.BlueRealtimeScore.Cards, game.Rules,
web.arena.RedRealtimeScore.Cards, web.arena.BlueRealtimeScore.Cards, game.GetAllRules(),
!(web.arena.RedRealtimeScore.FoulsCommitted && web.arena.BlueRealtimeScore.FoulsCommitted)}
err = template.ExecuteTemplate(w, "referee_panel.html", data)
if err != nil {
@@ -114,11 +114,9 @@ func (web *Web) refereePanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
switch messageType {
case "addFoul":
args := struct {
Alliance string
TeamId int
Rule string
IsTechnical bool
IsRankingPoint bool
Alliance string
TeamId int
RuleId int
}{}
err = mapstructure.Decode(data, &args)
if err != nil {
@@ -127,9 +125,7 @@ func (web *Web) refereePanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
}
// Add the foul to the correct alliance's list.
foul := game.Foul{Rule: game.Rule{RuleNumber: args.Rule, IsTechnical: args.IsTechnical,
IsRankingPoint: args.IsRankingPoint},
TeamId: args.TeamId, TimeInMatchSec: web.arena.MatchTimeSec()}
foul := game.Foul{RuleId: args.RuleId, TeamId: args.TeamId, TimeInMatchSec: web.arena.MatchTimeSec()}
if args.Alliance == "red" {
web.arena.RedRealtimeScore.CurrentScore.Fouls =
append(web.arena.RedRealtimeScore.CurrentScore.Fouls, foul)
@@ -142,9 +138,7 @@ func (web *Web) refereePanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
args := struct {
Alliance string
TeamId int
Rule string
IsTechnical bool
IsRankingPoint bool
RuleId int
TimeInMatchSec float64
}{}
err = mapstructure.Decode(data, &args)
@@ -154,9 +148,7 @@ func (web *Web) refereePanelWebsocketHandler(w http.ResponseWriter, r *http.Requ
}
// Remove the foul from the correct alliance's list.
deleteFoul := game.Foul{Rule: game.Rule{RuleNumber: args.Rule, IsTechnical: args.IsTechnical,
IsRankingPoint: args.IsRankingPoint},
TeamId: args.TeamId, TimeInMatchSec: args.TimeInMatchSec}
deleteFoul := game.Foul{RuleId: args.RuleId, TeamId: args.TeamId, TimeInMatchSec: args.TimeInMatchSec}
var fouls *[]game.Foul
if args.Alliance == "red" {
fouls = &web.arena.RedRealtimeScore.CurrentScore.Fouls

View File

@@ -37,13 +37,12 @@ func TestRefereePanelWebsocket(t *testing.T) {
foulData := struct {
Alliance string
TeamId int
Rule string
IsTechnical bool
RuleId int
TimeInMatchSec float64
}{"red", 256, "G22", false, 0}
}{"red", 256, 1, 0}
ws.Write("addFoul", foulData)
foulData.TeamId = 359
foulData.IsTechnical = true
foulData.RuleId = 3
ws.Write("addFoul", foulData)
foulData.Alliance = "blue"
foulData.TeamId = 1680
@@ -53,17 +52,14 @@ func TestRefereePanelWebsocket(t *testing.T) {
readWebsocketType(t, ws, "reload")
if assert.Equal(t, 2, len(web.arena.RedRealtimeScore.CurrentScore.Fouls)) {
assert.Equal(t, 256, web.arena.RedRealtimeScore.CurrentScore.Fouls[0].TeamId)
assert.Equal(t, "G22", web.arena.RedRealtimeScore.CurrentScore.Fouls[0].RuleNumber)
assert.Equal(t, false, web.arena.RedRealtimeScore.CurrentScore.Fouls[0].IsTechnical)
assert.Equal(t, 1, web.arena.RedRealtimeScore.CurrentScore.Fouls[0].RuleId)
assert.Equal(t, 0.0, web.arena.RedRealtimeScore.CurrentScore.Fouls[0].TimeInMatchSec)
assert.Equal(t, 359, web.arena.RedRealtimeScore.CurrentScore.Fouls[1].TeamId)
assert.Equal(t, "G22", web.arena.RedRealtimeScore.CurrentScore.Fouls[1].RuleNumber)
assert.Equal(t, true, web.arena.RedRealtimeScore.CurrentScore.Fouls[1].IsTechnical)
assert.Equal(t, 3, web.arena.RedRealtimeScore.CurrentScore.Fouls[1].RuleId)
}
if assert.Equal(t, 1, len(web.arena.BlueRealtimeScore.CurrentScore.Fouls)) {
assert.Equal(t, 1680, web.arena.BlueRealtimeScore.CurrentScore.Fouls[0].TeamId)
assert.Equal(t, "G22", web.arena.BlueRealtimeScore.CurrentScore.Fouls[0].RuleNumber)
assert.Equal(t, true, web.arena.BlueRealtimeScore.CurrentScore.Fouls[0].IsTechnical)
assert.Equal(t, 3, web.arena.BlueRealtimeScore.CurrentScore.Fouls[0].RuleId)
assert.Equal(t, 0.0, web.arena.BlueRealtimeScore.CurrentScore.Fouls[0].TimeInMatchSec)
}
assert.False(t, web.arena.RedRealtimeScore.FoulsCommitted)