< Summary

Class:PlayerInfoCardHUDController
Assembly:PlayerInfoCardHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PlayerInfoCardHUD/PlayerInfoCardHUDController.cs
Covered lines:106
Uncovered lines:41
Coverable lines:147
Total lines:316
Line coverage:72.1% (106 of 147)
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%110100%
OnCurrentPlayerIdUpdated(...)0%550100%
<OnCurrentPlayerIdUpdated()0%3.333066.67%
SetUserProfile(...)0%110100%
>c__DisplayClass27_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 DCl.Social.Friends;
 9using DCL.Social.Friends;
 10using UnityEngine;
 11using UnityEngine.Assertions;
 12
 13public class PlayerInfoCardHUDController : IHUD
 14{
 15    internal readonly PlayerInfoCardHUDView view;
 16    internal readonly StringVariable currentPlayerId;
 17    internal UserProfile currentUserProfile;
 18
 19    private UserProfile viewingUserProfile;
 6220    private UserProfile ownUserProfile => userProfileBridge.GetOwn();
 21
 22    private readonly IFriendsController friendsController;
 23    private readonly InputAction_Trigger toggleFriendsTrigger;
 24    private readonly InputAction_Trigger closeWindowTrigger;
 25    private readonly InputAction_Trigger toggleWorldChatTrigger;
 26    private readonly IUserProfileBridge userProfileBridge;
 27    private readonly IWearableCatalogBridge wearableCatalogBridge;
 28    private readonly IProfanityFilter profanityFilter;
 29    private readonly DataStore dataStore;
 30    private readonly BooleanVariable playerInfoCardVisibleState;
 2031    private readonly List<string> loadedWearables = new List<string>();
 32    private readonly ISocialAnalytics socialAnalytics;
 33    private double passportOpenStartTime = 0;
 34
 2035    public PlayerInfoCardHUDController(IFriendsController friendsController,
 36        StringVariable currentPlayerIdData,
 37        IUserProfileBridge userProfileBridge,
 38        IWearableCatalogBridge wearableCatalogBridge,
 39        ISocialAnalytics socialAnalytics,
 40        IProfanityFilter profanityFilter,
 41        DataStore dataStore,
 42        BooleanVariable playerInfoCardVisibleState)
 43    {
 2044        this.friendsController = friendsController;
 2045        view = PlayerInfoCardHUDView.CreateView();
 2046        view.Initialize(() => OnCloseButtonPressed(),
 47            ReportPlayer, BlockPlayer, UnblockPlayer,
 48            AddPlayerAsFriend, CancelInvitation, AcceptFriendRequest, RejectFriendRequest);
 2049        currentPlayerId = currentPlayerIdData;
 2050        this.userProfileBridge = userProfileBridge;
 2051        this.wearableCatalogBridge = wearableCatalogBridge;
 2052        this.socialAnalytics = socialAnalytics;
 2053        this.profanityFilter = profanityFilter;
 2054        this.dataStore = dataStore;
 2055        this.playerInfoCardVisibleState = playerInfoCardVisibleState;
 2056        currentPlayerId.OnSame += OnCurrentPlayerIdUpdated;
 2057        currentPlayerId.OnChange += OnCurrentPlayerIdChanged;
 2058        OnCurrentPlayerIdUpdated(currentPlayerId);
 59
 2060        toggleFriendsTrigger = Resources.Load<InputAction_Trigger>("ToggleFriends");
 2061        toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 2062        toggleFriendsTrigger.OnTriggered += OnCloseButtonPressed;
 63
 2064        closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 2065        closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 2066        closeWindowTrigger.OnTriggered += OnCloseButtonPressed;
 67
 2068        toggleWorldChatTrigger = Resources.Load<InputAction_Trigger>("ToggleWorldChat");
 2069        toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 2070        toggleWorldChatTrigger.OnTriggered += OnCloseButtonPressed;
 71
 2072        friendsController.OnUpdateFriendship -= OnFriendStatusUpdated;
 2073        friendsController.OnUpdateFriendship += OnFriendStatusUpdated;
 2074    }
 75
 76    public void CloseCard()
 77    {
 078        currentPlayerId.Set(null);
 079    }
 80
 81    private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow)
 82    {
 083        CloseCard();
 084    }
 85
 86    private void AddPlayerAsFriend()
 87    {
 088        UserProfile currentUserProfile = userProfileBridge.Get(currentPlayerId);
 89
 90        // Add fake action to avoid waiting for kernel
 091        userProfileBridge.AddUserProfileToCatalog(new UserProfileModel
 92        {
 93            userId = currentPlayerId,
 94            name = currentUserProfile != null ? currentUserProfile.userName : currentPlayerId
 95        });
 96
 097        friendsController.RequestFriendship(currentPlayerId);
 098        socialAnalytics.SendFriendRequestSent(ownUserProfile.userId, currentPlayerId, 0, PlayerActionSource.Passport);
 099    }
 100
 101    private void CancelInvitation()
 102    {
 0103        friendsController.CancelRequest(currentPlayerId);
 0104        socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0105    }
 106
 107    private void AcceptFriendRequest()
 108    {
 0109        friendsController.AcceptFriendship(currentPlayerId);
 0110        socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0111    }
 112
 113    private void RejectFriendRequest()
 114    {
 0115        friendsController.RejectFriendship(currentPlayerId);
 0116        socialAnalytics.SendFriendRequestRejected(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0117    }
 118
 119    private void OnCurrentPlayerIdChanged(string current, string previous)
 120    {
 25121        OnCurrentPlayerIdUpdated(current);
 25122    }
 123
 124    private void OnCurrentPlayerIdUpdated(string current)
 125    {
 45126        if (currentUserProfile != null)
 4127            currentUserProfile.OnUpdate -= SetUserProfile;
 128
 45129        currentUserProfile = string.IsNullOrEmpty(current)
 130            ? null
 131            : userProfileBridge.Get(current);
 132
 45133        if (currentUserProfile == null)
 134        {
 23135            if (playerInfoCardVisibleState.Get())
 4136                socialAnalytics.SendPassportClose(Time.realtimeSinceStartup - passportOpenStartTime);
 137
 23138            view.SetCardActive(false);
 23139            wearableCatalogBridge.RemoveWearablesInUse(loadedWearables);
 23140            loadedWearables.Clear();
 141        }
 142        else
 143        {
 22144            currentUserProfile.OnUpdate += SetUserProfile;
 145
 22146            TaskUtils.Run(async () =>
 147                     {
 22148                         await AsyncSetUserProfile(currentUserProfile);
 22149                         view.SetCardActive(true);
 22150                         socialAnalytics.SendPassportOpen();
 22151                     })
 152                     .Forget();
 153
 22154            passportOpenStartTime = Time.realtimeSinceStartup;
 155        }
 22156    }
 157
 158    private void SetUserProfile(UserProfile userProfile)
 159    {
 9160        Assert.IsTrue(userProfile != null, "userProfile can't be null");
 161
 18162        TaskUtils.Run(async () => await AsyncSetUserProfile(userProfile)).Forget();
 9163    }
 164    private async UniTask AsyncSetUserProfile(UserProfile userProfile)
 165    {
 31166        string filterName = await FilterName(userProfile);
 31167        string filterDescription = await FilterDescription(userProfile);
 31168        await UniTask.SwitchToMainThread();
 169
 31170        view.SetName(filterName);
 31171        view.SetDescription(filterDescription);
 31172        view.ClearCollectibles();
 31173        view.SetIsBlocked(IsBlocked(userProfile.userId));
 31174        LoadAndShowWearables(userProfile);
 31175        UpdateFriendshipInteraction();
 176
 31177        if (viewingUserProfile != null)
 13178            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 179
 31180        userProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 31181        viewingUserProfile = userProfile;
 31182    }
 183
 184    public void SetVisibility(bool visible)
 185    {
 1186        view.SetVisibility(visible);
 187
 1188        if (viewingUserProfile != null)
 0189            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 190
 1191        if (visible)
 192        {
 1193            if (viewingUserProfile != null)
 0194                viewingUserProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 195        }
 1196    }
 197
 198    private void BlockPlayer()
 199    {
 0200        if (ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0201        ownUserProfile.Block(currentUserProfile.userId);
 0202        view.SetIsBlocked(true);
 0203        WebInterface.SendBlockPlayer(currentUserProfile.userId);
 0204        socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pass
 0205    }
 206
 207    private void UnblockPlayer()
 208    {
 0209        if (!ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0210        ownUserProfile.Unblock(currentUserProfile.userId);
 0211        view.SetIsBlocked(false);
 0212        WebInterface.SendUnblockPlayer(currentUserProfile.userId);
 0213        socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pa
 0214    }
 215
 216    private void ReportPlayer()
 217    {
 0218        WebInterface.SendReportPlayer(currentPlayerId, currentUserProfile?.name);
 0219        socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport);
 0220    }
 221
 222    public void Dispose()
 223    {
 20224        if (currentUserProfile != null)
 18225            currentUserProfile.OnUpdate -= SetUserProfile;
 226
 20227        if (currentPlayerId != null)
 228        {
 20229            currentPlayerId.OnSame -= OnCurrentPlayerIdUpdated;
 20230            currentPlayerId.OnChange -= OnCurrentPlayerIdChanged;
 231        }
 232
 20233        if (closeWindowTrigger != null)
 20234            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 235
 20236        if (closeWindowTrigger != null)
 20237            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 238
 20239        if (toggleWorldChatTrigger != null)
 20240            toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 241
 20242        if (toggleFriendsTrigger != null)
 20243            toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 244
 20245        if (viewingUserProfile != null)
 18246            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 247
 20248        if (view != null)
 20249            Object.Destroy(view.gameObject);
 20250    }
 251
 252    private void OnFriendStatusUpdated(string userId, FriendshipAction action)
 253    {
 0254        if (currentUserProfile == null)
 0255            return;
 256
 0257        UpdateFriendshipInteraction();
 0258    }
 259
 260    private void UpdateFriendshipInteraction()
 261    {
 31262        if (currentUserProfile == null)
 263        {
 0264            view.HideFriendshipInteraction();
 0265            return;
 266        }
 267
 31268        view.UpdateFriendshipInteraction(CanBeFriends(),
 269            friendsController.GetUserStatus(currentUserProfile.userId));
 31270    }
 271
 272    private bool CanBeFriends()
 273    {
 31274        return friendsController != null && friendsController.IsInitialized && currentUserProfile.hasConnectedWeb3;
 275    }
 276
 277    private void LoadAndShowWearables(UserProfile userProfile)
 278    {
 31279        wearableCatalogBridge.RequestOwnedWearables(userProfile.userId)
 280                             .Then(wearables =>
 281                             {
 186282                                 var wearableIds = wearables.Select(x => x.id).ToArray();
 31283                                 userProfile.SetInventory(wearableIds);
 31284                                 loadedWearables.AddRange(wearableIds);
 31285                                 var containedWearables = wearables
 286                                     // this makes any sense?
 155287                                     .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id));
 31288                                 view.SetWearables(containedWearables);
 31289                             })
 290                             .Catch(Debug.LogError);
 31291    }
 292
 293    private bool IsBlocked(string userId)
 294    {
 31295        return ownUserProfile != null && ownUserProfile.IsBlocked(userId);
 296    }
 297
 298    private async UniTask<string> FilterName(UserProfile userProfile)
 299    {
 31300        return IsProfanityFilteringEnabled()
 301            ? await profanityFilter.Filter(userProfile.userName)
 302            : userProfile.userName;
 31303    }
 304
 305    private async UniTask<string> FilterDescription(UserProfile userProfile)
 306    {
 31307        return IsProfanityFilteringEnabled()
 308            ? await profanityFilter.Filter(userProfile.description)
 309            : userProfile.description;
 31310    }
 311
 312    private bool IsProfanityFilteringEnabled()
 313    {
 62314        return dataStore.settings.profanityChatFilteringEnabled.Get();
 315    }
 316}