diff --git a/MoCha/Assets/Scenes/Leaderboard.unity b/MoCha/Assets/Scenes/Leaderboard.unity index 5f69d20..c1830d1 100644 Binary files a/MoCha/Assets/Scenes/Leaderboard.unity and b/MoCha/Assets/Scenes/Leaderboard.unity differ diff --git a/MoCha/Assets/Scripts/GetLeaderboard.cs b/MoCha/Assets/Scripts/GetLeaderboard.cs deleted file mode 100644 index 81d21a4..0000000 --- a/MoCha/Assets/Scripts/GetLeaderboard.cs +++ /dev/null @@ -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)); - } -} - diff --git a/MoCha/Assets/Scripts/LeaderboardManager.cs b/MoCha/Assets/Scripts/LeaderboardManager.cs new file mode 100644 index 0000000..e4b6321 --- /dev/null +++ b/MoCha/Assets/Scripts/LeaderboardManager.cs @@ -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 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 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; + } + } +} diff --git a/MoCha/Assets/Scripts/GetLeaderboard.cs.meta b/MoCha/Assets/Scripts/LeaderboardManager.cs.meta similarity index 77% rename from MoCha/Assets/Scripts/GetLeaderboard.cs.meta rename to MoCha/Assets/Scripts/LeaderboardManager.cs.meta index c3cdcc8..60b63f9 100644 --- a/MoCha/Assets/Scripts/GetLeaderboard.cs.meta +++ b/MoCha/Assets/Scripts/LeaderboardManager.cs.meta @@ -1,6 +1,6 @@ fileFormatVersion: 2 -guid: 4bc1364c87acb4744a9444f492464508 -timeCreated: 1524103750 +guid: 5456004561983334783798d8d62b1869 +timeCreated: 1524239051 licenseType: Free MonoImporter: externalObjects: {} diff --git a/MoCha/Assets/Scripts/MochaParser.cs b/MoCha/Assets/Scripts/MochaParser.cs index e95db43..2534190 100644 --- a/MoCha/Assets/Scripts/MochaParser.cs +++ b/MoCha/Assets/Scripts/MochaParser.cs @@ -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); + } + } }