Example #1
0
    private void ShowSuperiorPlayer()
    {
        //Debug.Log("Called ShowSuperiorPlayer!");
        var updatedScore = CurrentGameMode.Score;

        Leaderboard.DTOUserScore nextHighScore = null;
        IEnumerable <Leaderboard.DTOUserScore> higherScores = null;

        if (GlobalData.Instance.FriendsScores != null)
        {
            higherScores = (from score in GlobalData.Instance.FriendsScores
                            where score.GameMode == CurrentGameMode.ModeName &&
                            score.Score > updatedScore
                            orderby score.Score ascending
                            select score);

            nextHighScore = higherScores.FirstOrDefault();
        }

        //foreach (var highScore in GlobalData.Instance.FriendsScores) {
        //    Debug.Log("Name: " + highScore.Name + " - Score: " + highScore.Score + " - GameMode: " + highScore.GameMode);
        //}

        if (higherScores != null && nextHighScore != null)
        {
            targetScoreLbl.Text = nextHighScore.Score.ToString();
            CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
            TextInfo    textInfo    = cultureInfo.TextInfo;
            targetPlayerLbl.Text = string.Format("{0} - {1} {2}", nextHighScore.Name.Substring(0, Mathf.Min(nextHighScore.Name.Length, 18)), textInfo.ToTitleCase(I2.Loc.ScriptLocalization.Get("RANK")), higherScores.Count());
        }
        else
        {
            //Fetch the global high score
            if (GlobalData.Instance.GlobalTopScores != null)
            {
                var globalTopScore = GlobalData.Instance.GlobalTopScores.SingleOrDefault(s => s.Score > updatedScore && s.GameMode == CurrentGameMode.ModeName);
                if (globalTopScore != null)
                {
                    targetPlayerLbl.Text = I2.Loc.ScriptLocalization.Get("GlobalScoreChallenge");
                    targetScoreLbl.Text  = globalTopScore.Score.ToString();
                }
                else
                {
                    targetPlayerLbl.Text = I2.Loc.ScriptLocalization.Get("GlobalRecord");
                    targetScoreLbl.Text  = CurrentGameMode.Score.ToString();
                }
            }
            else
            {
                targetPlayerLbl.Text = "Fetching Top Score";
                targetScoreLbl.Text  = "0";
            }
        }
    }
    IEnumerator GetFriendsScores()
    {
        HashSet <string> friendsIDs = new HashSet <string>();

        foreach (var friend in friendsInfo)
        {
            friendsIDs.Add(friend.Key);
        }

        var parameters = new Dictionary <string, object> {
            { "gameVersion", GlobalData.Instance.version },
            { "friendsIDs", friendsIDs.ToList() },
            { "gameModes", new List <string> {
                  "ClassicMode", "GrowthMode", "PeriodicMode"
              } }
        };

        var friendsHighScoresTask = ParseCloud.CallFunctionAsync <IEnumerable>("FriendsHighScores", parameters);

        while (!friendsHighScoresTask.IsCompleted)
        {
            yield return(null);
        }

        if (friendsHighScoresTask.IsFaulted)
        {
            foreach (var e in friendsHighScoresTask.Exception.InnerExceptions)
            {
                ParseException parseException = (ParseException)e;
                Debug.Log("Error getting friends scores.\r\nError message: " + parseException.Message + "\r\nErrorCode: " + parseException.Code);
            }
        }
        else
        {
            IEnumerable <ParseObject> friendsHighScores = friendsHighScoresTask.Result.OfType <ParseObject>();

            var friendsScores = new List <Leaderboard.DTOUserScore>();

            foreach (var friendHighScore in friendsHighScores)
            {
                var facebookID  = friendHighScore.Get <string>("facebookUserID");
                var friendScore = new Leaderboard.DTOUserScore {
                    Name     = friendsInfo[facebookID].Name,
                    UserID   = facebookID,
                    Score    = friendHighScore.Get <int>("score"),
                    GameMode = friendHighScore.Get <string>("gameMode")
                };
                friendsScores.Add(friendScore);
            }

            GlobalData.Instance.FriendsScores = friendsScores;
        }
    }