Skip to content

Commit

Permalink
feat(api): Tournament handles double dip as b side match (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
codephobia authored Feb 19, 2024
1 parent 26be154 commit 91bcd66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion libs/go/challonge/challonge.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (c *Challonge) getNextMatchForTable(table int) (*Match, error) {
}

// set race for a/b side
if match.IsOnASide() {
if c.Tournament.matchIsDoubleDip(match) || (!c.Tournament.matchIsDoubleDip(match) && match.IsOnASide()) {
if err := c.tables[table].Game.SetRaceTo(c.Settings.ASideRaceTo); err != nil {
return nil, err
}
Expand Down
34 changes: 25 additions & 9 deletions libs/go/challonge/tournament.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ type ParticipantsResp []struct {

// Tournament is a tournament bracket on the Challonge account.
type Tournament struct {
ID int `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
TournamentType string `json:"tournament_type"`
Rounds int `json:"rounds"`
Matches []*Match `json:"matches"`
CreatedAt time.Time `json:"created_at"`
CompletedAt *time.Time `json:"completed_at"`
ID int `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
TournamentType string `json:"tournament_type"`
ParticipantsCount int `json:"participants_count"`
Rounds int `json:"rounds"`
Matches []*Match `json:"matches"`
CreatedAt time.Time `json:"created_at"`
CompletedAt *time.Time `json:"completed_at"`

Participants []*Participant `json:"participants"`
}
Expand Down Expand Up @@ -265,6 +266,21 @@ func (t *Tournament) getParticipants(username, apiKey string) error {
return nil
}

// Returns if the match is the last possible match in the tournament
func (t *Tournament) matchIsDoubleDip(match *Match) bool {
if t.TournamentType == "double elimination" && match.SuggestedPlayOrder == t.getTotalMatchCount() {
return true
}

return false
}

// Returns the total number of matches possible, including the double dip
func (t *Tournament) getTotalMatchCount() int {
// TODO: handle single elimination here
return ((t.ParticipantsCount - 1) * 2) + 1
}

func (t *Tournament) getMatchesByPlayOrder() []*Match {
sortedMatches := make([]*Match, len(t.Matches))
copy(sortedMatches, t.Matches)
Expand Down Expand Up @@ -304,7 +320,7 @@ func getLatestTournaments(username, apiKey string) ([]*Tournament, error) {
for _, tournament := range tournamentsResp {
currentTournament := tournament.Tournament

if currentTournament.CompletedAt == nil {
if currentTournament.CompletedAt == nil {
tournaments = append(tournaments, &currentTournament)
}
}
Expand Down

0 comments on commit 91bcd66

Please sign in to comment.