Skip to content

Commit

Permalink
Merge pull request #119 from dennis/fix-iracing-crash
Browse files Browse the repository at this point in the history
Bugfix: Handle if new car appears in new session
  • Loading branch information
dennis authored Jul 25, 2021
2 parents 1183f4c + cc6e464 commit f9d9151
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 34 deletions.
77 changes: 43 additions & 34 deletions Components/IRacing/Trackers/TrackPositionTracker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Slipstream.Components.IRacing.Models;
using Slipstream.Shared;

using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -279,6 +280,9 @@ private void RecalculatePositions(GameState.IState currentState)

private bool UpdateTrackerState(GameState.IState currentState, bool someoneIsBlinking, GameState.Car car)
{
if (!LastCarData.ContainsKey(car.CarIdx))
AddToLastCarData(currentState, car);

var carTrackerState = LastCarData[car.CarIdx];

carTrackerState.LastAbsolutePosition = carTrackerState.AbsolutePosition;
Expand Down Expand Up @@ -408,47 +412,52 @@ private void InitializeTrackerState(GameState.IState currentState)

foreach (var car in currentState.Cars)
{
if (car.UserId != -1 && !car.IsSpectator)
{
var ignoredUntilOnTrack = car.Location == IIRacingEventFactory.CarLocation.InPitStall;
var preLap = car.Laps == 1 && LastSessionState != IIRacingEventFactory.IRacingSessionStateEnum.Invalid; // Initially lastsessionstate is invalid, so if we see it here, it mean that we're dropped directly into a race without any warmup/getincar/paradealps etc
var laps = car.Laps;
var lapDistPct = car.LapDistPct;
AddToLastCarData(currentState, car);
}
}

// Some tracks some of the cars starts in front of the s/f line (like Bathurst) and the
// rest behind the line. To properly be able to sort this, we'll pretend they are a
// lap down, so they dont get calculated as in front of everbody. We do this by setting
// preLap
private void AddToLastCarData(GameState.IState currentState, GameState.Car car)
{
if (car.UserId != -1 && !car.IsSpectator)
{
var ignoredUntilOnTrack = car.Location == IIRacingEventFactory.CarLocation.InPitStall;
var preLap = car.Laps == 1 && LastSessionState != IIRacingEventFactory.IRacingSessionStateEnum.Invalid; // Initially lastsessionstate is invalid, so if we see it here, it mean that we're dropped directly into a race without any warmup/getincar/paradealps etc
var laps = car.Laps;
var lapDistPct = car.LapDistPct;

if (lapDistPct > 0.5)
{
preLap = true;
}
// Some tracks some of the cars starts in front of the s/f line (like Bathurst) and the
// rest behind the line. To properly be able to sort this, we'll pretend they are a
// lap down, so they dont get calculated as in front of everbody. We do this by setting
// preLap

Debug($"{currentState.SessionTime} Hello caridx-#{car.CarIdx} {car.CarNumber} {car.UserName} laps={car.Laps}, LapDistPct={car.LapDistPct}, PreLap={preLap}, Location={car.Location}, IgnoredUntilOnTrack={ignoredUntilOnTrack}");
if (lapDistPct > 0.5)
{
preLap = true;
}

if (preLap)
{
laps--;
}
Debug($"{currentState.SessionTime} Hello caridx-#{car.CarIdx} {car.CarNumber} {car.UserName} laps={car.Laps}, LapDistPct={car.LapDistPct}, PreLap={preLap}, Location={car.Location}, IgnoredUntilOnTrack={ignoredUntilOnTrack}");

LastCarData.Add(car.CarIdx, new CarData
{
AbsolutePosition = laps + car.LapDistPct,
CurrentPositionInClass = car.ClassPosition,
CurrentPositionInRace = car.Position,
LastLap = laps,
LastLapDistPct = car.LapDistPct,
LastLocation = car.Location,
PreLap = preLap,
IgnoredUntilOnTrack = ignoredUntilOnTrack,
CarClassId = car.CarClassId,
});
}
else
if (preLap)
{
Debug($"Ignoring {car.CarIdx} {car.UserName}");
laps--;
}

LastCarData.Add(car.CarIdx, new CarData
{
AbsolutePosition = laps + car.LapDistPct,
CurrentPositionInClass = car.ClassPosition,
CurrentPositionInRace = car.Position,
LastLap = laps,
LastLapDistPct = car.LapDistPct,
LastLocation = car.Location,
PreLap = preLap,
IgnoredUntilOnTrack = ignoredUntilOnTrack,
CarClassId = car.CarClassId,
});
}
else
{
Debug($"Ignoring {car.CarIdx} {car.UserName}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,35 @@ public void CarOvertakesOtherCarInDifferentClass()
}
}
}

[Fact]
public void NewCarMidSession()
{
const float NOW = 1234.2f;

// arrange

// car0 is front of car1
Builder
.AtSessionTime(NOW)
.Set(a => a.DriverCarIdx = 0)
.Set(a => a.SessionType = IIRacingEventFactory.IRacingSessionTypeEnum.Race)
.Set(a => a.SessionState = IIRacingEventFactory.IRacingSessionStateEnum.Racing)
.Car(0).OnTrack().Set(a => a.LapDistPct = 0.44f).CarDone()
.Commit();
// wild car1 appears
Builder
.AtSessionTime(NOW + 1)
.Car(1).OnTrack().Set(a => a.LapDistPct = 0.50f).CarDone()
.Commit();

// act
var sut = new TrackPositionTracker(EventBus, EventFactory);
foreach (var s in Builder.States)
sut.Handle(s, TrackerState, new EventEnvelope("sender"));

// assert
Assert.Equal(1, EventBus.Events.Count);
}
}
}

0 comments on commit f9d9151

Please sign in to comment.