< 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:45
Coverable lines:151
Total lines:341
Line coverage:70.1% (106 of 151)
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 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.OnSame += OnCurrentPlayerIdUpdated;
 2055        currentPlayerId.OnChange += OnCurrentPlayerIdChanged;
 2056        OnCurrentPlayerIdUpdated(currentPlayerId);
 57
 2058        toggleFriendsTrigger = Resources.Load<InputAction_Trigger>("ToggleFriends");
 2059        toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 2060        toggleFriendsTrigger.OnTriggered += OnCloseButtonPressed;
 61
 2062        closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 2063        closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 2064        closeWindowTrigger.OnTriggered += OnCloseButtonPressed;
 65
 2066        toggleWorldChatTrigger = Resources.Load<InputAction_Trigger>("ToggleWorldChat");
 2067        toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 2068        toggleWorldChatTrigger.OnTriggered += OnCloseButtonPressed;
 69
 2070        friendsController.OnUpdateFriendship -= OnFriendStatusUpdated;
 2071        friendsController.OnUpdateFriendship += OnFriendStatusUpdated;
 2072    }
 73
 74    public void CloseCard()
 75    {
 076        currentPlayerId.Set(null);
 077    }
 78
 79    private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow)
 80    {
 081        CloseCard();
 082    }
 83
 84    private void AddPlayerAsFriend()
 85    {
 086        UserProfile currentUserProfile = userProfileBridge.Get(currentPlayerId);
 87
 88        // Add fake action to avoid waiting for kernel
 089        userProfileBridge.AddUserProfileToCatalog(new UserProfileModel
 90        {
 91            userId = currentPlayerId,
 92            name = currentUserProfile != null ? currentUserProfile.userName : currentPlayerId
 93        });
 94
 095        friendsController.RequestFriendship(currentPlayerId);
 96
 097        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage
 98        {
 99            userId = currentPlayerId, action = FriendshipAction.REQUESTED_TO
 100        });
 101
 0102        socialAnalytics.SendFriendRequestSent(ownUserProfile.userId, currentPlayerId, 0, PlayerActionSource.Passport);
 0103    }
 104
 105    private void CancelInvitation()
 106    {
 107        // Add fake action to avoid waiting for kernel
 0108        friendsController.CancelRequest(currentPlayerId);
 109
 0110        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 111        {
 112            userId = currentPlayerId, action = FriendshipAction.CANCELLED
 113        });
 114
 0115        socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0116    }
 117
 118    private void AcceptFriendRequest()
 119    {
 120        // Add fake action to avoid waiting for kernel
 0121        friendsController.AcceptFriendship(currentPlayerId);
 122
 0123        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 124        {
 125            userId = currentPlayerId, action = FriendshipAction.APPROVED
 126        });
 127
 0128        socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0129    }
 130
 131    private void RejectFriendRequest()
 132    {
 133        // Add fake action to avoid waiting for kernel
 0134        friendsController.RejectFriendship(currentPlayerId);
 135
 0136        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 137        {
 138            userId = currentPlayerId, action = FriendshipAction.REJECTED
 139        });
 140
 0141        socialAnalytics.SendFriendRequestRejected(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0142    }
 143
 144    private void OnCurrentPlayerIdChanged(string current, string previous)
 145    {
 25146        OnCurrentPlayerIdUpdated(current);
 25147    }
 148
 149    private void OnCurrentPlayerIdUpdated(string current)
 150    {
 45151        if (currentUserProfile != null)
 4152            currentUserProfile.OnUpdate -= SetUserProfile;
 153
 45154        currentUserProfile = string.IsNullOrEmpty(current)
 155            ? null
 156            : userProfileBridge.Get(current);
 157
 45158        if (currentUserProfile == null)
 159        {
 23160            if (playerInfoCardVisibleState.Get())
 20161                socialAnalytics.SendPassportClose(Time.realtimeSinceStartup - passportOpenStartTime);
 162
 23163            view.SetCardActive(false);
 23164            wearableCatalogBridge.RemoveWearablesInUse(loadedWearables);
 23165            loadedWearables.Clear();
 166        }
 167        else
 168        {
 22169            currentUserProfile.OnUpdate += SetUserProfile;
 170
 22171            TaskUtils.Run(async () =>
 172                     {
 22173                         await AsyncSetUserProfile(currentUserProfile);
 22174                         view.SetCardActive(true);
 22175                         socialAnalytics.SendPassportOpen();
 22176                     })
 177                     .Forget();
 178
 22179            passportOpenStartTime = Time.realtimeSinceStartup;
 180        }
 22181    }
 182
 183    private void SetUserProfile(UserProfile userProfile)
 184    {
 9185        Assert.IsTrue(userProfile != null, "userProfile can't be null");
 186
 18187        TaskUtils.Run(async () => await AsyncSetUserProfile(userProfile)).Forget();
 9188    }
 189    private async UniTask AsyncSetUserProfile(UserProfile userProfile)
 190    {
 31191        string filterName = await FilterName(userProfile);
 31192        string filterDescription = await FilterDescription(userProfile);
 31193        await UniTask.SwitchToMainThread();
 194
 31195        view.SetName(filterName);
 31196        view.SetDescription(filterDescription);
 31197        view.ClearCollectibles();
 31198        view.SetIsBlocked(IsBlocked(userProfile.userId));
 31199        LoadAndShowWearables(userProfile);
 31200        UpdateFriendshipInteraction();
 201
 31202        if (viewingUserProfile != null)
 13203            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 204
 31205        userProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 31206        viewingUserProfile = userProfile;
 31207    }
 208
 209    public void SetVisibility(bool visible)
 210    {
 1211        view.SetVisibility(visible);
 212
 1213        if (viewingUserProfile != null)
 0214            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 215
 1216        if (visible)
 217        {
 1218            if (viewingUserProfile != null)
 0219                viewingUserProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 220        }
 1221    }
 222
 223    private void BlockPlayer()
 224    {
 0225        if (ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0226        ownUserProfile.Block(currentUserProfile.userId);
 0227        view.SetIsBlocked(true);
 0228        WebInterface.SendBlockPlayer(currentUserProfile.userId);
 0229        socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pass
 0230    }
 231
 232    private void UnblockPlayer()
 233    {
 0234        if (!ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0235        ownUserProfile.Unblock(currentUserProfile.userId);
 0236        view.SetIsBlocked(false);
 0237        WebInterface.SendUnblockPlayer(currentUserProfile.userId);
 0238        socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pa
 0239    }
 240
 241    private void ReportPlayer()
 242    {
 0243        WebInterface.SendReportPlayer(currentPlayerId, currentUserProfile?.name);
 0244        socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport);
 0245    }
 246
 247    public void Dispose()
 248    {
 20249        if (currentUserProfile != null)
 18250            currentUserProfile.OnUpdate -= SetUserProfile;
 251
 20252        if (currentPlayerId != null)
 253        {
 20254            currentPlayerId.OnSame -= OnCurrentPlayerIdUpdated;
 20255            currentPlayerId.OnChange -= OnCurrentPlayerIdChanged;
 256        }
 257
 20258        if (closeWindowTrigger != null)
 20259            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 260
 20261        if (closeWindowTrigger != null)
 20262            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 263
 20264        if (toggleWorldChatTrigger != null)
 20265            toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 266
 20267        if (toggleFriendsTrigger != null)
 20268            toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 269
 20270        if (viewingUserProfile != null)
 18271            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 272
 20273        if (view != null)
 20274            Object.Destroy(view.gameObject);
 20275    }
 276
 277    private void OnFriendStatusUpdated(string userId, FriendshipAction action)
 278    {
 0279        if (currentUserProfile == null)
 0280            return;
 281
 0282        UpdateFriendshipInteraction();
 0283    }
 284
 285    private void UpdateFriendshipInteraction()
 286    {
 31287        if (currentUserProfile == null)
 288        {
 0289            view.HideFriendshipInteraction();
 0290            return;
 291        }
 292
 31293        view.UpdateFriendshipInteraction(CanBeFriends(),
 294            friendsController.GetUserStatus(currentUserProfile.userId));
 31295    }
 296
 297    private bool CanBeFriends()
 298    {
 31299        return friendsController != null && friendsController.isInitialized && currentUserProfile.hasConnectedWeb3;
 300    }
 301
 302    private void LoadAndShowWearables(UserProfile userProfile)
 303    {
 31304        wearableCatalogBridge.RequestOwnedWearables(userProfile.userId)
 305                             .Then(wearables =>
 306                             {
 186307                                 var wearableIds = wearables.Select(x => x.id).ToArray();
 31308                                 userProfile.SetInventory(wearableIds);
 31309                                 loadedWearables.AddRange(wearableIds);
 31310                                 var containedWearables = wearables
 311                                     // this makes any sense?
 155312                                     .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id));
 31313                                 view.SetWearables(containedWearables);
 31314                             })
 315                             .Catch(Debug.LogError);
 31316    }
 317
 318    private bool IsBlocked(string userId)
 319    {
 31320        return ownUserProfile != null && ownUserProfile.IsBlocked(userId);
 321    }
 322
 323    private async UniTask<string> FilterName(UserProfile userProfile)
 324    {
 31325        return IsProfanityFilteringEnabled()
 326            ? await profanityFilter.Filter(userProfile.userName)
 327            : userProfile.userName;
 31328    }
 329
 330    private async UniTask<string> FilterDescription(UserProfile userProfile)
 331    {
 31332        return IsProfanityFilteringEnabled()
 333            ? await profanityFilter.Filter(userProfile.description)
 334            : userProfile.description;
 31335    }
 336
 337    private bool IsProfanityFilteringEnabled()
 338    {
 62339        return dataStore.settings.profanityChatFilteringEnabled.Get();
 340    }
 341}