Implement translation of double-elimination match keys when pushing to TBA.

This commit is contained in:
Patrick Fairbank
2022-09-03 09:02:54 -07:00
parent 4bee40e66b
commit cf58a2174e
4 changed files with 69 additions and 53 deletions

View File

@@ -6,7 +6,6 @@
package model package model
import ( import (
"fmt"
"github.com/Team254/cheesy-arena-lite/game" "github.com/Team254/cheesy-arena-lite/game"
"sort" "sort"
"strings" "strings"
@@ -40,8 +39,6 @@ type Match struct {
Status game.MatchStatus Status game.MatchStatus
} }
var elimRoundNames = map[int]string{1: "F", 2: "SF", 4: "QF", 8: "EF"}
func (database *Database) CreateMatch(match *Match) error { func (database *Database) CreateMatch(match *Match) error {
return database.matchTable.create(match) return database.matchTable.create(match)
} }
@@ -141,16 +138,6 @@ func (match *Match) TypePrefix() string {
return "" return ""
} }
func (match *Match) TbaCode() string {
if match.Type == "qualification" {
return fmt.Sprintf("qm%s", match.DisplayName)
} else if match.Type == "elimination" {
return fmt.Sprintf("%s%dm%d", strings.ToLower(elimRoundNames[match.ElimRound]), match.ElimGroup,
match.ElimInstance)
}
return ""
}
// Returns true if the match is of a type that allows substitution of teams. // Returns true if the match is of a type that allows substitution of teams.
func (match *Match) ShouldAllowSubstitution() bool { func (match *Match) ShouldAllowSubstitution() bool {
return match.Type != "qualification" return match.Type != "qualification"

View File

@@ -112,18 +112,3 @@ func TestGetMatchesByType(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, 1, len(matches)) assert.Equal(t, 1, len(matches))
} }
func TestTbaCode(t *testing.T) {
match := Match{Type: "practice", DisplayName: "3"}
assert.Equal(t, "", match.TbaCode())
match = Match{Type: "qualification", DisplayName: "26"}
assert.Equal(t, "qm26", match.TbaCode())
match = Match{Type: "elimination", DisplayName: "EF2-1", ElimRound: 8, ElimGroup: 2, ElimInstance: 1}
assert.Equal(t, "ef2m1", match.TbaCode())
match = Match{Type: "elimination", DisplayName: "QF3-2", ElimRound: 4, ElimGroup: 3, ElimInstance: 2}
assert.Equal(t, "qf3m2", match.TbaCode())
match = Match{Type: "elimination", DisplayName: "SF1-3", ElimRound: 2, ElimGroup: 1, ElimInstance: 3}
assert.Equal(t, "sf1m3", match.TbaCode())
match = Match{Type: "elimination", DisplayName: "F2", ElimRound: 1, ElimGroup: 1, ElimInstance: 2}
assert.Equal(t, "f1m2", match.TbaCode())
}

View File

@@ -48,15 +48,17 @@ type TbaAlliance struct {
} }
type TbaRanking struct { type TbaRanking struct {
TeamKey string `json:"team_key"` TeamKey string `json:"team_key"`
Rank int `json:"rank"` Rank int `json:"rank"`
RP float32 RP float32
Auto int Auto int
Endgame int Endgame int
Teleop int Teleop int
WinLossTie string Wins int `json:"wins"`
Dqs int `json:"dqs"` Losses int `json:"losses"`
Played int `json:"played"` Ties int `json:"ties"`
Dqs int `json:"dqs"`
Played int `json:"played"`
} }
type TbaRankings struct { type TbaRankings struct {
@@ -101,6 +103,33 @@ type TbaPublishedAward struct {
Awardee string `json:"awardee"` Awardee string `json:"awardee"`
} }
type elimMatchKey struct {
elimRound int
elimGroup int
}
type tbaElimMatchKey struct {
compLevel string
setNumber int
}
var doubleEliminationMatchKeyMapping = map[elimMatchKey]tbaElimMatchKey{
{1, 1}: {"ef", 1},
{1, 2}: {"ef", 2},
{1, 3}: {"ef", 3},
{1, 4}: {"ef", 4},
{2, 1}: {"ef", 5},
{2, 2}: {"ef", 6},
{2, 3}: {"qf", 1},
{2, 4}: {"qf", 2},
{3, 1}: {"qf", 3},
{3, 2}: {"qf", 4},
{4, 1}: {"sf", 1},
{4, 2}: {"sf", 2},
{5, 1}: {"f", 1},
{6, 1}: {"f", 2},
}
func NewTbaClient(eventCode, secretId, secret string) *TbaClient { func NewTbaClient(eventCode, secretId, secret string) *TbaClient {
return &TbaClient{BaseUrl: tbaBaseUrl, eventCode: eventCode, secretId: secretId, secret: secret, return &TbaClient{BaseUrl: tbaBaseUrl, eventCode: eventCode, secretId: secretId, secret: secret,
eventNamesCache: make(map[string]string)} eventNamesCache: make(map[string]string)}
@@ -266,6 +295,10 @@ func (client *TbaClient) PublishMatches(database *model.Database) error {
if err != nil { if err != nil {
return err return err
} }
eventSettings, err := database.GetEventSettings()
if err != nil {
return err
}
matches := append(qualMatches, elimMatches...) matches := append(qualMatches, elimMatches...)
tbaMatches := make([]TbaMatch, len(matches)) tbaMatches := make([]TbaMatch, len(matches))
@@ -301,9 +334,7 @@ func (client *TbaClient) PublishMatches(database *model.Database) error {
tbaMatches[i] = TbaMatch{"qm", 0, matchNumber, alliances, match.Time.Local().Format("3:04 PM"), tbaMatches[i] = TbaMatch{"qm", 0, matchNumber, alliances, match.Time.Local().Format("3:04 PM"),
match.Time.UTC().Format("2006-01-02T15:04:05")} match.Time.UTC().Format("2006-01-02T15:04:05")}
if match.Type == "elimination" { if match.Type == "elimination" {
tbaMatches[i].CompLevel = map[int]string{1: "f", 2: "sf", 4: "qf", 8: "ef"}[match.ElimRound] setElimMatchKey(&tbaMatches[i], &match, eventSettings.ElimType)
tbaMatches[i].SetNumber = match.ElimGroup
tbaMatches[i].MatchNumber = match.ElimInstance
} }
} }
jsonBody, err := json.Marshal(tbaMatches) jsonBody, err := json.Marshal(tbaMatches)
@@ -331,14 +362,21 @@ func (client *TbaClient) PublishRankings(database *model.Database) error {
} }
// Build a JSON object of TBA-format rankings. // Build a JSON object of TBA-format rankings.
breakdowns := []string{"RP", "Auto", "Endgame", "Teleop", "WinLossTie"} breakdowns := []string{"RP", "Auto", "Endgame", "Teleop"}
tbaRankings := make([]TbaRanking, len(rankings)) tbaRankings := make([]TbaRanking, len(rankings))
for i, ranking := range rankings { for i, ranking := range rankings {
tbaRankings[i] = TbaRanking{getTbaTeam(ranking.TeamId), ranking.Rank, tbaRankings[i] = TbaRanking{
float32(ranking.RankingPoints) / float32(ranking.Played), ranking.AutoPoints, ranking.EndgamePoints, TeamKey: getTbaTeam(ranking.TeamId),
ranking.TeleopPoints, Rank: ranking.Rank,
fmt.Sprintf("%d-%d-%d", ranking.Wins, ranking.Losses, ranking.Ties), 0, RP: float32(ranking.RankingPoints) / float32(ranking.Played),
ranking.Played} Auto: ranking.AutoPoints,
Endgame: ranking.EndgamePoints,
Teleop: ranking.TeleopPoints,
Wins: ranking.Wins,
Losses: ranking.Losses,
Ties: ranking.Ties,
Played: ranking.Played,
}
} }
jsonBody, err := json.Marshal(TbaRankings{breakdowns, tbaRankings}) jsonBody, err := json.Marshal(TbaRankings{breakdowns, tbaRankings})
if err != nil { if err != nil {
@@ -503,11 +541,17 @@ func (client *TbaClient) PublishAwards(database *model.Database) error {
return nil return nil
} }
// Returns the sum of all values in the slice representing different stages for a power cell goal. // Sets the match key attributes on TbaMatch based on the match and bracket type.
func sumPowerCells(cells []int) int { func setElimMatchKey(tbaMatch *TbaMatch, match *model.Match, elimType string) {
var total int if elimType == "single" {
for _, cell := range cells { tbaMatch.CompLevel = map[int]string{1: "ef", 2: "qf", 3: "sf", 4: "f"}[match.ElimRound]
total += cell tbaMatch.SetNumber = match.ElimGroup
tbaMatch.MatchNumber = match.ElimInstance
} else if elimType == "double" {
if tbaKey, ok := doubleEliminationMatchKeyMapping[elimMatchKey{match.ElimRound, match.ElimGroup}]; ok {
tbaMatch.CompLevel = tbaKey.compLevel
tbaMatch.SetNumber = tbaKey.setNumber
}
tbaMatch.MatchNumber = match.ElimInstance
} }
return total
} }

View File

@@ -43,7 +43,7 @@ func TestPublishMatches(t *testing.T) {
match1 := model.Match{Type: "qualification", DisplayName: "2", Time: time.Unix(600, 0), Red1: 7, Red2: 8, Red3: 9, match1 := model.Match{Type: "qualification", DisplayName: "2", Time: time.Unix(600, 0), Red1: 7, Red2: 8, Red3: 9,
Blue1: 10, Blue2: 11, Blue3: 12, Status: game.RedWonMatch} Blue1: 10, Blue2: 11, Blue3: 12, Status: game.RedWonMatch}
match2 := model.Match{Type: "elimination", DisplayName: "SF2-2", ElimRound: 2, ElimGroup: 2, ElimInstance: 2} match2 := model.Match{Type: "elimination", DisplayName: "SF2-2", ElimRound: 3, ElimGroup: 2, ElimInstance: 2}
database.CreateMatch(&match1) database.CreateMatch(&match1)
database.CreateMatch(&match2) database.CreateMatch(&match2)
matchResult1 := model.BuildTestMatchResult(match1.Id, 1) matchResult1 := model.BuildTestMatchResult(match1.Id, 1)