Skip to content

Commit

Permalink
Merge comparer classes into one class RankComparer (#115)
Browse files Browse the repository at this point in the history
RankComparer can now handle the following rules to build ranking tables:
* LegacyRankComparer: The rules Augsburg volleyball leagues until August 2023 for 2 or 3 winning set matches (depending on season)
* TwoWinningSetsRankComparer: The rules of Bavarian Volleyball Association (BVV) as of 2020-06-19 for 2 winning set matches.
*  ThreeWinningSetsRankComparer: The rules of German Volleyball National League (DVV), in use since season 2013/14 for 3 winning set matches, and applying the 3-point rule.
* HroThreeWinningSetsRankComparer: The rules of the Volleyball-Stadtliga Rostock, starting with season 2023/24 for 3 winning set matches, and applying the 3-point rule.

Refactor RankComparer and Ranking:
* Change RankComparerEnum to RankComparison enum
* Remove double initialization of Rank properties
* Add property RankComparison to IRankComparer
* Remove setters from properties of type PointResult
* RankComparer.Ranking is not nullable
* Fix initialization of RankingChart.GraphBackgroundColorArgb and RankingChart.PlotAreaBackgroundColorArgb
* RankingTeamHistory: Remove parameterless constructor
* Add unit tests: Increase coverage of namespace TournamentManager.Ranking to 93%

Bump version to v6.6.0
  • Loading branch information
axunonb authored Sep 17, 2023
1 parent 4301e52 commit 274314e
Show file tree
Hide file tree
Showing 20 changed files with 1,933 additions and 417 deletions.
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<Copyright>Copyright 2011-$(CurrentYear) axuno gGmbH</Copyright>
<RepositoryUrl>https://github.com/axuno/Volleyball-League</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Version>6.5.0</Version>
<FileVersion>6.5.0</FileVersion>
<Version>6.6.0</Version>
<FileVersion>6.6.0</FileVersion>
<AssemblyVersion>6.0.0.0</AssemblyVersion> <!--only update AssemblyVersion with major releases -->
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
Expand Down
2 changes: 1 addition & 1 deletion League/BackgroundTasks/RankingUpdateTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ await TenantContext.DbContext.AppDb.TeamInRoundRepository.GetTeamInRoundAsync(
await TenantContext.DbContext.AppDb.RoundRepository.GetMatchRuleAsync(roundId, cancellationToken);
// filter matches to only contain a single round
var newRanking = new Ranking(matchesPlayed.Where(mp => mp.RoundId == roundId),
matchesToPlay.Where(mtp => mtp.RoundId == roundId), (RankComparerEnum) matchRule.RankComparer);
matchesToPlay.Where(mtp => mtp.RoundId == roundId), (RankComparison) matchRule.RankComparer);
// Save the current last update
var currentLastUpdated = currentRanking.Any() ? currentRanking.Where(l => l.RoundId == roundId).Max(l => l.ModifiedOn) : DateTime.MinValue;
var newRankingList = newRanking.GetList(out var newLastUpdated);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using FluentAssertions;
using NUnit.Framework;
using TournamentManager.DAL.EntityClasses;
using TournamentManager.DAL.TypedViewClasses;
using TournamentManager.Ranking;

namespace TournamentManager.Tests.Ranking;

[TestFixture]
internal class GeneralRankingTests
{
[TestCase(RankComparison.LegacyRankComparison)]
[TestCase(RankComparison.TwoWinningSetsRankComparison)]
[TestCase(RankComparison.ThreeWinningSetsRankComparison)]
[TestCase(RankComparison.HroThreeWinningSetsRankComparison)]
public void CreateRankingWithRankComparisonTests(RankComparison comparison)
{
var ranking = new TournamentManager.Ranking.Ranking(new List<MatchCompleteRawRow>(),
new List<MatchToPlayRawRow>(), comparison);

Assert.That(ranking.RankComparer.RankComparison, Is.EqualTo(comparison));
Assert.That(ranking.RankComparer.Description.Length, Is.AtLeast(1));
}

[Test]
public void CreateRankingWithUnknownRankComparisonShouldThrow()
{
Assert.That(() =>
{
var ranking = new TournamentManager.Ranking.Ranking(new List<MatchCompleteRawRow>(),
new List<MatchToPlayRawRow>(), (RankComparison) int.MaxValue);

}, Throws.Exception.TypeOf<ArgumentOutOfRangeException>());


}

[Test]
public void RankPointsCalculationTests()
{
var matchId = 1;
var matches = new List<MatchEntity> {
RankingTestUtilities.GetMatch(matchId++, 2, 1, "25:1 25:2 25:3"),
RankingTestUtilities.GetMatch(matchId, 1, 2, "1:25 2:25 25:3 25:4 15:14")
};

var ranking = new TournamentManager.Ranking.Ranking(RankingTestUtilities.CreateMatchCompleteRows(matches),
new List<MatchToPlayRawRow> { new() { HomeTeamId = 3, GuestTeamId = 4 } },
RankComparison.ThreeWinningSetsRankComparison);

var ranks = ranking.GetList(out var lastUpdateOn);
var r1 = ranks[0];

Assert.Multiple(() =>
{
Assert.That(lastUpdateOn, Is.EqualTo(DateTime.MinValue));
Assert.That(ranks.LastUpdatedOn, Is.EqualTo(lastUpdateOn));
Assert.That(r1.MatchesPlayed, Is.EqualTo(2));
Assert.That(r1.MatchesToPlay, Is.EqualTo(0));
Assert.That(ranks[3].MatchesToPlay, Is.EqualTo(1));

Assert.That(ranks.Count, Is.EqualTo(4)); // 2 played for Team Ids 1 and 2, 2 to play for Team Ids 3 and 4
Assert.That(r1.TeamId, Is.EqualTo(2));
Assert.That(r1.ToString(), Is.EqualTo("1"));

r1.MatchesWon.Should().BeEquivalentTo(new PointResult(1, 1));
r1.MatchPoints.Should().BeEquivalentTo(new PointResult(4, 2));
r1.SetsWon.Should().BeEquivalentTo(new PointResult(5, 3));
r1.SetPoints.Should().BeEquivalentTo(new PointResult(5, 3));
r1.BallPoints.Should().BeEquivalentTo(new PointResult(146, 74));
});
}

[Test]
public void GetMatchDaysTest()
{
var matchId = 1;
var completedMatches = RankingTestUtilities.CreateMatchCompleteRows(new List<MatchEntity> {
RankingTestUtilities.GetMatch(matchId++, 2, 1, "25:1 25:2 25:3"),
RankingTestUtilities.GetMatch(matchId, 1, 2, "25:1 25:2 25:3")
});
completedMatches[0].MatchDate = new DateTime(2024, 7, 1);
completedMatches[1].MatchDate = new DateTime(2024, 7, 2);

var ranking = new TournamentManager.Ranking.Ranking(completedMatches,
new List<MatchToPlayRawRow>(), RankComparison.ThreeWinningSetsRankComparison);

var matchDays = ranking.GetMatchDays();

Assert.That(matchDays.Count, Is.EqualTo(2));
}

[Test]
public void GetRankingHistoryTest()
{
var matchId = 1;
var completedMatches = RankingTestUtilities.CreateMatchCompleteRows(new List<MatchEntity> {
RankingTestUtilities.GetMatch(matchId++, 2, 1, "25:1 25:2 25:3"),
RankingTestUtilities.GetMatch(matchId, 1, 2, "25:3 25:4 25:5")
});
completedMatches[0].MatchDate = new DateTime(2024, 7, 1);
completedMatches[1].MatchDate = new DateTime(2024, 7, 2);

var ranking = new TournamentManager.Ranking.Ranking(completedMatches,
new List<MatchToPlayRawRow>(), RankComparison.ThreeWinningSetsRankComparison);

var history = ranking.GetRankingHistory();
history.ReCalculate();
history = ranking.GetRankingHistory();
var chart = new RankingChart(ranking, new List<(long TeamId, string TeamName)> { (1, "1"), (2, "2") },
new RankingChart.ChartSettings()) { UseMatchDayMarker = true, ShowUpperDateLimit = true };

Assert.That(history.GetMatchDays().Count, Is.EqualTo(2));
Assert.That(history.GetByTeam(1).Count, Is.EqualTo(2));
Assert.That(history.GetByMatchDay().Count, Is.EqualTo(2));
Assert.That(() => { chart.GetSvg(); chart.GetPng(); }, Throws.Nothing);
}
}
Loading

0 comments on commit 274314e

Please sign in to comment.