| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Interface; |
| | 3 | | using SocialFeaturesAnalytics; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL.Social.Passports |
| | 10 | | { |
| | 11 | | public class PlayerPassportHUDController : IHUD |
| | 12 | | { |
| | 13 | | private const string URL_COLLECTIBLE_NAME = "https://market.decentraland.org/accounts/{userId}?section=ens"; |
| | 14 | | private const string URL_COLLECTIBLE_LAND = "https://market.decentraland.org/accounts/{userId}?section=land"; |
| | 15 | | private const string URL_BUY_SPECIFIC_COLLECTIBLE = "https://market.decentraland.org/contracts/{collectionId}/to |
| | 16 | | private const string URL_COLLECTIBLE_GENERIC = "https://market.decentraland.org?utm_source=dcl_explorer"; |
| | 17 | | private const string NAME_TYPE = "name"; |
| 0 | 18 | | private static readonly string[] ALLOWED_TYPES = { NAME_TYPE, "parcel", "estate" }; |
| | 19 | |
|
| | 20 | | private readonly IPlayerPassportHUDView view; |
| | 21 | | private readonly StringVariable currentPlayerId; |
| | 22 | | private readonly IUserProfileBridge userProfileBridge; |
| | 23 | | private readonly IPassportApiBridge passportApiBridge; |
| | 24 | | private readonly ISocialAnalytics socialAnalytics; |
| | 25 | | private readonly DataStore dataStore; |
| | 26 | | private readonly InputAction_Trigger closeWindowTrigger; |
| | 27 | | private readonly PassportPlayerInfoComponentController playerInfoController; |
| | 28 | | private readonly PassportPlayerPreviewComponentController playerPreviewController; |
| | 29 | | private readonly PassportNavigationComponentController passportNavigationController; |
| | 30 | | private readonly BooleanVariable playerInfoCardVisibleState; |
| | 31 | |
|
| | 32 | | private UserProfile currentUserProfile; |
| 0 | 33 | | private List<Nft> ownedNftCollectionsL1 = new (); |
| 0 | 34 | | private List<Nft> ownedNftCollectionsL2 = new (); |
| | 35 | | private double passportOpenStartTime; |
| 0 | 36 | | private CancellationTokenSource cts = new CancellationTokenSource(); |
| | 37 | |
|
| | 38 | | private bool isOpen; |
| | 39 | |
|
| 0 | 40 | | public PlayerPassportHUDController( |
| | 41 | | IPlayerPassportHUDView view, |
| | 42 | | PassportPlayerInfoComponentController playerInfoController, |
| | 43 | | PassportPlayerPreviewComponentController playerPreviewController, |
| | 44 | | PassportNavigationComponentController passportNavigationController, |
| | 45 | | StringVariable currentPlayerId, |
| | 46 | | IUserProfileBridge userProfileBridge, |
| | 47 | | IPassportApiBridge passportApiBridge, |
| | 48 | | ISocialAnalytics socialAnalytics, |
| | 49 | | DataStore dataStore, |
| | 50 | | MouseCatcher mouseCatcher, |
| | 51 | | BooleanVariable playerInfoCardVisibleState) |
| | 52 | | { |
| 0 | 53 | | this.view = view; |
| 0 | 54 | | this.playerInfoController = playerInfoController; |
| 0 | 55 | | this.playerPreviewController = playerPreviewController; |
| 0 | 56 | | this.passportNavigationController = passportNavigationController; |
| 0 | 57 | | this.currentPlayerId = currentPlayerId; |
| 0 | 58 | | this.userProfileBridge = userProfileBridge; |
| 0 | 59 | | this.passportApiBridge = passportApiBridge; |
| 0 | 60 | | this.socialAnalytics = socialAnalytics; |
| 0 | 61 | | this.dataStore = dataStore; |
| 0 | 62 | | this.playerInfoCardVisibleState = playerInfoCardVisibleState; |
| | 63 | |
|
| 0 | 64 | | view.Initialize(mouseCatcher); |
| 0 | 65 | | view.OnClose += ClosePassport; |
| | 66 | |
|
| 0 | 67 | | closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow"); |
| 0 | 68 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| 0 | 69 | | closeWindowTrigger.OnTriggered += OnCloseButtonPressed; |
| | 70 | |
|
| 0 | 71 | | passportNavigationController.OnClickBuyNft += ClickedBuyNft; |
| 0 | 72 | | passportNavigationController.OnClickedLink += ClickedLink; |
| 0 | 73 | | passportNavigationController.OnClickCollectibles += ClickedCollectibles; |
| | 74 | |
|
| 0 | 75 | | currentPlayerId.OnChange += OnCurrentPlayerIdChanged; |
| 0 | 76 | | OnCurrentPlayerIdChanged(currentPlayerId, null); |
| | 77 | |
|
| 0 | 78 | | playerInfoController.OnClosePassport += ClosePassport; |
| 0 | 79 | | dataStore.HUDs.closedWalletModal.OnChange += ClosedGuestWalletPanel; |
| 0 | 80 | | dataStore.HUDs.currentPassportSortingOrder.Set(view.PassportCurrentSortingOrder); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | private void ClosedGuestWalletPanel(bool current, bool previous) |
| | 84 | | { |
| 0 | 85 | | if (current) |
| | 86 | | { |
| 0 | 87 | | ClosePassport(); |
| 0 | 88 | | dataStore.HUDs.closedWalletModal.Set(false, false); |
| | 89 | | } |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | private void ClosePassport() |
| | 93 | | { |
| 0 | 94 | | if(!isOpen) |
| 0 | 95 | | return; |
| | 96 | |
|
| 0 | 97 | | isOpen = false; |
| | 98 | |
|
| 0 | 99 | | passportNavigationController.CloseAllNFTItemInfos(); |
| 0 | 100 | | passportNavigationController.SetViewInitialPage(); |
| 0 | 101 | | playerInfoController.ClosePassport(); |
| 0 | 102 | | currentPlayerId.Set(null); |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Called from <see cref="HUDBridge"/> |
| | 107 | | /// so it just should control the root object visibility |
| | 108 | | /// </summary> |
| | 109 | | public void SetVisibility(bool visible) |
| | 110 | | { |
| 0 | 111 | | view.SetVisibility(visible); |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow) |
| | 115 | | { |
| 0 | 116 | | ClosePassport(); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public void Dispose() |
| | 120 | | { |
| 0 | 121 | | cts?.Cancel(); |
| 0 | 122 | | cts?.Dispose(); |
| 0 | 123 | | cts = null; |
| | 124 | |
|
| 0 | 125 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| 0 | 126 | | currentPlayerId.OnChange -= OnCurrentPlayerIdChanged; |
| 0 | 127 | | playerInfoController.OnClosePassport -= ClosePassport; |
| 0 | 128 | | dataStore.HUDs.closedWalletModal.OnChange -= ClosedGuestWalletPanel; |
| | 129 | |
|
| 0 | 130 | | playerInfoController.Dispose(); |
| 0 | 131 | | playerPreviewController.Dispose(); |
| 0 | 132 | | passportNavigationController.Dispose(); |
| | 133 | |
|
| 0 | 134 | | view?.Dispose(); |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | private void OnCurrentPlayerIdChanged(string current, string previous) |
| | 138 | | { |
| 0 | 139 | | if (currentUserProfile != null) |
| 0 | 140 | | currentUserProfile.OnUpdate -= UpdateUserProfile; |
| | 141 | |
|
| 0 | 142 | | ownedNftCollectionsL1 = new List<Nft>(); |
| 0 | 143 | | ownedNftCollectionsL2 = new List<Nft>(); |
| 0 | 144 | | currentUserProfile = string.IsNullOrEmpty(current) |
| | 145 | | ? null |
| | 146 | | : userProfileBridge.Get(current); |
| | 147 | |
|
| 0 | 148 | | if (currentUserProfile == null) |
| | 149 | | { |
| 0 | 150 | | socialAnalytics.SendPassportClose(Time.realtimeSinceStartup - passportOpenStartTime); |
| 0 | 151 | | SetPassportPanelVisibility(false); |
| | 152 | | } |
| | 153 | | else |
| | 154 | | { |
| 0 | 155 | | SetPassportPanelVisibility(true); |
| 0 | 156 | | passportOpenStartTime = Time.realtimeSinceStartup; |
| 0 | 157 | | socialAnalytics.SendPassportOpen(); |
| 0 | 158 | | QueryNftCollectionsAsync(currentUserProfile.userId).Forget(); |
| 0 | 159 | | userProfileBridge.RequestFullUserProfile(currentUserProfile.userId); |
| 0 | 160 | | currentUserProfile.OnUpdate += UpdateUserProfile; |
| 0 | 161 | | UpdateUserProfile(currentUserProfile, true); |
| | 162 | | } |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | private void SetPassportPanelVisibility(bool visible) |
| | 166 | | { |
| 0 | 167 | | isOpen = visible; |
| | 168 | |
|
| 0 | 169 | | if (visible && userProfileBridge.GetOwn().isGuest) |
| | 170 | | { |
| 0 | 171 | | dataStore.HUDs.connectWalletModalVisible.Set(true); |
| | 172 | | } |
| 0 | 173 | | playerInfoCardVisibleState.Set(visible); |
| 0 | 174 | | view.SetPassportPanelVisibility(visible); |
| 0 | 175 | | playerPreviewController.SetPassportPanelVisibility(visible); |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | private async UniTask QueryNftCollectionsAsync(string userId) |
| | 179 | | { |
| 0 | 180 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 181 | | return; |
| | 182 | |
|
| 0 | 183 | | (ownedNftCollectionsL1, ownedNftCollectionsL2) = await UniTask.WhenAll(passportApiBridge.QueryNftCollections |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | private void ClickedLink() |
| | 187 | | { |
| 0 | 188 | | socialAnalytics.SendLinkClick(PlayerActionSource.Passport); |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | private void ClickedBuyNft(string id, string wearableType) |
| | 192 | | { |
| | 193 | | async UniTaskVoid QueryNftCollectionByUrnAsync(string urn) |
| | 194 | | { |
| 0 | 195 | | var nft = await passportApiBridge.QueryNftCollectionAsync(currentUserProfile.userId, urn, NftCollections |
| | 196 | |
|
| 0 | 197 | | if (nft == null) |
| 0 | 198 | | nft = await passportApiBridge.QueryNftCollectionAsync(currentUserProfile.userId, urn, NftCollections |
| | 199 | |
|
| 0 | 200 | | if (nft != null) |
| 0 | 201 | | OpenNftMarketUrl(nft); |
| | 202 | | else |
| 0 | 203 | | passportApiBridge.OpenURL(URL_COLLECTIBLE_GENERIC); |
| 0 | 204 | | } |
| | 205 | |
|
| 0 | 206 | | if (ALLOWED_TYPES.Contains(wearableType)) |
| | 207 | | { |
| 0 | 208 | | passportApiBridge.OpenURL((wearableType is NAME_TYPE ? URL_COLLECTIBLE_NAME : URL_COLLECTIBLE_LAND).Repl |
| 0 | 209 | | socialAnalytics.SendNftBuy(PlayerActionSource.Passport); |
| 0 | 210 | | return; |
| | 211 | | } |
| | 212 | |
|
| 0 | 213 | | var ownedCollectible = ownedNftCollectionsL1.FirstOrDefault(nft => nft.urn == id); |
| 0 | 214 | | if (ownedCollectible == null) |
| 0 | 215 | | ownedCollectible = ownedNftCollectionsL2.FirstOrDefault(nft => nft.urn == id); |
| | 216 | |
|
| 0 | 217 | | if (ownedCollectible != null) |
| 0 | 218 | | OpenNftMarketUrl(ownedCollectible); |
| | 219 | | else |
| | 220 | | { |
| 0 | 221 | | cts?.Cancel(); |
| 0 | 222 | | cts?.Dispose(); |
| 0 | 223 | | cts = new CancellationTokenSource(); |
| | 224 | |
|
| | 225 | | // In case the NFT's information is not found neither on ownedNftCollectionsL1 nor or ownedNftCollection |
| | 226 | | // to the TheGraph queries only return a maximum of 100 entries by default), we request the information |
| 0 | 227 | | QueryNftCollectionByUrnAsync(id).Forget(); |
| | 228 | | } |
| 0 | 229 | | } |
| | 230 | |
|
| | 231 | | private void OpenNftMarketUrl(Nft nft) |
| | 232 | | { |
| 0 | 233 | | passportApiBridge.OpenURL(URL_BUY_SPECIFIC_COLLECTIBLE.Replace("{collectionId}", nft.collectionId).Replace(" |
| | 234 | | //TODO: integrate ItemType itemType once new lambdas are active |
| 0 | 235 | | socialAnalytics.SendNftBuy(PlayerActionSource.Passport); |
| 0 | 236 | | } |
| | 237 | |
|
| | 238 | | private void ClickedCollectibles() |
| | 239 | | { |
| 0 | 240 | | socialAnalytics.SendClickedOnCollectibles(); |
| 0 | 241 | | } |
| | 242 | |
|
| | 243 | | private void UpdateUserProfile(UserProfile userProfile) |
| | 244 | | { |
| 0 | 245 | | UpdateUserProfile(userProfile, false); |
| 0 | 246 | | } |
| | 247 | |
|
| | 248 | | private void UpdateUserProfile(UserProfile userProfile, bool activateLoading) |
| | 249 | | { |
| 0 | 250 | | playerPreviewController.UpdateWithUserProfile(userProfile, activateLoading); |
| 0 | 251 | | playerInfoController.UpdateWithUserProfile(userProfile); |
| 0 | 252 | | passportNavigationController.UpdateWithUserProfile(userProfile); |
| 0 | 253 | | } |
| | 254 | |
|
| | 255 | | } |
| | 256 | | } |