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