| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading; |
| | 4 | |
|
| | 5 | | namespace DCLServices.WearablesCatalogService |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// This service will choose LambdasWearablesCatalogService or WebInterfaceWearablesCatalogService for requesting |
| | 9 | | /// wearables depending on the flag 'usingUrlParamsForDebug' received in the kernel configuration. |
| | 10 | | /// This will be temporal while we have to live with both services. |
| | 11 | | /// </summary> |
| | 12 | | public class WearablesCatalogServiceProxy : IWearablesCatalogService |
| | 13 | | { |
| | 14 | | private const string FORCE_TO_REQUEST_WEARABLES_THROUGH_KERNEL_FF = "force_to_request_wearables_through_kernel"; |
| | 15 | |
|
| | 16 | | public BaseDictionary<string, WearableItem> WearablesCatalog => |
| 0 | 17 | | wearablesCatalogServiceInUse?.WearablesCatalog; |
| | 18 | |
|
| | 19 | | private IWearablesCatalogService wearablesCatalogServiceInUse; |
| | 20 | | private readonly IWearablesCatalogService lambdasWearablesCatalogService; |
| | 21 | | private readonly WebInterfaceWearablesCatalogService webInterfaceWearablesCatalogService; |
| | 22 | | private readonly BaseDictionary<string, WearableItem> wearablesCatalog; |
| | 23 | | private readonly KernelConfig kernelConfig; |
| | 24 | | private readonly WearablesWebInterfaceBridge wearablesWebInterfaceBridge; |
| | 25 | | private readonly BaseVariable<FeatureFlag> featureFlags; |
| | 26 | | private bool isInitialized; |
| | 27 | |
|
| 115 | 28 | | public WearablesCatalogServiceProxy( |
| | 29 | | IWearablesCatalogService lambdasWearablesCatalogService, |
| | 30 | | WebInterfaceWearablesCatalogService webInterfaceWearablesCatalogService, |
| | 31 | | BaseDictionary<string, WearableItem> wearablesCatalog, |
| | 32 | | KernelConfig kernelConfig, |
| | 33 | | WearablesWebInterfaceBridge wearablesWebInterfaceBridge, |
| | 34 | | BaseVariable<FeatureFlag> featureFlags) |
| | 35 | | { |
| 115 | 36 | | this.lambdasWearablesCatalogService = lambdasWearablesCatalogService; |
| 115 | 37 | | this.webInterfaceWearablesCatalogService = webInterfaceWearablesCatalogService; |
| 115 | 38 | | this.wearablesCatalog = wearablesCatalog; |
| 115 | 39 | | this.kernelConfig = kernelConfig; |
| 115 | 40 | | this.wearablesWebInterfaceBridge = wearablesWebInterfaceBridge; |
| 115 | 41 | | this.featureFlags = featureFlags; |
| 115 | 42 | | } |
| | 43 | |
|
| | 44 | | public void Initialize() |
| | 45 | | { |
| 115 | 46 | | if (!featureFlags.Get().IsInitialized) |
| 115 | 47 | | featureFlags.OnChange += CheckFeatureFlag; |
| | 48 | | else |
| 0 | 49 | | CheckFeatureFlag(featureFlags.Get()); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Dispose() |
| | 53 | | { |
| 115 | 54 | | wearablesCatalogServiceInUse?.Dispose(); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public async UniTask<WearableCollectionsAPIData.Collection[]> GetThirdPartyCollectionsAsync(CancellationToken ca |
| | 58 | | { |
| 0 | 59 | | if (!isInitialized) |
| 0 | 60 | | await UniTask.WaitUntil(() => isInitialized, cancellationToken: cancellationToken); |
| | 61 | |
|
| 0 | 62 | | return await lambdasWearablesCatalogService.GetThirdPartyCollectionsAsync(cancellationToken); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public async UniTask<(IReadOnlyList<WearableItem> wearables, int totalAmount)> RequestOwnedWearablesAsync(string |
| | 66 | | NftRarity rarity = NftRarity.None, |
| | 67 | | NftCollectionType collectionTypeMask = NftCollectionType.All, |
| | 68 | | ICollection<string> thirdPartyCollectionIds = null, string name = null, |
| | 69 | | (NftOrderByOperation type, bool directionAscendent)? orderBy = null) |
| | 70 | | { |
| 0 | 71 | | if (!isInitialized) |
| 0 | 72 | | await UniTask.WaitUntil(() => isInitialized, cancellationToken: cancellationToken); |
| | 73 | |
|
| 0 | 74 | | return await lambdasWearablesCatalogService.RequestOwnedWearablesAsync(userId, pageNumber, pageSize, |
| | 75 | | cancellationToken, category, rarity, collectionTypeMask, thirdPartyCollectionIds, name, orderBy); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public async UniTask<(IReadOnlyList<WearableItem> wearables, int totalAmount)> RequestOwnedWearablesAsync(string |
| | 79 | | { |
| 0 | 80 | | if (!isInitialized) |
| 0 | 81 | | await UniTask.WaitUntil(() => isInitialized, cancellationToken: ct); |
| | 82 | |
|
| 0 | 83 | | return await wearablesCatalogServiceInUse.RequestOwnedWearablesAsync(userId, pageNumber, pageSize, cleanCach |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | public async UniTask RequestBaseWearablesAsync(CancellationToken ct) |
| | 87 | | { |
| 0 | 88 | | if (!isInitialized) |
| 0 | 89 | | await UniTask.WaitUntil(() => isInitialized, cancellationToken: ct); |
| | 90 | |
|
| 0 | 91 | | await wearablesCatalogServiceInUse.RequestBaseWearablesAsync(ct); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | public async UniTask<(IReadOnlyList<WearableItem> wearables, int totalAmount)> RequestThirdPartyWearablesByColle |
| | 95 | | { |
| 0 | 96 | | if (!isInitialized) |
| 0 | 97 | | await UniTask.WaitUntil(() => isInitialized, cancellationToken: ct); |
| | 98 | |
|
| 0 | 99 | | return await wearablesCatalogServiceInUse.RequestThirdPartyWearablesByCollectionAsync(userId, collectionId, |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public async UniTask<WearableItem> RequestWearableAsync(string wearableId, CancellationToken ct) |
| | 103 | | { |
| 0 | 104 | | if (!isInitialized) |
| 0 | 105 | | await UniTask.WaitUntil(() => isInitialized, cancellationToken: ct); |
| | 106 | |
|
| 0 | 107 | | return await wearablesCatalogServiceInUse.RequestWearableAsync(wearableId, ct); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | public async UniTask<WearableItem> RequestWearableFromBuilderAsync(string wearableId, CancellationToken ct) |
| | 111 | | { |
| 0 | 112 | | if (!isInitialized) |
| 0 | 113 | | await UniTask.WaitUntil(() => isInitialized, cancellationToken: ct); |
| | 114 | |
|
| 0 | 115 | | return await lambdasWearablesCatalogService.RequestWearableFromBuilderAsync(wearableId, ct); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | public async UniTask<IReadOnlyList<WearableItem>> RequestWearableCollection(IEnumerable<string> collectionIds, |
| | 119 | | CancellationToken cancellationToken, List<WearableItem> wearableBuffer = null) |
| | 120 | | { |
| 0 | 121 | | if (!isInitialized) |
| 0 | 122 | | await UniTask.WaitUntil(() => isInitialized, cancellationToken: cancellationToken); |
| | 123 | |
|
| 0 | 124 | | return await lambdasWearablesCatalogService.RequestWearableCollection(collectionIds, cancellationToken, wear |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | public async UniTask<(IReadOnlyList<WearableItem> wearables, int totalAmount)> RequestWearableCollectionInBuilde |
| | 128 | | IEnumerable<string> collectionIds, CancellationToken cancellationToken, |
| | 129 | | List<WearableItem> collectionBuffer = null, string nameFilter = null, int pageNumber = 1, int pageSize = 500 |
| | 130 | | { |
| 0 | 131 | | if (!isInitialized) |
| 0 | 132 | | await UniTask.WaitUntil(() => isInitialized, cancellationToken: cancellationToken); |
| | 133 | |
|
| 0 | 134 | | return await lambdasWearablesCatalogService.RequestWearableCollectionInBuilder(collectionIds, cancellationTo |
| | 135 | | collectionBuffer, nameFilter, pageNumber, pageSize); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | public void AddWearablesToCatalog(IEnumerable<WearableItem> wearableItems) => |
| 0 | 139 | | wearablesCatalogServiceInUse?.AddWearablesToCatalog(wearableItems); |
| | 140 | |
|
| | 141 | | public void RemoveWearablesFromCatalog(IEnumerable<string> wearableIds) => |
| 0 | 142 | | wearablesCatalogServiceInUse?.RemoveWearablesFromCatalog(wearableIds); |
| | 143 | |
|
| | 144 | | public void RemoveWearableFromCatalog(string wearableId) => |
| 0 | 145 | | wearablesCatalogServiceInUse?.RemoveWearableFromCatalog(wearableId); |
| | 146 | |
|
| | 147 | | public void RemoveWearablesInUse(IEnumerable<string> wearablesInUseToRemove) => |
| 0 | 148 | | wearablesCatalogServiceInUse?.RemoveWearablesInUse(wearablesInUseToRemove); |
| | 149 | |
|
| | 150 | | public void AddEmbeddedWearablesToCatalog(IEnumerable<WearableItem> wearables) => |
| 11 | 151 | | wearablesCatalogServiceInUse?.AddEmbeddedWearablesToCatalog(wearables); |
| | 152 | |
|
| | 153 | | public void Clear() => |
| 0 | 154 | | wearablesCatalogServiceInUse?.Clear(); |
| | 155 | |
|
| | 156 | | public bool IsValidWearable(string wearableId) => |
| 0 | 157 | | wearablesCatalogServiceInUse.IsValidWearable(wearableId); |
| | 158 | |
|
| | 159 | | private void CheckFeatureFlag(FeatureFlag currentFeatureFlags, FeatureFlag _ = null) |
| | 160 | | { |
| | 161 | | async UniTaskVoid SetServiceInUseDependingOnKernelConfig() |
| | 162 | | { |
| 40 | 163 | | var currentKernelConfig = kernelConfig.EnsureConfigInitialized(); |
| 98 | 164 | | await currentKernelConfig; |
| 40 | 165 | | SetCurrentService(currentKernelConfig.value.urlParamsForWearablesDebug); |
| 40 | 166 | | } |
| | 167 | |
|
| 40 | 168 | | featureFlags.OnChange -= CheckFeatureFlag; |
| | 169 | |
|
| 40 | 170 | | if (currentFeatureFlags.IsFeatureEnabled(FORCE_TO_REQUEST_WEARABLES_THROUGH_KERNEL_FF)) |
| 0 | 171 | | SetCurrentService(true); |
| | 172 | | else |
| 40 | 173 | | SetServiceInUseDependingOnKernelConfig().Forget(); |
| 40 | 174 | | } |
| | 175 | |
|
| | 176 | | private void SetCurrentService(bool useKernel) |
| | 177 | | { |
| 40 | 178 | | if (useKernel) |
| | 179 | | { |
| 0 | 180 | | webInterfaceWearablesCatalogService.Initialize(wearablesWebInterfaceBridge, wearablesCatalog); |
| 0 | 181 | | wearablesCatalogServiceInUse = webInterfaceWearablesCatalogService; |
| | 182 | | } |
| | 183 | | else |
| | 184 | | { |
| 40 | 185 | | lambdasWearablesCatalogService.Initialize(); |
| 40 | 186 | | wearablesCatalogServiceInUse = lambdasWearablesCatalogService; |
| 40 | 187 | | webInterfaceWearablesCatalogService?.Dispose(); |
| | 188 | | } |
| | 189 | |
|
| 40 | 190 | | isInitialized = true; |
| 40 | 191 | | } |
| | 192 | | } |
| | 193 | | } |