< 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:321
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%6200%
AcceptFriendRequest()0%2100%
RejectFriendRequest()0%2100%
OnCurrentPlayerIdChanged(...)0%110100%
OnCurrentPlayerIdUpdated(...)0%550100%
<OnCurrentPlayerIdUpdated()0%3.333066.67%
SetUserProfile(...)0%110100%
>c__DisplayClass29_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
 034    private bool isNewFriendRequestsEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled("new_friend_requests"
 35    private double passportOpenStartTime = 0;
 36
 2037    public PlayerInfoCardHUDController(IFriendsController friendsController,
 38        StringVariable currentPlayerIdData,
 39        IUserProfileBridge userProfileBridge,
 40        IWearableCatalogBridge wearableCatalogBridge,
 41        ISocialAnalytics socialAnalytics,
 42        IProfanityFilter profanityFilter,
 43        DataStore dataStore,
 44        BooleanVariable playerInfoCardVisibleState)
 45    {
 2046        this.friendsController = friendsController;
 2047        view = PlayerInfoCardHUDView.CreateView();
 2048        view.Initialize(() => OnCloseButtonPressed(),
 49            ReportPlayer, BlockPlayer, UnblockPlayer,
 50            AddPlayerAsFriend, CancelInvitation, AcceptFriendRequest, RejectFriendRequest);
 2051        currentPlayerId = currentPlayerIdData;
 2052        this.userProfileBridge = userProfileBridge;
 2053        this.wearableCatalogBridge = wearableCatalogBridge;
 2054        this.socialAnalytics = socialAnalytics;
 2055        this.profanityFilter = profanityFilter;
 2056        this.dataStore = dataStore;
 2057        this.playerInfoCardVisibleState = playerInfoCardVisibleState;
 2058        currentPlayerId.OnSame += OnCurrentPlayerIdUpdated;
 2059        currentPlayerId.OnChange += OnCurrentPlayerIdChanged;
 2060        OnCurrentPlayerIdUpdated(currentPlayerId);
 61
 2062        toggleFriendsTrigger = Resources.Load<InputAction_Trigger>("ToggleFriends");
 2063        toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 2064        toggleFriendsTrigger.OnTriggered += OnCloseButtonPressed;
 65
 2066        closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 2067        closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 2068        closeWindowTrigger.OnTriggered += OnCloseButtonPressed;
 69
 2070        toggleWorldChatTrigger = Resources.Load<InputAction_Trigger>("ToggleWorldChat");
 2071        toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 2072        toggleWorldChatTrigger.OnTriggered += OnCloseButtonPressed;
 73
 2074        friendsController.OnUpdateFriendship -= OnFriendStatusUpdated;
 2075        friendsController.OnUpdateFriendship += OnFriendStatusUpdated;
 2076    }
 77
 78    public void CloseCard()
 79    {
 080        currentPlayerId.Set(null);
 081    }
 82
 83    private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow)
 84    {
 085        CloseCard();
 086    }
 87
 88    private void AddPlayerAsFriend()
 89    {
 090        if (isNewFriendRequestsEnabled)
 91        {
 092            dataStore.HUDs.sendFriendRequest.Set(currentPlayerId);
 093            dataStore.HUDs.sendFriendRequestSource.Set((int)PlayerActionSource.Passport);
 94        }
 95        else
 96        {
 097            friendsController.RequestFriendship(currentPlayerId);
 098            socialAnalytics.SendFriendRequestSent(ownUserProfile.userId, currentPlayerId, 0, PlayerActionSource.Passport
 99        }
 0100    }
 101
 102    private void CancelInvitation()
 103    {
 0104        if (isNewFriendRequestsEnabled)
 0105            friendsController.CancelRequestByUserIdAsync(currentPlayerId).Forget();
 106        else
 0107            friendsController.CancelRequestByUserId(currentPlayerId);
 108
 0109        socialAnalytics.SendFriendRequestCancelled(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0110    }
 111
 112    private void AcceptFriendRequest()
 113    {
 0114        friendsController.AcceptFriendship(currentPlayerId);
 0115        socialAnalytics.SendFriendRequestApproved(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0116    }
 117
 118    private void RejectFriendRequest()
 119    {
 0120        friendsController.RejectFriendship(currentPlayerId);
 0121        socialAnalytics.SendFriendRequestRejected(ownUserProfile.userId, currentPlayerId, PlayerActionSource.Passport);
 0122    }
 123
 124    private void OnCurrentPlayerIdChanged(string current, string previous)
 125    {
 25126        OnCurrentPlayerIdUpdated(current);
 25127    }
 128
 129    private void OnCurrentPlayerIdUpdated(string current)
 130    {
 45131        if (currentUserProfile != null)
 4132            currentUserProfile.OnUpdate -= SetUserProfile;
 133
 45134        currentUserProfile = string.IsNullOrEmpty(current)
 135            ? null
 136            : userProfileBridge.Get(current);
 137
 45138        if (currentUserProfile == null)
 139        {
 23140            if (playerInfoCardVisibleState.Get())
 3141                socialAnalytics.SendPassportClose(Time.realtimeSinceStartup - passportOpenStartTime);
 142
 23143            view.SetCardActive(false);
 23144            wearableCatalogBridge.RemoveWearablesInUse(loadedWearables);
 23145            loadedWearables.Clear();
 146        }
 147        else
 148        {
 22149            currentUserProfile.OnUpdate += SetUserProfile;
 150
 22151            TaskUtils.Run(async () =>
 152                     {
 22153                         await AsyncSetUserProfile(currentUserProfile);
 22154                         view.SetCardActive(true);
 22155                         socialAnalytics.SendPassportOpen();
 22156                     })
 157                     .Forget();
 158
 22159            passportOpenStartTime = Time.realtimeSinceStartup;
 160        }
 22161    }
 162
 163    private void SetUserProfile(UserProfile userProfile)
 164    {
 9165        Assert.IsTrue(userProfile != null, "userProfile can't be null");
 166
 18167        TaskUtils.Run(async () => await AsyncSetUserProfile(userProfile)).Forget();
 9168    }
 169    private async UniTask AsyncSetUserProfile(UserProfile userProfile)
 170    {
 31171        string filterName = await FilterName(userProfile);
 31172        string filterDescription = await FilterDescription(userProfile);
 31173        await UniTask.SwitchToMainThread();
 174
 31175        view.SetName(filterName);
 31176        view.SetDescription(filterDescription);
 31177        view.ClearCollectibles();
 31178        view.SetIsBlocked(IsBlocked(userProfile.userId));
 31179        LoadAndShowWearables(userProfile);
 31180        UpdateFriendshipInteraction();
 181
 31182        if (viewingUserProfile != null)
 13183            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 184
 31185        userProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 31186        viewingUserProfile = userProfile;
 31187    }
 188
 189    public void SetVisibility(bool visible)
 190    {
 1191        view.SetVisibility(visible);
 192
 1193        if (viewingUserProfile != null)
 0194            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 195
 1196        if (visible)
 197        {
 1198            if (viewingUserProfile != null)
 0199                viewingUserProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 200        }
 1201    }
 202
 203    private void BlockPlayer()
 204    {
 0205        if (ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0206        ownUserProfile.Block(currentUserProfile.userId);
 0207        view.SetIsBlocked(true);
 0208        WebInterface.SendBlockPlayer(currentUserProfile.userId);
 0209        socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pass
 0210    }
 211
 212    private void UnblockPlayer()
 213    {
 0214        if (!ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0215        ownUserProfile.Unblock(currentUserProfile.userId);
 0216        view.SetIsBlocked(false);
 0217        WebInterface.SendUnblockPlayer(currentUserProfile.userId);
 0218        socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pa
 0219    }
 220
 221    private void ReportPlayer()
 222    {
 0223        WebInterface.SendReportPlayer(currentPlayerId, currentUserProfile?.name);
 0224        socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport);
 0225    }
 226
 227    public void Dispose()
 228    {
 20229        if (currentUserProfile != null)
 18230            currentUserProfile.OnUpdate -= SetUserProfile;
 231
 20232        if (currentPlayerId != null)
 233        {
 20234            currentPlayerId.OnSame -= OnCurrentPlayerIdUpdated;
 20235            currentPlayerId.OnChange -= OnCurrentPlayerIdChanged;
 236        }
 237
 20238        if (closeWindowTrigger != null)
 20239            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 240
 20241        if (closeWindowTrigger != null)
 20242            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 243
 20244        if (toggleWorldChatTrigger != null)
 20245            toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 246
 20247        if (toggleFriendsTrigger != null)
 20248            toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 249
 20250        if (viewingUserProfile != null)
 18251            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 252
 20253        if (view != null)
 20254            Object.Destroy(view.gameObject);
 20255    }
 256
 257    private void OnFriendStatusUpdated(string userId, FriendshipAction action)
 258    {
 0259        if (currentUserProfile == null)
 0260            return;
 261
 0262        UpdateFriendshipInteraction();
 0263    }
 264
 265    private void UpdateFriendshipInteraction()
 266    {
 31267        if (currentUserProfile == null)
 268        {
 0269            view.HideFriendshipInteraction();
 0270            return;
 271        }
 272
 31273        view.UpdateFriendshipInteraction(CanBeFriends(),
 274            friendsController.GetUserStatus(currentUserProfile.userId));
 31275    }
 276
 277    private bool CanBeFriends()
 278    {
 31279        return friendsController != null && friendsController.IsInitialized && currentUserProfile.hasConnectedWeb3;
 280    }
 281
 282    private void LoadAndShowWearables(UserProfile userProfile)
 283    {
 31284        wearableCatalogBridge.RequestOwnedWearables(userProfile.userId)
 285                             .Then(wearables =>
 286                             {
 186287                                 var wearableIds = wearables.Select(x => x.id).ToArray();
 31288                                 userProfile.SetInventory(wearableIds);
 31289                                 loadedWearables.AddRange(wearableIds);
 31290                                 var containedWearables = wearables
 291                                     // this makes any sense?
 155292                                     .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id));
 31293                                 view.SetWearables(containedWearables);
 31294                             })
 295                             .Catch(Debug.LogError);
 31296    }
 297
 298    private bool IsBlocked(string userId)
 299    {
 31300        return ownUserProfile != null && ownUserProfile.IsBlocked(userId);
 301    }
 302
 303    private async UniTask<string> FilterName(UserProfile userProfile)
 304    {
 31305        return IsProfanityFilteringEnabled()
 306            ? await profanityFilter.Filter(userProfile.userName)
 307            : userProfile.userName;
 31308    }
 309
 310    private async UniTask<string> FilterDescription(UserProfile userProfile)
 311    {
 31312        return IsProfanityFilteringEnabled()
 313            ? await profanityFilter.Filter(userProfile.description)
 314            : userProfile.description;
 31315    }
 316
 317    private bool IsProfanityFilteringEnabled()
 318    {
 62319        return dataStore.settings.profanityChatFilteringEnabled.Get();
 320    }
 321}