Leaderboard Scene.
This commit is contained in:
parent
36fd467819
commit
51d06cfd0c
10 changed files with 280 additions and 145 deletions
|
@ -1,12 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace AssemblyCSharp
|
|
||||||
{
|
|
||||||
public class EmptyClass
|
|
||||||
{
|
|
||||||
public EmptyClass ()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Binary file not shown.
|
@ -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;
|
||||||
|
|
||||||
void Start()
|
public string request = "http://corder.tech/mocha/users/*";
|
||||||
{
|
|
||||||
objectText = GetComponentInParent<Text> ();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void DisplayStuff(string request)
|
void Start()
|
||||||
{
|
{
|
||||||
StartCoroutine (MakeRequest (request));
|
thisbutton = GetComponent<Button> ();
|
||||||
|
objectText = GetComponent<Text> ();
|
||||||
|
|
||||||
|
thisbutton.onClick.AddListener (CallGetData);
|
||||||
}
|
}
|
||||||
|
|
||||||
IEnumerator MakeRequest(string request)
|
public void CallGetData()
|
||||||
{
|
|
||||||
using (UnityWebRequest www = UnityWebRequest.Get(request))
|
|
||||||
{
|
|
||||||
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>();
|
DisplayAll(request, objectText);
|
||||||
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 void DisplayAll(string request, Text objectText)
|
||||||
public PlayerInfo ParseJson(string json)
|
|
||||||
{
|
{
|
||||||
return JsonUtility.FromJson<PlayerInfo>(json);
|
MochaParser parser = new MochaParser();
|
||||||
}
|
StartCoroutine (parser.DisplayOutput(request, objectText));
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
fileFormatVersion: 2
|
fileFormatVersion: 2
|
||||||
guid: 9ccd5f892b119d749980bbde89a80e70
|
guid: 4bc1364c87acb4744a9444f492464508
|
||||||
timeCreated: 1524075474
|
timeCreated: 1524103750
|
||||||
licenseType: Free
|
licenseType: Free
|
||||||
MonoImporter:
|
MonoImporter:
|
||||||
externalObjects: {}
|
externalObjects: {}
|
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:
|
|
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
m_EditorVersion: 2017.3.1f1
|
m_EditorVersion: 2017.2.1f1
|
||||||
|
|
Reference in a new issue