HighScores.cs 1.45 KB
Newer Older
1
using Assets.Scripts.API;
2
using Assets.Scripts.Utils;
3
using UnityEngine;
PLN (Algolia) committed
4
using UnityEngine.SceneManagement;
5 6 7
using UnityEngine.UI;
using System;
using System.Collections;
PLN (Algolia) committed
8 9 10

public class HighScores : MonoBehaviour {

11 12 13 14 15 16
	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?

PLN (Algolia) committed
17 18
	// Use this for initialization
	void Start () {
19 20
		ScoreText.text = "";
		Server = gameObject.GetComponent(typeof(Server)) as Server;
PLN (Algolia) committed
21 22 23 24
	}

	// Update is called once per frame
	void Update () {
25
		BackButtonHelper.playOnBack();
26 27 28 29 30 31 32
		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) {
PLN (Algolia) committed
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47
					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;
			}
		}
PLN (Algolia) committed
48 49 50 51 52 53 54
	}

	public void OnButtonBackClicked()
	{
		SceneManager.LoadScene("Menu");
	}
}
55