From bef194280ea775ffdc457197b45a7b05dfc1b8c4 Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 1 Aug 2023 18:04:18 +1000 Subject: [PATCH] sector diffs --- Assets/Scenes/testTrack/TestTrack.unity | 4 +++ Assets/Src/Laps.cs | 23 +++++++-------- Assets/Src/Timing.cs | 38 ++++++++----------------- Assets/Src/UI/TimingUI.cs | 32 ++++++++++++++++----- 4 files changed, 52 insertions(+), 45 deletions(-) diff --git a/Assets/Scenes/testTrack/TestTrack.unity b/Assets/Scenes/testTrack/TestTrack.unity index b8f0520..1584e96 100644 --- a/Assets/Scenes/testTrack/TestTrack.unity +++ b/Assets/Scenes/testTrack/TestTrack.unity @@ -463,6 +463,10 @@ PrefabInstance: propertyPath: shipUi value: objectReference: {fileID: 1292078235} + - target: {fileID: 5313449993149091347, guid: ebed5434f18ca13e38d2d7bfcc18d6c3, type: 3} + propertyPath: timingUi + value: + objectReference: {fileID: 1292078235} - target: {fileID: 5313449993149091347, guid: ebed5434f18ca13e38d2d7bfcc18d6c3, type: 3} propertyPath: timingDoc value: diff --git a/Assets/Src/Laps.cs b/Assets/Src/Laps.cs index d81c524..9fb9018 100644 --- a/Assets/Src/Laps.cs +++ b/Assets/Src/Laps.cs @@ -14,16 +14,15 @@ public enum SectorColour { } [Serializable] -public struct SectorTime { +public class SectorTime { public int Sector; public double Seconds; public override string ToString() => $"{Seconds:0.000}"; } [Serializable] -public struct Lap { +public class Lap { public string Map; - public int Number; public List Sectors; // 0:1:39 @@ -69,11 +68,16 @@ static class LapStorageService { public static LapJSON GetPlayerLaps(string mapName) { LapJSON currentLaps; bool found = lapsByMap.TryGetValue(mapName, out currentLaps); - if (!found) - return new LapJSON { Map = mapName, Laps = new List() }; + if (!found) return EnsureMap(mapName); return currentLaps; } + public static LapJSON EnsureMap(string mapName) { + LapJSON lapJson = new LapJSON { Map = mapName, Laps = new List() }; + lapsByMap[mapName] = lapJson; + return lapJson; + } + static void Persist() { LapStorage lapStorage = new LapStorage { Laps = lapsByMap.Values.ToList() }; File.WriteAllText(lapStoragePath, JsonUtility.ToJson(lapStorage)); @@ -86,14 +90,9 @@ static class LapStorageService { public static int Rank() { return 1; } public static void PersistLap(Lap lap) { - LapJSON lapJson; - bool found = lapsByMap.TryGetValue(lap.Map, out lapJson); - if (!found) - lapJson = new LapJSON { Map = lap.Map, Laps = new List() }; - - // Add lap and persist + LapJSON lapJson = GetPlayerLaps(lap.Map); lapJson.Laps.Add(lap); - lapsByMap[lap.Map] = lapJson; + lapsByMap[lap.Map] = lapJson; // not needed? Persist(); } diff --git a/Assets/Src/Timing.cs b/Assets/Src/Timing.cs index ac6ec25..91e931d 100644 --- a/Assets/Src/Timing.cs +++ b/Assets/Src/Timing.cs @@ -9,24 +9,17 @@ using UnityEditor; using UnityEngine.SceneManagement; public class Timing: MonoBehaviour { - [SerializeField] - public List lapTimes; - [SerializeField] public List sectorTimes; public bool timerEnabled; + public string mapName; public double sectorTime; public double lapTime; double sectorStart; double lapStart; public Sector[] sectors; - public LapStorage lapStorage; - - public string mapName; - void Awake() { - lapTimes = new(); sectorTimes = new List(); sectorStart = 0.0; sectorTime = 0.0; @@ -36,6 +29,8 @@ public class Timing: MonoBehaviour { Scene scene = SceneManager.GetActiveScene(); mapName = scene.name; Debug.Log("Track Sector Count - " + sectors.Length); + + LapStorageService.GetPlayerLaps(mapName); } void Update() { @@ -98,7 +93,6 @@ public class Timing: MonoBehaviour { } void NextLap() { - Debug.Log("NextLap" + lapTimes.Count); sectorTimes = new List(); StartTimer(); } @@ -108,27 +102,19 @@ public class Timing: MonoBehaviour { if (sectorTimes == null) Awake(); - Debug.Log($"Ending lap Sectors={sectorTimes.Count} Name={mapName} Lap={lapTimes.Count}"); - Lap l = new Lap { Map = mapName, Number = lapTimes.Count, Sectors = sectorTimes }; + Debug.Log($"Ending lap Sectors={sectorTimes.Count} Name={mapName}"); + Lap l = new Lap { Map = mapName, Sectors = sectorTimes }; Debug.Log($"EndLap {l}"); - lapTimes.Add(l); - LapStorageService.PersistLap(l); - Debug.Log($"Laps Stored {LapStorageService.GetPlayerLaps(mapName)}"); } - public SectorColour GetSectorColour(SectorTime s) { - if (lapTimes.Count == 0) - return SectorColour.Green; - - var fastestLap = lapTimes.MinBy(l => l.TotalTime()); - var fastestSector = fastestLap.Sectors.Single(fls => fls.Sector == s.Sector); - - return (s.Seconds < fastestSector.Seconds) switch { - true => SectorColour.Green, - false => SectorColour.Yellow, - }; - } + public double PBSectorDiff(SectorTime s) => PB() switch { + Lap pb => pb.Sectors.SingleOrDefault(fls => fls.Sector == s.Sector) switch { + SectorTime fastestSector => s.Seconds - fastestSector.Seconds, + null => -1d, + }, + null => -1d, + }; public Lap PB() { return LapStorageService.PB(mapName); diff --git a/Assets/Src/UI/TimingUI.cs b/Assets/Src/UI/TimingUI.cs index 4d033f3..19ea4a9 100644 --- a/Assets/Src/UI/TimingUI.cs +++ b/Assets/Src/UI/TimingUI.cs @@ -28,6 +28,8 @@ public class TimingUI : MonoBehaviour { timer = uiDocument.rootVisualElement.Q