Last active
January 12, 2024 12:19
-
-
Save cherring/68c1acb17de2325e2c32f3eba2204156 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using GameReaderCommon; | |
using iRacingSDK; | |
using System.Linq; | |
using SimHub.Plugins; | |
using System.Text; | |
using SimHub.Plugins.OutputPlugins.GraphicalDash.Behaviors.StringText.Imp; | |
using System; | |
namespace cherring.AdjacentPositionsPlugin | |
{ | |
[PluginDescription("Information on the positon ahead and behind in class in iRacing")] | |
[PluginAuthor("cherring")] | |
[PluginName("Adjacent Positions Plugin")] | |
public class AdjacentPositionsPlugin : IPlugin, IDataPlugin | |
{ | |
public iRacingConnection connection; | |
public PluginManager PluginManager { get; set; } | |
private PluginManager _pluginManager; | |
public void Init(PluginManager pluginManager) | |
{ | |
connection = new iRacingConnection(); | |
_pluginManager = pluginManager; | |
_pluginManager.AddProperty("PlayerPositionInClass", this.GetType(), 0); | |
_pluginManager.AddProperty("PositionAheadInClass", this.GetType(), PositionAheadInClass()); | |
_pluginManager.AddProperty("PositionBehindInClass", this.GetType(), 0); | |
_pluginManager.AddProperty("PositionAheadInClassIdx", this.GetType(), 0); | |
_pluginManager.AddProperty("PositionBehindInClassIdx", this.GetType(), 0); | |
_pluginManager.AddProperty("PlayerCarIdx", this.GetType(), 0); | |
_pluginManager.AddProperty("PositionAheadInClassRelative", this.GetType(), 0); | |
_pluginManager.AddProperty("PositionBehindInClassRelative", this.GetType(), 0); | |
_pluginManager.AddProperty("DistanceToPositionAhead", this.GetType(), 0.0); | |
_pluginManager.AddProperty("DistanceToPositionBehind", this.GetType(), 0.0); | |
_pluginManager.AddProperty("PositionAheadDriverName", this.GetType(), "Unknown"); | |
_pluginManager.AddProperty("PositionBehindDriverName", this.GetType(), "Unknown"); | |
_pluginManager.AddProperty("TrackLength", this.GetType(), "Unknown"); | |
_pluginManager.AddProperty("DriverDistanceDriven", this.GetType(), 0.0); | |
} | |
public void DataUpdate(PluginManager pluginManager, ref GameData data) | |
{ | |
_pluginManager.AddProperty("PlayerPositionInClass", this.GetType(), DriverPositionInClass()); | |
_pluginManager.AddProperty("PositionAheadInClass", this.GetType(), PositionAheadInClass()); | |
_pluginManager.AddProperty("PositionBehindInClass", this.GetType(), PositionBehindInClass()); | |
_pluginManager.AddProperty("PlayerCarIdx", this.GetType(), PlayerCarIdx()); | |
_pluginManager.AddProperty("PositionAheadInClassIdx", this.GetType(), CarAheadInClassIdx()); | |
_pluginManager.AddProperty("PositionBehindInClassIdx", this.GetType(), CarBehindInClassIdx()); | |
_pluginManager.AddProperty("PositionAheadInClassRelative", this.GetType(), PlayerRelativeGapToCarIdx(CarAheadInClassIdx())); | |
_pluginManager.AddProperty("PositionBehindInClassRelative", this.GetType(), PlayerRelativeGapToCarIdx(CarBehindInClassIdx())); | |
_pluginManager.AddProperty("PositionAheadDriverName", this.GetType(), DriverName(CarAheadInClassIdx())); | |
_pluginManager.AddProperty("PositionBehindDriverName", this.GetType(), DriverName(CarBehindInClassIdx())); | |
_pluginManager.AddProperty("TrackLength", this.GetType(), TrackLength()); | |
_pluginManager.AddProperty("DriverDistanceDriven", this.GetType(), DriverDistanceDriven(PlayerCarIdx())); | |
_pluginManager.AddProperty("DistanceToPositionAhead", this.GetType(), DistanceToDriver(CarAheadInClassIdx())); | |
_pluginManager.AddProperty("DistanceToPositionBehind", this.GetType(), DistanceToDriver(CarBehindInClassIdx())); | |
_pluginManager.AddProperty("CarAheadRelativeSpeed", this.GetType(), CarRelativeSpeedToPlayer(CarAheadInClassIdx())); | |
} | |
public void End(PluginManager pluginManager) | |
{ | |
} | |
private double CarRelativeSpeedToPlayer(long carIdx) | |
{ | |
var playerAverageSpeed = CarAverageSpeed(PlayerCarIdx()); | |
var otherCarAverageSpeed = CarAverageSpeed(carIdx); | |
var relativeSpeed = (playerAverageSpeed + otherCarAverageSpeed) / 2; | |
return relativeSpeed; | |
} | |
private double CarAverageSpeed(long carIdx) | |
{ | |
var data = connection.GetDataFeed().FirstOrDefault(); | |
var sessionTime = data.Telemetry.SessionTimeSpan; | |
var sessionTimeSeconds = sessionTime.TotalSeconds; | |
var distance = DriverDistanceDriven(carIdx); | |
var carAverageSpeed = distance / sessionTimeSeconds; | |
return carAverageSpeed; | |
} | |
private double TrackLength() { | |
var data = connection.GetDataFeed().FirstOrDefault(); | |
string trackLength = data.SessionData.WeekendInfo.TrackLength; | |
double kilometres = double.Parse(trackLength.Replace("km", "")); | |
double metres = kilometres * 1000; | |
return metres; | |
} | |
private int DriverPositionInClass() | |
{ | |
var data = connection.GetDataFeed().FirstOrDefault(); | |
var position = data.Telemetry.CarIdxClassPosition[PlayerCarIdx()]; | |
return position; // Because of 0 based array | |
} | |
private long PlayerCarIdx() { | |
var data = connection.GetDataFeed().FirstOrDefault(); | |
return data.SessionData.DriverInfo.DriverCarIdx; | |
} | |
private long CarAheadInClassIdx() { | |
return OtherCarClassIdx(PositionAheadInClass()); | |
} | |
private long CarBehindInClassIdx() { | |
return OtherCarClassIdx(PositionBehindInClass()); | |
} | |
private long OtherCarClassIdx(int position) | |
{ | |
var data = connection.GetDataFeed().FirstOrDefault(); | |
var carsInClass = data.Telemetry.CarIdxClassPosition; | |
var otherCar = Array.IndexOf(carsInClass, position); | |
return otherCar; | |
} | |
private string DriverName(long carIdx) | |
{ | |
var data = connection.GetDataFeed().FirstOrDefault(); | |
var driverInfo = data.SessionData.DriverInfo.Drivers[carIdx]; | |
return driverInfo.UserName; | |
} | |
private float DriverDistanceDriven(long carIdx) { | |
var data = connection.GetDataFeed().FirstOrDefault(); | |
var telemetry = data.Telemetry; | |
var laps = telemetry.CarIdxDistance[carIdx]; | |
var metres = laps * (float)TrackLength(); | |
return metres; | |
} | |
private dynamic PlayerRelativeGapToCarIdx(long carIdx) | |
{ | |
var distance = DistanceToDriver(carIdx); | |
var speed = CarRelativeSpeedToPlayer(carIdx); | |
var relativeGap = distance / speed; | |
return relativeGap; | |
} | |
private float DistanceToDriver(long otherDriver) | |
{ | |
var data = connection.GetDataFeed().FirstOrDefault(); | |
var telemetry = data.Telemetry; | |
var playerDistance = DriverDistanceDriven(PlayerCarIdx()); | |
var otherDriverDistance = DriverDistanceDriven(otherDriver); | |
var distanceGap = otherDriverDistance - playerDistance; | |
return distanceGap; | |
} | |
private float PlayerCarSpeed() | |
{ | |
var data = connection.GetDataFeed().FirstOrDefault(); | |
var speed = data.Telemetry.Speed; | |
return speed; | |
} | |
private int PositionAheadInClass() | |
{ | |
var position = DriverPositionInClass() - 1; | |
var otherCarIdx = OtherCarClassIdx(position); | |
var playerDistance = DriverDistanceDriven(PlayerCarIdx()); | |
var otherCarDistance = DriverDistanceDriven(otherCarIdx); | |
while (playerDistance > otherCarDistance) { | |
position--; | |
otherCarIdx = OtherCarClassIdx(position); | |
playerDistance = DriverDistanceDriven(PlayerCarIdx()); | |
otherCarDistance = DriverDistanceDriven(otherCarIdx); | |
} | |
return position; | |
} | |
private int PositionBehindInClass() | |
{ | |
var position = DriverPositionInClass() + 1; | |
var otherCarIdx = OtherCarClassIdx(position); | |
var playerDistance = DriverDistanceDriven(PlayerCarIdx()); | |
var otherCarDistance = DriverDistanceDriven(otherCarIdx); | |
while (playerDistance < otherCarDistance) { | |
position++; | |
otherCarIdx = OtherCarClassIdx(position); | |
playerDistance = DriverDistanceDriven(PlayerCarIdx()); | |
otherCarDistance = DriverDistanceDriven(otherCarIdx); | |
} | |
return position; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment