79 lines
2.3 KiB
C#
79 lines
2.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.IO;
|
|
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using UnityEngine.UIElements;
|
|
|
|
[Serializable]
|
|
public class PlayerData {
|
|
public string Id;
|
|
}
|
|
|
|
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 LapFetchService {
|
|
static string playerDataPath = Path.Combine(Application.persistentDataPath, "playerData.json");
|
|
string _mapName;
|
|
PlayerData player;
|
|
|
|
public LapFetchService(string mapName) {
|
|
_mapName = mapName;
|
|
try {
|
|
string json = File.ReadAllText(playerDataPath);
|
|
JsonUtility.FromJsonOverwrite(json, player);
|
|
} catch {
|
|
Debug.Log("No existing playerData.json");
|
|
CreatePlayerData();
|
|
}
|
|
}
|
|
|
|
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
|
|
public static int NumberOfCompletions() { return 0; }
|
|
|
|
// finds your PB in all completions
|
|
public static int Position() { return 1; }
|
|
|
|
public static void PersistLap(Lap lap) {
|
|
Debug.Log(JsonUtility.ToJson(lap));
|
|
}
|
|
|
|
PlayerData CreatePlayerData() {
|
|
player = new PlayerData { Id = Guid.NewGuid().ToString() };
|
|
Debug.Log(player);
|
|
Debug.Log(JsonUtility.ToJson(player));
|
|
File.WriteAllText(playerDataPath, JsonUtility.ToJson(player));
|
|
return player;
|
|
}
|
|
}
|