diff --git a/Assets/Scripts/GlobalBehaviour.cs b/Assets/Scripts/GlobalBehaviour.cs index 21b75000ee785e696c90349c7dbe0bcae067c04b..071a85bae356483e9c4d825dd35dcdb102f3bf19 100644 --- a/Assets/Scripts/GlobalBehaviour.cs +++ b/Assets/Scripts/GlobalBehaviour.cs @@ -1,4 +1,9 @@ -using UnityEngine; +using Newtonsoft.Json; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using UnityEngine; +using UnityEngine.Networking; public class GlobalBehaviour : MonoBehaviour, ISerializationCallbackReceiver { @@ -7,7 +12,7 @@ public static GlobalBehaviour Instance get => _Instance; set { - if (_Instance == null) + if (_Instance == null) _Instance = value; else Destroy(value); @@ -73,5 +78,72 @@ void ISerializationCallbackReceiver.OnBeforeSerialize() private void Awake() { Instance = this; + DontDestroyOnLoad(this); + } + + private void Start() + { + PostServerConnection(); + } + + + //TODO: Move where appropiate + + public int tryScrollListTimes = 2; + static public List<Scroll> AvailableScrolls; + + private void PostServerConnection() + { + StartCoroutine(getScrollsfromServer()); + + IEnumerator getScrollsfromServer() + { + //Try /scroll/listAll endpoint when scroll/list is not working + UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list"); + //Postman-Echo-Mock + //UnityWebRequest request = UnityWebRequest.Get("https://019a8ea5-843a-498b-8d0c-778669aef987.mock.pstmn.io/get"); + + 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; + } + + + string jsonString = null; + + if (request.result == UnityWebRequest.Result.ConnectionError + || request.result == UnityWebRequest.Result.ProtocolError) + { + Debug.LogWarning(request.error); + } + else + { + CommunicationEvents.ServerRunning = true; + 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 startTime = System.DateTime.UtcNow; + AvailableScrolls = JsonConvert.DeserializeObject<List<Scroll>>(jsonString); + Debug.Log("Scroll Parsing in: " + (System.DateTime.UtcNow - startTime).TotalMilliseconds + "ms"); + } } } diff --git a/Assets/Scripts/InventoryStuff/DisplayScrolls.cs b/Assets/Scripts/InventoryStuff/DisplayScrolls.cs index c3551e2cacca01dd0a93940c134fa3ca8b4e020a..1b7bf8ec0a3a76c5064087a617f5922c0f05d802 100644 --- a/Assets/Scripts/InventoryStuff/DisplayScrolls.cs +++ b/Assets/Scripts/InventoryStuff/DisplayScrolls.cs @@ -1,120 +1,35 @@ -using Newtonsoft.Json; -using System.Collections; -using System.Collections.Generic; -using System.IO; +using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; -using UnityEngine.Networking; public class DisplayScrolls : MonoBehaviour { public string preferredStartScrollName; - public int tryScrollListTimes = 2; - static public List<Scroll> AvailableScrolls; static public List<Scroll> AllowedScrolls; public GameObject[] ScrollButtons; public GameObject ScrollPrefab; public GameObject DetailScreen; - public Transform scrollscreenContent; - - - public int x_Start; - public int y_Start; - public int X_Pacece_Between_Items; - public int y_Pacece_Between_Items; - public int number_of_Column; - - - public Vector3 GetPosition(int i) - { - //return new Vector3(x_Start + (X_Pacece_Between_Items * (i % number_of_Column)), y_Start + (-y_Pacece_Between_Items * (i / number_of_Column)), 0f); - return Vector3.zero; - } - - // Start is called before the first frame update void Start() { - var rect = GetComponent<RectTransform>(); - x_Start = (int)(rect.rect.x + X_Pacece_Between_Items * .5f); - y_Start = (int)(-rect.rect.y - y_Pacece_Between_Items * .5f);//); - number_of_Column = Mathf.Max(1, (int)(rect.rect.width / ScrollPrefab.GetComponent<RectTransform>().rect.width) - 1); - //get Scrolls from Backend; - - //string path = "Mock-Scrolls.json"; - //string jsonString = File.ReadAllText(path); - //buildScrollSelection(jsonString); - StartCoroutine(getScrollsfromServer()); - - } - - IEnumerator getScrollsfromServer() - { - //Try /scroll/listAll endpoint when scroll/list is not working - UnityWebRequest request = UnityWebRequest.Get(CommunicationEvents.ServerAdress + "/scroll/list"); - //Postman-Echo-Mock - //UnityWebRequest request = UnityWebRequest.Get("https://019a8ea5-843a-498b-8d0c-778669aef987.mock.pstmn.io/get"); - - 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; - } - } - - if (request.result == UnityWebRequest.Result.ConnectionError - || request.result == UnityWebRequest.Result.ProtocolError) - { - Debug.LogWarning(request.error); - string jsonString = File.ReadAllText(Application.streamingAssetsPath + "/scrolls.json"); - Debug.Log(jsonString); - BuildScrolls(jsonString); - } - else - { - CommunicationEvents.ServerRunning = true; - string jsonString = request.downloadHandler.text; - Debug.Log("JsonString from Server: \n" + jsonString); - if (jsonString.Equals("[]")) - jsonString = File.ReadAllText(Application.streamingAssetsPath + "/scrolls.json"); - Debug.Log("Used JsonString: \n" + jsonString); - //scroll display not yet implemented; - //buildScrollSelection(jsonString); - BuildScrolls(jsonString); - } + BuildScrollGUI(); } - void BuildScrolls(string jsonString) + void BuildScrollGUI() { - System.DateTime startTime = System.DateTime.UtcNow; - AvailableScrolls = JsonConvert.DeserializeObject<List<Scroll>>(jsonString); - Debug.Log("Scroll Parsing in: " + (System.DateTime.UtcNow - startTime).TotalMilliseconds + "ms"); - - AllowedScrolls = AvailableScrolls + AllowedScrolls = GlobalBehaviour.AvailableScrolls .Where(s => StageStatic.stage.AllowedScrolls?.Contains(s.@ref) ?? true) .ToList(); //Build Selection-GUI of Scrolls ScrollButtons = new GameObject[AllowedScrolls.Count()]; for (int i = 0; i < AllowedScrolls.Count; i++) - { - var obj = Instantiate(ScrollPrefab, Vector3.zero, Quaternion.identity, scrollscreenContent); - obj.GetComponent<RectTransform>().localPosition = GetPosition(i); + { + var obj = Instantiate(ScrollPrefab, scrollscreenContent); obj.GetComponent<ScrollClickedScript>().scroll = AllowedScrolls[i]; obj.GetComponent<ScrollClickedScript>().DetailScreen = this.DetailScreen; obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = AllowedScrolls[i].label; diff --git a/Assets/Scripts/InventoryStuff/ScrollDetails.cs b/Assets/Scripts/InventoryStuff/ScrollDetails.cs index f52183d90d2b4910fea6f3b0e5f9ae88b54cbcc0..5d2cfca9faa5d66bd3fc15aed2b42df6c631c187 100644 --- a/Assets/Scripts/InventoryStuff/ScrollDetails.cs +++ b/Assets/Scripts/InventoryStuff/ScrollDetails.cs @@ -118,8 +118,6 @@ IEnumerator newAssignment() } else { - // Todo delte maybe - Debug.Log("Current mmt answer: " + currentMmtAnswer); Scroll.ScrollDynamicInfo scrollDynamicInfo = JsonConvert.DeserializeObject<Scroll.ScrollDynamicInfo>(currentMmtAnswer); processScrollDynamicInfo(scrollDynamicInfo); } diff --git a/Assets/Scripts/StageStatic.cs b/Assets/Scripts/StageStatic.cs index 447ddf59a7dc2544e20ac576603d3fb0f5143232..a7795875b4deef7ef0947cba70355486ce702b53 100644 --- a/Assets/Scripts/StageStatic.cs +++ b/Assets/Scripts/StageStatic.cs @@ -300,11 +300,13 @@ public static bool ContainsNumber(string category, int i, bool local) /// <summary> /// Looks for and initial loads (see <see cref="Stage.ShallowLoad(out Stage, string)"/>) <see cref="Stage">Stages</see> in <see cref="local_stage"/> and !<see cref="local_stage"/>. /// </summary> - public static void ShallowLoadStages() + public static void ShallowLoadStages(bool force = false) { - StageOfficial = StageLocal = null; - StageOfficial = Stage.Grup(null, true); - StageLocal = Stage.Grup(null, false); + if(force) + StageOfficial = StageLocal = null; + + StageOfficial ??= Stage.Grup(null, true); + StageLocal ??= Stage.Grup(null, false); } /// <summary> diff --git a/Assets/Scripts/StartServer.cs b/Assets/Scripts/StartServer.cs index 35122044a4363593fe1b107d2e445b3710f3654f..30d84a1a3d0aaa48e2fd7331e5f88a72475fcb47 100644 --- a/Assets/Scripts/StartServer.cs +++ b/Assets/Scripts/StartServer.cs @@ -33,7 +33,7 @@ void Start() StartCoroutine(ServerRoutine1()); } } - // Update is called once per frame + void Update() { if (autoend == 1) @@ -47,9 +47,6 @@ void Update() } } - - - void PrepareGame() { if (autoprepareGame !=0) @@ -58,16 +55,8 @@ void PrepareGame() CommunicationEvents.ServerRunning = true; UnityEngine.Debug.Log("server fin"); } - - } - - - - - - IEnumerator ServerRoutine1() { @@ -89,13 +78,8 @@ IEnumerator ServerRoutine1() process = Process.Start(processInfo); } else - /* - */ Process.Start("powershell.exe", command); - - - // *** Read the streams *** // Warning: This approach can lead to deadlocks, see Edit #2 //string output = process.StandardOutput.ReadToEnd(); @@ -109,15 +93,9 @@ IEnumerator ServerRoutine1() // Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand"); // process.Close(); - - - - - yield return null; } - IEnumerator ServerRoutine() { CommunicationEvents.ServerAddressLocal = ServerAddressLocalhost + ":" + ServerPortDefault; @@ -153,7 +131,7 @@ IEnumerator ServerRoutine() #endif - while (true && autocheckIfServerIsRunning != 0) + while (autocheckIfServerIsRunning != 0) { //Wait for 2 seconds yield return new WaitForSecondsRealtime(2f); @@ -171,8 +149,6 @@ IEnumerator ServerRoutine() break; } - - yield return null; } @@ -192,6 +168,4 @@ IEnumerator ServerRoutine() PrepareGame(); yield return null; } - - -} +} \ No newline at end of file diff --git a/Assets/Scripts/StartServer_mctrl.cs b/Assets/Scripts/StartServer_mctrl.cs index 7a1e1a668d5e1eef7eb6104c23ad2d64065b90a3..eb47559732dc1978a1a8d0fae3b12cdfdded43d9 100644 --- a/Assets/Scripts/StartServer_mctrl.cs +++ b/Assets/Scripts/StartServer_mctrl.cs @@ -1,17 +1,11 @@ using System.Collections; -using System.Diagnostics; using UnityEngine; -using UnityEngine.Networking; -using UnityEngine.SceneManagement; -using static UIconfig; -using static CommunicationEvents; public class StartServer_mctrl : MonoBehaviour { - public GameObject ServerScriptGObj; - // Start is called before the first frame update + void Start() { StartCoroutine(ServerRoutine()); @@ -19,9 +13,11 @@ void Start() IEnumerator ServerRoutine() { - //Wait for 1 seconds - yield return new WaitForSecondsRealtime(1f); - if (ServerAutoStart == true) + ////Wait for 1 seconds + //yield return new WaitForSecondsRealtime(1f); + if (CommunicationEvents.ServerAutoStart) ServerScriptGObj.SetActive(true); + + yield break; } } \ No newline at end of file