spaceraceshared

This commit is contained in:
ntr committed 2023-08-29 17:38:16 +10:00
1 parent 0b2d4ccd14
commit 96e552a2e1
14 files changed
+118 -93

No files matched your search

+34 -40
View File
@@ -8,45 +8,26 @@ using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UIElements;
[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 => Sectors.Sum(s => s.Seconds);
}
[Serializable]
public class PlayerData {
public Guid Id;
public string Id;
}
public class LapStorage {
public string Map = "unset";
public List<Lap> Laps = new();
public int Completions = 0;
public int Position = 0;
public class LapDTO {
public DateTimeOffset Created { get; set; }
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Map { get; set; }
public double Time { get; set; }
}
static class LapStorageService {
public class LapFetchService {
static string playerDataPath = Path.Combine(Application.persistentDataPath, "playerData.json");
string _mapName;
PlayerData player;
LapStorage lapStorage;
static LapStorageService() {
public LapFetchService(string mapName) {
_mapName = mapName;
try {
string json = File.ReadAllText(playerDataPath);
JsonUtility.FromJsonOverwrite(json, player);
@@ -56,9 +37,25 @@ static class LapStorageService {
}
}
public static LapJSON GetPlayerLaps(string mapName) {
// get
return new Lap;
public IEnumerator GetPb() {
string uri = $"localhost:40080/Pb?userId={player.Id}&mapName={_mapName}";
Debug.Log(uri);
using (UnityWebRequest req = UnityWebRequest.Get(uri)) {
yield return req.SendWebRequest();
switch (req.result) {
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
Debug.LogError("Error: " + req.error);
break;
case UnityWebRequest.Result.ProtocolError:
Debug.LogError("HTTP Error: " + req.error);
break;
case UnityWebRequest.Result.Success:
Debug.Log("nReceived: " + req.downloadHandler.text);
break;
}
}
}
// laps.count
@@ -72,13 +69,10 @@ static class LapStorageService {
}
PlayerData CreatePlayerData() {
player = new PlayerData { Id = Guid.NewGuid() }
File.WriteAllText(playerDataPath, JsonUtility.ToJson(data));
player = new PlayerData { Id = Guid.NewGuid().ToString() };
Debug.Log(player);
Debug.Log(JsonUtility.ToJson(player));
File.WriteAllText(playerDataPath, JsonUtility.ToJson(player));
return player;
}
public static Lap PB(string mapName) {
return lapsByMap[mapName].Laps
.MinBy(l => l.TotalTime);
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 00d682b1176e566f9aae5613d818f385
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+1
View File
@@ -0,0 +1 @@
../../Server/Models/SpaceRaceShared.cs
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 920bca976301d21d2bfd4560944f9803
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+6 -3
View File
@@ -25,6 +25,8 @@ public class Timing: MonoBehaviour {
IEnumerator noticeLifecycle;
LapFetchService lapFetchService;
void Awake() {
ui = GetComponent<TimingUI>();
sectorTimes = new List<SectorTime>();
@@ -38,6 +40,9 @@ public class Timing: MonoBehaviour {
Debug.Log("Track Sector Count - " + sectors.Length);
LapStorageService.GetPlayerLaps(mapName);
lapFetchService = new LapFetchService(mapName);
StartCoroutine(lapFetchService.GetPb());
}
void Update() {
@@ -129,9 +134,7 @@ public class Timing: MonoBehaviour {
ui.HideRecentLap();
}
public Lap PB() {
return LapStorageService.PB(mapName);
}
public Lap PB() => LapStorageService.PB(mapName);
public TimingNotice PBSectorDiff(SectorTime s) => PB() switch {
Lap pb => pb.Sectors.SingleOrDefault(fls => fls.Sector == s.Sector) switch {