Skip to content
This repository was archived by the owner on Aug 20, 2025. It is now read-only.

Commit 29a5748

Browse files
committed
Update UserScore to allow for arbitrary data storage.
Change-Id: I43111760d0f4c58d812de8fd19acc04f20fef28b
1 parent ab70f8a commit 29a5748

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

Firebase_Leaderboard/Scripts/LeaderboardController.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,18 @@ public Task AddScore(string userId, long score, long timestamp = -1L) {
389389
/// <param name="timestamp">
390390
/// The timestamp the score was achieved, in Epoch seconds. If <= 0, current time is used.
391391
/// </param>
392+
/// <param name="otherData">Miscellaneous data to store with the score object.</param>
392393
/// <returns>Task running to add the score.</returns>
393-
public Task AddScore(string userId, string username, long score, long timestamp = -1L) {
394+
public Task AddScore(
395+
string userId,
396+
string username,
397+
long score,
398+
long timestamp = -1L,
399+
Dictionary<string, object> otherData=null) {
394400
if (timestamp <= 0) {
395401
timestamp = DateTime.UtcNow.Ticks / TimeSpan.TicksPerSecond;
396402
}
397-
var scoreOb = new UserScore(userId, username, score, timestamp);
403+
var scoreOb = new UserScore(userId, username, score, timestamp, otherData);
398404
return AddScore(scoreOb);
399405
}
400406

Firebase_Leaderboard/Scripts/UserScore.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ public class UserScore {
3131
public static string UsernamePath = "username";
3232
public static string ScorePath = "score";
3333
public static string TimestampPath = "timestamp";
34+
public static string OtherDataPath = "data";
3435

3536
public string UserID;
3637
public string Username;
3738
public long Score;
3839
public long Timestamp;
40+
public Dictionary<string, object> OtherData;
3941

4042
/// <summary>
4143
/// Timestamp is stored as a long value, seconds since Epoch time. This property
@@ -71,11 +73,18 @@ public UserScore(string userId, long score, long timestamp) {
7173
/// <param name="username">The user's display name.</param>
7274
/// <param name="score">The score value.</param>
7375
/// <param name="timestamp">The timestamp the score was achieved.</param>
74-
public UserScore(string userId, string username, long score, long timestamp) {
76+
/// <param name="otherData">Miscellaneous data to store with the score object.</param>
77+
public UserScore(
78+
string userId,
79+
string username,
80+
long score,
81+
long timestamp,
82+
Dictionary<string, object> otherData=null) {
7583
UserID = userId;
7684
Username = username;
7785
Score = score;
7886
Timestamp = timestamp;
87+
OtherData = otherData;
7988
}
8089

8190
/// <summary>
@@ -97,6 +106,12 @@ public UserScore(DataSnapshot record) {
97106
if (Int64.TryParse(record.Child(TimestampPath).Value.ToString(), out timestamp)) {
98107
Timestamp = timestamp;
99108
}
109+
if (record.Child(OtherDataPath).Exists && record.Child(OtherDataPath).HasChildren) {
110+
OtherData = new Dictionary<string, object>();
111+
foreach (var datum in record.Child(OtherDataPath).Children) {
112+
OtherData[datum.Key] = datum.Value;
113+
}
114+
}
100115
}
101116

102117
/// <summary>
@@ -109,7 +124,8 @@ public Dictionary<string, object> ToDictionary() {
109124
{UserIDPath, UserID},
110125
{UsernamePath, Username},
111126
{ScorePath, Score},
112-
{TimestampPath, Timestamp}
127+
{TimestampPath, Timestamp},
128+
{OtherDataPath, OtherData}
113129
};
114130
}
115131

0 commit comments

Comments
 (0)