< 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:45
Coverable lines:148
Total lines:332
Line coverage:69.5% (103 of 148)
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);
 95
 096        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage
 97        {
 98            userId = currentPlayerId, action = FriendshipAction.REQUESTED_TO
 99        });
 100
 0101        socialAnalytics.SendFriendRequestSent(ownUserProfile.userId, currentPlayerId, 0, PlayerActionSource.Passport);
 0102    }
 103
 104    private void CancelInvitation()
 105    {
 106        // Add fake action to avoid waiting for kernel
 0107        friendsController.CancelRequest(currentPlayerId);
 108
 0109        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 110        {
 111            userId = currentPlayerId, action = FriendshipAction.CANCELLED
 112        });
 113
 0114        socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0115    }
 116
 117    private void AcceptFriendRequest()
 118    {
 119        // Add fake action to avoid waiting for kernel
 0120        friendsController.AcceptFriendship(currentPlayerId);
 121
 0122        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 123        {
 124            userId = currentPlayerId, action = FriendshipAction.APPROVED
 125        });
 126
 0127        socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0128    }
 129
 130    private void RejectFriendRequest()
 131    {
 132        // Add fake action to avoid waiting for kernel
 0133        friendsController.RejectFriendship(currentPlayerId);
 134
 0135        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 136        {
 137            userId = currentPlayerId, action = FriendshipAction.REJECTED
 138        });
 139
 0140        socialAnalytics.SendFriendRequestRejected(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0141    }
 142
 143    private void OnCurrentPlayerIdChanged(string current, string previous)
 144    {
 45145        if (currentUserProfile != null)
 4146            currentUserProfile.OnUpdate -= SetUserProfile;
 147
 45148        currentUserProfile = string.IsNullOrEmpty(current)
 149            ? null
 150            : userProfileBridge.Get(current);
 151
 45152        if (currentUserProfile == null)
 153        {
 23154            if (playerInfoCardVisibleState.Get())
 3155                socialAnalytics.SendPassportClose(Time.realtimeSinceStartup - passportOpenStartTime);
 156
 23157            view.SetCardActive(false);
 23158            wearableCatalogBridge.RemoveWearablesInUse(loadedWearables);
 23159            loadedWearables.Clear();
 23160        }
 161        else
 162        {
 22163            currentUserProfile.OnUpdate += SetUserProfile;
 164
 22165            TaskUtils.Run(async () =>
 166                     {
 22167                         await AsyncSetUserProfile(currentUserProfile);
 22168                         view.SetCardActive(true);
 22169                         socialAnalytics.SendPassportOpen();
 22170                     })
 171                     .Forget();
 172
 22173            passportOpenStartTime = Time.realtimeSinceStartup;
 174        }
 22175    }
 176
 177    private void SetUserProfile(UserProfile userProfile)
 178    {
 9179        Assert.IsTrue(userProfile != null, "userProfile can't be null");
 180
 18181        TaskUtils.Run(async () => await AsyncSetUserProfile(userProfile)).Forget();
 9182    }
 183    private async UniTask AsyncSetUserProfile(UserProfile userProfile)
 184    {
 31185        string filterName = await FilterName(userProfile);
 31186        string filterDescription = await FilterDescription(userProfile);
 31187        await UniTask.SwitchToMainThread();
 188
 31189        view.SetName(filterName);
 31190        view.SetDescription(filterDescription);
 31191        view.ClearCollectibles();
 31192        view.SetIsBlocked(IsBlocked(userProfile.userId));
 31193        LoadAndShowWearables(userProfile);
 31194        UpdateFriendshipInteraction();
 195
 31196        if (viewingUserProfile != null)
 13197            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 198
 31199        userProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 31200        viewingUserProfile = userProfile;
 31201    }
 202
 203    public void SetVisibility(bool visible)
 204    {
 1205        view.SetVisibility(visible);
 206
 1207        if (viewingUserProfile != null)
 0208            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 209
 1210        if (visible)
 211        {
 1212            if (viewingUserProfile != null)
 0213                viewingUserProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 214        }
 1215    }
 216
 217    private void BlockPlayer()
 218    {
 0219        if (ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0220        ownUserProfile.Block(currentUserProfile.userId);
 0221        view.SetIsBlocked(true);
 0222        WebInterface.SendBlockPlayer(currentUserProfile.userId);
 0223        socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pass
 0224    }
 225
 226    private void UnblockPlayer()
 227    {
 0228        if (!ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0229        ownUserProfile.Unblock(currentUserProfile.userId);
 0230        view.SetIsBlocked(false);
 0231        WebInterface.SendUnblockPlayer(currentUserProfile.userId);
 0232        socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pa
 0233    }
 234
 235    private void ReportPlayer()
 236    {
 0237        WebInterface.SendReportPlayer(currentPlayerId, currentUserProfile?.name);
 0238        socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport);
 0239    }
 240
 241    public void Dispose()
 242    {
 20243        if (currentUserProfile != null)
 18244            currentUserProfile.OnUpdate -= SetUserProfile;
 245
 20246        if (currentPlayerId != null)
 20247            currentPlayerId.OnChange -= OnCurrentPlayerIdChanged;
 248
 20249        if (closeWindowTrigger != null)
 20250            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 251
 20252        if (closeWindowTrigger != null)
 20253            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 254
 20255        if (toggleWorldChatTrigger != null)
 20256            toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 257
 20258        if (toggleFriendsTrigger != null)
 20259            toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 260
 20261        if (viewingUserProfile != null)
 18262            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 263
 20264        if (view != null)
 20265            Object.Destroy(view.gameObject);
 20266    }
 267
 268    private void OnFriendStatusUpdated(string userId, FriendshipAction action)
 269    {
 0270        if (currentUserProfile == null)
 0271            return;
 272
 0273        UpdateFriendshipInteraction();
 0274    }
 275
 276    private void UpdateFriendshipInteraction()
 277    {
 31278        if (currentUserProfile == null)
 279        {
 0280            view.HideFriendshipInteraction();
 0281            return;
 282        }
 283
 31284        view.UpdateFriendshipInteraction(CanBeFriends(),
 285            friendsController.GetUserStatus(currentUserProfile.userId));
 31286    }
 287
 288    private bool CanBeFriends()
 289    {
 31290        return friendsController != null && friendsController.isInitialized && currentUserProfile.hasConnectedWeb3;
 291    }
 292
 293    private void LoadAndShowWearables(UserProfile userProfile)
 294    {
 31295        wearableCatalogBridge.RequestOwnedWearables(userProfile.userId)
 296                             .Then(wearables =>
 297                             {
 186298                                 var wearableIds = wearables.Select(x => x.id).ToArray();
 31299                                 userProfile.SetInventory(wearableIds);
 31300                                 loadedWearables.AddRange(wearableIds);
 31301                                 var containedWearables = wearables
 302                                     // this makes any sense?
 155303                                     .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id));
 31304                                 view.SetWearables(containedWearables);
 31305                             })
 306                             .Catch(Debug.LogError);
 31307    }
 308
 309    private bool IsBlocked(string userId)
 310    {
 31311        return ownUserProfile != null && ownUserProfile.IsBlocked(userId);
 312    }
 313
 314    private async UniTask<string> FilterName(UserProfile userProfile)
 315    {
 31316        return IsProfanityFilteringEnabled()
 317            ? await profanityFilter.Filter(userProfile.userName)
 318            : userProfile.userName;
 31319    }
 320
 321    private async UniTask<string> FilterDescription(UserProfile userProfile)
 322    {
 31323        return IsProfanityFilteringEnabled()
 324            ? await profanityFilter.Filter(userProfile.description)
 325            : userProfile.description;
 31326    }
 327
 328    private bool IsProfanityFilteringEnabled()
 329    {
 62330        return dataStore.settings.profanityChatFilteringEnabled.Get();
 331    }
 332}