Merge remote-tracking branch 'origin/alex' into darrel
This commit is contained in:
commit
1501bd9bc9
12 changed files with 326 additions and 34 deletions
Binary file not shown.
|
@ -1,5 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: f9d68b692a268dd4ba72bf2d814a1204
|
||||
guid: b6e77f2ba4a9de64fa14948947b5767e
|
||||
timeCreated: 1524106311
|
||||
licenseType: Free
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
Binary file not shown.
|
@ -1,44 +1,29 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using System.Collections;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class GetData : MonoBehaviour
|
||||
{
|
||||
//string displayText = "";
|
||||
Text objectText;
|
||||
public Button thisbutton;
|
||||
public Text objectText;
|
||||
|
||||
public string request = "http://corder.tech/mocha/users/*";
|
||||
|
||||
void Start()
|
||||
{
|
||||
objectText = GetComponentInParent<Text> ();
|
||||
thisbutton = GetComponent<Button> ();
|
||||
objectText = GetComponent<Text> ();
|
||||
|
||||
thisbutton.onClick.AddListener (CallGetData);
|
||||
}
|
||||
|
||||
public void DisplayStuff()
|
||||
public void CallGetData()
|
||||
{
|
||||
StartCoroutine (MakeRequest ());
|
||||
//Debug.Log (displayText);
|
||||
//objectText.text = displayText;
|
||||
DisplayAll(request, objectText);
|
||||
}
|
||||
|
||||
IEnumerator MakeRequest()
|
||||
public void DisplayAll(string request, Text objectText)
|
||||
{
|
||||
using (UnityWebRequest www = UnityWebRequest.Get("https://corder.tech/mocha/users/1"))
|
||||
{
|
||||
yield return www.SendWebRequest();
|
||||
|
||||
if (www.isNetworkError || www.isHttpError)
|
||||
{
|
||||
Debug.Log(www.error);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Show results as text
|
||||
//Debug.Log(www.downloadHandler.text);
|
||||
objectText.text = www.downloadHandler.text;
|
||||
|
||||
// Or retrieve results as binary data
|
||||
byte[] results = www.downloadHandler.data;
|
||||
}
|
||||
}
|
||||
MochaParser parser = new MochaParser();
|
||||
StartCoroutine (parser.DisplayOutput(request, objectText));
|
||||
}
|
||||
}
|
||||
|
|
41
MoCha/Assets/Scripts/GetLeaderboard.cs
Normal file
41
MoCha/Assets/Scripts/GetLeaderboard.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
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.
|
||||
*
|
||||
* */
|
||||
|
||||
// http://corder.tech/mocha/users/*
|
||||
|
||||
public class GetLeaderboard : MonoBehaviour
|
||||
{
|
||||
public Button thisbutton;
|
||||
public Text PlayerIDs;
|
||||
public Text Usernames;
|
||||
public Text Scores;
|
||||
|
||||
public string request = "http://corder.tech/mocha/users/*";
|
||||
|
||||
void Start()
|
||||
{
|
||||
thisbutton.onClick.AddListener (CallGetData);
|
||||
}
|
||||
|
||||
public void CallGetData()
|
||||
{
|
||||
DisplayAllSeparated(request, PlayerIDs, Usernames, Scores);
|
||||
}
|
||||
|
||||
public void DisplayAllSeparated(string request, Text PlayerIDs, Text Usernames, Text Scores)
|
||||
{
|
||||
MochaParser parser = new MochaParser ();
|
||||
StartCoroutine (parser.DisplayOutputSeparated(request, PlayerIDs, Usernames, Scores));
|
||||
}
|
||||
}
|
||||
|
13
MoCha/Assets/Scripts/GetLeaderboard.cs.meta
Normal file
13
MoCha/Assets/Scripts/GetLeaderboard.cs.meta
Normal file
|
@ -0,0 +1,13 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4bc1364c87acb4744a9444f492464508
|
||||
timeCreated: 1524103750
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
208
MoCha/Assets/Scripts/MochaParser.cs
Normal file
208
MoCha/Assets/Scripts/MochaParser.cs
Normal file
|
@ -0,0 +1,208 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class MochaParser
|
||||
{
|
||||
public List<PlayerInfo> players;
|
||||
|
||||
public MochaParser()
|
||||
{
|
||||
players = new List<PlayerInfo> ();
|
||||
}
|
||||
|
||||
/*
|
||||
* You need to call this to make an HTTP request.
|
||||
* */
|
||||
public IEnumerator MakeRequest(string request)
|
||||
{
|
||||
using (UnityWebRequest www = UnityWebRequest.Get(request))
|
||||
{
|
||||
yield return www.SendWebRequest();
|
||||
|
||||
if (www.isNetworkError || www.isHttpError)
|
||||
{
|
||||
Debug.Log(www.error);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Do the parsing.
|
||||
ParseJsonRaw(www.downloadHandler.text);
|
||||
|
||||
// Or retrieve results as binary data
|
||||
byte[] results = www.downloadHandler.data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Call this to do all the parsing.
|
||||
* JSON objects will be separated and saved
|
||||
* in a public class variable "players".
|
||||
* */
|
||||
public void ParseJsonRaw(string json)
|
||||
{
|
||||
players = new List<PlayerInfo>();
|
||||
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 = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Don't use this unless you want to experience pain.
|
||||
* It's useless because you can just use the above function.
|
||||
* */
|
||||
public PlayerInfo ParseJson(string json)
|
||||
{
|
||||
return JsonUtility.FromJson<PlayerInfo>(json);
|
||||
}
|
||||
|
||||
/*
|
||||
* Call this after parsing if you just want the text
|
||||
* output of the parsed JSON.
|
||||
* */
|
||||
public string OutputInfo()
|
||||
{
|
||||
string output = "";
|
||||
|
||||
foreach (PlayerInfo pi in players)
|
||||
{
|
||||
output += "Player ID: " + pi.user_id +
|
||||
"\t\tUser: " + pi.username +
|
||||
"\t\tScore: " + pi.score + "\n";
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
public string OutputIds()
|
||||
{
|
||||
string output = "Player ID\n";
|
||||
|
||||
foreach (PlayerInfo pi in players)
|
||||
{
|
||||
output += pi.user_id + "\n";
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
public string OutputUsernames()
|
||||
{
|
||||
string output = "Username\n";
|
||||
|
||||
foreach (PlayerInfo pi in players)
|
||||
{
|
||||
output += pi.username + "\n";
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
public string OutputScores()
|
||||
{
|
||||
string output = "Score\n";
|
||||
|
||||
foreach (PlayerInfo pi in players)
|
||||
{
|
||||
output += pi.score + "\n";
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/* Display the Output of the request sent through
|
||||
* the request string on the given Text parameter.
|
||||
* (i.e. call MakeRequest with the given HTTP
|
||||
* request, then call OutputInfo and display
|
||||
* that on the given Text element reference)
|
||||
* */
|
||||
public IEnumerator DisplayOutput(string request, Text t)
|
||||
{
|
||||
yield return MakeRequest (request);
|
||||
t.text = OutputInfo ();
|
||||
}
|
||||
|
||||
public IEnumerator DisplayOutputSeparated(string request, Text playerids, Text usernames, Text scores)
|
||||
{
|
||||
yield return MakeRequest (request);
|
||||
|
||||
// Remove later.
|
||||
SortLeaderboard ();
|
||||
|
||||
playerids.text = OutputIds ();
|
||||
usernames.text = OutputUsernames ();
|
||||
scores.text = OutputScores ();
|
||||
}
|
||||
|
||||
/*
|
||||
* Get rid of this later.
|
||||
* */
|
||||
public void SortLeaderboard()
|
||||
{
|
||||
PlayerInfo s;
|
||||
|
||||
for(int o = 0; o < players.Count - 1; ++o)
|
||||
{
|
||||
for (int i = o + 1; i < players.Count; ++i)
|
||||
{
|
||||
Debug.Log (i + " " + o + " " + players.Count);
|
||||
if (players [o].score < players[i].score)
|
||||
{
|
||||
s = players [i];
|
||||
players [i] = players [o];
|
||||
players [o] = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
13
MoCha/Assets/Scripts/MochaParser.cs.meta
Normal file
13
MoCha/Assets/Scripts/MochaParser.cs.meta
Normal file
|
@ -0,0 +1,13 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 704cc8cda1c18554da0d1882f3f4a3f3
|
||||
timeCreated: 1524096226
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
17
MoCha/Assets/Scripts/PlayerInfo.cs
Normal file
17
MoCha/Assets/Scripts/PlayerInfo.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class PlayerInfo
|
||||
{
|
||||
public string username = "[{\"username\":\"test\", \"user_id\": 1}]";
|
||||
public int user_id = 0;
|
||||
public int score = 69;
|
||||
|
||||
public PlayerInfo()
|
||||
{
|
||||
username = "";
|
||||
user_id = 0;
|
||||
score = 69;
|
||||
}
|
||||
}
|
||||
|
13
MoCha/Assets/Scripts/PlayerInfo.cs.meta
Normal file
13
MoCha/Assets/Scripts/PlayerInfo.cs.meta
Normal file
|
@ -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:
|
Binary file not shown.
|
@ -1 +1 @@
|
|||
m_EditorVersion: 2017.3.1f1
|
||||
m_EditorVersion: 2017.2.1f1
|
||||
|
|
Reference in a new issue