| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Networking; |
| | 5 | | using Random = UnityEngine.Random; |
| | 6 | | using Collection = WearableCollectionsAPIData.Collection; |
| | 7 | |
|
| | 8 | | namespace DCL.Helpers |
| | 9 | | { |
| | 10 | | public static class WearablesFetchingHelper |
| | 11 | | { |
| | 12 | | public const string WEARABLES_FETCH_URL = "https://peer.decentraland.org/lambdas/collections/wearables?"; |
| | 13 | | private const string COLLECTIONS_FETCH_URL = "https://peer.decentraland.org/lambdas/collections"; |
| | 14 | | private const string BASE_WEARABLES_COLLECTION_ID = "urn:decentraland:off-chain:base-avatars"; |
| | 15 | |
|
| | 16 | | private static Collection[] collections; |
| | 17 | | private static IEnumerator EnsureCollectionsData() |
| | 18 | | { |
| 0 | 19 | | if (collections?.Length > 0) |
| 0 | 20 | | yield break; |
| | 21 | |
|
| 0 | 22 | | yield return Environment.i.platform.webRequest.Get( |
| | 23 | | url: COLLECTIONS_FETCH_URL, |
| | 24 | | downloadHandler: new DownloadHandlerBuffer(), |
| | 25 | | timeout: 5000, |
| | 26 | | disposeOnCompleted: false, |
| | 27 | | OnFail: (webRequest) => |
| | 28 | | { |
| 0 | 29 | | Debug.LogWarning($"Request error! collections couldn't be fetched! -- {webRequest.error}"); |
| 0 | 30 | | }, |
| | 31 | | OnSuccess: (webRequest) => |
| | 32 | | { |
| 0 | 33 | | var collectionsApiData = JsonUtility.FromJson<WearableCollectionsAPIData>(webRequest.downloadHandler |
| 0 | 34 | | collections = collectionsApiData.collections; |
| 0 | 35 | | }); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Fetches all the existent collection ids and triggers a randomized selection to be loaded |
| | 40 | | /// </summary> |
| | 41 | | /// <param name="finalCollectionIdsList">A strings list that will be filled with the randomized collection ids</ |
| | 42 | | public static IEnumerator GetRandomCollections(int amount, bool ensureBaseWearables, List<string> finalCollectio |
| | 43 | | { |
| 0 | 44 | | yield return EnsureCollectionsData(); |
| | 45 | |
|
| 0 | 46 | | List<int> randomizedIndices = new List<int>(); |
| | 47 | | int randomIndex; |
| 0 | 48 | | bool addedBaseWearablesCollection = false; |
| 0 | 49 | | for (int i = 0; i < amount; i++) |
| | 50 | | { |
| 0 | 51 | | randomIndex = Random.Range(0, collections.Length); |
| 0 | 52 | | while (randomizedIndices.Contains(randomIndex)) |
| | 53 | | { |
| 0 | 54 | | randomIndex = Random.Range(0, collections.Length); |
| | 55 | | } |
| | 56 | |
|
| 0 | 57 | | if (ensureBaseWearables && collections[randomIndex].id == BASE_WEARABLES_COLLECTION_ID) |
| 0 | 58 | | addedBaseWearablesCollection = true; |
| | 59 | |
|
| 0 | 60 | | finalCollectionIdsList.Add(collections[randomIndex].id); |
| 0 | 61 | | randomizedIndices.Add(randomIndex); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | // We add the base wearables collection to make sure we have at least 1 of each avatar body-part |
| 0 | 65 | | if (!addedBaseWearablesCollection) |
| 0 | 66 | | finalCollectionIdsList[0] = BASE_WEARABLES_COLLECTION_ID; |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <summary> |
| | 70 | | /// Given a base url for fetching wearables, this method recursively downloads all the 'pages' responded by the |
| | 71 | | /// and populates the global Catalogue with those wearables. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="url">The API url to fetch the list of wearables</param> |
| | 74 | | /// <param name="finalWearableItemsList">A WearableItems list that will be filled with the fetched wearables</pa |
| | 75 | | public static IEnumerator GetWearableItems(string url, List<WearableItem> finalWearableItemsList) |
| | 76 | | { |
| 0 | 77 | | string nextPageParams = null; |
| | 78 | |
|
| 0 | 79 | | yield return Environment.i.platform.webRequest.Get( |
| | 80 | | url: url, |
| | 81 | | downloadHandler: new DownloadHandlerBuffer(), |
| | 82 | | timeout: 5000, |
| | 83 | | disposeOnCompleted: false, |
| | 84 | | OnFail: (webRequest) => |
| | 85 | | { |
| 0 | 86 | | Debug.LogWarning($"Request error! wearables couldn't be fetched! -- {webRequest.error}"); |
| 0 | 87 | | }, |
| | 88 | | OnSuccess: (webRequest) => |
| | 89 | | { |
| 0 | 90 | | var wearablesApiData = JsonUtility.FromJson<WearablesAPIData>(webRequest.downloadHandler.text); |
| 0 | 91 | | var wearableItemsList = wearablesApiData.GetWearableItems(); |
| 0 | 92 | | finalWearableItemsList.AddRange(wearableItemsList); |
| | 93 | |
|
| 0 | 94 | | nextPageParams = wearablesApiData.pagination.next; |
| 0 | 95 | | }); |
| | 96 | |
|
| 0 | 97 | | if (!string.IsNullOrEmpty(nextPageParams)) |
| | 98 | | { |
| | 99 | | // Since the wearables deployments response returns only a batch of elements, we need to fetch all the |
| | 100 | | // batches sequentially |
| 0 | 101 | | yield return GetWearableItems(WEARABLES_FETCH_URL + nextPageParams, finalWearableItemsList); |
| | 102 | | } |
| 0 | 103 | | } |
| | 104 | | } |
| | 105 | | } |