Skip to content

Commit

Permalink
Initial version of sectortimes
Browse files Browse the repository at this point in the history
  • Loading branch information
NickThissen committed Oct 20, 2015
1 parent 0b04590 commit 6c5af97
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
59 changes: 58 additions & 1 deletion iRacingSimulator/Drivers/Driver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using iRacingSdkWrapper;

namespace iRacingSimulator.Drivers
Expand Down Expand Up @@ -144,10 +145,66 @@ internal void UpdatePrivateInfo(TelemetryInfo e)

public void UpdateSectorTimes(Track track, TelemetryInfo previousTelemetry, TelemetryInfo telemetry)
{
if (track == null) return;
if (track.Sectors.Count == 0) return;

var results = this.CurrentResults;
if (results != null)
{

// Set array
if (results.SectorTimes == null || results.SectorTimes.Length == 0)
{
results.SectorTimes = track.Sectors.Select(s => s.Copy()).ToArray();
}

var p0 = previousTelemetry.CarIdxLapDistPct.Value[this.Id];
var p1 = telemetry.CarIdxLapDistPct.Value[this.Id];
var dp = p1 - p0;

var t = telemetry.SessionTime.Value;

// Check lap crossing
if (p0 - p1 > 0.5) // more than 50% jump in track distance == lap crossing occurred from 0.99xx -> 0.00x
{
var crossTime = (float)(t - (p1 * dp)); // linear interpolation of actual crossing time (somewhere between t0 and t1)

// Finish previous sector
var sector = results.SectorTimes.Last();
if (sector != null && sector.EnterSessionTime > 0)
{
sector.SectorTime = new Laptime(crossTime - sector.EnterSessionTime);
}

// Begin first sector
sector = results.SectorTimes[0];
sector.EnterSessionTime = crossTime;

this.Live.CurrentSector = 0;
}
else
{
// Check all sectors
foreach (var s in results.SectorTimes)
{
if (p1 > s.StartPercentage && s.Number > this.Live.CurrentSector)
{
// Crossed into new sector
var crossTime = (float)(t - (p1 - s.StartPercentage) * dp);

// Finish previous
var sector = results.SectorTimes[s.Number - 1];
if (sector != null && sector.EnterSessionTime > 0)
{
sector.SectorTime = new Laptime(crossTime - sector.EnterSessionTime);
}

// Begin next sector
s.EnterSessionTime = crossTime;

this.Live.CurrentSector = s.Number;
}
}
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions iRacingSimulator/Drivers/DriverLiveInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public float TotalLapDistance
public string DeltaToLeader { get; set; }
public string DeltaToNext { get; set; }

public int CurrentSector { get; set; }

public void ParseTelemetry(TelemetryInfo e)
{
this.Lap = e.CarIdxLap.Value[this.Driver.Id];
Expand Down
7 changes: 1 addition & 6 deletions iRacingSimulator/Drivers/DriverResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public DriverSessionResults(Driver driver, int sessionNumber)
_sessionNumber = sessionNumber;

this.Laps = new LaptimeCollection();
this.SectorTimes = new List<Laptime>();
this.IsEmpty = true;
}

Expand All @@ -111,11 +110,7 @@ public DriverSessionResults(Driver driver, int sessionNumber)

public LaptimeCollection Laps { get; set; }

public List<Laptime> SectorTimes { get; set; }

public Laptime Sector1 { get; set; }
public Laptime Sector2 { get; set; }
public Laptime Sector3 { get; set; }
public Sector[] SectorTimes { get; set; }

public string OutReason { get; set; }
public int OutReasonId { get; set; }
Expand Down
10 changes: 9 additions & 1 deletion iRacingSimulator/Sector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ public class Sector
public int Number { get; set; }
public float StartPercentage { get; set; }

public double EnterSessionTime { get; set; }
public float EnterSessionTime { get; set; }
public Laptime SectorTime { get; set; }

public Sector Copy()
{
var s = new Sector();
s.Number = this.Number;
s.StartPercentage = this.StartPercentage;
return s;
}
}
}

0 comments on commit 6c5af97

Please sign in to comment.