-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeaderboards.cs
62 lines (54 loc) · 1.72 KB
/
Leaderboards.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.Networking;
using TMPro;
public class Entry
{
public string name;
public int score;
}
public class Leaderboards : MonoBehaviour
{
public Card[] cardArr;
public Card userCard;
public TextMeshProUGUI loadingText;
void Start()
{
loadingText.enabled = true;
StartCoroutine(RetrieveScores());
}
IEnumerator RetrieveScores()
{
loadingText.enabled = true;
UnityWebRequest www = UnityWebRequest.Get("https://astrojumpgame.firebaseio.com/leaderboards.json?orderBy=\"score\"&limitToLast=5");
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
JSONObject obj = new JSONObject(www.downloadHandler.text);
UpdateBoard(obj);
}
}
void UpdateBoard(JSONObject json)
{
loadingText.enabled = false;
Entry[] entries = new Entry[json.list.Count];
for (int i = 0; i < json.list.Count; i++) {
JSONObject entry = (JSONObject)json.list[i];
string name = entry.list[0].str;
int score = (int) entry.list[1].n;
entries[i] = new Entry();
entries[i].name = name;
entries[i].score = score;
}
Array.Sort(entries, delegate(Entry x, Entry y) { return -x.score.CompareTo(y.score); });
for (int i = 0; i < entries.Length; i++) {
cardArr[i].Initialize(i + 1, entries[i].name, entries[i].score.ToString());
}
int hiscore = PlayerPrefs.GetInt("hiscore", 0);
userCard.Initialize(0, "You", hiscore.ToString());
}
}