From 5ffdafcd6a1076426c42046d5b3909e60964886a Mon Sep 17 00:00:00 2001 From: tudor <7089284+tudddorrr@users.noreply.github.com> Date: Tue, 6 May 2025 21:16:50 +0100 Subject: [PATCH] get entries options, filter entries by prop key and/or value --- .../Talo/Runtime/APIs/LeaderboardsAPI.cs | 46 +++++++++++++++++-- .../Scripts/LeaderboardUIController.cs | 6 ++- .../Leaderboards/GetLeaderboardEntries.cs | 2 +- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Assets/Talo Game Services/Talo/Runtime/APIs/LeaderboardsAPI.cs b/Assets/Talo Game Services/Talo/Runtime/APIs/LeaderboardsAPI.cs index eb4c792..cd94c99 100644 --- a/Assets/Talo Game Services/Talo/Runtime/APIs/LeaderboardsAPI.cs +++ b/Assets/Talo Game Services/Talo/Runtime/APIs/LeaderboardsAPI.cs @@ -6,6 +6,15 @@ namespace TaloGameServices { + public class GetEntriesOptions + { + public int page = 0; + public int aliasId = -1; + public bool includeArchived = false; + public string propKey = ""; + public string propValue = ""; + } + public class LeaderboardsAPI : BaseAPI { private LeaderboardEntriesManager _entriesManager = new(); @@ -24,9 +33,22 @@ public List GetCachedEntriesForCurrentPlayer(string internalNa return _entriesManager.GetEntries(internalName).FindAll(e => e.playerAlias.id == Talo.CurrentAlias.id); } - public async Task GetEntries(string internalName, int page, int aliasId = -1, bool includeArchived = false) + private string BuildGetEntriesQueryParams(GetEntriesOptions options) + { + options ??= new GetEntriesOptions(); + + var query = new Dictionary { ["page"] = options.page.ToString() }; + if (options.aliasId != -1) query["aliasId"] = options.aliasId.ToString(); + if (options.includeArchived) query["withDeleted"] = "1"; + if (!string.IsNullOrEmpty(options.propKey)) query["propKey"] = options.propKey; + if (!string.IsNullOrEmpty(options.propValue)) query["propValue"] = options.propValue; + + return string.Join("&", query.Select(x => $"{x.Key}={x.Value}")); + } + + public async Task GetEntries(string internalName, GetEntriesOptions options = null) { - var uri = new Uri($"{baseUrl}/{internalName}/entries?page={page}" + (aliasId != -1 ? $"&aliasId={aliasId}" : "") + (includeArchived ? "&withDeleted=1" : "")); + var uri = new Uri($"{baseUrl}/{internalName}/entries?{BuildGetEntriesQueryParams(options)}"); var json = await Call(uri, "GET"); var res = JsonUtility.FromJson(json); @@ -39,10 +61,28 @@ public async Task GetEntries(string internalName, in return res; } + [Obsolete("Use GetEntries(string internalName, GetEntriesOptions options) instead.")] + public async Task GetEntries(string internalName, int page, int aliasId = -1, bool includeArchived = false) + { + return await GetEntries(internalName, new GetEntriesOptions + { + page = page, + aliasId = aliasId, + includeArchived = includeArchived + }); + } + + [Obsolete("Use GetEntries(string internalName, GetEntriesOptions options) and set options.aliasId = Talo.CurrentAlias.id instead.")] public async Task GetEntriesForCurrentPlayer(string internalName, int page, bool includeArchived = false) { Talo.IdentityCheck(); - return await GetEntries(internalName, page, Talo.CurrentAlias.id, includeArchived); + + return await GetEntries(internalName, new GetEntriesOptions + { + page = page, + aliasId = Talo.CurrentAlias.id, + includeArchived = includeArchived + }); } public async Task<(LeaderboardEntry, bool)> AddEntry(string internalName, float score, params (string, string)[] propTuples) diff --git a/Assets/Talo Game Services/Talo/Samples/LeaderboardsDemo/Scripts/LeaderboardUIController.cs b/Assets/Talo Game Services/Talo/Samples/LeaderboardsDemo/Scripts/LeaderboardUIController.cs index 0f3b8c3..1038aac 100644 --- a/Assets/Talo Game Services/Talo/Samples/LeaderboardsDemo/Scripts/LeaderboardUIController.cs +++ b/Assets/Talo Game Services/Talo/Samples/LeaderboardsDemo/Scripts/LeaderboardUIController.cs @@ -75,7 +75,11 @@ private async Task LoadEntries() { try { - var res = await Talo.Leaderboards.GetEntries(leaderboardName, page, includeArchived: includeArchived); + var res = await Talo.Leaderboards.GetEntries(leaderboardName, new GetEntriesOptions() { + page = page, + includeArchived = includeArchived + }); + page++; done = res.isLastPage; } diff --git a/Assets/Talo Game Services/Talo/Samples/Playground/Scripts/Leaderboards/GetLeaderboardEntries.cs b/Assets/Talo Game Services/Talo/Samples/Playground/Scripts/Leaderboards/GetLeaderboardEntries.cs index 9f414bf..6afdf02 100644 --- a/Assets/Talo Game Services/Talo/Samples/Playground/Scripts/Leaderboards/GetLeaderboardEntries.cs +++ b/Assets/Talo Game Services/Talo/Samples/Playground/Scripts/Leaderboards/GetLeaderboardEntries.cs @@ -26,7 +26,7 @@ private async Task FetchEntries() try { int score = UnityEngine.Random.Range(0, 10000); - LeaderboardEntriesResponse res = await Talo.Leaderboards.GetEntries(leaderboardInternalName, page); + LeaderboardEntriesResponse res = await Talo.Leaderboards.GetEntries(leaderboardInternalName, new GetEntriesOptions() { page = page }); LeaderboardEntry[] entries = res.entries; if (entries.Length == 0)