sector diffs

This commit is contained in:
ntr committed 2023-08-01 18:04:18 +10:00
1 parent 7c6c6902c5
commit bef194280e
4 files changed
+51 -44

No files matched your search

+4
View File
@@ -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:
+11 -12
View File
@@ -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<SectorTime> 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<Lap>() };
if (!found) return EnsureMap(mapName);
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() {
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<Lap>() };
// Add lap and persist
LapJSON lapJson = GetPlayerLaps(lap.Map);
lapJson.Laps.Add(lap);
lapsByMap[lap.Map] = lapJson;
lapsByMap[lap.Map] = lapJson; // not needed?
Persist();
}
+11 -25
View File
@@ -9,24 +9,17 @@ using UnityEditor;
using UnityEngine.SceneManagement;
public class Timing: MonoBehaviour {
[SerializeField]
public List<Lap> lapTimes;
[SerializeField]
public List<SectorTime> 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<SectorTime>();
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<SectorTime>();
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);
+25 -7
View File
@@ -28,6 +28,8 @@ public class TimingUI : MonoBehaviour {
timer = uiDocument.rootVisualElement.Q<Label>("timer");
pb = uiDocument.rootVisualElement.Q<Label>("pb");
sectors = uiDocument.rootVisualElement.Q<VisualElement>("sectors");
pb.text = "-";
}
void FixedUpdate() {
@@ -47,22 +49,38 @@ public class TimingUI : MonoBehaviour {
if (sectors == null)
Debug.LogWarning("null sectors");
pb.text = $"PB: {timing.PB().ToString()}";
Lap pbLap = timing.PB();
if (pbLap != null)
pb.text = $"{pbLap.ToString()}";
sectors.Clear();
foreach (Sector sector in timing.sectors) {
Label l = timing.sectorTimes
.Where(s => s.Sector == sector.sector)
.Select(s => {
Label l = new Label($"{s}");
string colourClass = timing.GetSectorColour(s).ToString().ToLowerInvariant();
l.AddToClassList(colourClass);
return l;
})
.Select(s => SectorLabel(s))
.DefaultIfEmpty(new Label("-"))
.Single();
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;
}
}