/// <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; })); }
// Uploads the time data to the database, and returns the current top time list. public static Task <UserScore> UploadReplay( long time, LevelMap map, ReplayData replay) { // Get a client-generated unique id based on timestamp and random number. string key = FirebaseDatabase.DefaultInstance.RootReference.Push().Key; Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; string name = (auth.CurrentUser != null && !string.IsNullOrEmpty(auth.CurrentUser.DisplayName)) ? auth.CurrentUser.DisplayName : StringConstants.UploadScoreDefaultName; string userId = (auth.CurrentUser != null && !string.IsNullOrEmpty(auth.CurrentUser.UserId)) ? auth.CurrentUser.UserId : StringConstants.UploadScoreDefaultName; string replayPath = replay != null ? Storage_Replay_Root_Folder + GetPath(map) + key : null; var userScore = new Firebase.Leaderboard.UserScore( userId, name, time, DateTime.Now.Ticks / TimeSpan.TicksPerSecond, new Dictionary <string, object> { { Database_Property_ReplayPath, replayPath } }); UploadConfig config = new UploadConfig() { key = key, storagePath = replayPath, dbRankPath = GetDBRankPath(map) + key, dbSharedReplayPath = GetDBSharedReplayPath(map) + key, shareReplay = replay != null //TODO(chkuang): && GameOption.shareReplay }; if (replay == null) { // Nothing to upload, return user score to upload to leaderboard. return(Task.FromResult(userScore)); } else { return(UploadReplayData(userScore, replay, config) .ContinueWith(task => { if (config.shareReplay) { var dbRef = FirebaseDatabase.DefaultInstance.RootReference; return dbRef.Child(config.dbSharedReplayPath) .SetValueAsync(userScore.ToDictionary()); } else { return null; }; }).ContinueWith(task => userScore)); } }