diff --git a/MoCha/Assets/EmptyClass.cs b/MoCha/Assets/EmptyClass.cs new file mode 100644 index 0000000..4b9bdfe --- /dev/null +++ b/MoCha/Assets/EmptyClass.cs @@ -0,0 +1,12 @@ +using System; + +namespace AssemblyCSharp +{ + public class EmptyClass + { + public EmptyClass () + { + } + } +} + diff --git a/MoCha/Assets/EmptyClass.cs.meta b/MoCha/Assets/EmptyClass.cs.meta new file mode 100644 index 0000000..2f0d987 --- /dev/null +++ b/MoCha/Assets/EmptyClass.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: 9ccd5f892b119d749980bbde89a80e70 +timeCreated: 1524075474 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MoCha/Assets/PlayerInfo.cs b/MoCha/Assets/PlayerInfo.cs new file mode 100644 index 0000000..8288825 --- /dev/null +++ b/MoCha/Assets/PlayerInfo.cs @@ -0,0 +1,35 @@ +using UnityEngine; +using System.Collections; + +public class PlayerInfo +{ + public string username = "[{\"username\":\"test\", \"user_id\": 1}]"; + public int user_id = 0; + + public PlayerInfo() + { + username = ""; + user_id = 0; + } + + public void setUsername(string user) + { + username = user; + } + + public string getUsername() + { + return username; + } + + public void setUserId(int id) + { + user_id = id; + } + + public int getUserId() + { + return user_id; + } +} + diff --git a/MoCha/Assets/PlayerInfo.cs.meta b/MoCha/Assets/PlayerInfo.cs.meta new file mode 100644 index 0000000..b926322 --- /dev/null +++ b/MoCha/Assets/PlayerInfo.cs.meta @@ -0,0 +1,13 @@ +fileFormatVersion: 2 +guid: db6fec37a2679474e90cb02700d9533e +timeCreated: 1524075765 +licenseType: Free +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MoCha/Assets/Scenes/Testing.unity b/MoCha/Assets/Scenes/Testing.unity index 7f5561a..c11dac8 100644 Binary files a/MoCha/Assets/Scenes/Testing.unity and b/MoCha/Assets/Scenes/Testing.unity differ diff --git a/MoCha/Assets/Scripts/GetData.cs b/MoCha/Assets/Scripts/GetData.cs index ed3a068..8f96f2f 100644 --- a/MoCha/Assets/Scripts/GetData.cs +++ b/MoCha/Assets/Scripts/GetData.cs @@ -1,11 +1,21 @@ using UnityEngine; using UnityEngine.Networking; using System.Collections; +using System.Collections.Generic; using UnityEngine.UI; +/* + * USAGE + * + * Just put this script into a button that needs + * to execute the HTTP request, and in the text field + * write the exact URL needed for the request. + * + * */ + + public class GetData : MonoBehaviour { - //string displayText = ""; Text objectText; void Start() @@ -13,16 +23,14 @@ public class GetData : MonoBehaviour objectText = GetComponentInParent (); } - public void DisplayStuff() + public void DisplayStuff(string request) { - StartCoroutine (MakeRequest ()); - //Debug.Log (displayText); - //objectText.text = displayText; + StartCoroutine (MakeRequest (request)); } - IEnumerator MakeRequest() + IEnumerator MakeRequest(string request) { - using (UnityWebRequest www = UnityWebRequest.Get("https://corder.tech/mocha/users/1")) + using (UnityWebRequest www = UnityWebRequest.Get(request)) { yield return www.SendWebRequest(); @@ -31,14 +39,88 @@ public class GetData : MonoBehaviour Debug.Log(www.error); } else - { + { + + //this should be saved somewhere else... + List players; + // Show results as text - //Debug.Log(www.downloadHandler.text); - objectText.text = www.downloadHandler.text; + players = ParseJsonRaw(www.downloadHandler.text); + objectText.text = DisplayInfo (players); // Or retrieve results as binary data byte[] results = www.downloadHandler.data; } } } + + public List ParseJsonRaw(string json) + { + List players = new List(); + string tempjson = ""; + + bool ignorequotes = false; + bool specialchar = false; + bool insideobject = false; + + foreach (char c in json) + { + if (insideobject) + { + tempjson += c; + } + + if (c == '\"') + { + if (!specialchar) + { + ignorequotes = !ignorequotes; + } + } + + if(specialchar) + { + specialchar = false; + } + + if(c == '\\') + { + specialchar = true; + } + + if (!ignorequotes) + { + if (c == '{') + { + insideobject = true; + tempjson += c; + } + else if (c == '}') + { + insideobject = false; + players.Add(ParseJson(tempjson)); + tempjson = ""; + } + } + } + + return players; + } + + public PlayerInfo ParseJson(string json) + { + return JsonUtility.FromJson(json); + } + + public string DisplayInfo(List p) + { + string output = ""; + + foreach (PlayerInfo pi in p) + { + output += "Player ID: " + pi.user_id + "\t\tUser: " + pi.username + "\n"; + } + + return output; + } }