2018-04-18 21:49:39 -05:00
|
|
|
|
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 ();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:17:39 -05:00
|
|
|
|
public IEnumerator DisplayOutputSeparated(string request, Text usernames, Text scores)
|
2018-04-18 21:49:39 -05:00
|
|
|
|
{
|
|
|
|
|
yield return MakeRequest (request);
|
|
|
|
|
|
|
|
|
|
usernames.text = OutputUsernames ();
|
|
|
|
|
scores.text = OutputScores ();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|