post and pull

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