Skip to content

Commit

Permalink
* Fix position change not shown when driver did not participate in la…
Browse files Browse the repository at this point in the history
…test race

* Fix position change shown for the first event
  • Loading branch information
SSchulze1989 committed Dec 1, 2024
1 parent a65957b commit 2236c57
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public override Task<StandingCalculationResult> Calculate(StandingCalculationDat
var finalStandingRows = new List<StandingRowCalculationResult>();
foreach (var (memberStandingRow, position) in memberStandingRows.Select((x, i) => (x, i + 1)))
{
memberStandingRow.current.PositionChange = -(position - memberStandingRow.previous.Position);
if (data.PreviousEventResults.None())
{
memberStandingRow.current.PositionChange = 0;
}
memberStandingRow.current.Position = position;
var final = DiffStandingRows(memberStandingRow.previous, memberStandingRow.current);
finalStandingRows.Add(final);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ protected static StandingRowCalculationResult DiffStandingRows(StandingRowCalcul
diff.LeadLapsChange = current.LeadLaps - previous.LeadLaps;
diff.PenaltyPointsChange = current.PenaltyPoints - previous.PenaltyPoints;
diff.PolePositionsChange = current.PolePositions - previous.PolePositions;
diff.PositionChange = -(current.Position - previous.Position);
diff.RacePointsChange = current.RacePoints - previous.RacePoints;
diff.TotalPointsChange = current.TotalPoints - previous.TotalPoints;
diff.WinsChange = current.Wins - previous.Wins;
Expand Down
6 changes: 6 additions & 0 deletions src/iRLeagueDatabaseCore/PopulateTestDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ private static string GetRandomIracingId(Random random)

public static class PopulateDatabaseExtensions
{
public static T PopFirst<T>(this ICollection<T> collection)
{
var pop = collection.First();
collection.Remove(pop);
return pop;
}
/// <summary>
/// Returns a random entry from the list and removes it from the list at the same time
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,41 @@ public async Task Calculate_ShouldCombinePracticeAndQualyPoints_ForMultiHeader()
testRow.TotalPoints.Should().Be(-9);
}

[Fact]
public async Task Calculate_ShouldSetPositionChangeZero_WhenFirstEvent()
{
var nEvents = 1;
var nRaces = 3;
var data = CalculationDataBuilder(nEvents, nRaces, false).Create();

var config = CalculationConfigurationBuilder(data.LeagueId, data.EventId).Create();
config.SortOptions = [SortOptions.TotalPtsDesc];
var sut = CreateSut(config);

var test = await sut.Calculate(data);
test.StandingRows.Should().AllSatisfy(x => x.PositionChange.Should().Be(0));
}

[Fact]
public async Task Calculate_ShouldSetPositionChange_WhenDriverMissingInLastEvent()
{
var nEvents = 3;
var nRaces = 1;
var memberIds = fixture.CreateMany<long>(3);
var testMemberId = memberIds.ElementAt(1);
var data = CalculationDataBuilder(nEvents, nRaces, false)
.With(x => x.PreviousEventResults, () => EventResultDataBuilder(memberIds, nRaces: nRaces, randomMemberOrder: false).CreateMany(nEvents - 1).ToList())
.With(x => x.CurrentEventResult, () => EventResultDataBuilder(memberIds.Except([testMemberId]), nRaces: nRaces, randomMemberOrder: false).Create())
.Create();

var config = CalculationConfigurationBuilder(data.LeagueId, data.EventId).Create();
var sut = CreateSut(config);

var test = await sut.Calculate(data);
var testRow = test.StandingRows.First(x => x.MemberId == testMemberId);
testRow.PositionChange.Should().Be(-1);
}

private static MemberStandingCalculationService CreateSut(StandingCalculationConfiguration config)
{
return new(config);
Expand All @@ -405,21 +440,26 @@ private StandingCalculationData GetCalculationData()
return CalculationDataBuilder().Create();
}

private IPostprocessComposer<StandingCalculationData> CalculationDataBuilder(int nEvents = 3, int nRacesPerEvent = 3, bool hasCombinedResult = false)
private IPostprocessComposer<StandingCalculationData> CalculationDataBuilder(int nEvents = 3, int nRacesPerEvent = 3, bool hasCombinedResult = false,
bool randomMemberOrder = true)
{
var memberIds = fixture.CreateMany<long>(10);
return fixture.Build<StandingCalculationData>()
.With(x => x.PreviousEventResults, () => EventResultDataBuilder(memberIds, nRacesPerEvent, hasCombinedResult: hasCombinedResult).CreateMany(nEvents - 1).ToList())
.With(x => x.CurrentEventResult, () => EventResultDataBuilder(memberIds, nRacesPerEvent, hasCombinedResult: hasCombinedResult).Create());
.With(x => x.PreviousEventResults, () => EventResultDataBuilder(memberIds, nRacesPerEvent, hasCombinedResult: hasCombinedResult,
randomMemberOrder: randomMemberOrder).CreateMany(nEvents - 1).ToList())
.With(x => x.CurrentEventResult, () => EventResultDataBuilder(memberIds, nRacesPerEvent, hasCombinedResult: hasCombinedResult,
randomMemberOrder: randomMemberOrder).Create());
}

private IPostprocessComposer<EventCalculationResult> EventResultDataBuilder(IEnumerable<long> memberIds, int nRaces = 2, bool hasCombinedResult = false)
private IPostprocessComposer<EventCalculationResult> EventResultDataBuilder(IEnumerable<long> memberIds, int nRaces = 2, bool hasCombinedResult = false,
bool randomMemberOrder = true)
{
return fixture.Build<EventCalculationResult>()
.With(x => x.SessionResults, () => SessionResultDataBuilder(memberIds).CreateMany(nRaces));
.With(x => x.SessionResults, () => SessionResultDataBuilder(memberIds, randomMemberOrder: randomMemberOrder).CreateMany(nRaces));
}

private IPostprocessComposer<SessionCalculationResult> SessionResultDataBuilder(IEnumerable<long> memberIds, SessionType sessionType = SessionType.Race)
private IPostprocessComposer<SessionCalculationResult> SessionResultDataBuilder(IEnumerable<long> memberIds, SessionType sessionType = SessionType.Race,
bool randomMemberOrder = true)
{
return fixture
.Build<SessionCalculationResult>()
Expand All @@ -429,7 +469,7 @@ private IPostprocessComposer<SessionCalculationResult> SessionResultDataBuilder(
{
var getMemberIds = memberIds.ToList();
return fixture.Build<ResultRowCalculationResult>()
.With(x => x.MemberId, () => getMemberIds.PopRandom())
.With(x => x.MemberId, () => randomMemberOrder ? getMemberIds.PopRandom() : getMemberIds.PopFirst())
.With(x => x.SessionType, sessionType)
.CreateMany(memberIds.Count());
}
Expand Down

0 comments on commit 2236c57

Please sign in to comment.