sector diffs

This commit is contained in:
ntr committed 2023-08-01 18:04:18 +10:00
1 parent 42fcc9fe1b
commit 758ee8e8b2
4 files changed
+52 -45

No files matched your search

+4
View File
@@ -463,6 +463,10 @@ PrefabInstance:
propertyPath: shipUi propertyPath: shipUi
value: value:
objectReference: {fileID: 1292078235} objectReference: {fileID: 1292078235}
- target: {fileID: 5313449993149091347, guid: ebed5434f18ca13e38d2d7bfcc18d6c3, type: 3}
propertyPath: timingUi
value:
objectReference: {fileID: 1292078235}
- target: {fileID: 5313449993149091347, guid: ebed5434f18ca13e38d2d7bfcc18d6c3, type: 3} - target: {fileID: 5313449993149091347, guid: ebed5434f18ca13e38d2d7bfcc18d6c3, type: 3}
propertyPath: timingDoc propertyPath: timingDoc
value: value:
+11 -12
View File
@@ -14,16 +14,15 @@ public enum SectorColour {
} }
[Serializable] [Serializable]
public struct SectorTime { public class SectorTime {
public int Sector; public int Sector;
public double Seconds; public double Seconds;
public override string ToString() => $"{Seconds:0.000}"; public override string ToString() => $"{Seconds:0.000}";
} }
[Serializable] [Serializable]
public struct Lap { public class Lap {
public string Map; public string Map;
public int Number;
public List<SectorTime> Sectors; public List<SectorTime> Sectors;
// 0:1:39 // 0:1:39
@@ -69,11 +68,16 @@ static class LapStorageService {
public static LapJSON GetPlayerLaps(string mapName) { public static LapJSON GetPlayerLaps(string mapName) {
LapJSON currentLaps; LapJSON currentLaps;
bool found = lapsByMap.TryGetValue(mapName, out currentLaps); bool found = lapsByMap.TryGetValue(mapName, out currentLaps);
if (!found) if (!found) return EnsureMap(mapName);
return new LapJSON { Map = mapName, Laps = new List<Lap>() };
return currentLaps; return currentLaps;
} }
public static LapJSON EnsureMap(string mapName) {
LapJSON lapJson = new LapJSON { Map = mapName, Laps = new List<Lap>() };
lapsByMap[mapName] = lapJson;
return lapJson;
}
static void Persist() { static void Persist() {
LapStorage lapStorage = new LapStorage { Laps = lapsByMap.Values.ToList() }; LapStorage lapStorage = new LapStorage { Laps = lapsByMap.Values.ToList() };
File.WriteAllText(lapStoragePath, JsonUtility.ToJson(lapStorage)); File.WriteAllText(lapStoragePath, JsonUtility.ToJson(lapStorage));
@@ -86,14 +90,9 @@ static class LapStorageService {
public static int Rank() { return 1; } public static int Rank() { return 1; }
public static void PersistLap(Lap lap) { public static void PersistLap(Lap lap) {
LapJSON lapJson; LapJSON lapJson = GetPlayerLaps(lap.Map);
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); lapJson.Laps.Add(lap);
lapsByMap[lap.Map] = lapJson; lapsByMap[lap.Map] = lapJson; // not needed?
Persist(); Persist();
} }
+12 -26
View File
@@ -9,24 +9,17 @@ using UnityEditor;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
public class Timing: MonoBehaviour { public class Timing: MonoBehaviour {
[SerializeField]
public List<Lap> lapTimes;
[SerializeField]
public List<SectorTime> sectorTimes; public List<SectorTime> sectorTimes;
public bool timerEnabled; public bool timerEnabled;
public string mapName;
public double sectorTime; public double sectorTime;
public double lapTime; public double lapTime;
double sectorStart; double sectorStart;
double lapStart; double lapStart;
public Sector[] sectors; public Sector[] sectors;
public LapStorage lapStorage;
public string mapName;
void Awake() { void Awake() {
lapTimes = new();
sectorTimes = new List<SectorTime>(); sectorTimes = new List<SectorTime>();
sectorStart = 0.0; sectorStart = 0.0;
sectorTime = 0.0; sectorTime = 0.0;
@@ -36,6 +29,8 @@ public class Timing: MonoBehaviour {
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);
LapStorageService.GetPlayerLaps(mapName);
} }
void Update() { void Update() {
@@ -98,7 +93,6 @@ public class Timing: MonoBehaviour {
} }
void NextLap() { void NextLap() {
Debug.Log("NextLap" + lapTimes.Count);
sectorTimes = new List<SectorTime>(); sectorTimes = new List<SectorTime>();
StartTimer(); StartTimer();
} }
@@ -108,27 +102,19 @@ public class Timing: MonoBehaviour {
if (sectorTimes == null) if (sectorTimes == null)
Awake(); Awake();
Debug.Log($"Ending lap Sectors={sectorTimes.Count} Name={mapName} Lap={lapTimes.Count}"); Debug.Log($"Ending lap Sectors={sectorTimes.Count} Name={mapName}");
Lap l = new Lap { Map = mapName, Number = lapTimes.Count, Sectors = sectorTimes }; Lap l = new Lap { Map = mapName, Sectors = sectorTimes };
Debug.Log($"EndLap {l}"); Debug.Log($"EndLap {l}");
lapTimes.Add(l);
LapStorageService.PersistLap(l); LapStorageService.PersistLap(l);
Debug.Log($"Laps Stored {LapStorageService.GetPlayerLaps(mapName)}");
} }
public SectorColour GetSectorColour(SectorTime s) { public double PBSectorDiff(SectorTime s) => PB() switch {
if (lapTimes.Count == 0) Lap pb => pb.Sectors.SingleOrDefault(fls => fls.Sector == s.Sector) switch {
return SectorColour.Green; SectorTime fastestSector => s.Seconds - fastestSector.Seconds,
null => -1d,
var fastestLap = lapTimes.MinBy(l => l.TotalTime()); },
var fastestSector = fastestLap.Sectors.Single(fls => fls.Sector == s.Sector); null => -1d,
};
return (s.Seconds < fastestSector.Seconds) switch {
true => SectorColour.Green,
false => SectorColour.Yellow,
};
}
public Lap PB() { public Lap PB() {
return LapStorageService.PB(mapName); return LapStorageService.PB(mapName);
+25 -7
View File
@@ -28,6 +28,8 @@ public class TimingUI : MonoBehaviour {
timer = uiDocument.rootVisualElement.Q<Label>("timer"); timer = uiDocument.rootVisualElement.Q<Label>("timer");
pb = uiDocument.rootVisualElement.Q<Label>("pb"); pb = uiDocument.rootVisualElement.Q<Label>("pb");
sectors = uiDocument.rootVisualElement.Q<VisualElement>("sectors"); sectors = uiDocument.rootVisualElement.Q<VisualElement>("sectors");
pb.text = "-";
} }
void FixedUpdate() { void FixedUpdate() {
@@ -47,22 +49,38 @@ public class TimingUI : MonoBehaviour {
if (sectors == null) if (sectors == null)
Debug.LogWarning("null sectors"); Debug.LogWarning("null sectors");
pb.text = $"PB: {timing.PB().ToString()}"; Lap pbLap = timing.PB();
if (pbLap != null)
pb.text = $"{pbLap.ToString()}";
sectors.Clear(); sectors.Clear();
foreach (Sector sector in timing.sectors) { foreach (Sector sector in timing.sectors) {
Label l = timing.sectorTimes Label l = timing.sectorTimes
.Where(s => s.Sector == sector.sector) .Where(s => s.Sector == sector.sector)
.Select(s => { .Select(s => SectorLabel(s))
Label l = new Label($"{s}");
string colourClass = timing.GetSectorColour(s).ToString().ToLowerInvariant();
l.AddToClassList(colourClass);
return l;
})
.DefaultIfEmpty(new Label("-")) .DefaultIfEmpty(new Label("-"))
.Single(); .Single();
sectors.Add(l); sectors.Add(l);
} }
} }
Label SectorLabel(SectorTime s) {
double diff = timing.PBSectorDiff(s);
string sign = diff switch {
< 0 => "",
_ => "+",
};
SectorColour colour = diff switch {
< 0 => SectorColour.Green,
_ => SectorColour.Yellow,
};
Label l = new Label($"{sign}{diff:0.000}");
string colourClass = colour.ToString().ToLowerInvariant();
l.AddToClassList(colourClass);
return l;
}
} }