From 4ef0e6de15d1fcafbc3b00ad90715fdc7695c95b Mon Sep 17 00:00:00 2001 From: Paul-Louis NECH Date: Thu, 27 Oct 2016 20:49:21 -0400 Subject: [PATCH] Scores scene: HighScore display --- Assets/Scripts/API/Score.cs | 10 +++++----- Assets/Scripts/API/Server.cs | 2 +- Assets/Scripts/HighScores.cs | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/Assets/Scripts/API/Score.cs b/Assets/Scripts/API/Score.cs index 9a412fc..977467d 100644 --- a/Assets/Scripts/API/Score.cs +++ b/Assets/Scripts/API/Score.cs @@ -12,15 +12,15 @@ namespace Assets.Scripts.API public string date; public string _id; - public static Score CreateFromJson(string json) + public static Score CreateFromJson(string json) { return JsonUtility.FromJson(json); } - public override string ToString() - { - return string.Format("PlayerName: {0}, Team: {1}, Score: {2}, Date: {3}, Id: {4}", playerName, team, score, date, _id); - } + public override string ToString() + { + return string.Format("PlayerName: {0}, Team: {1}, Score: {2}, Date: {3}, Id: {4}", playerName, team, score, date, _id); + } } } diff --git a/Assets/Scripts/API/Server.cs b/Assets/Scripts/API/Server.cs index 869c344..a103a20 100644 --- a/Assets/Scripts/API/Server.cs +++ b/Assets/Scripts/API/Server.cs @@ -23,7 +23,7 @@ namespace Assets.Scripts.API GetScores(); } - public void GetScores () + public void GetScores() { StartCoroutine(GetScoresAsync()); Debug.Log("Done."); diff --git a/Assets/Scripts/HighScores.cs b/Assets/Scripts/HighScores.cs index 8c4e8e3..bb86758 100644 --- a/Assets/Scripts/HighScores.cs +++ b/Assets/Scripts/HighScores.cs @@ -1,17 +1,48 @@ -using UnityEngine; -using System.Collections; +using Assets.Scripts.API; +using UnityEngine; using UnityEngine.SceneManagement; +using UnityEngine.UI; +using System; +using System.Collections; public class HighScores : MonoBehaviour { + public Text ScoreText; + public Server Server; + public Score[] highscores; + + private bool updatedScores = false; //TODO: Not very elegant, can we instead load scores sync in Start? + // Use this for initialization void Start () { - + ScoreText.text = ""; + Server = gameObject.GetComponent(typeof(Server)) as Server; } // Update is called once per frame void Update () { + if (!updatedScores) { + if (Server.Scores.status.Equals("ok")) { + Score[] highScores = Server.Scores.scores; + Array.Sort(highScores, delegate(Score x, Score y) {return y.score.CompareTo(x.score);}); + + Debug.Log(String.Format("We got {0} scores.", highScores.Length)); + if (Server.Scores.scores.Length > 0) { + for (int i = 0; i < highScores.Length; i++) { + Score score = highScores [i]; + string name = score.playerName; + if (score.team != null && score.team.Length > 0) { + name += " (" + score.team + ")"; + } + ScoreText.text += WWW.UnEscapeURL(name) + ": " + score.score + " points\n"; + } + } else { + ScoreText.text = "No high-score yet... Play a game and show you are the best!"; + } + updatedScores = true; + } + } } public void OnButtonBackClicked() -- libgit2 0.27.0