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