From 7faa04547ac770caba0571f95f9abe4161e3e0cd Mon Sep 17 00:00:00 2001 From: Martin Tirion Date: Wed, 20 Dec 2017 09:18:37 +0100 Subject: [PATCH 1/3] Added timestamp to score --- src/cloud/functions/leaderboards/top-n/leaderboard/run.csx | 6 +++++- src/cloud/functions/leaderboards/top-n/score/run.csx | 4 ++++ src/cloud/functions/leaderboards/top-n/score/sample.dat | 1 + 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/cloud/functions/leaderboards/top-n/leaderboard/run.csx b/src/cloud/functions/leaderboards/top-n/leaderboard/run.csx index 8db757bc..ee86903f 100644 --- a/src/cloud/functions/leaderboards/top-n/leaderboard/run.csx +++ b/src/cloud/functions/leaderboards/top-n/leaderboard/run.csx @@ -58,7 +58,7 @@ public static async Task Run(HttpRequestMessage req, TraceW var query = (from s in leaderboard orderby s.Score descending - select new LeaderboardItem {Player = s.Player, Score = s.Score}).Take(lengthOfLeaderboard); + select new LeaderboardItem {Player = s.Player, Timestamp = s.Timestamp, Score = s.Score}).Take(lengthOfLeaderboard); var leaders = query.ToList(); @@ -98,6 +98,8 @@ public class ScoreItem public string Player { get; set;} [JsonProperty(PropertyName = "playerId")] public string PlayerId { get; set;} + [JsonProperty(PropertyName = "timestamp")] + public DateTime Timestamp { get; set; } [JsonProperty(PropertyName = "score")] public double Score { get; set;} } @@ -108,6 +110,8 @@ public class LeaderboardItem public int Rank { get; set; } [JsonProperty(PropertyName = "player")] public string Player { get; set; } + [JsonProperty(PropertyName = "timestamp")] + public DateTime Timestamp { get; set; } [JsonProperty(PropertyName = "score")] public double Score { get; set; } } diff --git a/src/cloud/functions/leaderboards/top-n/score/run.csx b/src/cloud/functions/leaderboards/top-n/score/run.csx index adb6a95f..71a4dcef 100644 --- a/src/cloud/functions/leaderboards/top-n/score/run.csx +++ b/src/cloud/functions/leaderboards/top-n/score/run.csx @@ -59,6 +59,7 @@ public static async Task Run(HttpRequestMessage req, TraceW postedScore.PlayerId = data?.playerId; postedScore.Player = data?.player; postedScore.Score = data?.score; + postedScore.Timestamp = DateTime.UtcNow; try { @@ -84,6 +85,7 @@ public static async Task Run(HttpRequestMessage req, TraceW if (postedScore.Score>existingPlayer.Score) { existingPlayer.Score = postedScore.Score; + existingPlayer.Timestamp = postedScore.Timestamp; await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(db, collection, existingPlayer.Id), postedScore); return req.CreateResponse(HttpStatusCode.OK, $"The score for user {existingPlayer.PlayerId} has been updated to {postedScore.Score}"); } @@ -107,6 +109,8 @@ public class ScoreItem public string Player { get; set;} [JsonProperty(PropertyName = "playerId")] public string PlayerId { get; set;} + [JsonProperty(PropertyName = "timestamp")] + public DateTime Timestamp { get; set;} [JsonProperty(PropertyName = "score")] public double Score { get; set;} } diff --git a/src/cloud/functions/leaderboards/top-n/score/sample.dat b/src/cloud/functions/leaderboards/top-n/score/sample.dat index 7d6f70a8..074c7b08 100644 --- a/src/cloud/functions/leaderboards/top-n/score/sample.dat +++ b/src/cloud/functions/leaderboards/top-n/score/sample.dat @@ -2,5 +2,6 @@ "leaderboard": "level1", "player": "mario", "playerId": "1337", + "timestamp": 1234, "score": 420 } \ No newline at end of file From 66286ddacab7f77a4f69f5123098455fe7340a66 Mon Sep 17 00:00:00 2001 From: Martin Tirion Date: Wed, 20 Dec 2017 09:20:30 +0100 Subject: [PATCH 2/3] Timestamp removed from sample payload for Score function --- src/cloud/functions/leaderboards/top-n/score/sample.dat | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cloud/functions/leaderboards/top-n/score/sample.dat b/src/cloud/functions/leaderboards/top-n/score/sample.dat index 074c7b08..7d6f70a8 100644 --- a/src/cloud/functions/leaderboards/top-n/score/sample.dat +++ b/src/cloud/functions/leaderboards/top-n/score/sample.dat @@ -2,6 +2,5 @@ "leaderboard": "level1", "player": "mario", "playerId": "1337", - "timestamp": 1234, "score": 420 } \ No newline at end of file From 858270c95ff7cb4ae134c2c7ee6938cbe6073719 Mon Sep 17 00:00:00 2001 From: Martin Tirion Date: Wed, 20 Dec 2017 09:28:06 +0100 Subject: [PATCH 3/3] Timestamp also added to around-me scenario --- .../leaderboards/around-me/leaderboard/run.csx | 6 +++++- .../functions/leaderboards/around-me/score/run.csx | 14 +++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/cloud/functions/leaderboards/around-me/leaderboard/run.csx b/src/cloud/functions/leaderboards/around-me/leaderboard/run.csx index 58ea0612..8502918d 100644 --- a/src/cloud/functions/leaderboards/around-me/leaderboard/run.csx +++ b/src/cloud/functions/leaderboards/around-me/leaderboard/run.csx @@ -118,7 +118,7 @@ public static async Task Run(HttpRequestMessage req, string { foreach (ScoreItem scr in sortedListPositionsBelowCurrent) { - sortedResult.Add(new LeaderboardItem { Rank = 0, Player = scr.Player, Score = scr.Score }); + sortedResult.Add(new LeaderboardItem { Rank = 0, Player = scr.Player, Timestamp = src.Timestamp, Score = scr.Score }); } } @@ -171,6 +171,8 @@ public class ScoreItem public string Player { get; set; } [JsonProperty(PropertyName = "playerId")] public string PlayerId { get; set; } + [JsonProperty(PropertyName = "timestamp")] + public DateTime Timestamp { get; set; } [JsonProperty(PropertyName = "score")] public double Score { get; set; } } @@ -181,6 +183,8 @@ public class LeaderboardItem public int Rank { get; set; } [JsonProperty(PropertyName = "player")] public string Player { get; set; } + [JsonProperty(PropertyName = "timestamp")] + public DateTime Timestamp { get; set; } [JsonProperty(PropertyName = "score")] public double Score { get; set; } } diff --git a/src/cloud/functions/leaderboards/around-me/score/run.csx b/src/cloud/functions/leaderboards/around-me/score/run.csx index 7e44f269..b63078b3 100644 --- a/src/cloud/functions/leaderboards/around-me/score/run.csx +++ b/src/cloud/functions/leaderboards/around-me/score/run.csx @@ -200,15 +200,17 @@ public static int NumberWithSameScores(List listToRank, int sta public class ScoreItem { [JsonProperty(PropertyName = "id")] - public string Id { get; set; } + public string Id { get; set;} [JsonProperty(PropertyName = "leaderboard")] - public string Leaderboard { get; set; } + public string Leaderboard { get; set;} [JsonProperty(PropertyName = "player")] - public string Player { get; set; } + public string Player { get; set;} [JsonProperty(PropertyName = "playerId")] - public string PlayerId { get; set; } + public string PlayerId { get; set;} + [JsonProperty(PropertyName = "timestamp")] + public DateTime Timestamp { get; set; } [JsonProperty(PropertyName = "score")] - public double Score { get; set; } + public double Score { get; set;} } public class LeaderboardItem @@ -217,6 +219,8 @@ public class LeaderboardItem public int Rank { get; set; } [JsonProperty(PropertyName = "player")] public string Player { get; set; } + [JsonProperty(PropertyName = "timestamp")] + public DateTime Timestamp { get; set; } [JsonProperty(PropertyName = "score")] public double Score { get; set; } }