| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL.Interface; |
| | 5 | | using SocialFeaturesAnalytics; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | | using System; |
| | 8 | |
|
| | 9 | | namespace DCL.Social.Passports |
| | 10 | | { |
| | 11 | | public class PlayerPassportHUDController : IHUD |
| | 12 | | { |
| | 13 | | internal readonly IPlayerPassportHUDView view; |
| | 14 | | internal readonly StringVariable currentPlayerId; |
| | 15 | | internal readonly IUserProfileBridge userProfileBridge; |
| | 16 | | private readonly IPassportApiBridge passportApiBridge; |
| | 17 | | private readonly ISocialAnalytics socialAnalytics; |
| | 18 | |
|
| | 19 | | internal UserProfile currentUserProfile; |
| | 20 | |
|
| | 21 | | private const string URL_BUY_SPECIFIC_COLLECTIBLE = "https://market.decentraland.org/contracts/{collectionId}/to |
| | 22 | | private const string URL_COLLECTIBLE_GENERIC = "https://market.decentraland.org/account"; |
| | 23 | | private readonly InputAction_Trigger closeWindowTrigger; |
| | 24 | |
|
| | 25 | | private PassportPlayerInfoComponentController playerInfoController; |
| | 26 | | private PassportPlayerPreviewComponentController playerPreviewController; |
| | 27 | | private PassportNavigationComponentController passportNavigationController; |
| | 28 | |
|
| 0 | 29 | | private List<Nft> ownedNftCollectionsL1 = new List<Nft>(); |
| 0 | 30 | | private List<Nft> ownedNftCollectionsL2 = new List<Nft>(); |
| | 31 | |
|
| 0 | 32 | | public PlayerPassportHUDController( |
| | 33 | | IPlayerPassportHUDView view, |
| | 34 | | PassportPlayerInfoComponentController playerInfoController, |
| | 35 | | PassportPlayerPreviewComponentController playerPreviewController, |
| | 36 | | PassportNavigationComponentController passportNavigationController, |
| | 37 | | StringVariable currentPlayerId, |
| | 38 | | IUserProfileBridge userProfileBridge, |
| | 39 | | IPassportApiBridge passportApiBridge, |
| | 40 | | ISocialAnalytics socialAnalytics) |
| | 41 | | { |
| 0 | 42 | | this.view = view; |
| 0 | 43 | | this.playerInfoController = playerInfoController; |
| 0 | 44 | | this.playerPreviewController = playerPreviewController; |
| 0 | 45 | | this.passportNavigationController = passportNavigationController; |
| 0 | 46 | | this.currentPlayerId = currentPlayerId; |
| 0 | 47 | | this.userProfileBridge = userProfileBridge; |
| 0 | 48 | | this.passportApiBridge = passportApiBridge; |
| 0 | 49 | | this.socialAnalytics = socialAnalytics; |
| | 50 | |
|
| 0 | 51 | | view.Initialize(); |
| 0 | 52 | | view.OnClose += RemoveCurrentPlayer; |
| | 53 | |
|
| 0 | 54 | | closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow"); |
| 0 | 55 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| 0 | 56 | | closeWindowTrigger.OnTriggered += OnCloseButtonPressed; |
| | 57 | |
|
| 0 | 58 | | passportNavigationController.OnClickBuyNft += ClickedBuyNft; |
| | 59 | |
|
| 0 | 60 | | currentPlayerId.OnChange += OnCurrentPlayerIdChanged; |
| 0 | 61 | | OnCurrentPlayerIdChanged(currentPlayerId, null); |
| | 62 | |
|
| 0 | 63 | | playerInfoController.OnClosePassport += ClosePassport; |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void ClosePassport() |
| | 67 | | { |
| 0 | 68 | | RemoveCurrentPlayer(); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Called from <see cref="HUDBridge"/> |
| | 73 | | /// so it just should control the root object visibility |
| | 74 | | /// </summary> |
| | 75 | | public void SetVisibility(bool visible) |
| | 76 | | { |
| 0 | 77 | | view.SetVisibility(visible); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow) |
| | 81 | | { |
| 0 | 82 | | ClosePassport(); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public void Dispose() |
| | 86 | | { |
| 0 | 87 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| 0 | 88 | | currentPlayerId.OnChange -= OnCurrentPlayerIdChanged; |
| 0 | 89 | | playerInfoController.OnClosePassport -= ClosePassport; |
| | 90 | |
|
| 0 | 91 | | playerPreviewController.Dispose(); |
| | 92 | |
|
| 0 | 93 | | if (view != null) |
| 0 | 94 | | view.Dispose(); |
| 0 | 95 | | } |
| | 96 | |
|
| | 97 | | private void OnCurrentPlayerIdChanged(string current, string previous) |
| | 98 | | { |
| 0 | 99 | | if (currentUserProfile != null) |
| 0 | 100 | | currentUserProfile.OnUpdate -= UpdateUserProfile; |
| | 101 | |
|
| 0 | 102 | | ownedNftCollectionsL1 = new List<Nft>(); |
| 0 | 103 | | ownedNftCollectionsL2 = new List<Nft>(); |
| 0 | 104 | | currentUserProfile = string.IsNullOrEmpty(current) |
| | 105 | | ? null |
| | 106 | | : userProfileBridge.Get(current); |
| | 107 | |
|
| 0 | 108 | | if (currentUserProfile == null) |
| | 109 | | { |
| 0 | 110 | | SetPassportPanelVisibility(false); |
| | 111 | | } |
| | 112 | | else |
| | 113 | | { |
| 0 | 114 | | SetPassportPanelVisibility(true); |
| 0 | 115 | | QueryNftCollectionsAsync(currentUserProfile.userId); |
| 0 | 116 | | userProfileBridge.RequestFullUserProfile(currentUserProfile.userId); |
| 0 | 117 | | currentUserProfile.OnUpdate += UpdateUserProfile; |
| 0 | 118 | | UpdateUserProfileInSubpanels(currentUserProfile); |
| | 119 | | } |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | private void SetPassportPanelVisibility(bool visible) |
| | 123 | | { |
| 0 | 124 | | view.SetPassportPanelVisibility(visible); |
| 0 | 125 | | playerPreviewController.SetPassportPanelVisibility(visible); |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private async Task QueryNftCollectionsAsync(string userId) |
| | 129 | | { |
| 0 | 130 | | if (string.IsNullOrEmpty(userId)) |
| 0 | 131 | | return; |
| | 132 | |
|
| 0 | 133 | | ownedNftCollectionsL1 = await passportApiBridge.QueryNftCollectionsEthereum(userId); |
| 0 | 134 | | ownedNftCollectionsL2 = await passportApiBridge.QueryNftCollectionsMatic(userId); |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | private void ClickedBuyNft(string wearableId) |
| | 138 | | { |
| 0 | 139 | | var ownedCollectible = ownedNftCollectionsL1.FirstOrDefault(nft => nft.urn == wearableId); |
| 0 | 140 | | if (ownedCollectible == null) |
| 0 | 141 | | ownedCollectible = ownedNftCollectionsL2.FirstOrDefault(nft => nft.urn == wearableId); |
| | 142 | |
|
| 0 | 143 | | if (ownedCollectible != null) |
| 0 | 144 | | WebInterface.OpenURL(URL_BUY_SPECIFIC_COLLECTIBLE.Replace("{collectionId}", ownedCollectible.collectionI |
| | 145 | | else |
| 0 | 146 | | WebInterface.OpenURL(URL_COLLECTIBLE_GENERIC); |
| 0 | 147 | | } |
| | 148 | |
|
| 0 | 149 | | private void UpdateUserProfile(UserProfile userProfile) => UpdateUserProfileInSubpanels(userProfile); |
| | 150 | |
|
| | 151 | | private void UpdateUserProfileInSubpanels(UserProfile userProfile) |
| | 152 | | { |
| 0 | 153 | | playerInfoController.UpdateWithUserProfile(userProfile); |
| 0 | 154 | | passportNavigationController.UpdateWithUserProfile(userProfile); |
| 0 | 155 | | playerPreviewController.UpdateWithUserProfile(userProfile); |
| 0 | 156 | | } |
| | 157 | |
|
| | 158 | | private void RemoveCurrentPlayer() |
| | 159 | | { |
| 0 | 160 | | currentPlayerId.Set(null); |
| 0 | 161 | | } |
| | 162 | |
|
| | 163 | | } |
| | 164 | | } |