Skip to content

Commit

Permalink
Support max rounds on generating tournaments
Browse files Browse the repository at this point in the history
  • Loading branch information
thordy committed Dec 1, 2024
1 parent 7b6283e commit 2e00250
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
37 changes: 25 additions & 12 deletions data/tournament.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,15 @@ func GenerateTournament(input models.GenerateTournamentInput) (*models.Tournamen
Players: []int{players[i].PlayerID, players[j].PlayerID},
Legs: []*models.Leg{{
StartingScore: input.StartingScore,
Parameters: &models.LegParameters{OutshotType: &models.OutshotType{ID: models.OUTSHOTDOUBLE}}}},
Parameters: &models.LegParameters{
OutshotType: &models.OutshotType{ID: models.OUTSHOTDOUBLE},
MaxRounds: func() null.Int {
if input.MaxRounds != -1 {
return null.IntFrom(int64(input.MaxRounds))
}
return null.Int{}
}(),
}}},
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -921,13 +929,15 @@ func GeneratePlayoffsTournament(tournamentID int, input models.GeneratePlayoffsI
}
var regularSeasonMatch models.Match
var startingScore int
maxRounds := null.Int{}
for _, value := range regularSeasonMatches {
regularSeasonMatch = *value[0]
legs, err := GetLegsForMatch(regularSeasonMatch.ID)
if err != nil {
return nil, err
}
startingScore = legs[0].StartingScore
maxRounds = legs[0].Parameters.MaxRounds
break
}
matchType := regularSeasonMatch.MatchType
Expand Down Expand Up @@ -978,7 +988,7 @@ func GeneratePlayoffsTournament(tournamentID int, input models.GeneratePlayoffsI
matches := make([]*models.Match, 0)
// Create Grand Final
match, err := createTournamentMatch(playoffs.ID, []int{placeholderHomeID, placeholderAwayID}, startingScore, -1,
tournament.OfficeID, matchType, input.MatchModeGFID)
tournament.OfficeID, matchType, input.MatchModeGFID, maxRounds)
if err != nil {
return nil, err
}
Expand All @@ -987,7 +997,7 @@ func GeneratePlayoffsTournament(tournamentID int, input models.GeneratePlayoffsI
// Create Semi Final Matches
if numPlayers > 4 {
semis, err := createTournamentMatches(2, playoffs.ID, []int{placeholderHomeID, placeholderAwayID}, startingScore, -1,
tournament.OfficeID, matchType, input.MatchModeSFID)
tournament.OfficeID, matchType, input.MatchModeSFID, maxRounds)
if err != nil {
return nil, err
}
Expand All @@ -1013,7 +1023,7 @@ func GeneratePlayoffsTournament(tournamentID int, input models.GeneratePlayoffsI
away = walkoverPlayerID
}
match, err := createTournamentMatch(playoffs.ID, []int{home, away}, startingScore, -1,
tournament.OfficeID, matchType, input.MatchModeSFID)
tournament.OfficeID, matchType, input.MatchModeSFID, maxRounds)
if err != nil {
return nil, err
}
Expand All @@ -1024,7 +1034,7 @@ func GeneratePlayoffsTournament(tournamentID int, input models.GeneratePlayoffsI
// Create Quarter Final Matches
if numPlayers > 8 {
quarters, err := createTournamentMatches(4, playoffs.ID, []int{placeholderHomeID, placeholderAwayID}, startingScore, -1,
tournament.OfficeID, matchType, input.MatchModeQFID)
tournament.OfficeID, matchType, input.MatchModeQFID, maxRounds)
if err != nil {
return nil, err
}
Expand All @@ -1044,7 +1054,7 @@ func GeneratePlayoffsTournament(tournamentID int, input models.GeneratePlayoffsI
away = walkoverPlayerID
}
match, err := createTournamentMatch(playoffs.ID, []int{home, away}, startingScore, -1,
tournament.OfficeID, matchType, input.MatchModeLast16ID)
tournament.OfficeID, matchType, input.MatchModeLast16ID, maxRounds)
if err != nil {
return nil, err
}
Expand All @@ -1071,7 +1081,7 @@ func GeneratePlayoffsTournament(tournamentID int, input models.GeneratePlayoffsI
away = walkoverPlayerID
}
match, err := createTournamentMatch(playoffs.ID, []int{home, away}, startingScore, -1,
tournament.OfficeID, matchType, input.MatchModeQFID)
tournament.OfficeID, matchType, input.MatchModeQFID, maxRounds)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1158,10 +1168,10 @@ func GeneratePlayoffsTournament(tournamentID int, input models.GeneratePlayoffsI
return GetTournament(playoffs.ID)
}

func createTournamentMatches(num int, tournamentID int, players []int, startingScore int, venueID int, officeID int, matchType *models.MatchType, matchModeID int) ([]*models.Match, error) {
func createTournamentMatches(num int, tournamentID int, players []int, startingScore int, venueID int, officeID int, matchType *models.MatchType, matchModeID int, maxRounds null.Int) ([]*models.Match, error) {
matches := make([]*models.Match, 0)
for i := 0; i < num; i++ {
match, err := createTournamentMatch(tournamentID, players, startingScore, venueID, officeID, matchType, matchModeID)
match, err := createTournamentMatch(tournamentID, players, startingScore, venueID, officeID, matchType, matchModeID, maxRounds)
if err != nil {
return nil, err
}
Expand All @@ -1170,7 +1180,7 @@ func createTournamentMatches(num int, tournamentID int, players []int, startingS
return matches, nil
}

func createTournamentMatch(tournamentID int, players []int, startingScore int, venueID int, officeID int, matchType *models.MatchType, matchModeID int) (*models.Match, error) {
func createTournamentMatch(tournamentID int, players []int, startingScore int, venueID int, officeID int, matchType *models.MatchType, matchModeID int, maxRounds null.Int) (*models.Match, error) {
match, err := NewMatch(models.Match{
MatchType: matchType,
MatchMode: &models.MatchMode{ID: matchModeID},
Expand All @@ -1181,7 +1191,10 @@ func createTournamentMatch(tournamentID int, players []int, startingScore int, v
Players: players,
Legs: []*models.Leg{{
StartingScore: startingScore,
Parameters: &models.LegParameters{OutshotType: &models.OutshotType{ID: models.OUTSHOTDOUBLE}}}},
Parameters: &models.LegParameters{
OutshotType: &models.OutshotType{ID: models.OUTSHOTDOUBLE},
MaxRounds: maxRounds,
}}},
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -1383,7 +1396,7 @@ func insertMetadata(matches []*models.MatchMetadata) error {
if err != nil {
return err
}
stmt, err := tx.Prepare(`INSERT INTO match_metadata (match_id, order_of_play, tournament_group_id, match_displayname, elimination, promotion,
stmt, err := tx.Prepare(`INSERT INTO match_metadata (match_id, order_of_play, tournament_group_id, match_displayname, elimination, promotion,
trophy, semi_final, grand_final, winner_outcome_match_id, is_winner_outcome_home) VALUES (?, ?, ?, ?, 1, 0, 0, ?, ?, ?, ?)`)
if err != nil {
tx.Rollback()
Expand Down
1 change: 1 addition & 0 deletions models/tournament.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ type GenerateTournamentInput struct {
MatchModeID int `json:"match_mode_id"`
MatchTypeID int `json:"match_type_id"`
StartingScore int `json:"starting_score"`
MaxRounds int `json:"max_rounds"`
Players []*Player2Tournament `json:"players,omitempty"`
}

Expand Down

0 comments on commit 2e00250

Please sign in to comment.