Beispiel #1
0
        /// <summary>
        /// Add a new score for the user at the given timestamp (default Now).
        /// </summary>
        /// <param name="userId">User ID for whom to add the new score.</param>
        /// <param name="username">Username to display for the score.</param>
        /// <param name="score">The score value.</param>
        /// <param name="timestamp">
        ///   The timestamp the score was achieved, in Epoch seconds. If <= 0, current time is used.
        /// </param>
        public void AddScore(string userId, string username, int score, long timestamp = -1L)
        {
            if (timestamp <= 0)
            {
                timestamp = DateTime.UtcNow.Ticks / TimeSpan.TicksPerSecond;
            }
            var scoreOb   = new UserScore(userId, username, score, timestamp);
            var scoreDict = scoreOb.ToDictionary();

            addingUserScore = true;

            var newEntry = dbref.Child(AllScoreDataPath).Push();

            newEntry.SetValueAsync(scoreDict).ContinueWith(task => {
                if (task.Exception != null)
                {
                    Debug.LogWarning("Exception adding score: " + task.Exception);
                }
                if (!task.IsCompleted)
                {
                    return;
                }
                userScoreArgs = new UserScoreArgs {
                    Score = scoreOb
                };
                sendScoreAddedEvent = true;
                addingUserScore     = false;
            });
        }
        /// <summary>
        /// Add a new pre-built UserScore to the DB.
        /// </summary>
        /// <param name="score">Score to add.</param>
        /// <returns>Task running to add the score.</returns>
        public Task <UserScore> AddScore(UserScore score)
        {
            var scoreDict = score.ToDictionary();

            addingUserScore = true;

            return(Task.Run(() => {
                // Waits to ensure the FirebaseLeaderboard is initialized before adding the new score.
                while (!initialized)
                {
                }
                var newEntry = dbref.Child(AllScoreDataPath).Push();
                return newEntry.SetValueAsync(scoreDict).ContinueWith(task => {
                    if (task.Exception != null)
                    {
                        Debug.LogWarning("Exception adding score: " + task.Exception);
                    }
                    if (!task.IsCompleted)
                    {
                        return null;
                    }
                    userScoreArgs = new UserScoreArgs {
                        Score = score
                    };
                    sendScoreAddedEvent = true;
                    addingUserScore = false;
                    return score;
                }).Result;
            }));
        }
        /// <summary>
        /// Add a new pre-built UserScore to the DB.
        /// </summary>
        /// <param name="score">Score to add.</param>
        /// <returns>Task running to add the score.</returns>
        public Task <UserScore> AddScore(UserScore score)
        {
            var scoreDict = score.ToDictionary();

            addingUserScore = true;

            var newEntry = dbref.Child(AllScoreDataPath).Push();

            return(newEntry.SetValueAsync(scoreDict).ContinueWith <UserScore>(task => {
                if (task.Exception != null)
                {
                    Debug.LogWarning("Exception adding score: " + task.Exception);
                }
                if (!task.IsCompleted)
                {
                    return null;
                }
                userScoreArgs = new UserScoreArgs {
                    Score = score
                };
                sendScoreAddedEvent = true;
                addingUserScore = false;
                return score;
            }));
        }
Beispiel #4
0
        /// <summary>
        /// Retrieve a single user's top score in a given time frame (default: top all time score).
        /// </summary>
        /// <param name="userId">The unique ID of the user.</param>
        public void GetUserScore(string userId)
        {
            if (!initialized && !getUserScoreCallQueued)
            {
                Debug.LogWarning(
                    "GetUserScore called before Firebase initialized. Waiting for initialization...");
                getUserScoreCallQueued = true;
                StartCoroutine(GetUserScoreWhenInitialized(userId));
                return;
            }
            if (getUserScoreCallQueued)
            {
                Debug.LogWarning("Still waiting for initialization...");
                return;
            }

            gettingUserScore = true;
            // Validate start and end times or use default values (Epoch time and now, respectively).
            var startTS = StartDate.Ticks / TimeSpan.TicksPerSecond;
            var endTS   = EndDate.Ticks / TimeSpan.TicksPerSecond;

            // Get user scores within time frame, then sort by score to find the highest one.
            dbref.Child(AllScoreDataPath)
            .OrderByChild(UserScore.UserIDPath)
            .StartAt(userId)
            .EndAt(userId)
            .GetValueAsync().ContinueWith(task => {
                if (task.Exception != null)
                {
                    throw task.Exception;
                }
                if (!task.IsCompleted)
                {
                    return;
                }
                if (task.Result.ChildrenCount == 0)
                {
                    userScoreArgs = new UserScoreArgs {
                        Message = String.Format("No scores for User {0}", userId)
                    };
                }
                else
                {
                    // Find the User's scores within the time range.
                    var scores = ParseValidUserScoreRecords(task.Result, startTS, endTS).ToList();
                    if (scores.Count() == 0)
                    {
                        userScoreArgs = new UserScoreArgs {
                            Message = String.Format("No scores for User {0} within time range ({1} - {2})",
                                                    userId,
                                                    startTS,
                                                    endTS)
                        };
                    }
                    else
                    {
                        var orderedScores = scores.OrderBy(score => score.Score);
                        userScoreArgs     = new UserScoreArgs {
                            Score = orderedScores.Last()
                        };
                    }
                }
                gettingUserScore   = false;
                sendUserScoreEvent = true;
            });
        }