post and pull

This commit is contained in:
ntr committed 2023-08-29 18:08:12 +10:00
1 parent 96e552a2e1
commit 093376d5dd
4 files changed
+44 -22

No files matched your search

+35 -22
View File
@@ -4,21 +4,16 @@ using System.Collections.Generic;
using System.Linq;
using System.IO;
using Newtonsoft.Json;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UIElements;
[Serializable]
public class PlayerData {
public string Id;
}
using SpaceRace.Shared;
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; }
public class PlayerData {
public Guid Id;
}
public class LapFetchService {
@@ -30,7 +25,8 @@ public class LapFetchService {
_mapName = mapName;
try {
string json = File.ReadAllText(playerDataPath);
JsonUtility.FromJsonOverwrite(json, player);
player = JsonConvert.DeserializeObject<PlayerData>(json);
Debug.Log($"Loaded player data {player}");
} catch {
Debug.Log("No existing playerData.json");
CreatePlayerData();
@@ -52,27 +48,44 @@ public class LapFetchService {
Debug.LogError("HTTP Error: " + req.error);
break;
case UnityWebRequest.Result.Success:
Debug.Log("nReceived: " + req.downloadHandler.text);
Debug.Log("Received: " + req.downloadHandler.text);
break;
}
}
}
// laps.count
public static int NumberOfCompletions() { return 0; }
public IEnumerator PostLap(Lap lap) {
LapDTO dto = new LapDTO {
Id = Guid.NewGuid(),
UserId = player.Id,
Map = _mapName,
Time = lap.TotalTime,
Created = DateTimeOffset.UtcNow,
};
// finds your PB in all completions
public static int Position() { return 1; }
Debug.Log(JsonConvert.SerializeObject(dto));
public static void PersistLap(Lap lap) {
Debug.Log(JsonUtility.ToJson(lap));
using (UnityWebRequest req = UnityWebRequest.Post("localhost:40080/Lap", JsonConvert.SerializeObject(dto), "application/json")) {
yield return req.SendWebRequest();
if (req.result != UnityWebRequest.Result.Success) {
Debug.Log(req.error);
}
Debug.Log("posted lap");
}
}
// laps.count
public int NumberOfCompletions() { return 0; }
// finds your PB in all completions
public int Position() { return 1; }
PlayerData CreatePlayerData() {
player = new PlayerData { Id = Guid.NewGuid().ToString() };
Debug.Log(player);
Debug.Log(JsonUtility.ToJson(player));
File.WriteAllText(playerDataPath, JsonUtility.ToJson(player));
player = new PlayerData { Id = Guid.NewGuid() };
Debug.Log(JsonConvert.SerializeObject(player));
File.WriteAllText(playerDataPath, JsonConvert.SerializeObject(player));
return player;
}
}
+1
View File
@@ -126,6 +126,7 @@ public class Timing: MonoBehaviour {
StartCoroutine(noticeLifecycle);
LapStorageService.PersistLap(l);
StartCoroutine(lapFetchService.PostLap(l));
}
IEnumerator RecentLapLifecycle(Lap l) {
+1
View File
@@ -3,6 +3,7 @@
"com.unity.collab-proxy": "2.0.5",
"com.unity.feature.development": "1.0.1",
"com.unity.inputsystem": "1.6.3",
"com.unity.nuget.newtonsoft-json": "3.2.1",
"com.unity.render-pipelines.universal": "14.0.8",
"com.unity.test-framework": "1.1.33",
"com.unity.textmeshpro": "3.0.6",
+7
View File
@@ -91,6 +91,13 @@
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.nuget.newtonsoft-json": {
"version": "3.2.1",
"depth": 0,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.com"
},
"com.unity.performance.profile-analyzer": {
"version": "1.2.2",
"depth": 1,