Replaced GetLeaderboard with LeaderboardManager. Manages all button functions and leaderboard stuff.

This commit is contained in:
shadow8t4 2018-04-20 12:22:14 -05:00
parent 586fb2ea8c
commit a1934f0e84
5 changed files with 152 additions and 42 deletions

View file

@ -1,40 +0,0 @@
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 Usernames;
public Text Scores;
public string request = "http://corder.tech/mocha/users/*";
void Start()
{
thisbutton.onClick.AddListener (CallGetData);
}
public void CallGetData()
{
DisplayAllSeparated(request, Usernames, Scores);
}
public void DisplayAllSeparated(string request, Text Usernames, Text Scores)
{
MochaParser parser = new MochaParser ();
StartCoroutine (parser.DisplayOutputSeparated(request, Usernames, Scores));
}
}

View file

@ -0,0 +1,123 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LeaderboardManager : MonoBehaviour
{
public string request = "http://corder.tech/mocha/users/*";
public Text Usernames;
public Text Scores;
private List<PlayerInfo> players;
private int currentPage;
void Start()
{
// Eventually get data on start.
}
public void CallGetData()
{
DisplayAllSeparated(request, Usernames, Scores);
}
public void DisplayAllSeparated(string request, Text Usernames, Text Scores)
{
MochaParser parser = new MochaParser ();
StartCoroutine (parser.GrabLeaderboard(request, Usernames, Scores, this));
}
public void setPlayers(List<PlayerInfo> p)
{
players = p;
}
public void setInitialBoard(Text u, Text s)
{
currentPage = 1;
Usernames = u;
Scores = s;
string usernameOutput = "Username";
string scoreOutput = "Score";
/*
* Just an extra step in case for whatever reason
* the leaderboard length is less than 10.
* */
int boardlength = players.Count;
if (boardlength > 10)
boardlength = 10;
for(int i = 0; i < boardlength; ++i)
{
usernameOutput += "\n" + players[i].username;
scoreOutput += "\n" + players [i].score;
}
Usernames.text = usernameOutput;
Scores.text = scoreOutput;
}
public void nextPage()
{
int currentIndex = currentPage * 10;
int test = players.Count - currentIndex;
if (test < 0)
{
return;
}
else
{
string usernameOutput = "Username";
string scoreOutput = "Score";
int pagelength = 10;
if (test < pagelength)
{
pagelength = test;
}
for (int i = currentIndex; i < (currentIndex + pagelength); ++i)
{
usernameOutput += "\n" + players[i].username;
scoreOutput += "\n" + players [i].score;
}
Usernames.text = usernameOutput;
Scores.text = scoreOutput;
currentPage += 1;
}
}
public void previousPage()
{
if (currentPage <= 1)
{
return;
}
else
{
currentPage -= 1;
int currentIndex = currentPage * 10;
string usernameOutput = "Username";
string scoreOutput = "Score";
int pagelength = 10;
for (int i = (currentIndex - pagelength); i < currentIndex; ++i) {
usernameOutput += "\n" + players [i].username;
scoreOutput += "\n" + players [i].score;
}
Usernames.text = usernameOutput;
Scores.text = scoreOutput;
}
}
}

View file

@ -1,6 +1,6 @@
fileFormatVersion: 2
guid: 4bc1364c87acb4744a9444f492464508
timeCreated: 1524103750
guid: 5456004561983334783798d8d62b1869
timeCreated: 1524239051
licenseType: Free
MonoImporter:
externalObjects: {}

View file

@ -179,5 +179,32 @@ public class MochaParser
usernames.text = OutputUsernames ();
scores.text = OutputScores ();
}
public IEnumerator GrabLeaderboard (string request, Text usernames, Text scores, LeaderboardManager lbm)
{
// Remove this later.
MakePlayers();
lbm.setPlayers (players);
lbm.setInitialBoard (usernames, scores);
// Move this back to the top of this function.
yield return MakeRequest (request);
}
/*
* Temporary hardcoded players.
* */
public void MakePlayers()
{
for(int i = 0; i < 69; i++)
{
PlayerInfo temp = new PlayerInfo ();
temp.username = "A test!" + (i + 1);
temp.score = i * 69;
players.Add (temp);
}
}
}