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

Best of match scheduling #136

Merged
merged 15 commits into from
Nov 21, 2024
Merged
1 change: 0 additions & 1 deletion data/lib/api/tournament/tournament_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ enum TournamentType {
miniRobin(2, minTeamReq: 3),
boxLeague(3, minTeamReq: 4),
doubleOut(4, minTeamReq: 4),
superOver(5, minTeamReq: 2),
bestOf(6, minTeamReq: 2),
custom(7, minTeamReq: 2);

Expand Down
1 change: 0 additions & 1 deletion data/lib/api/tournament/tournament_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions khelo/assets/locales/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,13 @@
"tournament_type_mini_robin": "Mini Robin",
"tournament_type_box_league": "Box League",
"tournament_type_double_out": "Double Out",
"tournament_type_super_over": "Super Over",
"tournament_type_best_of": "Best Of",
"tournament_type_best_of": "Best Of three",
"tournament_type_custom": "Custom",

"tournament_type_knock_out_description": "Teams face off in a single elimination, with the loser immediately knocked out.\nMinimum {count} teams required.",
"tournament_type_mini_robin_description": "A smaller round-robin format where each team plays once against all others.\nMinimum {count} teams required.",
"tournament_type_box_league_description": "Teams are divided into groups, with top teams advancing to the knockout stage.\nMinimum {count} teams required.",
"tournament_type_double_out_description": "Teams get two chances before being knocked out, with a winners and losers bracket.\nMinimum {count} teams required.",
"tournament_type_super_over_description": "A knockout format with a super over to decide tied matches.\nMinimum {count} teams required.",
"tournament_type_best_of_description": "Teams play a series of matches, and the first to win the majority is the champion.\nMinimum {count} teams required.",
"tournament_type_custom_description": "Fully flexible, create a structure that suits your needs.\nMinimum {count} teams required.",

Expand Down
4 changes: 0 additions & 4 deletions khelo/lib/domain/extensions/enum_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,6 @@ extension TournamentTypeString on TournamentType {
return context.l10n.tournament_type_box_league;
case TournamentType.doubleOut:
return context.l10n.tournament_type_double_out;
case TournamentType.superOver:
return context.l10n.tournament_type_super_over;
case TournamentType.bestOf:
return context.l10n.tournament_type_best_of;
case TournamentType.custom:
Expand All @@ -321,8 +319,6 @@ extension TournamentTypeString on TournamentType {
return context.l10n.tournament_type_box_league_description(minTeamReq);
case TournamentType.doubleOut:
return context.l10n.tournament_type_double_out_description(minTeamReq);
case TournamentType.superOver:
return context.l10n.tournament_type_super_over_description(minTeamReq);
case TournamentType.bestOf:
return context.l10n.tournament_type_best_of_description(minTeamReq);
case TournamentType.custom:
Expand Down
96 changes: 91 additions & 5 deletions khelo/lib/ui/flow/tournament/match_selection/match_scheduler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ class MatchScheduler {
return scheduleBoxLeagueMatches();
case TournamentType.doubleOut:
return scheduleDoubleOutMatches();
case TournamentType.superOver:
return scheduleSuperOverMatches();
case TournamentType.bestOf:
return scheduleBestOfMatches();
case TournamentType.custom:
Expand Down Expand Up @@ -340,12 +338,100 @@ class MatchScheduler {
return additionalScheduledMatches;
}

GroupedMatchMap scheduleSuperOverMatches() {
return {};
List<List<TeamModel>> createTeamPairsForBestOfThree(List<TeamModel> teams) {
final List<List<TeamModel>> pairs = [];
for (int i = 0; i < teams.length; i++) {
for (int j = i + 1; j < teams.length; j++) {
pairs.add([teams[i], teams[j]]);
}
}
return pairs;
}

MatchModel createBestOfThreeMatch(
TeamModel teamA,
TeamModel teamB,
MatchGroup group,
int roundNumber,
) {
return MatchModel(
id: '',
teams: [
MatchTeamModel(team_id: teamA.id, team: teamA),
MatchTeamModel(team_id: teamB.id, team: teamB),
],
match_type: MatchType.limitedOvers,
number_of_over: 10,
over_per_bowler: 2,
city: '',
ground: '',
ball_type: BallType.leather,
pitch_type: PitchType.rough,
created_by: '',
match_status: MatchStatus.yetToStart,
match_group: group,
match_group_number: roundNumber,
);
}

GroupedMatchMap scheduleBestOfMatches() {
return {};
final GroupedMatchMap additionalScheduledMatches = scheduledMatches;
final List<TeamModel> teamPool = List.from(teams);
final Map<String, TeamModel> roundWinners = {};

final roundOne = additionalScheduledMatches[MatchGroup.round]?[1] ?? [];

final teamPairs = createTeamPairsForBestOfThree(teamPool);

for (var pair in teamPairs) {
final teamA = pair[0];
final teamB = pair[1];

final existingMatches = roundOne.where((match) {
return match.teams.any((e) => pair.contains(e.team));
}).toList();

for (int i = existingMatches.length; i < 2; i++) {
final match = createBestOfThreeMatch(teamA, teamB, MatchGroup.round, 1);
roundOne.add(match);
}

if (existingMatches.length > 2) {
final teamAWins = existingMatches
.where((e) => e.matchResult?.teamId == teamA.id)
.length;
final teamBWins = existingMatches
.where((e) => e.matchResult?.teamId == teamB.id)
.length;

if (teamAWins == 1 && teamBWins == 1) {
final thirdMatch =
createBestOfThreeMatch(teamA, teamB, MatchGroup.round, 1);
roundOne.add(thirdMatch);
} else if (teamAWins == 2) {
roundWinners[teamA.id] = teamA;
} else if (teamBWins == 2) {
roundWinners[teamB.id] = teamB;
}
}
}
additionalScheduledMatches.addAll({
MatchGroup.round: {1: roundOne}
});

if (roundWinners.length == 2) {
final teamA = roundWinners.values.first;
final teamB = roundWinners.values.last;
final finalMatch =
createBestOfThreeMatch(teamA, teamB, MatchGroup.finals, 1);
additionalScheduledMatches.addAll({
MatchGroup.finals: {
1: [finalMatch]
}
});
}

return additionalScheduledMatches;
}

// Helper functions
Expand Down
Loading