2014-06-28 22:03:30 -07:00
|
|
|
// Copyright 2014 Team 254. All Rights Reserved.
|
|
|
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
|
|
|
//
|
|
|
|
|
// Web routes for editing match results.
|
|
|
|
|
|
2017-08-31 23:26:22 -07:00
|
|
|
package web
|
2014-06-28 22:03:30 -07:00
|
|
|
|
|
|
|
|
import (
|
2021-05-12 16:40:07 -07:00
|
|
|
"encoding/json"
|
2014-06-28 22:03:30 -07:00
|
|
|
"fmt"
|
2022-08-21 14:42:47 -07:00
|
|
|
"github.com/Team254/cheesy-arena-lite/game"
|
2021-05-16 11:00:29 -07:00
|
|
|
"github.com/Team254/cheesy-arena-lite/model"
|
2014-06-28 22:03:30 -07:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MatchReviewListItem struct {
|
2021-05-12 17:49:05 -07:00
|
|
|
Id int
|
2014-06-28 22:03:30 -07:00
|
|
|
DisplayName string
|
|
|
|
|
Time string
|
|
|
|
|
RedTeams []int
|
|
|
|
|
BlueTeams []int
|
|
|
|
|
RedScore int
|
|
|
|
|
BlueScore int
|
|
|
|
|
ColorClass string
|
2022-08-22 17:16:04 -07:00
|
|
|
IsComplete bool
|
2014-06-28 22:03:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Shows the match review interface.
|
2017-08-28 20:14:32 -07:00
|
|
|
func (web *Web) matchReviewHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
practiceMatches, err := web.buildMatchReviewList("practice")
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-08-28 20:14:32 -07:00
|
|
|
qualificationMatches, err := web.buildMatchReviewList("qualification")
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-08-28 20:14:32 -07:00
|
|
|
eliminationMatches, err := web.buildMatchReviewList("elimination")
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 23:26:22 -07:00
|
|
|
template, err := web.parseFiles("templates/match_review.html", "templates/base.html")
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
matchesByType := map[string][]MatchReviewListItem{"practice": practiceMatches,
|
|
|
|
|
"qualification": qualificationMatches, "elimination": eliminationMatches}
|
2018-09-22 01:10:12 -07:00
|
|
|
currentMatchType := web.arena.CurrentMatch.Type
|
|
|
|
|
if currentMatchType == "test" {
|
2014-06-28 22:03:30 -07:00
|
|
|
currentMatchType = "practice"
|
|
|
|
|
}
|
|
|
|
|
data := struct {
|
2017-08-23 22:41:56 -07:00
|
|
|
*model.EventSettings
|
2014-06-28 22:03:30 -07:00
|
|
|
MatchesByType map[string][]MatchReviewListItem
|
|
|
|
|
CurrentMatchType string
|
2017-08-28 20:14:32 -07:00
|
|
|
}{web.arena.EventSettings, matchesByType, currentMatchType}
|
2014-06-28 22:03:30 -07:00
|
|
|
err = template.ExecuteTemplate(w, "base", data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Shows the page to edit the results for a match.
|
2017-08-28 20:14:32 -07:00
|
|
|
func (web *Web) matchReviewEditGetHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if !web.userIsAdmin(w, r) {
|
2015-08-22 23:33:38 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-28 20:14:32 -07:00
|
|
|
match, matchResult, _, err := web.getMatchResultFromRequest(r)
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 23:26:22 -07:00
|
|
|
template, err := web.parseFiles("templates/edit_match_result.html", "templates/base.html")
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-05-12 16:40:07 -07:00
|
|
|
matchResultJson, err := json.Marshal(matchResult)
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data := struct {
|
2017-08-23 22:41:56 -07:00
|
|
|
*model.EventSettings
|
|
|
|
|
Match *model.Match
|
2021-05-12 16:40:07 -07:00
|
|
|
MatchResultJson string
|
|
|
|
|
}{web.arena.EventSettings, match, string(matchResultJson)}
|
2014-06-28 22:03:30 -07:00
|
|
|
err = template.ExecuteTemplate(w, "base", data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Updates the results for a match.
|
2017-08-28 20:14:32 -07:00
|
|
|
func (web *Web) matchReviewEditPostHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if !web.userIsAdmin(w, r) {
|
2015-08-22 23:33:38 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:40:07 -07:00
|
|
|
match, _, isCurrent, err := web.getMatchResultFromRequest(r)
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-12 16:40:07 -07:00
|
|
|
var matchResult model.MatchResult
|
|
|
|
|
if err = json.Unmarshal([]byte(r.PostFormValue("matchResultJson")), &matchResult); err != nil {
|
2014-06-28 22:03:30 -07:00
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-05-12 16:40:07 -07:00
|
|
|
if matchResult.MatchId != match.Id {
|
|
|
|
|
handleWebErr(w, fmt.Errorf("Error: match ID %d from result does not match expected", matchResult.MatchId))
|
|
|
|
|
return
|
|
|
|
|
}
|
2014-06-28 22:03:30 -07:00
|
|
|
|
2015-08-19 22:35:04 -07:00
|
|
|
if isCurrent {
|
|
|
|
|
// If editing the current match, just save it back to memory.
|
2020-04-14 19:38:14 -05:00
|
|
|
*web.arena.RedScore = *matchResult.RedScore
|
|
|
|
|
*web.arena.BlueScore = *matchResult.BlueScore
|
2015-08-19 22:35:04 -07:00
|
|
|
|
2017-09-02 14:08:16 -07:00
|
|
|
http.Redirect(w, r, "/match_play", 303)
|
2015-08-19 22:35:04 -07:00
|
|
|
} else {
|
2021-05-12 16:40:07 -07:00
|
|
|
err = web.commitMatchScore(match, &matchResult, true)
|
2015-08-19 22:35:04 -07:00
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2014-06-28 22:03:30 -07:00
|
|
|
|
2017-09-02 14:08:16 -07:00
|
|
|
http.Redirect(w, r, "/match_review", 303)
|
2015-08-19 22:35:04 -07:00
|
|
|
}
|
2014-06-28 22:03:30 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-06 22:25:12 -07:00
|
|
|
// Load the match result for the match referenced in the HTTP query string.
|
2017-08-28 20:14:32 -07:00
|
|
|
func (web *Web) getMatchResultFromRequest(r *http.Request) (*model.Match, *model.MatchResult, bool, error) {
|
2014-06-28 22:03:30 -07:00
|
|
|
vars := mux.Vars(r)
|
2015-08-19 22:35:04 -07:00
|
|
|
|
|
|
|
|
// If editing the current match, get it from memory instead of the DB.
|
|
|
|
|
if vars["matchId"] == "current" {
|
2017-08-28 20:14:32 -07:00
|
|
|
return web.arena.CurrentMatch, web.getCurrentMatchResult(), true, nil
|
2015-08-19 22:35:04 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-12 17:49:05 -07:00
|
|
|
matchId, _ := strconv.Atoi(vars["matchId"])
|
2017-08-28 20:14:32 -07:00
|
|
|
match, err := web.arena.Database.GetMatchById(matchId)
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
2015-08-19 22:35:04 -07:00
|
|
|
return nil, nil, false, err
|
2014-06-28 22:03:30 -07:00
|
|
|
}
|
|
|
|
|
if match == nil {
|
2015-08-19 22:35:04 -07:00
|
|
|
return nil, nil, false, fmt.Errorf("Error: No such match: %d", matchId)
|
2014-06-28 22:03:30 -07:00
|
|
|
}
|
2017-08-28 20:14:32 -07:00
|
|
|
matchResult, err := web.arena.Database.GetMatchResultForMatch(matchId)
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
2015-08-19 22:35:04 -07:00
|
|
|
return nil, nil, false, err
|
2014-06-28 22:03:30 -07:00
|
|
|
}
|
|
|
|
|
if matchResult == nil {
|
|
|
|
|
// We're scoring a match that hasn't been played yet, but that's okay.
|
2017-08-23 22:41:56 -07:00
|
|
|
matchResult = model.NewMatchResult()
|
2021-05-12 16:40:07 -07:00
|
|
|
matchResult.MatchId = matchId
|
2016-08-14 18:59:24 -07:00
|
|
|
matchResult.MatchType = match.Type
|
2014-06-28 22:03:30 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-19 22:35:04 -07:00
|
|
|
return match, matchResult, false, nil
|
2014-06-28 22:03:30 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-06 22:25:12 -07:00
|
|
|
// Constructs the list of matches to display in the match review interface.
|
2017-08-28 20:14:32 -07:00
|
|
|
func (web *Web) buildMatchReviewList(matchType string) ([]MatchReviewListItem, error) {
|
|
|
|
|
matches, err := web.arena.Database.GetMatchesByType(matchType)
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return []MatchReviewListItem{}, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
matchReviewList := make([]MatchReviewListItem, len(matches))
|
|
|
|
|
for i, match := range matches {
|
|
|
|
|
matchReviewList[i].Id = match.Id
|
2019-07-20 22:42:56 -07:00
|
|
|
matchReviewList[i].DisplayName = match.TypePrefix() + match.DisplayName
|
2014-08-21 00:12:49 -07:00
|
|
|
matchReviewList[i].Time = match.Time.Local().Format("Mon 1/02 03:04 PM")
|
2014-06-28 22:03:30 -07:00
|
|
|
matchReviewList[i].RedTeams = []int{match.Red1, match.Red2, match.Red3}
|
|
|
|
|
matchReviewList[i].BlueTeams = []int{match.Blue1, match.Blue2, match.Blue3}
|
2017-08-28 20:14:32 -07:00
|
|
|
matchResult, err := web.arena.Database.GetMatchResultForMatch(match.Id)
|
2014-06-28 22:03:30 -07:00
|
|
|
if err != nil {
|
|
|
|
|
return []MatchReviewListItem{}, err
|
|
|
|
|
}
|
|
|
|
|
if matchResult != nil {
|
2020-04-14 19:38:14 -05:00
|
|
|
matchReviewList[i].RedScore = matchResult.RedScoreSummary().Score
|
|
|
|
|
matchReviewList[i].BlueScore = matchResult.BlueScoreSummary().Score
|
2014-06-28 22:03:30 -07:00
|
|
|
}
|
2020-03-29 00:04:15 -07:00
|
|
|
switch match.Status {
|
2022-08-21 14:42:47 -07:00
|
|
|
case game.RedWonMatch:
|
2015-05-30 23:58:42 -07:00
|
|
|
matchReviewList[i].ColorClass = "danger"
|
2022-08-22 17:16:04 -07:00
|
|
|
matchReviewList[i].IsComplete = true
|
2022-08-21 14:42:47 -07:00
|
|
|
case game.BlueWonMatch:
|
2015-05-30 23:58:42 -07:00
|
|
|
matchReviewList[i].ColorClass = "info"
|
2022-08-22 17:16:04 -07:00
|
|
|
matchReviewList[i].IsComplete = true
|
2022-08-21 14:42:47 -07:00
|
|
|
case game.TieMatch:
|
2014-06-28 22:03:30 -07:00
|
|
|
matchReviewList[i].ColorClass = "warning"
|
2022-08-22 17:16:04 -07:00
|
|
|
matchReviewList[i].IsComplete = true
|
2015-05-30 23:58:42 -07:00
|
|
|
default:
|
|
|
|
|
matchReviewList[i].ColorClass = ""
|
2022-08-22 17:16:04 -07:00
|
|
|
matchReviewList[i].IsComplete = false
|
2014-06-28 22:03:30 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return matchReviewList, nil
|
|
|
|
|
}
|