Skip to content

Commit

Permalink
Fix "Interval" on "SubsessionChartLap"
Browse files Browse the repository at this point in the history
I really misunderstood what the "IntervalRaw" and "IntevalUnits"
properties were doing here. Makes sense now. Obvioiously sometimes your
interval is in time (you are on the same lap as the leader) or your
interval is in laps (you've been lapped by the leader).
  • Loading branch information
AdrianJSClark committed Nov 26, 2023
1 parent 15bd323 commit 902ae2d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Aydsko.iRacingData/Package Release Notes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

- Check Club Image Paths (Issue: 189)


- Fix "Interval" property on "SubsessionChartLap" so it doesn't throw an exception when driver has been lapped, which is a valid situation.

Contributions:

Expand Down
20 changes: 10 additions & 10 deletions src/Aydsko.iRacingData/Results/SubsessionChartLap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@

namespace Aydsko.iRacingData.Results;

/// <summary>Contains details about a particular lap for a particular driver suitable for creating a lap chart.</summary>
public class SubsessionChartLap : SubsessionLap
{
/// <summary>Contains the position of the car at the end of this lap.</summary>
[JsonPropertyName("lap_position")]
public int LapPosition { get; set; }

/// <summary>Contains the value of the interval at the end of this lap to the leader.</summary>
[JsonPropertyName("interval")]
public long? IntervalRaw { get; set; }

/// <summary>Describes the unit for the <see cref="IntervalRaw"/> value. Can be either <c>ms</c> for milliseconds or <c>lap</c> if the interval is one or more laps.</summary>
[JsonPropertyName("interval_units")]
public string IntervalUnits { get; set; } = null!;

// If there's one thing iRacing is consistent about, it is inconsistency!
// Normally an "interval" would be in ten-thousandth's of a second (i.e. 1 minute = "600000")
// Here they have the "interval" and "interval_units". In all the results I've harvested, the units are "ms" (obviously "milliseconds")
// So we can't use the normal "JsonConverter(typeof(TenThousandthSecondDurationConverter))" on the "interval" field.
/// <summary>If <see cref="IntervalUnits"/> is <c>ms</c> and <see cref="IntervalRaw"/> is available this returns the interval to the leader. Otherwise returns <see langword="null"/>.</summary>
/// <remarks>There's no reasonable value that can be used to return a value here for lapped cars so if the interval is a lap count we just return <see langword="null"/>.</remarks>
[JsonIgnore]
public TimeSpan? Interval => (IntervalRaw, IntervalUnits) switch
{
(null, _) => null,
(_, "ms") => (TimeSpan?)TimeSpan.FromMilliseconds((double)IntervalRaw),
_ => throw new NotSupportedException($"Unsupported \"IntervalUnits\" value for CustomerId \"{CustomerId}\" on lap number \"{LapNumber}\"")
};
public TimeSpan? Interval => IntervalUnits == "ms" && IntervalRaw is not null ? TimeSpan.FromMilliseconds((double)IntervalRaw) : null;

/// <summary>
/// Indicates if this lap time was the fastest lap of the race across all drivers.
/// </summary>
[JsonPropertyName("fastest_lap")]
public bool FastestLap { get; set; }
}
Expand Down

0 comments on commit 902ae2d

Please sign in to comment.