Skip to content

Commit

Permalink
Implement translation of double-elimination match keys when pushing t…
Browse files Browse the repository at this point in the history
…o TBA.
  • Loading branch information
patfair committed Sep 3, 2022
1 parent 4bee40e commit cf58a21
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 53 deletions.
13 changes: 0 additions & 13 deletions model/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package model

import (
"fmt"
"github.com/Team254/cheesy-arena-lite/game"
"sort"
"strings"
Expand Down Expand Up @@ -40,8 +39,6 @@ type Match struct {
Status game.MatchStatus
}

var elimRoundNames = map[int]string{1: "F", 2: "SF", 4: "QF", 8: "EF"}

func (database *Database) CreateMatch(match *Match) error {
return database.matchTable.create(match)
}
Expand Down Expand Up @@ -141,16 +138,6 @@ func (match *Match) TypePrefix() string {
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.
func (match *Match) ShouldAllowSubstitution() bool {
return match.Type != "qualification"
Expand Down
15 changes: 0 additions & 15 deletions model/match_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,3 @@ func TestGetMatchesByType(t *testing.T) {
assert.Nil(t, err)
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())
}
92 changes: 68 additions & 24 deletions partner/tba.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ type TbaAlliance struct {
}

type TbaRanking struct {
TeamKey string `json:"team_key"`
Rank int `json:"rank"`
RP float32
Auto int
Endgame int
Teleop int
WinLossTie string
Dqs int `json:"dqs"`
Played int `json:"played"`
TeamKey string `json:"team_key"`
Rank int `json:"rank"`
RP float32
Auto int
Endgame int
Teleop int
Wins int `json:"wins"`
Losses int `json:"losses"`
Ties int `json:"ties"`
Dqs int `json:"dqs"`
Played int `json:"played"`
}

type TbaRankings struct {
Expand Down Expand Up @@ -101,6 +103,33 @@ type TbaPublishedAward struct {
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 {
return &TbaClient{BaseUrl: tbaBaseUrl, eventCode: eventCode, secretId: secretId, secret: secret,
eventNamesCache: make(map[string]string)}
Expand Down Expand Up @@ -266,6 +295,10 @@ func (client *TbaClient) PublishMatches(database *model.Database) error {
if err != nil {
return err
}
eventSettings, err := database.GetEventSettings()
if err != nil {
return err
}
matches := append(qualMatches, elimMatches...)
tbaMatches := make([]TbaMatch, len(matches))

Expand Down Expand Up @@ -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"),
match.Time.UTC().Format("2006-01-02T15:04:05")}
if match.Type == "elimination" {
tbaMatches[i].CompLevel = map[int]string{1: "f", 2: "sf", 4: "qf", 8: "ef"}[match.ElimRound]
tbaMatches[i].SetNumber = match.ElimGroup
tbaMatches[i].MatchNumber = match.ElimInstance
setElimMatchKey(&tbaMatches[i], &match, eventSettings.ElimType)
}
}
jsonBody, err := json.Marshal(tbaMatches)
Expand Down Expand Up @@ -331,14 +362,21 @@ func (client *TbaClient) PublishRankings(database *model.Database) error {
}

// 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))
for i, ranking := range rankings {
tbaRankings[i] = TbaRanking{getTbaTeam(ranking.TeamId), ranking.Rank,
float32(ranking.RankingPoints) / float32(ranking.Played), ranking.AutoPoints, ranking.EndgamePoints,
ranking.TeleopPoints,
fmt.Sprintf("%d-%d-%d", ranking.Wins, ranking.Losses, ranking.Ties), 0,
ranking.Played}
tbaRankings[i] = TbaRanking{
TeamKey: getTbaTeam(ranking.TeamId),
Rank: ranking.Rank,
RP: float32(ranking.RankingPoints) / float32(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})
if err != nil {
Expand Down Expand Up @@ -503,11 +541,17 @@ func (client *TbaClient) PublishAwards(database *model.Database) error {
return nil
}

// Returns the sum of all values in the slice representing different stages for a power cell goal.
func sumPowerCells(cells []int) int {
var total int
for _, cell := range cells {
total += cell
// Sets the match key attributes on TbaMatch based on the match and bracket type.
func setElimMatchKey(tbaMatch *TbaMatch, match *model.Match, elimType string) {
if elimType == "single" {
tbaMatch.CompLevel = map[int]string{1: "ef", 2: "qf", 3: "sf", 4: "f"}[match.ElimRound]
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
}
2 changes: 1 addition & 1 deletion partner/tba_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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(&match2)
matchResult1 := model.BuildTestMatchResult(match1.Id, 1)
Expand Down

0 comments on commit cf58a21

Please sign in to comment.