Skip to content

Commit

Permalink
Merge pull request #80 from dennis/iracing-position-tracking
Browse files Browse the repository at this point in the history
IRacing: Adds event for realtime position tracking
  • Loading branch information
dennis committed Mar 24, 2021
2 parents 36d2e11 + e5e6e0d commit 424ceae
Show file tree
Hide file tree
Showing 13 changed files with 704 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Next version
[Full Changelog](https://github.com/dennis/slipstream/compare/v0.4.1...main)
- Event: Adds IRacingTrackPosition

## [0.4.1](https://github.com/dennis/slipstream/releases/tag/v0.4.1) (2021-03-20)
[Full Changelog](https://github.com/dennis/slipstream/compare/v0.4.0...v0.4.1)
Expand Down
19 changes: 17 additions & 2 deletions Components/IRacing/EventFactory/IRacingEventFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Slipstream.Components.IRacing.Events;
using Slipstream.Components.IRacing.Plugins.GameState;
using Slipstream.Shared;
using System;
using static Slipstream.Components.IRacing.IIRacingEventFactory;

Expand Down Expand Up @@ -433,13 +432,29 @@ public IRacingRaw CreateIRacingRaw(IState state)
};
}

public IEvent CreateIRacingTowed(double sessionTime, float remainingTowTime)
public IRacingTowed CreateIRacingTowed(double sessionTime, float remainingTowTime)
{
return new IRacingTowed
{
SessionTime = sessionTime,
RemainingTowTime = remainingTowTime,
};
}

public IRacingTrackPosition CreateIRacingTrackPosition(double sessionTime, long carIdx, bool localUser, int currentPositionInRace, int currentPositionInClass, int previousPositionInRace, int previousPositionInClass, int[] newCarsAhead, int[] newCarsBehind)
{
return new IRacingTrackPosition
{
SessionTime = sessionTime,
CarIdx = carIdx,
LocalUser = localUser,
CurrentPositionInRace = currentPositionInRace,
CurrentPositionInClass = currentPositionInClass,
PreviousPositionInRace = previousPositionInRace,
PreviousPositionInClass = previousPositionInClass,
NewCarsAhead = newCarsAhead,
NewCarsBehind = newCarsBehind,
};
}
}
}
15 changes: 15 additions & 0 deletions Components/IRacing/EventHandler/IRacing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public IRacing(EventHandlerController eventHandler)

public delegate void OnIRacingTowedHandler(EventHandlerController source, EventHandlerArgs<IRacingTowed> e);

public delegate void OnIRacingTrackPositionHandler(EventHandlerController source, EventHandlerArgs<IRacingTrackPosition> e);

public event OnIRacingCarCompletedLapHandler? OnIRacingCarCompletedLap;

public event OnIRacingCarInfoHandler? OnIRacingCarInfo;
Expand Down Expand Up @@ -110,6 +112,8 @@ public IRacing(EventHandlerController eventHandler)

public event OnIRacingTowedHandler? OnIRacingTowed;

public event OnIRacingTrackPositionHandler? OnIRacingTrackPosition;

public IEventHandler.HandledStatus HandleEvent(IEvent @event)
{
switch (@event)
Expand Down Expand Up @@ -354,6 +358,17 @@ public IEventHandler.HandledStatus HandleEvent(IEvent @event)
{
return IEventHandler.HandledStatus.UseDefault;
}

case IRacingTrackPosition tev:
if (OnIRacingTrackPosition != null)
{
OnIRacingTrackPosition(Parent, new EventHandlerArgs<IRacingTrackPosition>(tev));
return IEventHandler.HandledStatus.Handled;
}
else
{
return IEventHandler.HandledStatus.UseDefault;
}
}

return IEventHandler.HandledStatus.NotMine;
Expand Down
56 changes: 56 additions & 0 deletions Components/IRacing/Events/IRacingTrackPosition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#nullable enable

using Slipstream.Shared;
using System.Collections.Generic;

namespace Slipstream.Components.IRacing.Events
{
public class IRacingTrackPosition : IEvent
{
public string EventType => "IRacingTrackPosition";
public bool ExcludeFromTxrx => false;
public ulong Uptime { get; set; }
public double SessionTime { get; set; }
public long CarIdx { get; set; }
public bool LocalUser { get; set; }
public int CurrentPositionInClass { get; set; }
public int CurrentPositionInRace { get; set; }
public int PreviousPositionInClass { get; set; }
public int PreviousPositionInRace { get; set; }
public int[] NewCarsAhead { get; set; } = new int[] { };
public int[] NewCarsBehind { get; set; } = new int[] { };

public override bool Equals(object? obj)
{
return obj is IRacingTrackPosition position &&
EventType == position.EventType &&
ExcludeFromTxrx == position.ExcludeFromTxrx &&
SessionTime == position.SessionTime &&
CarIdx == position.CarIdx &&
LocalUser == position.LocalUser &&
CurrentPositionInClass == position.CurrentPositionInClass &&
CurrentPositionInRace == position.CurrentPositionInRace &&
PreviousPositionInClass == position.PreviousPositionInClass &&
PreviousPositionInRace == position.PreviousPositionInRace &&
EqualityComparer<int[]>.Default.Equals(NewCarsAhead, position.NewCarsAhead) &&
EqualityComparer<int[]>.Default.Equals(NewCarsBehind, position.NewCarsBehind);
}

public override int GetHashCode()
{
int hashCode = -317246058;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(EventType);
hashCode = hashCode * -1521134295 + ExcludeFromTxrx.GetHashCode();
hashCode = hashCode * -1521134295 + SessionTime.GetHashCode();
hashCode = hashCode * -1521134295 + CarIdx.GetHashCode();
hashCode = hashCode * -1521134295 + LocalUser.GetHashCode();
hashCode = hashCode * -1521134295 + CurrentPositionInClass.GetHashCode();
hashCode = hashCode * -1521134295 + CurrentPositionInRace.GetHashCode();
hashCode = hashCode * -1521134295 + PreviousPositionInClass.GetHashCode();
hashCode = hashCode * -1521134295 + PreviousPositionInRace.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<int[]>.Default.GetHashCode(NewCarsAhead);
hashCode = hashCode * -1521134295 + EqualityComparer<int[]>.Default.GetHashCode(NewCarsBehind);
return hashCode;
}
}
}
4 changes: 3 additions & 1 deletion Components/IRacing/IIRacingEventFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ IRacingCompletedLap CreateIRacingCompletedLap(
bool bestLap
);

IEvent CreateIRacingTowed(double sessionTime, float remainingTowTime);
IRacingTowed CreateIRacingTowed(double sessionTime, float remainingTowTime);

IRacingCarInfo CreateIRacingCarInfo(
double sessionTime,
Expand Down Expand Up @@ -178,5 +178,7 @@ IRacingWeatherInfo CreateIRacingWeatherInfo(
IRacingCarPosition CreateIRacingCarPosition(double sessionTime, int carIdx, bool localUser, int positionInClass, int positionInRace);

IRacingRaw CreateIRacingRaw(IState state);

IRacingTrackPosition CreateIRacingTrackPosition(double sessionTime, long carIdx, bool localUser, int currentPositionInRace, int currentPositionInClass, int previousPositionInRace, int previousPositionInClass, int[] newCarsAhead, int[] newCarsBehind);
}
}
6 changes: 6 additions & 0 deletions Components/IRacing/Plugins/GameState/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Car
public long IRating { get; set; }
public string License { get; set; } = string.Empty;
public bool IsSpectator { get; set; }
public int Laps { get; set; }
public int LapsCompleted { get; set; }
public bool OnPitRoad { get; set; }
public int ClassPosition { get; set; }
Expand All @@ -21,6 +22,8 @@ public class Car
public double LastLapTime { get; set; }
public float BestLapTime { get; set; }
public int BestLapNum { get; set; }
public float LapDistPct { get; set; }
public long CarClassId { get; set; }

public Car Clone()
{
Expand All @@ -37,6 +40,7 @@ public Car Clone()
IRating = IRating,
License = License,
IsSpectator = IsSpectator,
Laps = Laps,
LapsCompleted = LapsCompleted,
OnPitRoad = OnPitRoad,
ClassPosition = ClassPosition,
Expand All @@ -45,6 +49,8 @@ public Car Clone()
LastLapTime = LastLapTime,
BestLapTime = BestLapTime,
BestLapNum = BestLapNum,
CarClassId = CarClassId,
LapDistPct = LapDistPct,
};
}
}
Expand Down
17 changes: 13 additions & 4 deletions Components/IRacing/Plugins/GameState/StateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using iRacingSDK;
using System;
using System.Linq;
using System.Collections.Generic;
using static Slipstream.Components.IRacing.IIRacingEventFactory;

Expand Down Expand Up @@ -121,6 +122,11 @@ internal class StateFactory : IStateFactory
foreach (var d in ds.SessionData.DriverInfo.Drivers)
{
int idx = (int)d.CarIdx;
var location = (IIRacingEventFactory.CarLocation)(int)ds.Telemetry.CarIdxTrackSurface[idx];
var lastLapTime = ((float[])ds.Telemetry["CarIdxLastLapTime"])[idx];
var bestLapTime = ((float[])ds.Telemetry["CarIdxBestLapTime"])[idx];
var bestLapNum = ((int[])ds.Telemetry["CarIdxBestLapNum"])[idx];
var lapDistPct = ((float[])ds.Telemetry["CarIdxLapDistPct"])[idx];

var car = new Car
{
Expand All @@ -135,14 +141,17 @@ internal class StateFactory : IStateFactory
IRating = d.IRating,
License = d.LicString,
IsSpectator = d.IsSpectator != 0,
Laps = ds.Telemetry.CarIdxLap[idx],
LapsCompleted = ds.Telemetry.CarIdxLapCompleted[idx],
OnPitRoad = ds.Telemetry.CarIdxOnPitRoad[idx],
ClassPosition = ds.Telemetry.CarIdxClassPosition[idx],
Position = ds.Telemetry.CarIdxPosition[idx],
Location = (IIRacingEventFactory.CarLocation)(int)ds.Telemetry.CarIdxTrackSurface[idx],
LastLapTime = ((float[])ds.Telemetry["CarIdxLastLapTime"])[idx],
BestLapTime = ((float[])ds.Telemetry["CarIdxBestLapTime"])[idx],
BestLapNum = ((int[])ds.Telemetry["CarIdxBestLapNum"])[idx],
Location = location,
LastLapTime = lastLapTime,
BestLapTime = bestLapTime,
BestLapNum = bestLapNum,
LapDistPct = lapDistPct,
CarClassId = ds.SessionData.DriverInfo.Drivers.Where(a => a.CarIdx == idx).Select(a => a.CarClassID).FirstOrDefault()
};

if (Cars.TryGetValue(idx, out Car existingCar))
Expand Down
Loading

0 comments on commit 424ceae

Please sign in to comment.