| | 1 | | using AvatarSystem; |
| | 2 | | using Cysharp.Threading.Tasks; |
| | 3 | | using DCL.Emotes; |
| | 4 | | using DCL.EmotesCustomization; |
| | 5 | | using DCL.Interface; |
| | 6 | | using DCL.Tasks; |
| | 7 | | using DCLServices.CustomNftCollection; |
| | 8 | | using System; |
| | 9 | | using System.Collections.Generic; |
| | 10 | | using System.Linq; |
| | 11 | | using System.Threading; |
| | 12 | | using UnityEngine; |
| | 13 | | using UnityEngine.Pool; |
| | 14 | |
|
| | 15 | | namespace DCL.Backpack |
| | 16 | | { |
| | 17 | | public class BackpackEmotesSectionController : IBackpackEmotesSectionController |
| | 18 | | { |
| | 19 | | private const string URL_SELL_COLLECTIBLE_GENERIC = "https://market.decentraland.org/account"; |
| | 20 | | private const string URL_SELL_SPECIFIC_COLLECTIBLE = "https://market.decentraland.org/contracts/{collectionId}/t |
| | 21 | |
|
| | 22 | | public event Action<string> OnNewEmoteAdded; |
| | 23 | | public event Action<string> OnEmotePreviewed; |
| | 24 | | public event Action<string> OnEmoteEquipped; |
| | 25 | | public event Action<string> OnEmoteUnEquipped; |
| | 26 | |
|
| | 27 | | private readonly DataStore dataStore; |
| | 28 | | private readonly IUserProfileBridge userProfileBridge; |
| | 29 | | private readonly IEmotesCatalogService emotesCatalogService; |
| | 30 | | private readonly ICustomNftCollectionService customNftCollectionService; |
| | 31 | | private readonly IEmotesCustomizationComponentController emotesCustomizationComponentController; |
| 0 | 32 | | private CancellationTokenSource loadEmotesCts = new (); |
| 0 | 33 | | private List<Nft> ownedNftCollectionsL1 = new (); |
| 0 | 34 | | private List<Nft> ownedNftCollectionsL2 = new (); |
| | 35 | |
|
| 0 | 36 | | public BackpackEmotesSectionController( |
| | 37 | | DataStore dataStore, |
| | 38 | | Transform emotesSectionTransform, |
| | 39 | | IUserProfileBridge userProfileBridge, |
| | 40 | | IEmotesCatalogService emotesCatalogService, |
| | 41 | | IAvatarEmotesController emotesController, |
| | 42 | | ICustomNftCollectionService customNftCollectionService) |
| | 43 | | { |
| 0 | 44 | | this.dataStore = dataStore; |
| 0 | 45 | | this.userProfileBridge = userProfileBridge; |
| 0 | 46 | | this.emotesCatalogService = emotesCatalogService; |
| 0 | 47 | | this.customNftCollectionService = customNftCollectionService; |
| | 48 | |
|
| 0 | 49 | | emotesCustomizationComponentController = new EmotesCustomizationComponentController( |
| | 50 | | dataStore.emotesCustomization, |
| | 51 | | emotesController, |
| | 52 | | dataStore.exploreV2, |
| | 53 | | dataStore.HUDs, |
| | 54 | | emotesSectionTransform, |
| | 55 | | "EmotesCustomization/EmotesCustomizationSectionV2"); |
| | 56 | |
|
| 0 | 57 | | emotesCustomizationComponentController.SetEquippedBodyShape(userProfileBridge.GetOwn().avatar.bodyShape); |
| | 58 | |
|
| 0 | 59 | | userProfileBridge.GetOwn().OnUpdate += LoadUserProfile; |
| | 60 | |
|
| 0 | 61 | | dataStore.emotesCustomization.currentLoadedEmotes.OnAdded += NewEmoteAdded; |
| 0 | 62 | | emotesCustomizationComponentController.onEmotePreviewed += EmotePreviewed; |
| 0 | 63 | | emotesCustomizationComponentController.onEmoteEquipped += EmoteEquipped; |
| 0 | 64 | | emotesCustomizationComponentController.onEmoteUnequipped += EmoteUnEquipped; |
| 0 | 65 | | emotesCustomizationComponentController.onEmoteSell += EmoteSell; |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Dispose() |
| | 69 | | { |
| 0 | 70 | | loadEmotesCts.SafeCancelAndDispose(); |
| 0 | 71 | | loadEmotesCts = null; |
| | 72 | |
|
| 0 | 73 | | dataStore.emotesCustomization.currentLoadedEmotes.OnAdded -= NewEmoteAdded; |
| 0 | 74 | | emotesCustomizationComponentController.onEmotePreviewed -= EmotePreviewed; |
| 0 | 75 | | emotesCustomizationComponentController.onEmoteEquipped -= EmoteEquipped; |
| 0 | 76 | | emotesCustomizationComponentController.onEmoteUnequipped -= EmoteUnEquipped; |
| 0 | 77 | | emotesCustomizationComponentController.onEmoteSell -= EmoteSell; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | private void LoadUserProfile(UserProfile userProfile) => |
| 0 | 81 | | QueryNftCollections(userProfile.userId); |
| | 82 | |
|
| | 83 | | private void QueryNftCollections(string userId) |
| | 84 | | { |
| 0 | 85 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 86 | | return; |
| | 87 | |
|
| 0 | 88 | | Environment.i.platform.serviceProviders.theGraph.QueryNftCollections(userProfileBridge.GetOwn().userId, NftC |
| 0 | 89 | | .Then(nft => ownedNftCollectionsL1 = nft) |
| | 90 | | .Catch(Debug.LogError); |
| | 91 | |
|
| 0 | 92 | | Environment.i.platform.serviceProviders.theGraph.QueryNftCollections(userProfileBridge.GetOwn().userId, NftC |
| 0 | 93 | | .Then((nft) => ownedNftCollectionsL2 = nft) |
| | 94 | | .Catch(Debug.LogError); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | public void LoadEmotes() |
| | 98 | | { |
| | 99 | | async UniTaskVoid LoadEmotesAsync(CancellationToken ct = default) |
| | 100 | | { |
| | 101 | | try |
| | 102 | | { |
| 0 | 103 | | EmbeddedEmotesSO embeddedEmotesSo = await emotesCatalogService.GetEmbeddedEmotes(); |
| 0 | 104 | | List<WearableItem> allEmotes = new (); |
| 0 | 105 | | allEmotes.AddRange(await emotesCatalogService.RequestOwnedEmotesAsync(userProfileBridge.GetOwn().use |
| | 106 | |
|
| 0 | 107 | | Dictionary<string, WearableItem> consolidatedEmotes = new Dictionary<string, WearableItem>(); |
| | 108 | |
|
| 0 | 109 | | foreach (EmbeddedEmote emote in embeddedEmotesSo.GetAllEmotes()) |
| 0 | 110 | | consolidatedEmotes[emote.id] = emote; |
| | 111 | |
|
| 0 | 112 | | foreach (var emote in allEmotes) |
| | 113 | | { |
| 0 | 114 | | if (consolidatedEmotes.TryGetValue(emote.id, out WearableItem consolidatedEmote)) |
| 0 | 115 | | consolidatedEmote.amount += emote.amount; |
| | 116 | | else |
| | 117 | | { |
| 0 | 118 | | emote.amount = 1; |
| 0 | 119 | | consolidatedEmotes[emote.id] = emote; |
| | 120 | | } |
| | 121 | | } |
| | 122 | |
|
| 0 | 123 | | allEmotes = consolidatedEmotes.Values.ToList(); |
| | 124 | |
|
| | 125 | | try |
| | 126 | | { |
| 0 | 127 | | await FetchCustomEmoteItems(allEmotes, ct); |
| 0 | 128 | | await FetchCustomEmoteCollections(allEmotes, ct); |
| 0 | 129 | | } |
| 0 | 130 | | catch (Exception e) when (e is not OperationCanceledException) { Debug.LogException(e); } |
| 0 | 131 | | finally { UpdateEmotes(); } |
| | 132 | |
|
| | 133 | | void UpdateEmotes() |
| | 134 | | { |
| 0 | 135 | | dataStore.emotesCustomization.UnequipMissingEmotes(allEmotes); |
| 0 | 136 | | emotesCustomizationComponentController.SetEmotes(allEmotes.ToArray()); |
| 0 | 137 | | } |
| 0 | 138 | | } |
| 0 | 139 | | catch (OperationCanceledException) { } |
| 0 | 140 | | catch (Exception e) { Debug.LogException(e); } |
| 0 | 141 | | } |
| | 142 | |
|
| 0 | 143 | | loadEmotesCts = loadEmotesCts.SafeRestart(); |
| 0 | 144 | | LoadEmotesAsync(loadEmotesCts.Token).Forget(); |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | public void RestoreEmoteSlots() => |
| 0 | 148 | | emotesCustomizationComponentController.RestoreEmoteSlots(); |
| | 149 | |
|
| | 150 | | public void SetEquippedBodyShape(string bodyShapeId) => |
| 0 | 151 | | emotesCustomizationComponentController.SetEquippedBodyShape(bodyShapeId); |
| | 152 | |
|
| | 153 | | // TODO: Delete? |
| | 154 | | private void NewEmoteAdded(string emoteId) => |
| 0 | 155 | | OnNewEmoteAdded?.Invoke(emoteId); |
| | 156 | |
|
| | 157 | | // TODO: Delete? |
| | 158 | | private void EmotePreviewed(string emoteId) => |
| 0 | 159 | | OnEmotePreviewed?.Invoke(emoteId); |
| | 160 | |
|
| | 161 | | // TODO: Delete? |
| | 162 | | private void EmoteEquipped(string emoteId) => |
| 0 | 163 | | OnEmoteEquipped?.Invoke(emoteId); |
| | 164 | |
|
| | 165 | | // TODO: Delete? |
| | 166 | | private void EmoteUnEquipped(string emoteId) => |
| 0 | 167 | | OnEmoteUnEquipped?.Invoke(emoteId); |
| | 168 | |
|
| | 169 | | private void EmoteSell(string collectibleId) |
| | 170 | | { |
| 0 | 171 | | var ownedCollectible = ownedNftCollectionsL1.FirstOrDefault(nft => nft.urn == collectibleId) ?? |
| 0 | 172 | | ownedNftCollectionsL2.FirstOrDefault(nft => nft.urn == collectibleId); |
| | 173 | |
|
| 0 | 174 | | WebInterface.OpenURL(ownedCollectible != null ? URL_SELL_SPECIFIC_COLLECTIBLE.Replace("{collectionId}", owne |
| 0 | 175 | | } |
| | 176 | |
|
| | 177 | | private async UniTask FetchCustomEmoteItems(ICollection<WearableItem> emotes, |
| | 178 | | CancellationToken cancellationToken) |
| | 179 | | { |
| 0 | 180 | | IReadOnlyList<string> customItems = await customNftCollectionService.GetConfiguredCustomNftItemsAsync(cancel |
| | 181 | |
|
| 0 | 182 | | WearableItem[] retrievedEmotes = await UniTask.WhenAll(customItems.Select(nftId => |
| 0 | 183 | | nftId.StartsWith("urn", StringComparison.OrdinalIgnoreCase) |
| | 184 | | ? emotesCatalogService.RequestEmoteAsync(nftId, cancellationToken) |
| | 185 | | : emotesCatalogService.RequestEmoteFromBuilderAsync(nftId, cancellationToken))); |
| | 186 | |
|
| 0 | 187 | | foreach (WearableItem emote in retrievedEmotes) |
| | 188 | | { |
| 0 | 189 | | if (emote == null) |
| | 190 | | { |
| 0 | 191 | | Debug.LogWarning("Custom emote item skipped is null"); |
| 0 | 192 | | continue; |
| | 193 | | } |
| | 194 | |
|
| 0 | 195 | | emotes.Add(emote); |
| | 196 | | } |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | private async UniTask FetchCustomEmoteCollections( |
| | 200 | | List<WearableItem> emotes, |
| | 201 | | CancellationToken cancellationToken) |
| | 202 | | { |
| 0 | 203 | | IReadOnlyList<string> customCollections = |
| | 204 | | await customNftCollectionService.GetConfiguredCustomNftCollectionAsync(cancellationToken); |
| | 205 | |
|
| 0 | 206 | | HashSet<string> publishedCollections = HashSetPool<string>.Get(); |
| 0 | 207 | | HashSet<string> collectionsInBuilder = HashSetPool<string>.Get(); |
| | 208 | |
|
| 0 | 209 | | foreach (string collectionId in customCollections) |
| | 210 | | { |
| 0 | 211 | | if (collectionId.StartsWith("urn", StringComparison.OrdinalIgnoreCase)) |
| 0 | 212 | | publishedCollections.Add(collectionId); |
| | 213 | | else |
| 0 | 214 | | collectionsInBuilder.Add(collectionId); |
| | 215 | | } |
| | 216 | |
|
| 0 | 217 | | await UniTask.WhenAll(emotesCatalogService.RequestEmoteCollectionAsync(publishedCollections, cancellationTok |
| | 218 | | emotesCatalogService.RequestEmoteCollectionInBuilderAsync(collectionsInBuilder, cancellationToken, emote |
| | 219 | |
|
| 0 | 220 | | HashSetPool<string>.Release(publishedCollections); |
| 0 | 221 | | HashSetPool<string>.Release(collectionsInBuilder); |
| 0 | 222 | | } |
| | 223 | | } |
| | 224 | | } |