Skip to content

Commit

Permalink
feat(api): automate table dropping
Browse files Browse the repository at this point in the history
  • Loading branch information
codephobia committed Apr 12, 2024
1 parent e8680f9 commit b8f7dfc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
27 changes: 19 additions & 8 deletions libs/go/challonge/challonge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package challonge
import (
"fmt"
"log"
"math"
"regexp"
"strconv"

Expand Down Expand Up @@ -135,13 +136,7 @@ func (c *Challonge) Continue(table int) error {
}

// should we drop max tables
// TODO: REMOVE THIS HACKY SHIT
if c.CurrentMatches[table].Round == -4 && c.Settings.MaxTables == 3 {
c.Settings.MaxTables = 2
}
if c.CurrentMatches[table].Round == -5 && c.Settings.MaxTables == 2 {
c.Settings.MaxTables = 1
}
c.checkMaxTables()

// unset the current table
c.CurrentMatches[table] = nil
Expand Down Expand Up @@ -387,11 +382,27 @@ func (c *Challonge) checkMatchForByes(match *Match) (bool, error) {
return false, nil
}

// checkMaxTables checks to see if we should reduce the max tables in use.
func (c *Challonge) checkMaxTables() {
// matches remaining divided by 2 minus 1
remaining := c.Tournament.RemainingMatches()
newMaxTables := int(math.Ceil(float64(remaining) / 2))

if newMaxTables < 0 {
newMaxTables = 1
}

if newMaxTables < c.Settings.MaxTables {
c.Settings.MaxTables = newMaxTables
}
}

// flagUnusedTables flags all tables above max tables as unused.
func (c *Challonge) flagUnusedTables() error {
for i := 1; i <= len(c.tables); i++ {
// check if current table is now outside of max tables bounds
if i > c.Settings.MaxTables {
// and table no longer has a match on it
if i > c.Settings.MaxTables && c.CurrentMatches[i] == nil {
c.tables[i].Overlay.TableNoLongerInUse = true

// unset waiting for players since this will never happen now
Expand Down
13 changes: 13 additions & 0 deletions libs/go/challonge/tournament.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ func (t *Tournament) HasMoreMatches() bool {
return false
}

// RemainingMatches returns the number of matches yet to be played.
func (t *Tournament) RemainingMatches() int {
count := 0

for i := range t.Matches {
if t.Matches[i].State != "complete" {
count++
}
}

return count
}

// CountParallelMatches returns a count of the maximum number of matches that could be played
// in parallel.
func (t *Tournament) CountParallelMatches() int {
Expand Down

0 comments on commit b8f7dfc

Please sign in to comment.