< Summary

Class:DCL.Social.Passports.PlayerPassportHUDController
Assembly:PassportHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Passport/Passport/PlayerPassportHUDController.cs
Covered lines:0
Uncovered lines:113
Coverable lines:113
Total lines:246
Line coverage:0% (0 of 113)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:16
Method coverage:0% (0 of 16)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerPassportHUDController()0%2100%
PlayerPassportHUDController(...)0%2100%
ClosePassport()0%12300%
SetVisibility(...)0%2100%
OnCloseButtonPressed(...)0%2100%
Dispose()0%20400%
OnCurrentPlayerIdChanged(...)0%20400%
SetPassportPanelVisibility(...)0%2100%
QueryNftCollectionsAsync()0%20400%
ClickedLink()0%2100%
>c__DisplayClass34_0/<<ClickedBuyNft()0%56700%
ClickedBuyNft(...)0%72800%
OpenNftMarketUrl(...)0%2100%
ClickedCollectibles()0%2100%
UpdateUserProfile(...)0%2100%
UpdateUserProfile(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Passport/Passport/PlayerPassportHUDController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using SocialFeaturesAnalytics;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using System.Threading;
 7using UnityEngine;
 8
 9namespace 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";
 020        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;
 035        private List<Nft> ownedNftCollectionsL1 = new ();
 036        private List<Nft> ownedNftCollectionsL2 = new ();
 37        private double passportOpenStartTime;
 038        private CancellationTokenSource cts = new CancellationTokenSource();
 39
 40        private bool isOpen;
 41
 042        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        {
 054            this.view = view;
 055            this.playerInfoController = playerInfoController;
 056            this.playerPreviewController = playerPreviewController;
 057            this.passportNavigationController = passportNavigationController;
 058            this.userProfileBridge = userProfileBridge;
 059            this.passportApiBridge = passportApiBridge;
 060            this.socialAnalytics = socialAnalytics;
 061            this.dataStore = dataStore;
 062            this.playerInfoCardVisibleState = playerInfoCardVisibleState;
 063            this.currentPlayerId = dataStore.HUDs.currentPlayerId;
 64
 065            view.Initialize(mouseCatcher);
 066            view.OnClose += ClosePassport;
 67
 068            closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 069            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 070            closeWindowTrigger.OnTriggered += OnCloseButtonPressed;
 71
 072            passportNavigationController.OnClickBuyNft += ClickedBuyNft;
 073            passportNavigationController.OnClickedLink += ClickedLink;
 074            passportNavigationController.OnClickCollectibles += ClickedCollectibles;
 75
 076            currentPlayerId.OnChange += OnCurrentPlayerIdChanged;
 077            OnCurrentPlayerIdChanged(currentPlayerId.Get(), currentPlayerId.Get());
 78
 079            playerInfoController.OnClosePassport += ClosePassport;
 080            dataStore.HUDs.currentPassportSortingOrder.Set(view.PassportCurrentSortingOrder);
 081        }
 82
 83        private void ClosePassport()
 84        {
 085            if(!isOpen)
 086                return;
 87
 088            isOpen = false;
 89
 090            if (userProfileBridge.GetOwn().isGuest)
 091                dataStore.HUDs.connectWalletModalVisible.Set(false);
 92
 093            passportNavigationController.CloseAllNFTItemInfos();
 094            passportNavigationController.ResetNavigationTab();
 095            playerInfoController.ClosePassport();
 096            currentPlayerId.Set((null, null));
 097        }
 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        {
 0105            view.SetVisibility(visible);
 0106        }
 107
 108        private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow)
 109        {
 0110            ClosePassport();
 0111        }
 112
 113        public void Dispose()
 114        {
 0115            cts?.Cancel();
 0116            cts?.Dispose();
 0117            cts = null;
 118
 0119            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 0120            currentPlayerId.OnChange -= OnCurrentPlayerIdChanged;
 0121            playerInfoController.OnClosePassport -= ClosePassport;
 122
 0123            playerInfoController.Dispose();
 0124            playerPreviewController.Dispose();
 0125            passportNavigationController.Dispose();
 126
 0127            view?.Dispose();
 0128        }
 129
 130        private void OnCurrentPlayerIdChanged((string playerId, string source) current, (string playerId, string source)
 131        {
 0132            if (currentUserProfile != null)
 0133                currentUserProfile.OnUpdate -= UpdateUserProfile;
 134
 0135            ownedNftCollectionsL1 = new List<Nft>();
 0136            ownedNftCollectionsL2 = new List<Nft>();
 0137            currentUserProfile = string.IsNullOrEmpty(current.playerId)
 138                ? null
 139                : userProfileBridge.Get(current.playerId);
 140
 0141            if (currentUserProfile == null)
 142            {
 0143                socialAnalytics.SendPassportClose(previous.playerId, Time.realtimeSinceStartup - passportOpenStartTime);
 0144                SetPassportPanelVisibility(false);
 145            }
 146            else
 147            {
 0148                SetPassportPanelVisibility(true);
 0149                passportOpenStartTime = Time.realtimeSinceStartup;
 0150                Enum.TryParse(current.source, out AvatarOpenSource source);
 0151                socialAnalytics.SendPassportOpen(current.playerId, source: source);
 0152                QueryNftCollectionsAsync(currentUserProfile.userId).Forget();
 0153                userProfileBridge.RequestFullUserProfile(currentUserProfile.userId);
 0154                currentUserProfile.OnUpdate += UpdateUserProfile;
 0155                UpdateUserProfile(currentUserProfile, true);
 156            }
 0157        }
 158
 159        private void SetPassportPanelVisibility(bool visible)
 160        {
 0161            isOpen = visible;
 162
 0163            playerInfoCardVisibleState.Set(visible);
 0164            view.SetPassportPanelVisibility(visible);
 0165            playerPreviewController.SetPassportPanelVisibility(visible);
 0166        }
 167
 168        private async UniTask QueryNftCollectionsAsync(string userId)
 169        {
 0170            if (string.IsNullOrEmpty(userId))
 0171                return;
 172
 0173            (ownedNftCollectionsL1, ownedNftCollectionsL2) = await UniTask.WhenAll(passportApiBridge.QueryNftCollections
 0174        }
 175
 176        private void ClickedLink()
 177        {
 0178            socialAnalytics.SendLinkClick(PlayerActionSource.Passport);
 0179        }
 180
 181        private void ClickedBuyNft(string id, string wearableType)
 182        {
 183            async UniTaskVoid QueryNftCollectionByUrnAsync(string urn)
 184            {
 0185                var nft = await passportApiBridge.QueryNftCollectionAsync(currentUserProfile.userId, urn, NftCollections
 186
 0187                if (nft == null)
 0188                    nft = await passportApiBridge.QueryNftCollectionAsync(currentUserProfile.userId, urn, NftCollections
 189
 0190                if (nft != null)
 0191                    OpenNftMarketUrl(nft);
 192                else
 0193                    passportApiBridge.OpenURL(URL_COLLECTIBLE_GENERIC);
 0194            }
 195
 0196            if (ALLOWED_TYPES.Contains(wearableType))
 197            {
 0198                passportApiBridge.OpenURL((wearableType is NAME_TYPE ? URL_COLLECTIBLE_NAME : URL_COLLECTIBLE_LAND).Repl
 0199                socialAnalytics.SendNftBuy(PlayerActionSource.Passport);
 0200                return;
 201            }
 202
 0203            var ownedCollectible = ownedNftCollectionsL1.FirstOrDefault(nft => nft.urn == id);
 0204            if (ownedCollectible == null)
 0205                ownedCollectible = ownedNftCollectionsL2.FirstOrDefault(nft => nft.urn == id);
 206
 0207            if (ownedCollectible != null)
 0208                OpenNftMarketUrl(ownedCollectible);
 209            else
 210            {
 0211                cts?.Cancel();
 0212                cts?.Dispose();
 0213                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 
 0217                QueryNftCollectionByUrnAsync(id).Forget();
 218            }
 0219        }
 220
 221        private void OpenNftMarketUrl(Nft nft)
 222        {
 0223            passportApiBridge.OpenURL(URL_BUY_SPECIFIC_COLLECTIBLE.Replace("{collectionId}", nft.collectionId).Replace("
 224            //TODO: integrate ItemType itemType once new lambdas are active
 0225            socialAnalytics.SendNftBuy(PlayerActionSource.Passport);
 0226        }
 227
 228        private void ClickedCollectibles()
 229        {
 0230            socialAnalytics.SendClickedOnCollectibles();
 0231        }
 232
 233        private void UpdateUserProfile(UserProfile userProfile)
 234        {
 0235            UpdateUserProfile(userProfile, false);
 0236        }
 237
 238        private void UpdateUserProfile(UserProfile userProfile, bool activateLoading)
 239        {
 0240            playerPreviewController.UpdateWithUserProfile(userProfile, activateLoading);
 0241            playerInfoController.UpdateWithUserProfile(userProfile);
 0242            passportNavigationController.UpdateWithUserProfile(userProfile);
 0243        }
 244
 245    }
 246}