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