Centralize some match-type-dependent logic.

This commit is contained in:
Patrick Fairbank
2019-07-20 22:42:56 -07:00
parent 016fcc907b
commit 02fa6954bd
6 changed files with 41 additions and 32 deletions

View File

@@ -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)

View File

@@ -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"
}

View File

@@ -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)

View File

@@ -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 {

View File

@@ -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}

View File

@@ -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)