Leaderboard Scene.

This commit is contained in:
Alex Huddleston 2018-04-18 21:49:39 -05:00
parent 36fd467819
commit 51d06cfd0c
10 changed files with 280 additions and 145 deletions

View file

@ -1,12 +0,0 @@
using System;
namespace AssemblyCSharp
{
public class EmptyClass
{
public EmptyClass ()
{
}
}
}

Binary file not shown.

View file

@ -1,126 +1,29 @@
using UnityEngine; using UnityEngine;
using UnityEngine.Networking;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI; 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 public class GetData : MonoBehaviour
{ {
Text objectText; public Button thisbutton;
public Text objectText;
public string request = "http://corder.tech/mocha/users/*";
void Start() void Start()
{ {
objectText = GetComponentInParent<Text> (); thisbutton = GetComponent<Button> ();
objectText = GetComponent<Text> ();
thisbutton.onClick.AddListener (CallGetData);
} }
public void DisplayStuff(string request) public void CallGetData()
{ {
StartCoroutine (MakeRequest (request)); DisplayAll(request, objectText);
} }
public void DisplayAll(string request, Text objectText)
IEnumerator MakeRequest(string request)
{ {
using (UnityWebRequest www = UnityWebRequest.Get(request)) MochaParser parser = new MochaParser();
{ StartCoroutine (parser.DisplayOutput(request, objectText));
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
//this should be saved somewhere else...
List<PlayerInfo> players;
// Show results as text
players = ParseJsonRaw(www.downloadHandler.text);
objectText.text = DisplayInfo (players);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
}
public List<PlayerInfo> ParseJsonRaw(string json)
{
List<PlayerInfo> 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 = "";
}
}
}
return players;
}
public PlayerInfo ParseJson(string json)
{
return JsonUtility.FromJson<PlayerInfo>(json);
}
public string DisplayInfo(List<PlayerInfo> p)
{
string output = "";
foreach (PlayerInfo pi in p)
{
output += "Player ID: " + pi.user_id + "\t\tUser: " + pi.username + "\n";
}
return output;
} }
} }

View 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));
}
}

View file

@ -1,6 +1,6 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 9ccd5f892b119d749980bbde89a80e70 guid: 4bc1364c87acb4744a9444f492464508
timeCreated: 1524075474 timeCreated: 1524103750
licenseType: Free licenseType: Free
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}

View 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;
}
}
}
}
}

View 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:

View file

@ -5,31 +5,13 @@ public class PlayerInfo
{ {
public string username = "[{\"username\":\"test\", \"user_id\": 1}]"; public string username = "[{\"username\":\"test\", \"user_id\": 1}]";
public int user_id = 0; public int user_id = 0;
public int score = 69;
public PlayerInfo() public PlayerInfo()
{ {
username = ""; username = "";
user_id = 0; user_id = 0;
} score = 69;
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;
} }
} }

View file

@ -1 +1 @@
m_EditorVersion: 2017.3.1f1 m_EditorVersion: 2017.2.1f1