Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Tournament handles double dip as b side match #12

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading