From 19f26c7ca96eca64189d0efbf84ba7fd2418744b Mon Sep 17 00:00:00 2001 From: ntr Date: Sun, 30 Jul 2023 22:42:10 +1000 Subject: [PATCH] lap storage working --- Assets/Src/Timing.cs | 109 +++++++++++++++++++++------------------- Assets/Src/UI/ShipUi.cs | 6 +-- 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/Assets/Src/Timing.cs b/Assets/Src/Timing.cs index 19a4a94..690d6aa 100644 --- a/Assets/Src/Timing.cs +++ b/Assets/Src/Timing.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.IO; + using UnityEngine; using UnityEditor; using UnityEngine.SceneManagement; @@ -36,43 +38,65 @@ public struct Lap { } } -static class LapStorageService { - static Dictionary lapsByMap; - static string lapStoragePath = Path.Combine(Application.persistentDataPath, "lapStorage.json"); - - static void GetPlayerLaps() { - // read json - // assign it to lapStorage - } - - // laps.count - static int NumberOfCompletions() {} - - // finds your PB in all completions - static int Rank() {} - - // open json - public static void PersistLap(Lap lap) { - bool found = lapsByMap.TryGetValue(lap.Map, out var lapJson); - if (!found) - lapJson = new LapJSON { Map = lap.Map, Laps = new List() }; - LapJson.Laps.Add(lap); - lapsByMap[lap.Map] = lapJson; - - LapStorage lapStorage = new LapStorage { Laps = lapsByMap.Values.ToList() }; - File.WriteAllText(lapStoragePath, JsonUtility.ToJson(lapStorage)); - } - -} [Serializable] public class LapJSON { public string Map; - public List Laps; + public List Laps = new(); } public class LapStorage { - public List Laps; + public List Laps = new(); +} + +static class LapStorageService { + // static Dictionary lapsByMap = new Dictionary(); + static Dictionary lapsByMap = new(); + static string lapStoragePath = Path.Combine(Application.persistentDataPath, "lapStorage.json"); + + static LapStorageService() { + LapStorage lapStorage = new LapStorage(); + + try { + string json = File.ReadAllText(lapStoragePath); + JsonUtility.FromJsonOverwrite(json, lapStorage); + } catch { + Debug.Log("No existing lapStorage.json"); + } + + lapsByMap = lapStorage.Laps.ToDictionary((lapJson) => lapJson.Map); + } + + 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() }; + return currentLaps; + } + + static void Persist() { + LapStorage lapStorage = new LapStorage { Laps = lapsByMap.Values.ToList() }; + File.WriteAllText(lapStoragePath, JsonUtility.ToJson(lapStorage)); + } + + // laps.count + public static int NumberOfCompletions() { return 0; } + + // finds your PB in all completions + 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.Laps.Add(lap); + lapsByMap[lap.Map] = lapJson; + Persist(); + } } public class Timing: MonoBehaviour { @@ -90,7 +114,7 @@ public class Timing: MonoBehaviour { public LapStorage lapStorage; - string mapName; + public string mapName; void Awake() { lapTimes = new(); @@ -102,9 +126,7 @@ public class Timing: MonoBehaviour { // set map name Scene scene = SceneManager.GetActiveScene(); mapName = scene.name; - Debug.Log("Track Sector Count - " + sectors.Length); - Persist(); } void Update() { @@ -178,11 +200,12 @@ public class Timing: MonoBehaviour { 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($"EndLap {l}"); lapTimes.Add(l); - Persist(); + + LapStorageService.PersistLap(l); + Debug.Log($"Laps Stored {LapStorageService.GetPlayerLaps(mapName)}"); } public SectorColour GetSectorColour(SectorTime s) { @@ -197,21 +220,5 @@ public class Timing: MonoBehaviour { false => SectorColour.Yellow, }; } - - void Load() { - try { - string json = File.ReadAllText(lapStoragePath); - JsonUtility.FromJsonOverwrite(json, lapStorage); - } catch { - // file doesn't exist yet - return Persist(); - } - } - - void Persist() { - LapJSON lapJson = new LapJSON { Map = mapName, Laps = lapTimes }; - string lapJsonStr = JsonUtility.ToJson(lapJson); - Debug.Log($"Lap JSON {lapJsonStr}"); - } } diff --git a/Assets/Src/UI/ShipUi.cs b/Assets/Src/UI/ShipUi.cs index 97025ac..39345e7 100644 --- a/Assets/Src/UI/ShipUi.cs +++ b/Assets/Src/UI/ShipUi.cs @@ -49,12 +49,8 @@ public class ShipUi : MonoBehaviour { if (sectors == null) Debug.LogWarning("null sectors"); - // timing not ready yet or smth? - if (timing.lapTimes == null) - return; - timeSheet.Clear(); - foreach (Lap lap in timing.lapTimes) { + foreach (Lap lap in LapStorageService.GetPlayerLaps(timing.mapName).Laps) { Label l = new Label(); l.text = $"{lap}"; timeSheet.Add(l);