| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public class CatalogController : MonoBehaviour |
| | 9 | | { |
| 1 | 10 | | public static bool VERBOSE = false; |
| | 11 | | private const string OWNED_WEARABLES_CONTEXT = "OwnedWearables"; |
| | 12 | | private const string BASE_WEARABLES_CONTEXT = "BaseWearables"; |
| | 13 | | private const int FRAMES_TO_CHECK_FOR_SENDING_PENDING_REQUESTS = 1; |
| | 14 | | private const float TIME_TO_CHECK_FOR_UNUSED_WEARABLES = 10f; |
| | 15 | | private const float REQUESTS_TIME_OUT_SECONDS = 45; |
| | 16 | |
|
| 0 | 17 | | public static CatalogController i { get; private set; } |
| | 18 | |
|
| 0 | 19 | | public static BaseDictionary<string, WearableItem> wearableCatalog => DataStore.i.wearables; |
| | 20 | |
|
| 1 | 21 | | private static Dictionary<string, int> wearablesInUseCounters = new Dictionary<string, int>(); |
| 1 | 22 | | private static Dictionary<string, Promise<WearableItem>> awaitingWearablePromises = new Dictionary<string, Promise<W |
| 1 | 23 | | private static Dictionary<string, float> pendingWearableRequestedTimes = new Dictionary<string, float>(); |
| 1 | 24 | | private static Dictionary<string, Promise<WearableItem[]>> awaitingWearablesByContextPromises = new Dictionary<strin |
| 1 | 25 | | private static Dictionary<string, float> pendingWearablesByContextRequestedTimes = new Dictionary<string, float>(); |
| 1 | 26 | | private static List<string> pendingRequestsToSend = new List<string>(); |
| | 27 | | private float timeSinceLastUnusedWearablesCheck = 0f; |
| | 28 | |
|
| 246 | 29 | | public void Awake() { i = this; } |
| | 30 | |
|
| | 31 | | private void Update() |
| | 32 | | { |
| | 33 | | // All the requests happened during the same frames interval are sent together |
| 21253 | 34 | | if (Time.frameCount % FRAMES_TO_CHECK_FOR_SENDING_PENDING_REQUESTS == 0) |
| | 35 | | { |
| 21253 | 36 | | CheckForSendingPendingRequests(); |
| 21253 | 37 | | CheckForRequestsTimeOuts(); |
| 21253 | 38 | | CheckForRequestsByContextTimeOuts(); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | // Check unused wearables (to be removed from our catalog) only every [TIME_TO_CHECK_FOR_UNUSED_WEARABLES] secon |
| 21253 | 42 | | timeSinceLastUnusedWearablesCheck += Time.deltaTime; |
| 21253 | 43 | | if (timeSinceLastUnusedWearablesCheck >= TIME_TO_CHECK_FOR_UNUSED_WEARABLES) |
| | 44 | | { |
| 1 | 45 | | CheckForUnusedWearables(); |
| 1 | 46 | | timeSinceLastUnusedWearablesCheck = 0f; |
| | 47 | | } |
| 21253 | 48 | | } |
| | 49 | |
|
| | 50 | | public void AddWearablesToCatalog(string payload) |
| | 51 | | { |
| 0 | 52 | | if (VERBOSE) |
| 0 | 53 | | Debug.Log("add wearables: " + payload); |
| | 54 | |
|
| 0 | 55 | | WearablesRequestResponse request = null; |
| | 56 | |
|
| | 57 | | try |
| | 58 | | { |
| 0 | 59 | | request = JsonUtility.FromJson<WearablesRequestResponse>(payload); |
| 0 | 60 | | } |
| 0 | 61 | | catch (Exception e) |
| | 62 | | { |
| 0 | 63 | | Debug.LogError($"Fail to parse wearables json {e}"); |
| 0 | 64 | | } |
| | 65 | |
|
| 0 | 66 | | if (request == null) |
| 0 | 67 | | return; |
| | 68 | |
|
| 0 | 69 | | AddWearablesToCatalog(request.wearables); |
| | 70 | |
|
| 0 | 71 | | if (!string.IsNullOrEmpty(request.context)) |
| | 72 | | { |
| 0 | 73 | | ResolvePendingWearablesByContextPromise(request.context, request.wearables); |
| 0 | 74 | | pendingWearablesByContextRequestedTimes.Remove(request.context); |
| | 75 | | } |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public void AddWearablesToCatalog(WearableItem[] wearableItems) |
| | 79 | | { |
| 0 | 80 | | foreach (WearableItem wearableItem in wearableItems) |
| | 81 | | { |
| 0 | 82 | | if (!wearableCatalog.ContainsKey(wearableItem.id)) |
| | 83 | | { |
| 0 | 84 | | wearableCatalog.Add(wearableItem.id, wearableItem); |
| | 85 | |
|
| 0 | 86 | | if (!wearablesInUseCounters.ContainsKey(wearableItem.id)) |
| 0 | 87 | | wearablesInUseCounters.Add(wearableItem.id, 1); |
| | 88 | |
|
| 0 | 89 | | ResolvePendingWearablePromise(wearableItem.id, wearableItem); |
| | 90 | |
|
| 0 | 91 | | pendingWearableRequestedTimes.Remove(wearableItem.id); |
| | 92 | | } |
| | 93 | | } |
| 0 | 94 | | } |
| | 95 | |
|
| 0 | 96 | | public void AddWearablesToCatalog(List<WearableItem> wearableItems) { AddWearablesToCatalog(wearableItems.ToArray()) |
| | 97 | |
|
| | 98 | | public void WearablesRequestFailed(string payload) |
| | 99 | | { |
| 0 | 100 | | WearablesRequestFailed requestFailedResponse = JsonUtility.FromJson<WearablesRequestFailed>(payload); |
| | 101 | |
|
| 0 | 102 | | if (requestFailedResponse.context == BASE_WEARABLES_CONTEXT || |
| | 103 | | requestFailedResponse.context == OWNED_WEARABLES_CONTEXT) |
| | 104 | | { |
| 0 | 105 | | ResolvePendingWearablesByContextPromise( |
| | 106 | | requestFailedResponse.context, |
| | 107 | | null, |
| | 108 | | requestFailedResponse.error); |
| 0 | 109 | | } |
| | 110 | | else |
| | 111 | | { |
| 0 | 112 | | string[] failedWearablesIds = requestFailedResponse.context.Split(','); |
| 0 | 113 | | for (int i = 0; i < failedWearablesIds.Length; i++) |
| | 114 | | { |
| 0 | 115 | | ResolvePendingWearablePromise( |
| | 116 | | failedWearablesIds[i], |
| | 117 | | null, |
| | 118 | | $"The request for the wearable '{failedWearablesIds[i]}' has failed: {requestFailedResponse.error}") |
| | 119 | | } |
| | 120 | | } |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public void RemoveWearablesFromCatalog(string payload) |
| | 124 | | { |
| 0 | 125 | | string[] itemIDs = JsonUtility.FromJson<string[]>(payload); |
| | 126 | |
|
| 0 | 127 | | int count = itemIDs.Length; |
| 0 | 128 | | for (int i = 0; i < count; ++i) |
| | 129 | | { |
| 0 | 130 | | wearableCatalog.Remove(itemIDs[i]); |
| 0 | 131 | | wearablesInUseCounters.Remove(itemIDs[i]); |
| | 132 | | } |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | public void ClearWearableCatalog() |
| | 136 | | { |
| 0 | 137 | | wearableCatalog?.Clear(); |
| 0 | 138 | | wearablesInUseCounters.Clear(); |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | public static Promise<WearableItem> RequestWearable(string wearableId) |
| | 142 | | { |
| 0 | 143 | | if (VERBOSE) |
| 0 | 144 | | Debug.Log("request wearables: " + wearableId); |
| | 145 | |
|
| | 146 | | Promise<WearableItem> promiseResult; |
| | 147 | |
|
| 0 | 148 | | if (wearableCatalog.TryGetValue(wearableId, out WearableItem wearable)) |
| | 149 | | { |
| 0 | 150 | | wearablesInUseCounters[wearableId]++; |
| 0 | 151 | | promiseResult = new Promise<WearableItem>(); |
| 0 | 152 | | promiseResult.Resolve(wearable); |
| 0 | 153 | | } |
| | 154 | | else |
| | 155 | | { |
| 0 | 156 | | if (!awaitingWearablePromises.ContainsKey(wearableId)) |
| | 157 | | { |
| 0 | 158 | | promiseResult = new Promise<WearableItem>(); |
| 0 | 159 | | awaitingWearablePromises.Add(wearableId, promiseResult); |
| | 160 | |
|
| | 161 | | // We accumulate all the requests during the same frames interval to send them all together |
| 0 | 162 | | pendingRequestsToSend.Add(wearableId); |
| 0 | 163 | | } |
| | 164 | | else |
| | 165 | | { |
| 0 | 166 | | awaitingWearablePromises.TryGetValue(wearableId, out promiseResult); |
| | 167 | | } |
| | 168 | | } |
| | 169 | |
|
| 0 | 170 | | return promiseResult; |
| | 171 | | } |
| | 172 | |
|
| | 173 | | public static Promise<WearableItem[]> RequestOwnedWearables(string userId) |
| | 174 | | { |
| | 175 | | Promise<WearableItem[]> promiseResult; |
| | 176 | |
|
| 5 | 177 | | if (!awaitingWearablesByContextPromises.ContainsKey(OWNED_WEARABLES_CONTEXT)) |
| | 178 | | { |
| 2 | 179 | | promiseResult = new Promise<WearableItem[]>(); |
| 2 | 180 | | awaitingWearablesByContextPromises.Add(OWNED_WEARABLES_CONTEXT, promiseResult); |
| | 181 | |
|
| 2 | 182 | | if (!pendingWearablesByContextRequestedTimes.ContainsKey(OWNED_WEARABLES_CONTEXT)) |
| 2 | 183 | | pendingWearablesByContextRequestedTimes.Add(OWNED_WEARABLES_CONTEXT, Time.realtimeSinceStartup); |
| | 184 | |
|
| 2 | 185 | | WebInterface.RequestWearables( |
| | 186 | | ownedByUser: userId, |
| | 187 | | wearableIds: null, |
| | 188 | | collectionIds: null, |
| | 189 | | context: OWNED_WEARABLES_CONTEXT |
| | 190 | | ); |
| 2 | 191 | | } |
| | 192 | | else |
| | 193 | | { |
| 3 | 194 | | awaitingWearablesByContextPromises.TryGetValue(OWNED_WEARABLES_CONTEXT, out promiseResult); |
| | 195 | | } |
| | 196 | |
|
| 5 | 197 | | return promiseResult; |
| | 198 | | } |
| | 199 | |
|
| | 200 | | public static Promise<WearableItem[]> RequestBaseWearables() |
| | 201 | | { |
| | 202 | | Promise<WearableItem[]> promiseResult; |
| | 203 | |
|
| 0 | 204 | | if (!awaitingWearablesByContextPromises.ContainsKey(BASE_WEARABLES_CONTEXT)) |
| | 205 | | { |
| 0 | 206 | | promiseResult = new Promise<WearableItem[]>(); |
| 0 | 207 | | awaitingWearablesByContextPromises.Add(BASE_WEARABLES_CONTEXT, promiseResult); |
| | 208 | |
|
| 0 | 209 | | if (!pendingWearablesByContextRequestedTimes.ContainsKey(BASE_WEARABLES_CONTEXT)) |
| 0 | 210 | | pendingWearablesByContextRequestedTimes.Add(BASE_WEARABLES_CONTEXT, Time.realtimeSinceStartup); |
| | 211 | |
|
| 0 | 212 | | WebInterface.RequestWearables( |
| | 213 | | ownedByUser: null, |
| | 214 | | wearableIds: null, |
| | 215 | | collectionIds: new string[] { "urn:decentraland:off-chain:base-avatars" }, |
| | 216 | | context: BASE_WEARABLES_CONTEXT |
| | 217 | | ); |
| 0 | 218 | | } |
| | 219 | | else |
| | 220 | | { |
| 0 | 221 | | awaitingWearablesByContextPromises.TryGetValue(BASE_WEARABLES_CONTEXT, out promiseResult); |
| | 222 | | } |
| | 223 | |
|
| 0 | 224 | | return promiseResult; |
| | 225 | | } |
| | 226 | |
|
| | 227 | | public static void RemoveWearablesInUse(List<string> wearablesInUseToRemove) |
| | 228 | | { |
| 3038 | 229 | | foreach (var wearableToRemove in wearablesInUseToRemove) |
| | 230 | | { |
| 0 | 231 | | if (wearablesInUseCounters.ContainsKey(wearableToRemove)) |
| | 232 | | { |
| 0 | 233 | | wearablesInUseCounters[wearableToRemove]--; |
| | 234 | | } |
| | 235 | | } |
| 1519 | 236 | | } |
| | 237 | |
|
| | 238 | | private void ResolvePendingWearablePromise(string wearableId, WearableItem newWearableAddedIntoCatalog = null, strin |
| | 239 | | { |
| 0 | 240 | | if (awaitingWearablePromises.TryGetValue(wearableId, out Promise<WearableItem> promise)) |
| | 241 | | { |
| 0 | 242 | | awaitingWearablePromises.Remove(wearableId); |
| | 243 | |
|
| 0 | 244 | | if (string.IsNullOrEmpty(errorMessage)) |
| 0 | 245 | | promise.Resolve(newWearableAddedIntoCatalog); |
| | 246 | | else |
| 0 | 247 | | promise.Reject(errorMessage); |
| | 248 | | } |
| 0 | 249 | | } |
| | 250 | |
|
| | 251 | | private void ResolvePendingWearablesByContextPromise(string context, WearableItem[] newWearablesAddedIntoCatalog = n |
| | 252 | | { |
| 2 | 253 | | if (awaitingWearablesByContextPromises.TryGetValue(context, out Promise<WearableItem[]> promise)) |
| | 254 | | { |
| 2 | 255 | | awaitingWearablesByContextPromises.Remove(context); |
| | 256 | |
|
| 2 | 257 | | if (string.IsNullOrEmpty(errorMessage)) |
| 0 | 258 | | promise.Resolve(newWearablesAddedIntoCatalog); |
| | 259 | | else |
| 2 | 260 | | promise.Reject(errorMessage); |
| | 261 | | } |
| 2 | 262 | | } |
| | 263 | |
|
| | 264 | | private void CheckForSendingPendingRequests() |
| | 265 | | { |
| 21253 | 266 | | if (pendingRequestsToSend.Count > 0) |
| | 267 | | { |
| 0 | 268 | | foreach (var request in pendingRequestsToSend) |
| | 269 | | { |
| 0 | 270 | | if (!pendingWearableRequestedTimes.ContainsKey(request)) |
| 0 | 271 | | pendingWearableRequestedTimes.Add(request, Time.realtimeSinceStartup); |
| | 272 | | } |
| | 273 | |
|
| 0 | 274 | | WebInterface.RequestWearables( |
| | 275 | | ownedByUser: null, |
| | 276 | | wearableIds: pendingRequestsToSend.ToArray(), |
| | 277 | | collectionIds: null, |
| | 278 | | context: string.Join(",", pendingRequestsToSend.ToArray()) |
| | 279 | | ); |
| | 280 | |
|
| 0 | 281 | | pendingRequestsToSend.Clear(); |
| | 282 | | } |
| 21253 | 283 | | } |
| | 284 | |
|
| | 285 | | private void CheckForRequestsTimeOuts() |
| | 286 | | { |
| 21253 | 287 | | if (pendingWearableRequestedTimes.Count > 0) |
| | 288 | | { |
| 0 | 289 | | List<string> expiredRequestes = new List<string>(); |
| 0 | 290 | | foreach (var promiseRequestedTime in pendingWearableRequestedTimes) |
| | 291 | | { |
| 0 | 292 | | if ((Time.realtimeSinceStartup - promiseRequestedTime.Value) > REQUESTS_TIME_OUT_SECONDS) |
| | 293 | | { |
| 0 | 294 | | expiredRequestes.Add(promiseRequestedTime.Key); |
| | 295 | | } |
| | 296 | | } |
| | 297 | |
|
| 0 | 298 | | foreach (var expiredRequestToRemove in expiredRequestes) |
| | 299 | | { |
| 0 | 300 | | pendingWearableRequestedTimes.Remove(expiredRequestToRemove); |
| | 301 | |
|
| 0 | 302 | | ResolvePendingWearablePromise( |
| | 303 | | expiredRequestToRemove, |
| | 304 | | null, |
| | 305 | | $"The request for the wearable '{expiredRequestToRemove}' has exceed the set timeout!"); |
| | 306 | | } |
| | 307 | | } |
| 21253 | 308 | | } |
| | 309 | |
|
| | 310 | | private void CheckForRequestsByContextTimeOuts() |
| | 311 | | { |
| 21253 | 312 | | if (pendingWearablesByContextRequestedTimes.Count > 0) |
| | 313 | | { |
| 4908 | 314 | | List<string> expiredRequests = new List<string>(); |
| 19632 | 315 | | foreach (var promiseByContextRequestedTime in pendingWearablesByContextRequestedTimes) |
| | 316 | | { |
| 4908 | 317 | | if ((Time.realtimeSinceStartup - promiseByContextRequestedTime.Value) > REQUESTS_TIME_OUT_SECONDS) |
| | 318 | | { |
| 2 | 319 | | expiredRequests.Add(promiseByContextRequestedTime.Key); |
| | 320 | | } |
| | 321 | | } |
| | 322 | |
|
| 9820 | 323 | | foreach (var expiredRequestToRemove in expiredRequests) |
| | 324 | | { |
| 2 | 325 | | pendingWearablesByContextRequestedTimes.Remove(expiredRequestToRemove); |
| | 326 | |
|
| 2 | 327 | | ResolvePendingWearablesByContextPromise( |
| | 328 | | expiredRequestToRemove, |
| | 329 | | null, |
| | 330 | | $"The request for the wearable context '{expiredRequestToRemove}' has exceed the set timeout!"); |
| | 331 | | } |
| | 332 | | } |
| 21253 | 333 | | } |
| | 334 | |
|
| | 335 | | private void CheckForUnusedWearables() |
| | 336 | | { |
| 1 | 337 | | if (wearablesInUseCounters.Count > 0) |
| | 338 | | { |
| 0 | 339 | | List<string> wearablesToDestroy = new List<string>(); |
| 0 | 340 | | foreach (var wearableInUse in wearablesInUseCounters) |
| | 341 | | { |
| 0 | 342 | | if (wearableInUse.Value <= 0) |
| | 343 | | { |
| 0 | 344 | | wearablesToDestroy.Add(wearableInUse.Key); |
| | 345 | | } |
| | 346 | | } |
| | 347 | |
|
| 0 | 348 | | foreach (var wearableToDestroy in wearablesToDestroy) |
| | 349 | | { |
| 0 | 350 | | wearableCatalog.Remove(wearableToDestroy); |
| 0 | 351 | | wearablesInUseCounters.Remove(wearableToDestroy); |
| | 352 | | } |
| | 353 | | } |
| 1 | 354 | | } |
| | 355 | | } |