Skip to content

Commit

Permalink
Merge pull request #181 from SSchulze1989/feature/penalty-bonus
Browse files Browse the repository at this point in the history
Feature/penalty bonus
  • Loading branch information
SSchulze1989 authored Dec 18, 2023
2 parents 24fd204 + efa764e commit d3b48f1
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/iRLeagueApiCore.Client/iRLeagueApiCore.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<OutputType>Library</OutputType>
<TargetFrameworks>net6.0</TargetFrameworks>
<PackageId>iRLeagueApiCore.Client</PackageId>
<Version>0.10.1</Version>
<Version>0.10.2-dev.1</Version>
<Authors>Simon Schulze</Authors>
<Company>Simon Schulze</Company>
<PackageDescription>This package contains shared objects for all members of the iRLeagueDatabase-iRLeagueApi stack</PackageDescription>
Expand Down
2 changes: 1 addition & 1 deletion src/iRLeagueApiCore.Server/iRLeagueApiCore.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>0.10.1</Version>
<Version>0.10.2-dev.1</Version>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,14 @@ private static IEnumerable<ResultRowCalculationResult> ApplyAddPenaltyPoints(IEn
{
foreach (var penalty in row.AddPenalties.Where(x => x.Type == PenaltyType.Points))
{
row.PenaltyPoints += penalty.Points;
if (penalty.Points < 0)
{
row.BonusPoints -= penalty.Points;
}
else
{
row.PenaltyPoints += penalty.Points;
}
}
}
return rows;
Expand Down Expand Up @@ -250,7 +257,14 @@ private static IEnumerable<ResultRowCalculationResult> ApplyReviewPenalties(IEnu
PenaltyPoints = vote.DefaultPenalty,
};
row.ReviewPenalties.Add(penalty);
row.PenaltyPoints += penalty.PenaltyPoints;
if (penalty.PenaltyPoints < 0)
{
row.BonusPoints -= penalty.PenaltyPoints;
}
else
{
row.PenaltyPoints += penalty.PenaltyPoints;
}
}
}
return rows;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public async Task Calculate_ShouldApplyPointsPenalty()
{
const int rowCount = 3;
const int spread = 5;
const int penaltyIndex = 0;
const int penaltyIndex = 1;
// create fixed intervals to test sorting
var points = Enumerable.Range(0, rowCount)
.Select(x => (rowCount - x) * spread)
Expand All @@ -365,11 +365,11 @@ public async Task Calculate_ShouldApplyPointsPenalty()
.With(x => x.Type, PenaltyType.Points)
.With(x => x.Points, spread + 1)
.Create();
var penaltyRow = data.ResultRows.ElementAt(0);
var penaltyRow = data.ResultRows.ElementAt(penaltyIndex);
penaltyRow.AddPenalties = new[] { addPenalty };
var config = GetCalculationConfiguration(data.LeagueId, data.SessionId);
config.PointRule = CalculationMockHelper.MockPointRule(
sortFinal: x => x.OrderBy(x => x.TotalPoints).ToList());
sortFinal: x => x.OrderByDescending(x => x.TotalPoints).ToList());
fixture.Register(() => config);
var sut = CreateSut();

Expand All @@ -382,6 +382,41 @@ public async Task Calculate_ShouldApplyPointsPenalty()
test.ResultRows.ToList().IndexOf(testResultRow).Should().Be(expIndex);
}

[Fact]
public async Task Calculate_ShouldApplyNegativePointsPenaltyAsBonus()
{
const int rowCount = 3;
const int spread = 5;
const int penaltyIndex = 1;
// create fixed intervals to test sorting
var points = Enumerable.Range(0, rowCount)
.Select(x => (rowCount - x) * spread)
.CreateSequence();
var data = GetCalculationData();
data.ResultRows = TestRowBuilder()
.With(x => x.RacePoints, () => points())
.CreateMany(rowCount);
var addPenalty = fixture.Build<AddPenaltyCalculationData>()
.With(x => x.Type, PenaltyType.Points)
.With(x => x.Points, -(spread + 1))
.Create();
var penaltyRow = data.ResultRows.ElementAt(penaltyIndex);
penaltyRow.AddPenalties = new[] { addPenalty };
var config = GetCalculationConfiguration(data.LeagueId, data.SessionId);
config.PointRule = CalculationMockHelper.MockPointRule(
sortFinal: x => x.OrderByDescending(x => x.TotalPoints).ToList());
fixture.Register(() => config);
var sut = CreateSut();

var test = await sut.Calculate(data);

var testResultRow = test.ResultRows.First(x => x.ScoredResultRowId == penaltyRow.ScoredResultRowId);
var expIndex = penaltyIndex - 1;
testResultRow.BonusPoints.Should().Be(-addPenalty.Points);
testResultRow.TotalPoints.Should().Be(testResultRow.RacePoints + testResultRow.BonusPoints);
test.ResultRows.ToList().IndexOf(testResultRow).Should().Be(expIndex);
}

[Fact]
public async Task Calculate_ShouldApplyPositionBonusPoints()
{
Expand Down

0 comments on commit d3b48f1

Please sign in to comment.