Skip to content

Commit

Permalink
Update MatchScheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Jan 20, 2024
1 parent 3d20cc4 commit 47a13b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions TournamentManager/TournamentManager/Plan/MatchScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private void SetMatchDates(RoundEntity round, ParticipantCombinations<long, long
// Get the selected turn combinations for the given turn.
var selectedTurnCombinations = combinations.GetCombinations(turn).ToList();

// Get match dates for every combination of a group.
// Get match dates for every combination of a turn.
// Matches in the same turnCombinations can even take place at the same time.
var datesFound = GetMatchDatesForTurn(turn, combinations, roundMatches);
_logger.LogDebug("Found dates for combination: {dates}",
Expand All @@ -139,7 +139,7 @@ private void SetMatchDates(RoundEntity round, ParticipantCombinations<long, long
.Select(bd => bd?.MatchStartTime.ToShortDateString() ?? "(null)"))
.Trim(',', ' '));

// MatchDates contains calculated dates in the same order as turn combinations,
// datesFound contains calculated dates in the same sequence as turn combinations,
// so the index can be used for both.
for (var index = 0; index < selectedTurnCombinations.Count; index++)
{
Expand Down Expand Up @@ -276,6 +276,8 @@ private static List<DateTime> GetOccupiedMatchDates(ParticipantCombination<long,
var turnCombinationsPeriod = combinations.TurnDateTimePeriods[turn]!.Value;

// These are possible date alternatives per combination:
// Outer list: One item per combination
// Inner list: The list of date alternatives for the combination
var matchDates = new List<List<AvailableMatchDateEntity>>();

for (var index = 0; index < turnCombinations.Count; index++)
Expand All @@ -287,6 +289,7 @@ private static List<DateTime> GetOccupiedMatchDates(ParticipantCombination<long,
// initialize MinTimeDiff for the whole list
availableDatesForCombination.ForEach(amd => amd.MinTimeDiff = TimeSpan.MaxValue);

#if DEBUG
// Get the last match for at least one of the teams, if any
var lastMatchOfCombination = roundMatches.OrderBy(gm => gm.PlannedStart).LastOrDefault(gm =>
gm.HomeTeamId == combination.Home || gm.GuestTeamId == combination.Guest);
Expand All @@ -298,8 +301,8 @@ private static List<DateTime> GetOccupiedMatchDates(ParticipantCombination<long,
{
_logger.LogDebug("No matches yet for home team '{homeTeam}' or guest team '{guestTeam}'", combination.Home, combination.Guest);
}

// If no dates could be found, the date will be left blank.
#endif
// If no dates could be found, the date will be set to null.
if (availableDatesForCombination.Count == 0)
{
_logger.LogDebug(
Expand All @@ -324,7 +327,10 @@ private static List<DateTime> GetOccupiedMatchDates(ParticipantCombination<long,

/// <summary>
/// Finds the best match dates from a list of available match dates for each combination.
/// The best match is the date that has the smallest distance to the smallest date in the other turn(s).
/// If no match dates could be determined for a team, bestDate will be set to null.
/// <para/>
/// This method is optimizing across all combinations of all turns.
/// </summary>
/// <param name="availableMatchDates"></param>
private static List<AvailableMatchDateEntity?> FindBestDates(List<List<AvailableMatchDateEntity>> availableMatchDates)
Expand Down Expand Up @@ -358,12 +364,12 @@ private static List<DateTime> GetOccupiedMatchDates(ParticipantCombination<long,
} // end dates1
} // end j

// Get the date that has the least distance to the most small date in other turn(s)
// Get the date that has the smallest distance to the smallest date in the other turn(s).
// Note: If no match dates could be determined for a team, bestDate will be null.
var bestDate = availableMatchDates[i]
.Where(md => md.MinTimeDiff == availableMatchDates[i]
.Min(d => d.MinTimeDiff))
.OrderBy(md => md.MinTimeDiff).FirstOrDefault();
.MinBy(md => md.MinTimeDiff);
bestMatchDatesPerCombination.Add(bestDate);

// process the last combination
Expand All @@ -372,7 +378,9 @@ private static List<DateTime> GetOccupiedMatchDates(ParticipantCombination<long,
// now the "j-loop" group is not processed yet:
if (i + 1 >= availableMatchDates.Count - 1)
{
bestDate = availableMatchDates[^1].Where(md => md.MinTimeDiff == availableMatchDates[^1].Min(d => d.MinTimeDiff))
bestDate = availableMatchDates[^1]
.Where(md => md.MinTimeDiff == availableMatchDates[^1].
Min(d => d.MinTimeDiff))
.MinBy(md => md.MinTimeDiff);
// the last "j-increment" is always the same as "matchDates[^1]" (loop condition)
bestMatchDatesPerCombination.Add(bestDate);
Expand Down
2 changes: 1 addition & 1 deletion TournamentManager/TournamentManager/TournamentCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public bool CopyRound(long fromTournamentId, long toTournamentId, IList<long> ex
newRound.RoundLegs.Add(newRoundLeg);
}

// save recursively (new round with its new round legs)
// save recursively (new round with the new round legs)
if (! da.SaveEntity(newRound, true, true))
{
// roll back if any round fails
Expand Down

0 comments on commit 47a13b1

Please sign in to comment.