From 28297574ea97f1d35a2b35a12ad7412a75af53dd Mon Sep 17 00:00:00 2001 From: ntr Date: Tue, 29 Aug 2023 18:08:12 +1000 Subject: [PATCH] post and pull --- Assets/Src/LapStorage.cs | 57 +++++++++++++++++++++++-------------- Assets/Src/Timing.cs | 1 + Packages/manifest.json | 1 + Packages/packages-lock.json | 7 +++++ 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/Assets/Src/LapStorage.cs b/Assets/Src/LapStorage.cs index 44d17ba..3b5ef7d 100644 --- a/Assets/Src/LapStorage.cs +++ b/Assets/Src/LapStorage.cs @@ -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(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; } } diff --git a/Assets/Src/Timing.cs b/Assets/Src/Timing.cs index cae4bfc..5fd643a 100644 --- a/Assets/Src/Timing.cs +++ b/Assets/Src/Timing.cs @@ -126,6 +126,7 @@ public class Timing: MonoBehaviour { StartCoroutine(noticeLifecycle); LapStorageService.PersistLap(l); + StartCoroutine(lapFetchService.PostLap(l)); } IEnumerator RecentLapLifecycle(Lap l) { diff --git a/Packages/manifest.json b/Packages/manifest.json index 6c7ce28..0d4660b 100755 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -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", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 15bdf6d..7c09d0f 100755 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -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,