From fc592cd848e9c425e031d5a075e661c7842bc2b0 Mon Sep 17 00:00:00 2001 From: ntr Date: Thu, 20 Jul 2023 16:26:53 +1000 Subject: [PATCH] timing colours --- Assets/Src/IEnumerableExtensions.cs | 17 ++++++++++++++++ Assets/Src/ShipUi.cs | 3 ++- Assets/Src/Timing.cs | 30 +++++++++++++++-------------- 3 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 Assets/Src/IEnumerableExtensions.cs diff --git a/Assets/Src/IEnumerableExtensions.cs b/Assets/Src/IEnumerableExtensions.cs new file mode 100644 index 0000000..e256bb2 --- /dev/null +++ b/Assets/Src/IEnumerableExtensions.cs @@ -0,0 +1,17 @@ +public static class IEnumerableExtensions { + public static T MinBy(this IEnumerable source, Func selector) { + T min = default; + + bool anyValue = false; + + foreach (var item in source) { + if (!anyValue || Comparer.Default.Compare(selector(item), selector(min)) < 0) { + min = item; + anyValue = true; + } + } + + return min; + } +} + diff --git a/Assets/Src/ShipUi.cs b/Assets/Src/ShipUi.cs index 223293d..59350dd 100644 --- a/Assets/Src/ShipUi.cs +++ b/Assets/Src/ShipUi.cs @@ -53,7 +53,8 @@ public class ShipUi : MonoBehaviour { .Where(s => s.Sector == sector.sector) .Select(s => { Label l = new Label($"{s}"); - l.AddToClassList(s.ColourClass()); + string colourClass = timing.GetSectorColour(s).ToString().ToLowerInvariant(); + l.AddToClassList(colourClass); return l; }) .DefaultIfEmpty(new Label("-")) diff --git a/Assets/Src/Timing.cs b/Assets/Src/Timing.cs index f9257b0..96fe9ca 100644 --- a/Assets/Src/Timing.cs +++ b/Assets/Src/Timing.cs @@ -25,13 +25,13 @@ public readonly struct SectorTime { public override string ToString() => $"{Value:0.000}"; - public string ColourClass() => Colour switch { - SectorColour.Green => "green", - SectorColour.Purple => "purple", - SectorColour.White => "white", - SectorColour.Yellow => "yellow", - _ => throw new Exception("unknown colour"), - }; + // public string ColourClass() => Colour switch { + // SectorColour.Green => "green", + // SectorColour.Purple => "purple", + // SectorColour.White => "white", + // SectorColour.Yellow => "yellow", + // _ => throw new Exception("unknown colour"), + // }; } public struct Lap { @@ -42,7 +42,7 @@ public struct Lap { 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); } } @@ -148,13 +148,15 @@ public class Timing: MonoBehaviour { } public SectorColour GetSectorColour(SectorTime s) { - var fastestLap = lapTimes.MinBy(l => l.TotalTime()); - var fastestSector = fastestLap.Single(fls => fls.Sector == s.Sector); + if (lapTimes.Count == 0) + return SectorColour.Green; - return s.Value switch { - < fastestSector.Value => SectorColour.Green, - > fastestSector.Value => SectorColour.Yellow, - _ => SectorColour.White, + var fastestLap = lapTimes.MinBy(l => l.TotalTime()); + var fastestSector = fastestLap.Sectors.Single(fls => fls.Sector == s.Sector); + + return (s.Value < fastestSector.Value) switch { + true => SectorColour.Green, + false => SectorColour.Yellow, }; } }