From 36fd467819470f54b3474999e893dc0803942e96 Mon Sep 17 00:00:00 2001 From: shadow8t4 Date: Wed, 18 Apr 2018 18:15:11 -0500 Subject: [PATCH] Script updates. --- MoCha/Assets/EmptyClass.cs | 12 ++++ MoCha/Assets/EmptyClass.cs.meta | 13 ++++ MoCha/Assets/PlayerInfo.cs | 35 ++++++++++ MoCha/Assets/PlayerInfo.cs.meta | 13 ++++ MoCha/Assets/Scenes/Testing.unity | Bin 36592 -> 39440 bytes MoCha/Assets/Scripts/GetData.cs | 102 +++++++++++++++++++++++++++--- 6 files changed, 165 insertions(+), 10 deletions(-) create mode 100644 MoCha/Assets/EmptyClass.cs create mode 100644 MoCha/Assets/EmptyClass.cs.meta create mode 100644 MoCha/Assets/PlayerInfo.cs create mode 100644 MoCha/Assets/PlayerInfo.cs.meta 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 7f5561af05aa23704e62ac10cbc58351ab292fd5..c11dac8df82d9ef15858745d1d5112f082517469 100644 GIT binary patch delta 1396 zcmai!-%FEW6vxjyr;W0l{ZPp{W+*sV&7}(~X(kq41Pz25<&EfMMyBnT&7gN8u5}~J zM?!*dRu|q#%hwAb3n?ifQ~v;oF1xYpt~>ia@1CdEn`rRxob!2~=bZDN^L@AT%#>L$ zS&4=i(NpYH3Oo1O+8I{7T7Q@+s;95k4o*}1iN>&-u}%x-I6%|}--6X3SP0Ce5EX$n z%Dcrxb@2CLc?DYrn}T16_2%oC8yf`<65Yklb1dbVN}GW4;9GGpbjV2ZPOzyGBgyB> z^JjU$Ww1vg;F@55yrv5bVd;xtNx=}7)&2Jg0&nb^20|AvI&*}<9BhKssy7WL>=()i0py|z*gb&&Scb+&#z%8$5A86W5H&@ zUW$Vgg87dT)tiXNB_>$19ED&cI8Kv-Wh#tr**tU|k7yC>djSzb(E3L&p9HB{8&N+D z{&pJzTC0q?ZweLwlbzYlHAMO4RWPZUAAXdhvPqD2pGI+Zy;i&UC*{l1P z%v|rRbG$8YY$Y}oE1Mg>nRjR6HO=yaz}=*=l0Mjff4jS?Pc8}hcv<~;WvM*rGg0;; zQTqorn{ctM2Ns*qynaKuU^PML9uKl*_n4{ z;ttk4YSFHYtQ2sAE!``&?QM`$5l^7Ua*bAKEn_u`Vq(EmwJG~vOQ(7irI2kuJnyW< zsZcaJ7&+&5cMpVng5guqV0XyfKhPcOa>pXUaKzo{^19Aw&h)1uW*xQhq&_)yomMY*A!JiDZ#-h=IJA5F-C(-9UnMFQcw)U0WZmq3w|7vhHUzwMBb?9BXc2thtq9fW# z`rMf|iw#Y~27=s>8pFKZ5)eiK$q^vS?7gn3 z?T)s{+GMn}wQ1htygy^A^TMc5PrJi@5mWWzRY19=7oXgAP5;JEDj;*^kbK@V<#EYW h*X5Xyt%IPntNjLNcB)AgJxK^5 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; + } }