-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubleElimination.go
293 lines (248 loc) · 8.94 KB
/
doubleElimination.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package tournament
import (
"fmt"
"math"
"sort"
"github.com/justinjudd/competition/models"
)
type roundType int
var doubleEliminationBrackets = []string{"Winning Bracket", "Losing Bracket", "Finals"}
const (
first roundType = iota // The first round of double elimination
lMajor // Play the loser bracket
lMinor // Wait to fill in the loser's bracket
noL // No teams in the Losers bracket queue
final // Play the final round
finalExtra // Play an extra final round (The team from the losers bracket won, so now each team has one loss)
)
// DoubleElimination fulfills the Tournament interface. Provides the logic for running a Tournament of a Double Elimination type. Commonly used as a conclusion of a season or competition
type DoubleElimination struct {
models.Tournament
gameCounter int
playIn int
gamesBracket map[uint32]int
roundType
winnerQue []models.Team
losersQue []models.Team
}
func NewDoubleElimination(baseTournament models.Tournament) models.Tournament {
teams := baseTournament.GetTeams()
tmp := math.Log2(float64(len(teams)))
var playInGames int
if tmp != math.Floor(tmp) {
playInGames = len(teams) - int(math.Pow(math.Floor(tmp), 2.0))
}
return &DoubleElimination{baseTournament, 0, playInGames, map[uint32]int{}, 0, []models.Team{}, []models.Team{}}
}
func (d *DoubleElimination) GetBracketOrder() []string {
return doubleEliminationBrackets
}
func (d *DoubleElimination) GetActiveStage() models.Tournament {
return d
}
func (d *DoubleElimination) Start() {
d.SetStatus(models.Status_ONGOING)
}
func (c *DoubleElimination) StartRound() {
if c.GetStatus() == models.Status_COMPLETED {
return
}
lastRound := c.Tournament.GetActiveRound()
lastRound.SetStatus(models.Status_ONGOING)
}
func getTeamCountToInclude(teamCount int, gameSize uint32) int {
tmp := math.Log2(float64(teamCount))
return int(math.Pow(float64(gameSize), math.Floor(tmp)))
}
func (c *DoubleElimination) NextRound() (models.Round, error) {
lastRound := c.GetActiveRound()
var winningTeams, losingTeams []models.Team
gameSize := int(c.Tournament.GetGameSize())
moveForward := c.GetAdvancing()
if len(c.GetAllRounds()) == 0 || lastRound == nil {
//Create first round
winningTeams = c.GetTeams()
idealTeamNum := int(math.Pow(2.0, math.Ceil(math.Log2(float64(len(winningTeams))))))
for i := len(winningTeams); i < idealTeamNum; i++ {
winningTeams = append(winningTeams, nil)
}
if c.IsSeeded() {
winningTeams = seed(winningTeams)
}
} else {
if c.roundType == first {
c.roundType = lMajor
}
if lastRound.GetStatus() != models.Status_COMPLETED {
return nil, fmt.Errorf("Can't start new round until previous round is completed")
}
losingQue := []models.Team{}
for _, game := range lastRound.GetGames() {
teams := game.GetTeams()
places := game.GetPlaces()
teamSlice := make([]TeamScore, 0)
for i, teamPlaced := range places {
if len(teams) <= i {
break
}
if models.IsByeTeam(teams[i]) {
continue
}
teamSlice = append(teamSlice, TeamScore{teams[i], int(teamPlaced)})
}
sort.Slice(teamSlice, BasicTeamScoreLess(teamSlice))
switch game.GetBracket() {
case doubleEliminationBrackets[0]: //Winner's Bracket
for _, teamPlaced := range teamSlice[:moveForward] { // Winners stay in the Winner's Bracket
c.winnerQue = append(c.winnerQue, teamPlaced.Team)
}
for _, teamPlaced := range teamSlice[moveForward:] { // Losers go to the Loser's Bracket
losingQue = append(losingQue, teamPlaced.Team)
}
case doubleEliminationBrackets[1]: // Losers Bracket
for _, teamPlaced := range teamSlice[:moveForward] { // Only Winners stay in, losers are eliminated
c.losersQue = append(c.losersQue, teamPlaced.Team)
}
case doubleEliminationBrackets[2]: // Final round(s)
winner := teamSlice[0].Team
records := winner.GetRecords()
prevGame := records[len(records)-1]
bracket := prevGame.GetBracket()
switch bracket {
case doubleEliminationBrackets[0]: // Winner of the Winner's Bracket won the final round (only one game needed)
fmt.Println("GAME OVER", winner.GetName(), "has won")
c.SetStatus(models.Status_COMPLETED)
return nil, fmt.Errorf("Too many rounds @ %d", len(c.GetAllRounds()))
case doubleEliminationBrackets[1]: // Winner of the Loser's Bracket one, second final round will be needed
// Need second final round
c.roundType = finalExtra
fmt.Println("Need second final round")
for _, teamPlaced := range teamSlice[:moveForward] {
c.winnerQue = append(c.winnerQue, teamPlaced.Team)
}
for _, teamPlaced := range teamSlice[moveForward:] {
losingQue = append(losingQue, teamPlaced.Team)
}
case doubleEliminationBrackets[2]: // The second Final Round was played, winner of it is the overall winner
fmt.Println("GAME OVER", winner.GetName(), "has won")
c.SetStatus(models.Status_COMPLETED)
return nil, fmt.Errorf("Too many rounds @ %d", len(c.GetAllRounds()))
}
}
}
c.losersQue = append(c.losersQue, losingQue...)
rounds := c.GetAllRounds()
if len(rounds) < 1 {
switch len(rounds) {
case 0:
winningTeams = c.winnerQue
c.winnerQue = []models.Team{}
case 1: // Handle losers play-in game
losersSize := len(c.losersQue)
for i := 0; i < c.playIn; i++ {
losingTeams = append(losingTeams, c.losersQue[i])
losingTeams = append(losingTeams, c.losersQue[losersSize-1-i])
}
c.losersQue = c.losersQue[c.playIn:]
c.losersQue = c.losersQue[:losersSize-1-c.playIn]
}
} else {
switch c.roundType {
case first, noL, lMinor:
c.roundType = lMajor
case lMajor:
c.roundType = lMinor
}
if len(c.losersQue) == 0 {
c.roundType = noL
}
if len(c.losersQue) > gameSize/2 {
losingTeams = c.losersQue
c.losersQue = []models.Team{}
} else {
// How many teams should I pull?
}
gameSize := c.GetGameSize()
if len(c.winnerQue) > int(c.GetAdvancing()) {
winningTeams = c.winnerQue
c.winnerQue = []models.Team{}
}
// Figure out if we need to move to the Final round
if (len(winningTeams) == 0 && len(losingTeams) == 0) &&
((len(c.winnerQue) <= int(c.GetAdvancing()) && len(c.losersQue) <= int(c.GetAdvancing())) ||
(len(c.winnerQue) == 0 && len(c.losersQue) == int(gameSize))) {
c.roundType = final
}
}
}
r, err := c.Tournament.NextRound()
if err != nil {
return r, err
}
r.SetStatus(models.Status_NEW)
if c.roundType == final {
teams := []models.Team{}
teams = append(teams, c.winnerQue...)
teams = append(teams, c.losersQue...)
c.winnerQue = []models.Team{}
c.losersQue = []models.Team{}
game := r.CreateGame(teams, c.IsScored())
game.SetBracket(doubleEliminationBrackets[2])
} else {
half := gameSize / 2
if len(winningTeams) > int(c.GetAdvancing()) {
//avoidRematches(winningTeams, int(gameSize))
gameCount := int(math.Ceil(float64(len(winningTeams)) / float64(gameSize)))
shortGames := gameCount*gameSize - len(winningTeams)
if len(winningTeams) < gameSize {
shortGames = 1
}
for i := 0; i < gameCount-shortGames; i++ {
game := r.CreateGame(winningTeams[i*gameSize:(i+1)*gameSize], c.IsScored())
game.SetBracket(doubleEliminationBrackets[0])
}
place := (gameCount - shortGames) * gameSize
for i := gameCount - shortGames; i < (gameCount - 1); i++ {
game := r.CreateGame(winningTeams[place:place+gameSize-1], c.IsScored())
game.SetBracket(doubleEliminationBrackets[0])
}
if shortGames > 0 {
game := r.CreateGame(winningTeams[place:], c.IsScored())
game.SetBracket(doubleEliminationBrackets[0])
}
}
if c.roundType == lMinor { //after first round, we should "shuffle" the teams up so that the people that just dropped from the winning round aren't playing each other
if len(losingTeams) > 0 && len(c.GetAllRounds()) != 2 {
halfTeamPoint := len(losingTeams) / 2
for i, team := range losingTeams[:halfTeamPoint] {
if i%2 == 1 { //"shuffle" every other
continue
}
team2 := losingTeams[i+halfTeamPoint]
losingTeams[i], losingTeams[i+halfTeamPoint] = team2, team
}
}
}
if len(losingTeams) > half { // Check if there are enough teams to play the losers bracket
//avoidRematches(losingTeams, int(gameSize))
gameCount := int(math.Ceil(float64(len(losingTeams)) / float64(gameSize)))
shortGames := gameCount*gameSize - len(losingTeams)
for i := 0; i < gameCount-shortGames; i++ {
game := r.CreateGame(losingTeams[i*gameSize:(i+1)*gameSize], c.IsScored())
game.SetBracket(doubleEliminationBrackets[1])
}
place := (gameCount - shortGames) * gameSize
for i := gameCount - shortGames; i < gameCount; i++ {
shortTeams := gameSize - 1
if i < 0 {
shortTeams--
i++
}
game := r.CreateGame(losingTeams[place:place+shortTeams], c.IsScored())
game.SetBracket(doubleEliminationBrackets[1])
place += shortTeams
}
}
}
return r, nil
}