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