From 02fa6954bd4b00c6cb10f1409ad6029d2742aa4f Mon Sep 17 00:00:00 2001 From: Patrick Fairbank Date: Sat, 20 Jul 2019 22:42:56 -0700 Subject: [PATCH] Centralize some match-type-dependent logic. --- field/arena.go | 2 +- model/match.go | 29 +++++++++++++++++++++++++++++ partner/tba.go | 2 +- web/match_play.go | 21 +++++++-------------- web/match_review.go | 8 +------- web/queueing_display.go | 11 ++--------- 6 files changed, 41 insertions(+), 32 deletions(-) diff --git a/field/arena.go b/field/arena.go index c4226be..18f9955 100644 --- a/field/arena.go +++ b/field/arena.go @@ -228,7 +228,7 @@ func (arena *Arena) LoadNextMatch() error { // Assigns the given team to the given station, also substituting it into the match record. func (arena *Arena) SubstituteTeam(teamId int, station string) error { - if arena.CurrentMatch.Type == "qualification" { + if !arena.CurrentMatch.ShouldAllowSubstitution() { return fmt.Errorf("Can't substitute teams for qualification matches.") } err := arena.assignTeam(teamId, station) diff --git a/model/match.go b/model/match.go index 603414d..a557ec1 100644 --- a/model/match.go +++ b/model/match.go @@ -104,6 +104,15 @@ func (match *Match) CapitalizedType() string { return strings.ToUpper(match.Type[0:1]) + match.Type[1:] } +func (match *Match) TypePrefix() string { + if match.Type == "practice" { + return "P" + } else if match.Type == "qualification" { + return "Q" + } + return "" +} + func (match *Match) TbaCode() string { if match.Type == "qualification" { return fmt.Sprintf("qm%s", match.DisplayName) @@ -113,3 +122,23 @@ func (match *Match) TbaCode() string { } return "" } + +// Returns true if the match is of a type that allows substitution of teams. +func (match *Match) ShouldAllowSubstitution() bool { + return match.Type != "qualification" +} + +// Returns true if the red and yellow cards should be updated as a result of the match. +func (match *Match) ShouldUpdateCards() bool { + return match.Type == "qualification" || match.Type == "elimination" +} + +// Returns true if the rankings should be updated as a result of the match. +func (match *Match) ShouldUpdateRankings() bool { + return match.Type == "qualification" +} + +// Returns true if the elimination match set should be updated as a result of the match. +func (match *Match) ShouldUpdateEliminationMatches() bool { + return match.Type == "elimination" +} diff --git a/partner/tba.go b/partner/tba.go index 0bfe927..1898aed 100644 --- a/partner/tba.go +++ b/partner/tba.go @@ -541,7 +541,7 @@ func createTbaScoringBreakdown(match *model.Match, matchResult *model.MatchResul breakdown.FaceTheBossRankingPoint = scoreSummary.FaceTheBoss breakdown.FoulPoints = scoreSummary.FoulPoints breakdown.TotalPoints = scoreSummary.Score - if match.Type == "qualification" { + if match.ShouldUpdateRankings() { // Calculate and set the ranking points for the match. var ranking game.Ranking ranking.AddScoreSummary(scoreSummary, opponentScoreSummary, false) diff --git a/web/match_play.go b/web/match_play.go index bc00b6e..ca98356 100644 --- a/web/match_play.go +++ b/web/match_play.go @@ -63,7 +63,6 @@ func (web *Web) matchPlayHandler(w http.ResponseWriter, r *http.Request) { if currentMatchType == "test" { currentMatchType = "practice" } - allowSubstitution := web.arena.CurrentMatch.Type != "qualification" matchResult, err := web.arena.Database.GetMatchResultForMatch(web.arena.CurrentMatch.Id) if err != nil { handleWebErr(w, err) @@ -77,8 +76,8 @@ func (web *Web) matchPlayHandler(w http.ResponseWriter, r *http.Request) { Match *model.Match AllowSubstitution bool IsReplay bool - }{web.arena.EventSettings, matchesByType, currentMatchType, web.arena.CurrentMatch, allowSubstitution, - isReplay} + }{web.arena.EventSettings, matchesByType, currentMatchType, web.arena.CurrentMatch, + web.arena.CurrentMatch.ShouldAllowSubstitution(), isReplay} err = template.ExecuteTemplate(w, "base", data) if err != nil { handleWebErr(w, err) @@ -363,12 +362,12 @@ func (web *Web) commitMatchScore(match *model.Match, matchResult *model.MatchRes return err } - if match.Type != "practice" { + if match.ShouldUpdateCards() { // Regenerate the residual yellow cards that teams may carry. tournament.CalculateTeamCards(web.arena.Database, match.Type) } - if match.Type == "qualification" { + if match.ShouldUpdateRankings() { // Recalculate all the rankings. err = tournament.CalculateRankings(web.arena.Database) if err != nil { @@ -376,7 +375,7 @@ func (web *Web) commitMatchScore(match *model.Match, matchResult *model.MatchRes } } - if match.Type == "elimination" { + if match.ShouldUpdateEliminationMatches() { // Generate any subsequent elimination matches. _, err = tournament.UpdateEliminationSchedule(web.arena.Database, time.Now().Add(time.Second*tournament.ElimMatchSpacingSec)) @@ -392,7 +391,7 @@ func (web *Web) commitMatchScore(match *model.Match, matchResult *model.MatchRes if err != nil { log.Printf("Failed to publish matches: %s", err.Error()) } - if match.Type == "qualification" { + if match.ShouldUpdateRankings() { err = web.arena.TbaClient.PublishRankings(web.arena.Database) if err != nil { log.Printf("Failed to publish rankings: %s", err.Error()) @@ -452,16 +451,10 @@ func (web *Web) buildMatchPlayList(matchType string) (MatchPlayList, error) { return MatchPlayList{}, err } - prefix := "" - if matchType == "practice" { - prefix = "P" - } else if matchType == "qualification" { - prefix = "Q" - } matchPlayList := make(MatchPlayList, len(matches)) for i, match := range matches { matchPlayList[i].Id = match.Id - matchPlayList[i].DisplayName = prefix + match.DisplayName + matchPlayList[i].DisplayName = match.TypePrefix() + match.DisplayName matchPlayList[i].Time = match.Time.Local().Format("3:04 PM") matchPlayList[i].Status = match.Status switch match.Winner { diff --git a/web/match_review.go b/web/match_review.go index 934a34f..b731374 100644 --- a/web/match_review.go +++ b/web/match_review.go @@ -183,16 +183,10 @@ func (web *Web) buildMatchReviewList(matchType string) ([]MatchReviewListItem, e return []MatchReviewListItem{}, err } - prefix := "" - if matchType == "practice" { - prefix = "P" - } else if matchType == "qualification" { - prefix = "Q" - } matchReviewList := make([]MatchReviewListItem, len(matches)) for i, match := range matches { matchReviewList[i].Id = match.Id - matchReviewList[i].DisplayName = prefix + match.DisplayName + matchReviewList[i].DisplayName = match.TypePrefix() + match.DisplayName matchReviewList[i].Time = match.Time.Local().Format("Mon 1/02 03:04 PM") matchReviewList[i].RedTeams = []int{match.Red1, match.Red2, match.Red3} matchReviewList[i].BlueTeams = []int{match.Blue1, match.Blue2, match.Blue3} diff --git a/web/queueing_display.go b/web/queueing_display.go index f402314..5c6e4e4 100644 --- a/web/queueing_display.go +++ b/web/queueing_display.go @@ -24,14 +24,7 @@ func (web *Web) queueingDisplayHandler(w http.ResponseWriter, r *http.Request) { return } - matchTypePrefix := "" - currentMatchType := web.arena.CurrentMatch.Type - if currentMatchType == "practice" { - matchTypePrefix = "P" - } else if currentMatchType == "qualification" { - matchTypePrefix = "Q" - } - matches, err := web.arena.Database.GetMatchesByType(currentMatchType) + matches, err := web.arena.Database.GetMatchesByType(web.arena.CurrentMatch.Type) if err != nil { handleWebErr(w, err) return @@ -58,7 +51,7 @@ func (web *Web) queueingDisplayHandler(w http.ResponseWriter, r *http.Request) { MatchTypePrefix string Matches []model.Match StatusMessage string - }{web.arena.EventSettings, matchTypePrefix, upcomingMatches, generateEventStatusMessage(matches)} + }{web.arena.EventSettings, web.arena.CurrentMatch.TypePrefix(), upcomingMatches, generateEventStatusMessage(matches)} err = template.ExecuteTemplate(w, "queueing_display.html", data) if err != nil { handleWebErr(w, err)