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