< Summary

Class:PlayerInfoCardHUDController
Assembly:PlayerInfoCardHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PlayerInfoCardHUD/PlayerInfoCardHUDController.cs
Covered lines:103
Uncovered lines:41
Coverable lines:144
Total lines:305
Line coverage:71.5% (103 of 144)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerInfoCardHUDController(...)0%110100%
CloseCard()0%2100%
OnCloseButtonPressed(...)0%2100%
AddPlayerAsFriend()0%6200%
CancelInvitation()0%2100%
AcceptFriendRequest()0%2100%
RejectFriendRequest()0%2100%
OnCurrentPlayerIdChanged(...)0%550100%
<OnCurrentPlayerIdChanged()0%3.333066.67%
SetUserProfile(...)0%110100%
>c__DisplayClass26_0/<<SetUserProfile()0%5.673033.33%
AsyncSetUserProfile()0%9.738070%
SetVisibility(...)0%4.374071.43%
BlockPlayer()0%6200%
UnblockPlayer()0%6200%
ReportPlayer()0%12300%
Dispose()0%990100%
OnFriendStatusUpdated(...)0%6200%
UpdateFriendshipInteraction()0%2.262060%
CanBeFriends()0%330100%
LoadAndShowWearables(...)0%110100%
IsBlocked(...)0%220100%
FilterName()0%64050%
FilterDescription()0%64050%
IsProfanityFilteringEnabled()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PlayerInfoCardHUD/PlayerInfoCardHUDController.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Helpers;
 4using DCL.Interface;
 5using SocialFeaturesAnalytics;
 6using System.Collections.Generic;
 7using System.Linq;
 8using UnityEngine;
 9using UnityEngine.Assertions;
 10
 11public class PlayerInfoCardHUDController : IHUD
 12{
 13    internal readonly PlayerInfoCardHUDView view;
 14    internal readonly StringVariable currentPlayerId;
 15    internal UserProfile currentUserProfile;
 16
 17    private UserProfile viewingUserProfile;
 6218    private UserProfile ownUserProfile => userProfileBridge.GetOwn();
 19
 20    private readonly IFriendsController friendsController;
 21    private readonly InputAction_Trigger toggleFriendsTrigger;
 22    private readonly InputAction_Trigger closeWindowTrigger;
 23    private readonly InputAction_Trigger toggleWorldChatTrigger;
 24    private readonly IUserProfileBridge userProfileBridge;
 25    private readonly IWearableCatalogBridge wearableCatalogBridge;
 26    private readonly IProfanityFilter profanityFilter;
 27    private readonly DataStore dataStore;
 28    private readonly BooleanVariable playerInfoCardVisibleState;
 2029    private readonly List<string> loadedWearables = new List<string>();
 30    private readonly ISocialAnalytics socialAnalytics;
 31    private double passportOpenStartTime = 0;
 32
 2033    public PlayerInfoCardHUDController(IFriendsController friendsController,
 34        StringVariable currentPlayerIdData,
 35        IUserProfileBridge userProfileBridge,
 36        IWearableCatalogBridge wearableCatalogBridge,
 37        ISocialAnalytics socialAnalytics,
 38        IProfanityFilter profanityFilter,
 39        DataStore dataStore,
 40        BooleanVariable playerInfoCardVisibleState)
 41    {
 2042        this.friendsController = friendsController;
 2043        view = PlayerInfoCardHUDView.CreateView();
 2044        view.Initialize(() => OnCloseButtonPressed(),
 45            ReportPlayer, BlockPlayer, UnblockPlayer,
 46            AddPlayerAsFriend, CancelInvitation, AcceptFriendRequest, RejectFriendRequest);
 2047        currentPlayerId = currentPlayerIdData;
 2048        this.userProfileBridge = userProfileBridge;
 2049        this.wearableCatalogBridge = wearableCatalogBridge;
 2050        this.socialAnalytics = socialAnalytics;
 2051        this.profanityFilter = profanityFilter;
 2052        this.dataStore = dataStore;
 2053        this.playerInfoCardVisibleState = playerInfoCardVisibleState;
 2054        currentPlayerId.OnChange += OnCurrentPlayerIdChanged;
 2055        OnCurrentPlayerIdChanged(currentPlayerId, null);
 56
 2057        toggleFriendsTrigger = Resources.Load<InputAction_Trigger>("ToggleFriends");
 2058        toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 2059        toggleFriendsTrigger.OnTriggered += OnCloseButtonPressed;
 60
 2061        closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 2062        closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 2063        closeWindowTrigger.OnTriggered += OnCloseButtonPressed;
 64
 2065        toggleWorldChatTrigger = Resources.Load<InputAction_Trigger>("ToggleWorldChat");
 2066        toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 2067        toggleWorldChatTrigger.OnTriggered += OnCloseButtonPressed;
 68
 2069        friendsController.OnUpdateFriendship -= OnFriendStatusUpdated;
 2070        friendsController.OnUpdateFriendship += OnFriendStatusUpdated;
 2071    }
 72
 73    public void CloseCard()
 74    {
 075        currentPlayerId.Set(null);
 076    }
 77
 78    private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow)
 79    {
 080        CloseCard();
 081    }
 82
 83    private void AddPlayerAsFriend()
 84    {
 085        UserProfile currentUserProfile = userProfileBridge.Get(currentPlayerId);
 86
 87        // Add fake action to avoid waiting for kernel
 088        userProfileBridge.AddUserProfileToCatalog(new UserProfileModel
 89        {
 90            userId = currentPlayerId,
 91            name = currentUserProfile != null ? currentUserProfile.userName : currentPlayerId
 92        });
 93
 094        friendsController.RequestFriendship(currentPlayerId);
 095        socialAnalytics.SendFriendRequestSent(ownUserProfile.userId, currentPlayerId, 0, PlayerActionSource.Passport);
 096    }
 97
 98    private void CancelInvitation()
 99    {
 0100        friendsController.CancelRequest(currentPlayerId);
 0101        socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0102    }
 103
 104    private void AcceptFriendRequest()
 105    {
 0106        friendsController.AcceptFriendship(currentPlayerId);
 0107        socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0108    }
 109
 110    private void RejectFriendRequest()
 111    {
 0112        friendsController.RejectFriendship(currentPlayerId);
 0113        socialAnalytics.SendFriendRequestRejected(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0114    }
 115
 116    private void OnCurrentPlayerIdChanged(string current, string previous)
 117    {
 45118        if (currentUserProfile != null)
 4119            currentUserProfile.OnUpdate -= SetUserProfile;
 120
 45121        currentUserProfile = string.IsNullOrEmpty(current)
 122            ? null
 123            : userProfileBridge.Get(current);
 124
 45125        if (currentUserProfile == null)
 126        {
 23127            if (playerInfoCardVisibleState.Get())
 3128                socialAnalytics.SendPassportClose(Time.realtimeSinceStartup - passportOpenStartTime);
 129
 23130            view.SetCardActive(false);
 23131            wearableCatalogBridge.RemoveWearablesInUse(loadedWearables);
 23132            loadedWearables.Clear();
 23133        }
 134        else
 135        {
 22136            currentUserProfile.OnUpdate += SetUserProfile;
 137
 22138            TaskUtils.Run(async () =>
 139                     {
 22140                         await AsyncSetUserProfile(currentUserProfile);
 22141                         view.SetCardActive(true);
 22142                         socialAnalytics.SendPassportOpen();
 22143                     })
 144                     .Forget();
 145
 22146            passportOpenStartTime = Time.realtimeSinceStartup;
 147        }
 22148    }
 149
 150    private void SetUserProfile(UserProfile userProfile)
 151    {
 9152        Assert.IsTrue(userProfile != null, "userProfile can't be null");
 153
 18154        TaskUtils.Run(async () => await AsyncSetUserProfile(userProfile)).Forget();
 9155    }
 156    private async UniTask AsyncSetUserProfile(UserProfile userProfile)
 157    {
 31158        string filterName = await FilterName(userProfile);
 31159        string filterDescription = await FilterDescription(userProfile);
 31160        await UniTask.SwitchToMainThread();
 161
 31162        view.SetName(filterName);
 31163        view.SetDescription(filterDescription);
 31164        view.ClearCollectibles();
 31165        view.SetIsBlocked(IsBlocked(userProfile.userId));
 31166        LoadAndShowWearables(userProfile);
 31167        UpdateFriendshipInteraction();
 168
 31169        if (viewingUserProfile != null)
 13170            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 171
 31172        userProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 31173        viewingUserProfile = userProfile;
 31174    }
 175
 176    public void SetVisibility(bool visible)
 177    {
 1178        view.SetVisibility(visible);
 179
 1180        if (viewingUserProfile != null)
 0181            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 182
 1183        if (visible)
 184        {
 1185            if (viewingUserProfile != null)
 0186                viewingUserProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 187        }
 1188    }
 189
 190    private void BlockPlayer()
 191    {
 0192        if (ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0193        ownUserProfile.Block(currentUserProfile.userId);
 0194        view.SetIsBlocked(true);
 0195        WebInterface.SendBlockPlayer(currentUserProfile.userId);
 0196        socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pass
 0197    }
 198
 199    private void UnblockPlayer()
 200    {
 0201        if (!ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0202        ownUserProfile.Unblock(currentUserProfile.userId);
 0203        view.SetIsBlocked(false);
 0204        WebInterface.SendUnblockPlayer(currentUserProfile.userId);
 0205        socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pa
 0206    }
 207
 208    private void ReportPlayer()
 209    {
 0210        WebInterface.SendReportPlayer(currentPlayerId, currentUserProfile?.name);
 0211        socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport);
 0212    }
 213
 214    public void Dispose()
 215    {
 20216        if (currentUserProfile != null)
 18217            currentUserProfile.OnUpdate -= SetUserProfile;
 218
 20219        if (currentPlayerId != null)
 20220            currentPlayerId.OnChange -= OnCurrentPlayerIdChanged;
 221
 20222        if (closeWindowTrigger != null)
 20223            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 224
 20225        if (closeWindowTrigger != null)
 20226            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 227
 20228        if (toggleWorldChatTrigger != null)
 20229            toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 230
 20231        if (toggleFriendsTrigger != null)
 20232            toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 233
 20234        if (viewingUserProfile != null)
 18235            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 236
 20237        if (view != null)
 20238            Object.Destroy(view.gameObject);
 20239    }
 240
 241    private void OnFriendStatusUpdated(string userId, FriendshipAction action)
 242    {
 0243        if (currentUserProfile == null)
 0244            return;
 245
 0246        UpdateFriendshipInteraction();
 0247    }
 248
 249    private void UpdateFriendshipInteraction()
 250    {
 31251        if (currentUserProfile == null)
 252        {
 0253            view.HideFriendshipInteraction();
 0254            return;
 255        }
 256
 31257        view.UpdateFriendshipInteraction(CanBeFriends(),
 258            friendsController.GetUserStatus(currentUserProfile.userId));
 31259    }
 260
 261    private bool CanBeFriends()
 262    {
 31263        return friendsController != null && friendsController.IsInitialized && currentUserProfile.hasConnectedWeb3;
 264    }
 265
 266    private void LoadAndShowWearables(UserProfile userProfile)
 267    {
 31268        wearableCatalogBridge.RequestOwnedWearables(userProfile.userId)
 269                             .Then(wearables =>
 270                             {
 186271                                 var wearableIds = wearables.Select(x => x.id).ToArray();
 31272                                 userProfile.SetInventory(wearableIds);
 31273                                 loadedWearables.AddRange(wearableIds);
 31274                                 var containedWearables = wearables
 275                                     // this makes any sense?
 155276                                     .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id));
 31277                                 view.SetWearables(containedWearables);
 31278                             })
 279                             .Catch(Debug.LogError);
 31280    }
 281
 282    private bool IsBlocked(string userId)
 283    {
 31284        return ownUserProfile != null && ownUserProfile.IsBlocked(userId);
 285    }
 286
 287    private async UniTask<string> FilterName(UserProfile userProfile)
 288    {
 31289        return IsProfanityFilteringEnabled()
 290            ? await profanityFilter.Filter(userProfile.userName)
 291            : userProfile.userName;
 31292    }
 293
 294    private async UniTask<string> FilterDescription(UserProfile userProfile)
 295    {
 31296        return IsProfanityFilteringEnabled()
 297            ? await profanityFilter.Filter(userProfile.description)
 298            : userProfile.description;
 31299    }
 300
 301    private bool IsProfanityFilteringEnabled()
 302    {
 62303        return dataStore.settings.profanityChatFilteringEnabled.Get();
 304    }
 305}