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.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) {