lap storage working

This commit is contained in:
ntr committed 2023-07-30 22:42:10 +10:00
1 parent 57c19e56fc
commit 19f26c7ca9
2 files changed
+59 -56

No files matched your search

+58 -51
View File
@@ -2,6 +2,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.IO;
using UnityEngine; using UnityEngine;
using UnityEditor; using UnityEditor;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
@@ -36,43 +38,65 @@ public struct Lap {
} }
} }
static class LapStorageService {
static Dictionary<string, LapJSON> 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<Lap>() };
LapJson.Laps.Add(lap);
lapsByMap[lap.Map] = lapJson;
LapStorage lapStorage = new LapStorage { Laps = lapsByMap.Values.ToList() };
File.WriteAllText(lapStoragePath, JsonUtility.ToJson(lapStorage));
}
}
[Serializable] [Serializable]
public class LapJSON { public class LapJSON {
public string Map; public string Map;
public List<Lap> Laps; public List<Lap> Laps = new();
} }
public class LapStorage { public class LapStorage {
public List<LapJSON> Laps; public List<LapJSON> Laps = new();
}
static class LapStorageService {
// static Dictionary<string, LapJSON> lapsByMap = new Dictionary<string, LapJSON>();
static Dictionary<string, LapJSON> 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<Lap>() };
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<Lap>() };
// Add lap and persist
lapJson.Laps.Add(lap);
lapsByMap[lap.Map] = lapJson;
Persist();
}
} }
public class Timing: MonoBehaviour { public class Timing: MonoBehaviour {
@@ -90,7 +114,7 @@ public class Timing: MonoBehaviour {
public LapStorage lapStorage; public LapStorage lapStorage;
string mapName; public string mapName;
void Awake() { void Awake() {
lapTimes = new(); lapTimes = new();
@@ -102,9 +126,7 @@ public class Timing: MonoBehaviour {
// set map name // set map name
Scene scene = SceneManager.GetActiveScene(); Scene scene = SceneManager.GetActiveScene();
mapName = scene.name; mapName = scene.name;
Debug.Log("Track Sector Count - " + sectors.Length); Debug.Log("Track Sector Count - " + sectors.Length);
Persist();
} }
void Update() { void Update() {
@@ -178,11 +200,12 @@ public class Timing: MonoBehaviour {
Awake(); Awake();
Debug.Log($"Ending lap Sectors={sectorTimes.Count} Name={mapName} Lap={lapTimes.Count}"); Debug.Log($"Ending lap Sectors={sectorTimes.Count} Name={mapName} Lap={lapTimes.Count}");
Lap l = new Lap { Map = mapName, Number = lapTimes.Count, Sectors = sectorTimes }; Lap l = new Lap { Map = mapName, Number = lapTimes.Count, Sectors = sectorTimes };
Debug.Log($"EndLap {l}"); Debug.Log($"EndLap {l}");
lapTimes.Add(l); lapTimes.Add(l);
Persist();
LapStorageService.PersistLap(l);
Debug.Log($"Laps Stored {LapStorageService.GetPlayerLaps(mapName)}");
} }
public SectorColour GetSectorColour(SectorTime s) { public SectorColour GetSectorColour(SectorTime s) {
@@ -197,21 +220,5 @@ public class Timing: MonoBehaviour {
false => SectorColour.Yellow, 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}");
}
} }
+1 -5
View File
@@ -49,12 +49,8 @@ public class ShipUi : MonoBehaviour {
if (sectors == null) if (sectors == null)
Debug.LogWarning("null sectors"); Debug.LogWarning("null sectors");
// timing not ready yet or smth?
if (timing.lapTimes == null)
return;
timeSheet.Clear(); timeSheet.Clear();
foreach (Lap lap in timing.lapTimes) { foreach (Lap lap in LapStorageService.GetPlayerLaps(timing.mapName).Laps) {
Label l = new Label(); Label l = new Label();
l.text = $"{lap}"; l.text = $"{lap}";
timeSheet.Add(l); timeSheet.Add(l);