예제 #1
0
    private IEnumerator RequestScores(bool _Multiplayer, string _Mode, int _Amount, ScoreDownloadHandler _Callback)
    {
        // Create JsonTable with necessary data
        JsonTable info = new JsonTable();

        info.Add("multiplayer", _Multiplayer);
        info.Add("mode", _Mode);
        info.Add("amount", _Amount);

        //Debug.Log("Json: " + info.ToJson());

        // Attach info to url
        string json = GetScoresURL + "?json=" + info.ToJson();

        //Debug.Log("URL: " + json);

        // Load url
        WWW get = new WWW(json);

        // Await response
        yield return(get);

        if (get.error != null)
        {
            Debug.LogError("CScoreCommunicator::RequestScores - Error getting scores from '" + GetScoresURL + "' with data '" + info.ToJson() + "'. Error: " + get.error);
            if (_Callback != null)
            {
                _Callback(null);
            }
            yield break;
        }

        json = get.text;
        //Debug.Log("CScoreCommunicator::RequestScores - Recieved scores: " + text);

        List <CScoreEntry> result = new List <CScoreEntry>(_Amount);

        if (json.Length > 2)
        {
            JsonObject obj = Json.Decode(json);
            info = obj.ToTable();
            JsonTable entry;
            for (int i = 0; i < _Amount; i++)
            {
                entry = info.GetSubtable("entry" + i);
                if (entry != null)
                {
                    result.Add(new CScoreEntry(entry.ToString("name"),
                                               (float)entry.ToDouble("score"),
                                               (float)entry.ToDouble("time"),
                                               _Multiplayer,
                                               _Mode,
                                               i));
                    //Debug.Log("Entry " + i + ": " + entry["name"] + ", " + entry["score"] + ", " + entry["time"]);
                }
                else
                {
                    result.Add(new CScoreEntry(_Multiplayer, _Mode[0].ToString().ToUpper(), i, false));
                    //Debug.Log("Entry " + i + ": No entry available.");
                }
            }
        }
        else
        {
            for (int i = 0; i < _Amount; i++)
            {
                result.Add(new CScoreEntry(_Multiplayer, _Mode[0].ToString().ToUpper(), i, false));
                //Debug.Log("Entry " + i + ": No entry available.");
            }
        }


        if (_Callback != null)
        {
            _Callback(result.ToArray());
        }
    }
예제 #2
0
 /// <summary>
 /// Gets the current online scores.
 /// </summary>
 /// <param name="_Multiplayer">Single or multiplayer scores.</param>
 /// <param name="_Mode">The selected mode.</param>
 /// <param name="_Amount">The maximum amount of scores to get.</param>
 public void GetScores(bool _Multiplayer, string _Mode, int _Amount, ScoreDownloadHandler _Callback)
 {
     // Get scores from server
     StartCoroutine(RequestScores(_Multiplayer, _Mode, _Amount, _Callback));
 }