131 lines
3.3 KiB
C#
131 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.IO;
|
|
|
|
using UnityEngine;
|
|
|
|
public enum NoticeCategory {
|
|
Initial,
|
|
Slower,
|
|
Faster,
|
|
Fastest,
|
|
}
|
|
|
|
// public record TimingNotice(
|
|
// double Seconds,
|
|
// NoticeCategory Category,
|
|
// ) {
|
|
// Label ToLabel() {
|
|
// Label l = Category switch {
|
|
// DiffType.Slower => new Label($"+{diff:0.000}"),
|
|
// DiffType.Faster => new Label($"-{diff:0.000}"),
|
|
// DiffType.Fastest => new Label($"-{diff:0.000}"),
|
|
// };
|
|
|
|
// string colourClass = Category Switch {
|
|
// DiffType.Slower => "yellow",
|
|
// DiffType.Faster => "green",
|
|
// DiffType.Fastest => "purple",
|
|
// };
|
|
|
|
// l.AddToClassList(colourClass);
|
|
// return l;
|
|
// }
|
|
// }
|
|
|
|
public enum DiffColour {
|
|
Green,
|
|
Purple,
|
|
White,
|
|
Yellow,
|
|
}
|
|
|
|
[Serializable]
|
|
public class SectorTime {
|
|
public int Sector;
|
|
public double Seconds;
|
|
public override string ToString() => $"{Seconds:0.000}";
|
|
}
|
|
|
|
[Serializable]
|
|
public class Lap {
|
|
public string Map;
|
|
public List<SectorTime> Sectors;
|
|
|
|
// 0:1:39
|
|
public override string ToString() {
|
|
return $"{TimeSpan.FromSeconds(TotalTime()):m\\:ss\\.fff}";
|
|
}
|
|
|
|
public double TotalTime() {
|
|
return Sectors.Sum(s => s.Seconds);
|
|
}
|
|
}
|
|
|
|
|
|
[Serializable]
|
|
public class LapJSON {
|
|
public string Map;
|
|
public List<Lap> Laps = new();
|
|
}
|
|
|
|
// We can't save a dictionary with the Unity JSONUtility
|
|
// so we use this to hold a list of maps
|
|
public class LapStorage {
|
|
public List<LapJSON> Laps = new();
|
|
}
|
|
|
|
static class LapStorageService {
|
|
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 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));
|
|
}
|
|
|
|
// 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 = GetPlayerLaps(lap.Map);
|
|
lapJson.Laps.Add(lap);
|
|
lapsByMap[lap.Map] = lapJson; // not needed?
|
|
Persist();
|
|
}
|
|
|
|
public static Lap PB(string mapName) {
|
|
return lapsByMap[mapName].Laps
|
|
.MinBy(l => l.TotalTime());
|
|
}
|
|
} |