Skip to content

Commit

Permalink
Add RefereeType to new TournamentContext.RefereeRuleSet
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Jan 17, 2024
1 parent 50afa77 commit 02c97dc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ public void NumberOfMatchesOfFirstAndReturnLegShouldBeEqual(RefereeType refereeT
{
var participants = GetParticipants(6);
var tenantContext = new TenantContext();
tenantContext.TournamentContext.RefereeRuleSet.RefereeType = refereeType;

var matchCreator = new MatchCreator<long, long>(tenantContext, NullLogger<MatchCreator<long, long>>.Instance);
var combinationsFirstLeg =
matchCreator.SetParticipants(participants).GetCombinations(refereeType, LegType.First);
matchCreator.SetParticipants(participants).GetCombinations(LegType.First);
var combinationsReturnLeg =
matchCreator.SetParticipants(participants).GetCombinations(refereeType, LegType.Return);
matchCreator.SetParticipants(participants).GetCombinations(LegType.Return);

Assert.That(combinationsFirstLeg.Count, Is.EqualTo(combinationsReturnLeg.Count));
}
Expand All @@ -48,13 +50,14 @@ public void CreateMatchesWithRefereeType(RefereeType refereeType)
{
var participants = GetParticipants(5);

// TODO: RefereeType should be configurable via ITenantContext
var tenantContext = new TenantContext();
tenantContext.TournamentContext.RefereeRuleSet.RefereeType = refereeType;

// build up match combinations for the teams of round
var matchCreator = new MatchCreator<long, long>(tenantContext, NullLogger<MatchCreator<long, long>>.Instance);

var combinations =
matchCreator.SetParticipants(participants).GetCombinations(refereeType, LegType.First);
matchCreator.SetParticipants(participants).GetCombinations(LegType.First);

var firstCombination = combinations.First();
var expectedReferee = refereeType switch
Expand All @@ -76,10 +79,12 @@ public void OtherOfRoundRefereeShouldNeverBeHomeOrGuest()
{
var participants = GetParticipants(5);
var tenantContext = new TenantContext();
tenantContext.TournamentContext.RefereeRuleSet.RefereeType = RefereeType.OtherFromRound;

var matchCreator = new MatchCreator<long, long>(tenantContext, NullLogger<MatchCreator<long, long>>.Instance);
var combinations =
matchCreator.SetParticipants(participants).GetCombinations(RefereeType.OtherFromRound, LegType.First)
.Union(matchCreator.SetParticipants(participants).GetCombinations(RefereeType.OtherFromRound, LegType.Return));
matchCreator.SetParticipants(participants).GetCombinations(LegType.First)
.Union(matchCreator.SetParticipants(participants).GetCombinations(LegType.Return));

Assert.That(combinations.All(c => c.Home != c.Referee && c.Guest != c.Referee), Is.True, "Referee is never home or guest");
}
Expand All @@ -89,11 +94,13 @@ public void CreateMatchesWithUndefinedRefereeTypeShouldThrow()
{
var participants = GetParticipants(5);
var tenantContext = new TenantContext();
tenantContext.TournamentContext.RefereeRuleSet.RefereeType = (RefereeType) 12345;

var matchCreator = new MatchCreator<long, long>(tenantContext, NullLogger<MatchCreator<long, long>>.Instance);

Assert.Throws<ArgumentOutOfRangeException>(() =>
{
_ = matchCreator.SetParticipants(participants).GetCombinations((RefereeType) 12345, LegType.First);
_ = matchCreator.SetParticipants(participants).GetCombinations(LegType.First);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
namespace TournamentManager.Tests.RoundRobin;

internal struct CustomTestStruct : IEquatable<CustomTestStruct>
/// <summary>
/// IEquatable is implemented implicitly.
/// </summary>
internal record struct CustomTestStruct
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }

public readonly bool Equals(CustomTestStruct other)
{
return A == other.A && B == other.B && C == other.C;
}

public override readonly bool Equals(object? obj)
{
return obj is CustomTestStruct other && Equals(other);
}

public override readonly int GetHashCode()
{
return HashCode.Combine(A, B, C);
}

public static bool operator ==(CustomTestStruct left, CustomTestStruct right)
{
return left.Equals(right);
}

public static bool operator !=(CustomTestStruct left, CustomTestStruct right)
{
return !left.Equals(right);
}

public override readonly string ToString()
{
return $"{A}-{B}-{C}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public class TournamentContext : ITournamentContext
/// </summary>
[YAXLib.Attributes.YAXComment("The rules which apply for creating and editing team data")]
public TeamRules TeamRuleSet { get; set; } = new();

/// <summary>
/// Rules for organizing referees.
/// </summary>
[YAXLib.Attributes.YAXComment("Rules for organizing referees")]
public RefereeRules RefereeRuleSet { get; set; } = new();
}

public class FixtureRuleSet
Expand Down Expand Up @@ -159,6 +165,18 @@ public class TeamRules
public HomeVenue HomeVenue { get; set; } = new();
}

/// <summary>
/// Rules for organizing referees.
/// </summary>
public class RefereeRules
{
/// <summary>
/// Rules for teams' home match time
/// </summary>
[YAXLib.Attributes.YAXComment("Rules for organizing referees")]
public Plan.RefereeType RefereeType { get; set; } = Plan.RefereeType.None;
}

/// <summary>
/// Rules for the <see cref="HomeMatchTime"/> of a team.
/// </summary>
Expand Down
6 changes: 3 additions & 3 deletions TournamentManager/TournamentManager/Plan/MatchCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private void CreateCombinations(RefereeType refereeType)
// re-assign referee according to settings:
var referees = new List<TR>();
referees.AddRange((IEnumerable<TR>) Participants);

var refereeAssigner = RefereeAssigners<TP, TR>.GetRefereeAssigner(refereeType, referees);

for (var count = 0; count < _maxNumOfCombinations; count++)
Expand Down Expand Up @@ -111,12 +112,11 @@ private void CreateCombinationsReturnLeg(RefereeType refereeType)
/// <summary>
/// Gets all combinations for the given participants, using the round-robin system.
/// </summary>
/// <param name="refereeType">Determines how referees will be assigned for the matches.</param>
/// <param name="legType">First leg or return leg.</param>
/// <returns>Return a collection of participant combinations.</returns>
public ParticipantCombinations<TP, TR> GetCombinations(RefereeType refereeType, LegType legType)
public ParticipantCombinations<TP, TR> GetCombinations(LegType legType)
{
CreateCombinations(refereeType);
CreateCombinations(_tenantContext.TournamentContext.RefereeRuleSet.RefereeType);

return ((legType == LegType.First) ? _participantCombinationsFirstLeg : _participantCombinationsReturnLeg);
}
Expand Down
2 changes: 1 addition & 1 deletion TournamentManager/TournamentManager/Plan/MatchScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private ParticipantCombinations<long, long> CreateCombinations(Collection<long>
var matchCreator = new MatchCreator<long, long>(_tenantContext, _loggerFactory.CreateLogger<MatchCreator<long, long>>());
// TODO: RefereeType should be configurable via ITenantContext
var combinations =
matchCreator.SetParticipants(teams).GetCombinations(RefereeType.Home,
matchCreator.SetParticipants(teams).GetCombinations(
roundLeg.SequenceNo % 2 == 1 ? LegType.First : LegType.Return);
return combinations;
}
Expand Down

0 comments on commit 02c97dc

Please sign in to comment.