Newer
Older
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
Marco Zimmer
committed
public class GlobalBehaviour : MonoBehaviour
Marco Zimmer
committed
{
public static GlobalBehaviour Instance
{
get => _Instance;
set
{
_Instance = value;
else
Destroy(value);
}
}
private static GlobalBehaviour _Instance;
Marco Zimmer
committed
//Make sure when using RGBA-Colors, the A-value of animationStartColor
//and animationEndColor is the same OR try with value = 255
public static Color HintAnimationStartColor => Instance._hintAnimationStartColor;
public static Color HintAnimationEndColor => Instance._hintAnimationEndColor;
public static float HintAnimationDuration => Instance._hintAnimationDuration;
public static float AnimationLerpDuration => Instance._AnimationLerpDuration;
Marco Zimmer
committed
public static Color StageAccomplished => Instance._StageAccomplished;
public static Color StageNotYetAccomplished => Instance._StageNotYetAccomplished;
public static Color StageError => Instance._StageError;
public static float GadgetLaserDistance => Instance._GadgetLaserDistance;
public static float GadgetPhysicalDistance => Instance._GadgetPhysicalDistance;
#region Unity Serialization
[SerializeField] private Color _hintAnimationStartColor;
[SerializeField] private Color _hintAnimationEndColor;
[SerializeField] private float _hintAnimationDuration;
[SerializeField] private float _AnimationLerpDuration;
[SerializeField] private Color _StageAccomplished;
[SerializeField] private Color _StageNotYetAccomplished;
[SerializeField] private Color _StageError;
[SerializeField] private float _GadgetLaserDistance;
[SerializeField] private float _GadgetPhysicalDistance;
if (Instance != this)
return;
PostServerConnection();
}
//TODO: Move where appropiate
public int tryScrollListTimes = 2;
static public List<REST_JSON_API.Scroll> AvailableScrolls;
public static IEnumerator InitiateScrolls = IEnumeratorExtensions.yield_break;
StartCoroutine(InitiateScrolls = getScrollsfromServer());
//Try /scroll/listall endpoint when scroll/list is not working
//UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/listAll");
//Postman-Echo-Mock
//UnityWebRequest request = UnityWebRequest.Get("https://019a8ea5-843a-498b-8d0c-778669aef987.mock.pstmn.io/get");
System.DateTime requestTime = System.DateTime.UtcNow;
//UnityWebRequest request = null;
//for (int i = 0; i < this.tryScrollListTimes; i++)
//{
// request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list");
// request.method = UnityWebRequest.kHttpVerbGET;
// yield return request.SendWebRequest();
// if (request.result == UnityWebRequest.Result.ConnectionError
// || request.result == UnityWebRequest.Result.ProtocolError)
// {
// Debug.LogWarning(request.error);
// Debug.Log("GET Scroll/list failed. Attempt: " + (i + 1).ToString());
// }
// else
// break;
//}
System.DateTime answerTime = System.DateTime.UtcNow;
//if (request.result == UnityWebRequest.Result.ConnectionError
// || request.result == UnityWebRequest.Result.ProtocolError)
//{
// Debug.LogWarning(request.error);
//}
//else
//{
// CommunicationEvents.ServerRunning = true;
// while (!request.downloadHandler.isDone)
// yield return null;
// jsonString = request.downloadHandler.text;
//}
if (string.IsNullOrEmpty(jsonString)
|| jsonString.Equals("[]"))
{
jsonString = File.ReadAllText(Application.streamingAssetsPath + "/scrolls.json");
Debug.Log("Using Fallback Scrolls: \n" + jsonString);
}
System.DateTime parseTime = System.DateTime.UtcNow;
AvailableScrolls = JsonConvert.DeserializeObject<List<REST_JSON_API.Scroll>>(jsonString);
Debug.Log(
$"Scroll Times:\n" +
$"Summ\t{(System.DateTime.UtcNow - requestTime).TotalMilliseconds}ms\n" +
$"Server\t{(answerTime - requestTime).TotalMilliseconds}ms\n" +
$"Download\t{(parseTime - answerTime).TotalMilliseconds}ms\n" +
$"Parsing\t{(System.DateTime.UtcNow - parseTime).TotalMilliseconds}ms");