| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using DCLServices.Lambdas; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | |
|
| | 9 | | namespace DCLServices.WorldsAPIService |
| | 10 | | { |
| | 11 | |
|
| | 12 | | public interface IWorldsAPIService : IService |
| | 13 | | { |
| | 14 | | UniTask<(IReadOnlyList<WorldsResponse.WorldInfo> worlds, int total)> SearchWorlds(string searchText, int pageNum |
| | 15 | | UniTask<(IReadOnlyList<WorldsResponse.WorldInfo> worlds, int total)> GetWorlds(int pageNumber, int pageSize, str |
| | 16 | | UniTask<IReadOnlyList<WorldsResponse.WorldInfo>> GetFavorites(int pageNumber, int pageSize, CancellationToken ct |
| | 17 | | UniTask<WorldsResponse.WorldInfo> GetWorld(string name, CancellationToken ct, bool renewCache = false); |
| | 18 | | UniTask<List<WorldsResponse.WorldInfo>> GetWorldsByNamesList(IEnumerable<string> namesList, CancellationToken ct |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public class WorldsAPIService : IWorldsAPIService, ILambdaServiceConsumer<WorldsResponse.WorldsAPIResponse> |
| | 22 | | { |
| | 23 | | private IWorldsAPIClient client; |
| 425 | 24 | | private readonly CancellationTokenSource disposeCts = new (); |
| 425 | 25 | | private readonly Dictionary<string, LambdaResponsePagePointer<WorldsResponse.WorldsAPIResponse>> activeWorldsPag |
| 425 | 26 | | private readonly Dictionary<string, WorldsResponse.WorldInfo> worldsByName = new (); |
| | 27 | |
|
| 425 | 28 | | public WorldsAPIService(IWorldsAPIClient client) |
| | 29 | | { |
| 425 | 30 | | this.client = client; |
| 425 | 31 | | } |
| 425 | 32 | | public void Initialize() { } |
| | 33 | |
|
| | 34 | | public async UniTask<(IReadOnlyList<WorldsResponse.WorldInfo> worlds, int total)> SearchWorlds(string searchText |
| | 35 | | { |
| 0 | 36 | | WorldsResponse.WorldsAPIResponse worldsAPIResponse = await client.SearchWorlds(searchText, pageNumber, pageS |
| 0 | 37 | | return (worldsAPIResponse.data, worldsAPIResponse.total); |
| 0 | 38 | | } |
| | 39 | |
|
| | 40 | | public async UniTask<(IReadOnlyList<WorldsResponse.WorldInfo> worlds, int total)> GetWorlds(int pageNumber, int |
| | 41 | | bool renewCache = false) |
| | 42 | | { |
| 0 | 43 | | var createNewPointer = false; |
| | 44 | |
|
| 0 | 45 | | if (!activeWorldsPagePointers.TryGetValue($"{pageSize}_{filter}_{sort}", out var pagePointer)) { createNewPo |
| 0 | 46 | | else if (renewCache) |
| | 47 | | { |
| 0 | 48 | | pagePointer.Dispose(); |
| 0 | 49 | | activeWorldsPagePointers.Remove($"{pageSize}_{filter}_{sort}"); |
| 0 | 50 | | createNewPointer = true; |
| | 51 | | } |
| | 52 | |
|
| 0 | 53 | | if (createNewPointer) |
| | 54 | | { |
| 0 | 55 | | activeWorldsPagePointers[$"{pageSize}_{filter}_{sort}"] = pagePointer = new LambdaResponsePagePointer<Wo |
| | 56 | | $"", // not needed, the consumer will compose the URL |
| | 57 | | pageSize, disposeCts.Token, this, TimeSpan.FromSeconds(30)); |
| | 58 | | } |
| | 59 | |
|
| 0 | 60 | | (WorldsResponse.WorldsAPIResponse response, bool _) = await pagePointer.GetPageAsync(pageNumber, ct, new Dic |
| | 61 | |
|
| 0 | 62 | | return (response.data, response.total); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public async UniTask<IReadOnlyList<WorldsResponse.WorldInfo>> GetFavorites(int pageNumber, int pageSize, Cancell |
| | 66 | | { |
| 0 | 67 | | var favorites = await client.GetFavorites(pageNumber, pageSize, disposeCts.Token); |
| 0 | 68 | | return favorites; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public async UniTask<WorldsResponse.WorldInfo> GetWorld(string name, CancellationToken ct, bool renewCache = fal |
| | 72 | | { |
| 0 | 73 | | if (renewCache) |
| 0 | 74 | | worldsByName.Remove(name); |
| 0 | 75 | | else if (worldsByName.TryGetValue(name, out var worldInfo)) |
| 0 | 76 | | return worldInfo; |
| | 77 | |
|
| 0 | 78 | | var world = await client.GetWorld(name, ct); |
| 0 | 79 | | worldsByName[world.world_name] = world; |
| 0 | 80 | | return world; |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | public async UniTask<List<WorldsResponse.WorldInfo>> GetWorldsByNamesList(IEnumerable<string> namesList, Cancell |
| | 84 | | { |
| 0 | 85 | | List<WorldsResponse.WorldInfo> alreadyCachedWorlds = new (); |
| 0 | 86 | | List<string> namesToRequest = new (); |
| | 87 | |
|
| 0 | 88 | | foreach (string name in namesList) |
| | 89 | | { |
| 0 | 90 | | if (renewCache) |
| | 91 | | { |
| 0 | 92 | | worldsByName.Remove(name); |
| 0 | 93 | | namesToRequest.Add(name); |
| | 94 | | } |
| | 95 | | else |
| | 96 | | { |
| 0 | 97 | | if (worldsByName.TryGetValue(name, out var worldInfo)) |
| 0 | 98 | | alreadyCachedWorlds.Add(worldInfo); |
| | 99 | | else |
| 0 | 100 | | namesToRequest.Add(name); |
| | 101 | | } |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | var worlds = new List<WorldsResponse.WorldInfo>(); |
| 0 | 105 | | if (namesToRequest.Count > 0) |
| | 106 | | { |
| 0 | 107 | | worlds = await client.GetWorldsByNamesList(namesToRequest, ct); |
| 0 | 108 | | foreach (var world in worlds) |
| 0 | 109 | | worldsByName[world.world_name] = world; |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | worlds.AddRange(alreadyCachedWorlds); |
| | 113 | |
|
| 0 | 114 | | return worlds; |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | public async UniTask<(WorldsResponse.WorldsAPIResponse response, bool success)> CreateRequest(string endPoint, i |
| | 118 | | { |
| 0 | 119 | | var response = await client.GetWorlds(pageNumber, pageSize,additionalData["filter"],additionalData["sort"], |
| | 120 | | // Client will handle most of the error handling and throw if needed |
| 0 | 121 | | return (response, true); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | public void Dispose() |
| | 125 | | { |
| 425 | 126 | | disposeCts.SafeCancelAndDispose(); |
| 425 | 127 | | } |
| | 128 | | } |
| | 129 | | } |