mirror of
https://github.com/MaSzyna-EU07/StarterNG.git
synced 2026-11-19 15:02:15 +01:00
fix(tapes): a command is not taken for the header, and a gap in a tape is neither driving nor distance
- The header is the line that doesn't start with a figure. A line whose command, which is free text, says "Security" was taken for it, moving the alerter's column and dropping the line. - A tape the black box writes goes on after a gap when the player comes back to its vehicle. A step of more than twenty usual steps between two lines (a second at least) is time not recorded: no distance over it, and neither running nor standing time; the average with the stops leaves it out too.
This commit is contained in:
@@ -86,6 +86,31 @@ public class HaslerTapeTests
|
|||||||
Assert.True(tape.Samples[1].SecurityAlarm);
|
Assert.True(tape.Samples[1].SecurityAlarm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_command_named_like_a_heading_is_read_as_a_line_not_as_the_header()
|
||||||
|
{
|
||||||
|
var tape = HaslerTape.Parse(Header + Line(0, 36, 0, 0, command: "Security") + Line(1, 36, 0, 0, security: 1));
|
||||||
|
|
||||||
|
Assert.Equal(2, tape.Samples.Count);
|
||||||
|
Assert.False(tape.Samples[0].SecurityAlarm);
|
||||||
|
Assert.True(tape.Samples[1].SecurityAlarm);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_gap_in_the_time_is_neither_driving_nor_standing_and_covers_no_distance()
|
||||||
|
{
|
||||||
|
// the player in another vehicle for ten minutes, then back in this one
|
||||||
|
var tape = HaslerTape.Parse(Header + Line(0, 36, 0, 0) + Line(1, 36, 0, 0) + Line(601, 36, 0, 0)
|
||||||
|
+ Line(602, 36, 0, 0) + Line(603, 0, 0, 0));
|
||||||
|
|
||||||
|
Assert.Equal(603, tape.Duration, 6);
|
||||||
|
Assert.Equal(600, tape.GapTime, 6);
|
||||||
|
Assert.Equal(2, tape.RunningTime, 6);
|
||||||
|
Assert.Equal(1, tape.StandingTime, 6); // the last second, stopping
|
||||||
|
Assert.Equal(10 + 10 + 5, tape.DistanceM, 6);
|
||||||
|
Assert.Equal(25 / 3.0 * 3.6, tape.AverageWithStopsKmh, 6);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void The_sample_under_a_point_of_the_tape_is_found_by_time_and_by_distance()
|
public void The_sample_under_a_point_of_the_tape_is_found_by_time_and_by_distance()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -41,14 +41,23 @@ public sealed class HaslerTape
|
|||||||
private const int SecurityFromEndOf25 = 6; // a log with no header to tell: the columns up to the water's temperature
|
private const int SecurityFromEndOf25 = 6; // a log with no header to tell: the columns up to the water's temperature
|
||||||
private const int MinimumColumns = 17;
|
private const int MinimumColumns = 17;
|
||||||
|
|
||||||
|
private const double MinimumGapSeconds = 1.0;
|
||||||
|
private const int GapSteps = 20;
|
||||||
|
|
||||||
public HaslerTape(IReadOnlyList<TapeSample> samples)
|
public HaslerTape(IReadOnlyList<TapeSample> samples)
|
||||||
{
|
{
|
||||||
Samples = samples;
|
Samples = samples;
|
||||||
foreach (var s in samples)
|
foreach (var s in samples)
|
||||||
MaxSpeedKmh = Math.Max(MaxSpeedKmh, s.SpeedKmh);
|
MaxSpeedKmh = Math.Max(MaxSpeedKmh, s.SpeedKmh);
|
||||||
|
double gap = GapAbove(samples);
|
||||||
for (int i = 1; i < samples.Count; i++)
|
for (int i = 1; i < samples.Count; i++)
|
||||||
if (samples[i].SpeedKmh > 0.5)
|
{
|
||||||
RunningTime += samples[i].Time - samples[i - 1].Time;
|
double step = samples[i].Time - samples[i - 1].Time;
|
||||||
|
if (step > gap)
|
||||||
|
GapTime += step;
|
||||||
|
else if (samples[i].SpeedKmh > 0.5)
|
||||||
|
RunningTime += step;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IReadOnlyList<TapeSample> Samples { get; }
|
public IReadOnlyList<TapeSample> Samples { get; }
|
||||||
@@ -63,54 +72,76 @@ public sealed class HaslerTape
|
|||||||
|
|
||||||
public double MaxSpeedKmh { get; }
|
public double MaxSpeedKmh { get; }
|
||||||
|
|
||||||
/// <summary>Seconds on the move; the rest of <see cref="Duration"/> was spent standing.</summary>
|
/// <summary>Seconds on the move; the rest of <see cref="Duration"/> was spent standing, or not recorded.</summary>
|
||||||
public double RunningTime { get; }
|
public double RunningTime { get; }
|
||||||
|
|
||||||
public double StandingTime => Duration - RunningTime;
|
/// <summary>Seconds of <see cref="Duration"/> not recorded: the gaps, see <see cref="GapAbove"/>.</summary>
|
||||||
|
public double GapTime { get; }
|
||||||
|
|
||||||
|
public double StandingTime => Duration - RunningTime - GapTime;
|
||||||
|
|
||||||
/// <summary>Mean speed while moving.</summary>
|
/// <summary>Mean speed while moving.</summary>
|
||||||
public double AverageSpeedKmh => RunningTime > 0 ? DistanceM / RunningTime * 3.6 : 0;
|
public double AverageSpeedKmh => RunningTime > 0 ? DistanceM / RunningTime * 3.6 : 0;
|
||||||
|
|
||||||
/// <summary>Mean speed over the whole recording, stops included - the commercial speed.</summary>
|
/// <summary>Mean speed over the whole recording, stops included - the commercial speed.</summary>
|
||||||
public double AverageWithStopsKmh => Duration > 0 ? DistanceM / Duration * 3.6 : 0;
|
public double AverageWithStopsKmh => Duration - GapTime > 0 ? DistanceM / (Duration - GapTime) * 3.6 : 0;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A step between two lines longer than this is time not recorded, the player in another vehicle before
|
||||||
|
/// coming back to this one: neither driving nor standing, and no distance. The lines come every 0.05 s of
|
||||||
|
/// simulated time or so; it is twenty of their usual steps, a second at least.
|
||||||
|
/// </summary>
|
||||||
|
public static double GapAbove(IReadOnlyList<TapeSample> samples)
|
||||||
|
{
|
||||||
|
if (samples.Count < 2)
|
||||||
|
return MinimumGapSeconds;
|
||||||
|
var steps = new double[samples.Count - 1];
|
||||||
|
for (int i = 1; i < samples.Count; i++)
|
||||||
|
steps[i - 1] = samples[i].Time - samples[i - 1].Time;
|
||||||
|
Array.Sort(steps);
|
||||||
|
return Math.Max(MinimumGapSeconds, GapSteps * steps[steps.Length / 2]);
|
||||||
|
}
|
||||||
|
|
||||||
public static HaslerTape Parse(string text)
|
public static HaslerTape Parse(string text)
|
||||||
{
|
{
|
||||||
var samples = new List<TapeSample>();
|
var samples = new List<TapeSample>();
|
||||||
double distance = 0;
|
|
||||||
TapeSample? previous = null;
|
|
||||||
|
|
||||||
int securityFromEnd = SecurityFromEndOf25;
|
int securityFromEnd = SecurityFromEndOf25;
|
||||||
foreach (var raw in text.Split('\n'))
|
foreach (var raw in text.Split('\n'))
|
||||||
{
|
{
|
||||||
var columns = raw.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries);
|
var columns = raw.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
if (columns.Length == 0)
|
||||||
|
continue;
|
||||||
|
if (!Number(columns[TimeColumn], out double time))
|
||||||
|
{
|
||||||
|
// the header; a line of figures may have the word too, in the free text of its command
|
||||||
int heading = Array.IndexOf(columns, SecurityHeading);
|
int heading = Array.IndexOf(columns, SecurityHeading);
|
||||||
if (heading >= 0)
|
if (heading >= 0)
|
||||||
{
|
|
||||||
securityFromEnd = columns.Length - heading;
|
securityFromEnd = columns.Length - heading;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (columns.Length < MinimumColumns
|
if (columns.Length < MinimumColumns || !Number(columns[VelocityColumn], out double velocity))
|
||||||
|| !Number(columns[TimeColumn], out double time)
|
continue; // a line cut short by a simulator still writing
|
||||||
|| !Number(columns[VelocityColumn], out double velocity))
|
|
||||||
continue; // the header, or a line cut short by a simulator still writing
|
|
||||||
|
|
||||||
Number(columns[BrakePressColumn], out double brake);
|
Number(columns[BrakePressColumn], out double brake);
|
||||||
Number(columns[PipePressColumn], out double pipe);
|
Number(columns[PipePressColumn], out double pipe);
|
||||||
Number(columns[MotorCurrentColumn], out double current);
|
Number(columns[MotorCurrentColumn], out double current);
|
||||||
bool security = securityFromEnd <= columns.Length && columns[^securityFromEnd] == "1";
|
bool security = securityFromEnd <= columns.Length && columns[^securityFromEnd] == "1";
|
||||||
|
|
||||||
double speed = Math.Abs(velocity);
|
if (samples.Count > 0 && time < samples[^1].Time)
|
||||||
if (previous is { } p)
|
|
||||||
{
|
|
||||||
if (time < p.Time)
|
|
||||||
continue;
|
continue;
|
||||||
distance += (p.SpeedKmh + speed) / 2 / 3.6 * (time - p.Time);
|
samples.Add(new TapeSample(time, Math.Abs(velocity), 0, brake, pipe, current, security));
|
||||||
}
|
}
|
||||||
|
|
||||||
var sample = new TapeSample(time, speed, distance, brake, pipe, current, security);
|
// the distance, trapezoid by trapezoid, none over the gaps
|
||||||
samples.Add(sample);
|
double gap = GapAbove(samples), distance = 0;
|
||||||
previous = sample;
|
for (int i = 1; i < samples.Count; i++)
|
||||||
|
{
|
||||||
|
var (before, sample) = (samples[i - 1], samples[i]);
|
||||||
|
double step = sample.Time - before.Time;
|
||||||
|
if (step <= gap)
|
||||||
|
distance += (before.SpeedKmh + sample.SpeedKmh) / 2 / 3.6 * step;
|
||||||
|
samples[i] = sample with { DistanceM = distance };
|
||||||
}
|
}
|
||||||
|
|
||||||
return new HaslerTape(samples);
|
return new HaslerTape(samples);
|
||||||
|
|||||||
Reference in New Issue
Block a user