timing colours

This commit is contained in:
ntr committed 2023-07-20 16:27:02 +10:00
1 parent 64727058df
commit fc592cd848
3 files changed
+35 -15

No files matched your search

+17
View File
@@ -0,0 +1,17 @@
public static class IEnumerableExtensions {
public static T MinBy<T, K>(this IEnumerable<T> source, Func<T, K> selector) {
T min = default;
bool anyValue = false;
foreach (var item in source) {
if (!anyValue || Comparer<K>.Default.Compare(selector(item), selector(min)) < 0) {
min = item;
anyValue = true;
}
}
return min;
}
}
+2 -1
View File
@@ -53,7 +53,8 @@ public class ShipUi : MonoBehaviour {
.Where(s => s.Sector == sector.sector) .Where(s => s.Sector == sector.sector)
.Select(s => { .Select(s => {
Label l = new Label($"{s}"); Label l = new Label($"{s}");
l.AddToClassList(s.ColourClass()); string colourClass = timing.GetSectorColour(s).ToString().ToLowerInvariant();
l.AddToClassList(colourClass);
return l; return l;
}) })
.DefaultIfEmpty(new Label("-")) .DefaultIfEmpty(new Label("-"))
+16 -14
View File
@@ -25,13 +25,13 @@ public readonly struct SectorTime {
public override string ToString() => $"{Value:0.000}"; public override string ToString() => $"{Value:0.000}";
public string ColourClass() => Colour switch { // public string ColourClass() => Colour switch {
SectorColour.Green => "green", // SectorColour.Green => "green",
SectorColour.Purple => "purple", // SectorColour.Purple => "purple",
SectorColour.White => "white", // SectorColour.White => "white",
SectorColour.Yellow => "yellow", // SectorColour.Yellow => "yellow",
_ => throw new Exception("unknown colour"), // _ => throw new Exception("unknown colour"),
}; // };
} }
public struct Lap { public struct Lap {
@@ -42,7 +42,7 @@ public struct Lap {
return $"Lap {Number} : {TimeSpan.FromSeconds(TotalTime()):g} {String.Join(" ", Sectors.ConvertAll(s => s.ToString()))}"; return $"Lap {Number} : {TimeSpan.FromSeconds(TotalTime()):g} {String.Join(" ", Sectors.ConvertAll(s => s.ToString()))}";
} }
double TotalTime() { public double TotalTime() {
return Sectors.Sum(s => s.Value); return Sectors.Sum(s => s.Value);
} }
} }
@@ -148,13 +148,15 @@ public class Timing: MonoBehaviour {
} }
public SectorColour GetSectorColour(SectorTime s) { public SectorColour GetSectorColour(SectorTime s) {
var fastestLap = lapTimes.MinBy(l => l.TotalTime()); if (lapTimes.Count == 0)
var fastestSector = fastestLap.Single(fls => fls.Sector == s.Sector); return SectorColour.Green;
return s.Value switch { var fastestLap = lapTimes.MinBy(l => l.TotalTime());
< fastestSector.Value => SectorColour.Green, var fastestSector = fastestLap.Sectors.Single(fls => fls.Sector == s.Sector);
> fastestSector.Value => SectorColour.Yellow,
_ => SectorColour.White, return (s.Value < fastestSector.Value) switch {
true => SectorColour.Green,
false => SectorColour.Yellow,
}; };
} }
} }