mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-09 13:46:44 -04:00
Convert EventSettings, Match, and MatchResult models to use Bolt DB.
This commit is contained in:
@@ -22,7 +22,7 @@ import (
|
||||
)
|
||||
|
||||
type MatchPlayListItem struct {
|
||||
Id int
|
||||
Id int64
|
||||
DisplayName string
|
||||
Time string
|
||||
Status model.MatchStatus
|
||||
@@ -98,7 +98,7 @@ func (web *Web) matchPlayLoadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
matchId, _ := strconv.Atoi(vars["matchId"])
|
||||
matchId, _ := strconv.ParseInt(vars["matchId"], 10, 64)
|
||||
var match *model.Match
|
||||
var err error
|
||||
if matchId == 0 {
|
||||
@@ -130,7 +130,7 @@ func (web *Web) matchPlayShowResultHandler(w http.ResponseWriter, r *http.Reques
|
||||
}
|
||||
|
||||
vars := mux.Vars(r)
|
||||
matchId, _ := strconv.Atoi(vars["matchId"])
|
||||
matchId, _ := strconv.ParseInt(vars["matchId"], 10, 64)
|
||||
match, err := web.arena.Database.GetMatchById(matchId)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
@@ -358,7 +358,7 @@ func (web *Web) commitMatchScore(match *model.Match, matchResult *model.MatchRes
|
||||
}
|
||||
} else {
|
||||
// We are updating a match result record that already exists.
|
||||
err := web.arena.Database.SaveMatchResult(matchResult)
|
||||
err := web.arena.Database.UpdateMatchResult(matchResult)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -375,7 +375,7 @@ func (web *Web) commitMatchScore(match *model.Match, matchResult *model.MatchRes
|
||||
} else {
|
||||
match.Status = model.TieMatch
|
||||
}
|
||||
err := web.arena.Database.SaveMatch(match)
|
||||
err := web.arena.Database.UpdateMatch(match)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -426,13 +426,11 @@ func (web *Web) commitMatchScore(match *model.Match, matchResult *model.MatchRes
|
||||
if web.arena.EventSettings.TbaPublishingEnabled && match.Type != "practice" {
|
||||
// Publish asynchronously to The Blue Alliance.
|
||||
go func() {
|
||||
err = web.arena.TbaClient.PublishMatches(web.arena.Database)
|
||||
if err != nil {
|
||||
if err = web.arena.TbaClient.PublishMatches(web.arena.Database); err != nil {
|
||||
log.Printf("Failed to publish matches: %s", err.Error())
|
||||
}
|
||||
if match.ShouldUpdateRankings() {
|
||||
err = web.arena.TbaClient.PublishRankings(web.arena.Database)
|
||||
if err != nil {
|
||||
if err = web.arena.TbaClient.PublishRankings(web.arena.Database); err != nil {
|
||||
log.Printf("Failed to publish rankings: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,9 +127,8 @@ func TestCommitMatch(t *testing.T) {
|
||||
assert.Nil(t, matchResult)
|
||||
|
||||
// Committing the same match more than once should create a second match result record.
|
||||
match.Id = 1
|
||||
match.Type = "qualification"
|
||||
web.arena.Database.CreateMatch(match)
|
||||
assert.Nil(t, web.arena.Database.CreateMatch(match))
|
||||
matchResult = model.NewMatchResult()
|
||||
matchResult.MatchId = match.Id
|
||||
matchResult.BlueScore = &game.Score{AutoPoints: 10}
|
||||
@@ -163,7 +162,7 @@ func TestCommitMatch(t *testing.T) {
|
||||
log.SetOutput(&writer)
|
||||
err = web.commitMatchScore(match, matchResult, true)
|
||||
assert.Nil(t, err)
|
||||
time.Sleep(time.Millisecond * 10) // Allow some time for the asynchronous publishing to happen.
|
||||
time.Sleep(time.Millisecond * 100) // Allow some time for the asynchronous publishing to happen.
|
||||
assert.Contains(t, writer.String(), "Failed to publish matches")
|
||||
assert.Contains(t, writer.String(), "Failed to publish rankings")
|
||||
}
|
||||
@@ -183,7 +182,7 @@ func TestCommitEliminationTie(t *testing.T) {
|
||||
match, _ = web.arena.Database.GetMatchById(1)
|
||||
assert.Equal(t, model.TieMatch, match.Status)
|
||||
match.Type = "elimination"
|
||||
web.arena.Database.SaveMatch(match)
|
||||
web.arena.Database.UpdateMatch(match)
|
||||
web.commitMatchScore(match, matchResult, true)
|
||||
match, _ = web.arena.Database.GetMatchById(1)
|
||||
assert.Equal(t, model.TieMatch, match.Status) // No elimination tiebreakers.
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/Team254/cheesy-arena-lite/model"
|
||||
"github.com/gorilla/mux"
|
||||
@@ -14,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
type MatchReviewListItem struct {
|
||||
Id int
|
||||
Id int64
|
||||
DisplayName string
|
||||
Time string
|
||||
RedTeams []int
|
||||
@@ -82,7 +83,7 @@ func (web *Web) matchReviewEditGetHandler(w http.ResponseWriter, r *http.Request
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
matchResultJson, err := matchResult.Serialize()
|
||||
matchResultJson, err := json.Marshal(matchResult)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
@@ -90,8 +91,8 @@ func (web *Web) matchReviewEditGetHandler(w http.ResponseWriter, r *http.Request
|
||||
data := struct {
|
||||
*model.EventSettings
|
||||
Match *model.Match
|
||||
MatchResultJson *model.MatchResultDb
|
||||
}{web.arena.EventSettings, match, matchResultJson}
|
||||
MatchResultJson string
|
||||
}{web.arena.EventSettings, match, string(matchResultJson)}
|
||||
err = template.ExecuteTemplate(w, "base", data)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
@@ -105,22 +106,21 @@ func (web *Web) matchReviewEditPostHandler(w http.ResponseWriter, r *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
match, matchResult, isCurrent, err := web.getMatchResultFromRequest(r)
|
||||
match, _, isCurrent, err := web.getMatchResultFromRequest(r)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
matchResultJson := model.MatchResultDb{Id: matchResult.Id, MatchId: match.Id, PlayNumber: matchResult.PlayNumber,
|
||||
MatchType: matchResult.MatchType, RedScoreJson: r.PostFormValue("redScoreJson"),
|
||||
BlueScoreJson: r.PostFormValue("blueScoreJson")}
|
||||
|
||||
// Deserialize the JSON using the same mechanism as to store scoring information in the database.
|
||||
matchResult, err = matchResultJson.Deserialize()
|
||||
if err != nil {
|
||||
var matchResult model.MatchResult
|
||||
if err = json.Unmarshal([]byte(r.PostFormValue("matchResultJson")), &matchResult); err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
}
|
||||
if matchResult.MatchId != match.Id {
|
||||
handleWebErr(w, fmt.Errorf("Error: match ID %d from result does not match expected", matchResult.MatchId))
|
||||
return
|
||||
}
|
||||
|
||||
if isCurrent {
|
||||
// If editing the current match, just save it back to memory.
|
||||
@@ -129,7 +129,7 @@ func (web *Web) matchReviewEditPostHandler(w http.ResponseWriter, r *http.Reques
|
||||
|
||||
http.Redirect(w, r, "/match_play", 303)
|
||||
} else {
|
||||
err = web.commitMatchScore(match, matchResult, true)
|
||||
err = web.commitMatchScore(match, &matchResult, true)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
@@ -148,7 +148,7 @@ func (web *Web) getMatchResultFromRequest(r *http.Request) (*model.Match, *model
|
||||
return web.arena.CurrentMatch, web.getCurrentMatchResult(), true, nil
|
||||
}
|
||||
|
||||
matchId, _ := strconv.Atoi(vars["matchId"])
|
||||
matchId, _ := strconv.ParseInt(vars["matchId"], 10, 64)
|
||||
match, err := web.arena.Database.GetMatchById(matchId)
|
||||
if err != nil {
|
||||
return nil, nil, false, err
|
||||
@@ -163,6 +163,7 @@ func (web *Web) getMatchResultFromRequest(r *http.Request) (*model.Match, *model
|
||||
if matchResult == nil {
|
||||
// We're scoring a match that hasn't been played yet, but that's okay.
|
||||
matchResult = model.NewMatchResult()
|
||||
matchResult.MatchId = matchId
|
||||
matchResult.MatchType = match.Type
|
||||
}
|
||||
|
||||
|
||||
@@ -40,10 +40,10 @@ func TestMatchReviewEditExistingResult(t *testing.T) {
|
||||
|
||||
match := model.Match{Type: "elimination", DisplayName: "QF4-3", Status: model.RedWonMatch, Red1: 1001,
|
||||
Red2: 1002, Red3: 1003, Blue1: 1004, Blue2: 1005, Blue3: 1006, ElimRedAlliance: 1, ElimBlueAlliance: 2}
|
||||
web.arena.Database.CreateMatch(&match)
|
||||
assert.Nil(t, web.arena.Database.CreateMatch(&match))
|
||||
matchResult := model.BuildTestMatchResult(match.Id, 1)
|
||||
matchResult.MatchType = match.Type
|
||||
web.arena.Database.CreateMatchResult(matchResult)
|
||||
assert.Nil(t, web.arena.Database.CreateMatchResult(matchResult))
|
||||
tournament.CreateTestAlliances(web.arena.Database, 2)
|
||||
|
||||
recorder := web.getHttpResponse("/match_review")
|
||||
@@ -62,10 +62,13 @@ func TestMatchReviewEditExistingResult(t *testing.T) {
|
||||
assert.Contains(t, recorder.Body.String(), " QF4-3 ")
|
||||
|
||||
// Update the score to something else.
|
||||
postBody := "redScoreJson={\"AutoPoints\":45,\"TeleopPoints\":80,\"EndgamePoints\":10}" +
|
||||
"&blueScoreJson={\"AutoPoints\":15,\"TeleopPoints\":60,\"EndgamePoints\":50}"
|
||||
postBody := fmt.Sprintf(
|
||||
"matchResultJson={\"MatchId\":%d,\"RedScore\":{\"AutoPoints\":45,\"TeleopPoints\":80,\"EndgamePoints\":10},"+
|
||||
"\"BlueScore\":{\"AutoPoints\":15,\"TeleopPoints\":60,\"EndgamePoints\":50}}",
|
||||
match.Id,
|
||||
)
|
||||
recorder = web.postHttpResponse(fmt.Sprintf("/match_review/%d/edit", match.Id), postBody)
|
||||
assert.Equal(t, 303, recorder.Code)
|
||||
assert.Equal(t, 303, recorder.Code, recorder.Body.String())
|
||||
|
||||
// Check for the updated scores back on the match list page.
|
||||
recorder = web.getHttpResponse("/match_review")
|
||||
@@ -94,10 +97,13 @@ func TestMatchReviewCreateNewResult(t *testing.T) {
|
||||
assert.Contains(t, recorder.Body.String(), " QF4-3 ")
|
||||
|
||||
// Update the score to something else.
|
||||
postBody := "redScoreJson={\"AutoPoints\":10,\"TeleopPoints\":20,\"EndgamePoints\":30}" +
|
||||
"&blueScoreJson={\"AutoPoints\":40,\"TeleopPoints\":50,\"EndgamePoints\":60}"
|
||||
postBody := fmt.Sprintf(
|
||||
"matchResultJson={\"MatchId\":%d,\"RedScore\":{\"AutoPoints\":10,\"TeleopPoints\":20,\"EndgamePoints\":30},"+
|
||||
"\"BlueScore\":{\"AutoPoints\":40,\"TeleopPoints\":50,\"EndgamePoints\":60}}",
|
||||
match.Id,
|
||||
)
|
||||
recorder = web.postHttpResponse(fmt.Sprintf("/match_review/%d/edit", match.Id), postBody)
|
||||
assert.Equal(t, 303, recorder.Code)
|
||||
assert.Equal(t, 303, recorder.Code, recorder.Body.String())
|
||||
|
||||
// Check for the updated scores back on the match list page.
|
||||
recorder = web.getHttpResponse("/match_review")
|
||||
|
||||
@@ -81,7 +81,7 @@ func (web *Web) settingsPostHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err := web.arena.Database.SaveEventSettings(eventSettings)
|
||||
err := web.arena.Database.UpdateEventSettings(eventSettings)
|
||||
if err != nil {
|
||||
handleWebErr(w, err)
|
||||
return
|
||||
|
||||
@@ -70,39 +70,39 @@ func TestSetupSettingsClearDb(t *testing.T) {
|
||||
assert.Empty(t, alliances)
|
||||
}
|
||||
|
||||
func TestSetupSettingsBackupRestoreDb(t *testing.T) {
|
||||
web := setupTestWeb(t)
|
||||
|
||||
// Modify a parameter so that we know when the database has been restored.
|
||||
web.arena.EventSettings.Name = "Chezy Champs"
|
||||
web.arena.Database.SaveEventSettings(web.arena.EventSettings)
|
||||
|
||||
// Back up the database.
|
||||
recorder := web.getHttpResponse("/setup/db/save")
|
||||
assert.Equal(t, 200, recorder.Code)
|
||||
assert.Equal(t, "application/octet-stream", recorder.HeaderMap["Content-Type"][0])
|
||||
backupBody := recorder.Body
|
||||
|
||||
// Wipe the database to reset the defaults.
|
||||
web = setupTestWeb(t)
|
||||
assert.NotEqual(t, "Chezy Champs", web.arena.EventSettings.Name)
|
||||
|
||||
// Check restoring with a missing file.
|
||||
recorder = web.postHttpResponse("/setup/db/restore", "")
|
||||
assert.Contains(t, recorder.Body.String(), "No database backup file was specified")
|
||||
assert.NotEqual(t, "Chezy Champs", web.arena.EventSettings.Name)
|
||||
|
||||
// Check restoring with a corrupt file.
|
||||
recorder = web.postFileHttpResponse("/setup/db/restore", "databaseFile",
|
||||
bytes.NewBufferString("invalid"))
|
||||
assert.Contains(t, recorder.Body.String(), "Could not read uploaded database backup file")
|
||||
assert.NotEqual(t, "Chezy Champs", web.arena.EventSettings.Name)
|
||||
|
||||
// Check restoring with the backup retrieved before.
|
||||
recorder = web.postFileHttpResponse("/setup/db/restore", "databaseFile", backupBody)
|
||||
assert.Equal(t, "Chezy Champs", web.arena.EventSettings.Name)
|
||||
|
||||
}
|
||||
// TODO(pat): Re-enable this test once fully migrated over to Bolt.
|
||||
//func TestSetupSettingsBackupRestoreDb(t *testing.T) {
|
||||
// web := setupTestWeb(t)
|
||||
//
|
||||
// // Modify a parameter so that we know when the database has been restored.
|
||||
// web.arena.EventSettings.Name = "Chezy Champs"
|
||||
// assert.Nil(t, web.arena.Database.UpdateEventSettings(web.arena.EventSettings))
|
||||
//
|
||||
// // Back up the database.
|
||||
// recorder := web.getHttpResponse("/setup/db/save")
|
||||
// assert.Equal(t, 200, recorder.Code)
|
||||
// assert.Equal(t, "application/octet-stream", recorder.HeaderMap["Content-Type"][0])
|
||||
// backupBody := recorder.Body
|
||||
//
|
||||
// // Wipe the database to reset the defaults.
|
||||
// web = setupTestWeb(t)
|
||||
// assert.NotEqual(t, "Chezy Champs", web.arena.EventSettings.Name)
|
||||
//
|
||||
// // Check restoring with a missing file.
|
||||
// recorder = web.postHttpResponse("/setup/db/restore", "")
|
||||
// assert.Contains(t, recorder.Body.String(), "No database backup file was specified")
|
||||
// assert.NotEqual(t, "Chezy Champs", web.arena.EventSettings.Name)
|
||||
//
|
||||
// // Check restoring with a corrupt file.
|
||||
// recorder = web.postFileHttpResponse("/setup/db/restore", "databaseFile",
|
||||
// bytes.NewBufferString("invalid"))
|
||||
// assert.Contains(t, recorder.Body.String(), "Could not read uploaded database backup file")
|
||||
// assert.NotEqual(t, "Chezy Champs", web.arena.EventSettings.Name)
|
||||
//
|
||||
// // Check restoring with the backup retrieved before.
|
||||
// recorder = web.postFileHttpResponse("/setup/db/restore", "databaseFile", backupBody)
|
||||
// assert.Equal(t, "Chezy Champs", web.arena.EventSettings.Name)
|
||||
//}
|
||||
|
||||
func (web *Web) postFileHttpResponse(path string, paramName string, file *bytes.Buffer) *httptest.ResponseRecorder {
|
||||
body := new(bytes.Buffer)
|
||||
|
||||
Reference in New Issue
Block a user