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

Double out match scheduling #135

Merged
merged 2 commits into from
Nov 14, 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
4 changes: 1 addition & 3 deletions data/lib/api/tournament/tournament_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ enum TournamentType {
doubleOut(4, minTeamReq: 4),
superOver(5, minTeamReq: 2),
bestOf(6, minTeamReq: 2),
gully(7, minTeamReq: 2),
mixed(8, minTeamReq: 2),
other(9, minTeamReq: 2);
custom(7, minTeamReq: 2);

final int value;
final int minTeamReq;
Expand Down
4 changes: 1 addition & 3 deletions 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.

2 changes: 1 addition & 1 deletion data/lib/service/tournament/tournament_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ class TournamentService {
id: tournamentId,
name: '',
created_by: '',
type: TournamentType.other,
type: TournamentType.knockOut,
start_date: DateTime.now(),
end_date: DateTime.now().add(const Duration(days: 1)),
);
Expand Down
8 changes: 2 additions & 6 deletions khelo/assets/locales/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,15 @@
"tournament_type_double_out": "Double Out",
"tournament_type_super_over": "Super Over",
"tournament_type_best_of": "Best Of",
"tournament_type_gully": "Gully",
"tournament_type_mixed": "Mixed",
"tournament_type_other": "Other",
"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_gully_description": "Casual street-style cricket with short games and flexible rules.\nMinimum {count} teams required.",
"tournament_type_mixed_description": "Teams are randomly mixed, creating fun and unpredictable matches.\nMinimum {count} teams required.",
"tournament_type_other_description": "A custom format for tournaments with unique rules or structures.\nMinimum {count} teams required.",
"tournament_type_custom_description": "Fully flexible, create a structure that suits your needs.\nMinimum {count} teams required.",

"add_team_screen_title": "Add Team",
"add_team_edit_team_screen_title": "Edit Team",
Expand Down
16 changes: 4 additions & 12 deletions khelo/lib/domain/extensions/enum_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,8 @@ extension TournamentTypeString on TournamentType {
return context.l10n.tournament_type_super_over;
case TournamentType.bestOf:
return context.l10n.tournament_type_best_of;
case TournamentType.gully:
return context.l10n.tournament_type_gully;
case TournamentType.mixed:
return context.l10n.tournament_type_mixed;
case TournamentType.other:
return context.l10n.tournament_type_other;
case TournamentType.custom:
return context.l10n.tournament_type_custom;
}
}

Expand All @@ -329,12 +325,8 @@ extension TournamentTypeString on TournamentType {
return context.l10n.tournament_type_super_over_description(minTeamReq);
case TournamentType.bestOf:
return context.l10n.tournament_type_best_of_description(minTeamReq);
case TournamentType.gully:
return context.l10n.tournament_type_gully_description(minTeamReq);
case TournamentType.mixed:
return context.l10n.tournament_type_mixed_description(minTeamReq);
case TournamentType.other:
return context.l10n.tournament_type_other_description(minTeamReq);
case TournamentType.custom:
return context.l10n.tournament_type_custom_description(minTeamReq);
}
}
}
Expand Down
84 changes: 74 additions & 10 deletions khelo/lib/ui/flow/tournament/match_selection/match_scheduler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ class MatchScheduler {
case TournamentType.boxLeague:
return scheduleBoxLeagueMatches();
case TournamentType.doubleOut:
return scheduleDoubleEliminationMatches();
return scheduleDoubleOutMatches();
case TournamentType.superOver:
return scheduleSuperOverMatches();
case TournamentType.bestOf:
return scheduleBestOfMatches();
default:
throw Exception('Unsupported match type');
case TournamentType.custom:
return scheduledMatches;
}
}

Expand All @@ -88,7 +88,7 @@ class MatchScheduler {
groupNumbers.forEach((number, matches) {
removeAlreadyScheduledTeams(matches, teamPool);

final teamPairs = createTeamPairsForKnockout(teamPool);
final teamPairs = createKnockoutTeamPairs(teamPool);
addMatches(matches, teamPairs, group, number);

teamPool.removeWhere((team) => teamPairs
Expand Down Expand Up @@ -143,7 +143,7 @@ class MatchScheduler {
rounds.forEach((number, matches) {
removeTeamsThatReachedLimit(matches, teamPool, matchesPerTeam);

final teamPairs = createTeamPairsForRoundRobin(
final teamPairs = createRoundRobinTeamPairs(
teamPool,
matchesPerTeam,
matches
Expand Down Expand Up @@ -224,7 +224,7 @@ class MatchScheduler {
teams.addAll(teamPool.take(boxSize - teams.length + 1));
}

final teamPairs = createTeamPairsForRoundRobin(
final teamPairs = createRoundRobinTeamPairs(
teams,
teams.length - 1,
matches
Expand Down Expand Up @@ -305,8 +305,39 @@ class MatchScheduler {
return additionalScheduledMatches;
}

GroupedMatchMap scheduleDoubleEliminationMatches() {
return {};
GroupedMatchMap scheduleDoubleOutMatches() {
final GroupedMatchMap additionalScheduledMatches = scheduledMatches;
final teamPool = List.of(teams);

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

final win = scheduleSingleBracketTeams(roundOne, teamPool, true);
final los = scheduleSingleBracketTeams(roundTwo, teamPool, false);
final rounds = {1: roundOne};
if (roundTwo.isNotEmpty) {
rounds.addAll({2: roundTwo});
}

additionalScheduledMatches.addAll({MatchGroup.round: rounds});
if (win != null && los != null) {
addNewGroupToGroupMap(additionalScheduledMatches, MatchGroup.finals);
final finalRounds =
additionalScheduledMatches[MatchGroup.finals]![1] ?? [];
bool isMatchAdded = finalRounds.any((match) =>
match.teams.map((e) => e.team_id).contains(win.id) &&
match.teams.map((e) => e.team_id).contains(los.id));
if (!isMatchAdded) {
addMatches(
additionalScheduledMatches[MatchGroup.finals]![1] ?? [],
[
[win, los]
],
MatchGroup.finals,
1);
}
}
return additionalScheduledMatches;
}

GroupedMatchMap scheduleSuperOverMatches() {
Expand Down Expand Up @@ -403,7 +434,7 @@ class MatchScheduler {
return teamPoints;
}

List<List<TeamModel>> createTeamPairsForRoundRobin(List<TeamModel> teamPool,
List<List<TeamModel>> createRoundRobinTeamPairs(List<TeamModel> teamPool,
int matchesPerTeam, List<List<TeamModel>> scheduledMatches) {
final List<List<TeamModel>> teamPairs = [];

Expand Down Expand Up @@ -444,7 +475,7 @@ class MatchScheduler {
return teamPairs;
}

List<List<TeamModel>> createTeamPairsForKnockout(List<TeamModel> teams) {
List<List<TeamModel>> createKnockoutTeamPairs(List<TeamModel> teams) {
final List<TeamModel> shuffledTeams = List.from(teams)..shuffle();
return shuffledTeams.chunked(2);
}
Expand Down Expand Up @@ -541,4 +572,37 @@ class MatchScheduler {
MapEntry(newGroup, {1: []})
]);
}

TeamModel? scheduleSingleBracketTeams(
List<MatchModel> matches,
List<TeamModel> teams,
bool isWinBracket,
) {
final finishedMatches =
matches.where((element) => element.match_status == MatchStatus.finish);

final loserTeams = finishedMatches.map((e) => e.teams
.firstWhere((element) => element.team_id != e.matchResult!.teamId)
.team);
final unFinishedMatches =
matches.where((element) => element.match_status != MatchStatus.finish);
teams.removeWhere((team) =>
loserTeams.contains(team) ||
unFinishedMatches
.any((element) => element.teams.map((e) => e.team).contains(team)));

final chunks = createKnockoutTeamPairs(teams);
addMatches(matches, chunks, MatchGroup.round, isWinBracket ? 1 : 2);
TeamModel? teamToReturn;
if (unFinishedMatches.isEmpty && teams.length == 1) {
teamToReturn = teams.first;
}
teams
.removeWhere((team) => chunks.any((element) => element.contains(team)));

if (isWinBracket) {
teams.addAll(loserTeams);
}
return teamToReturn;
}
}
Loading