< Summary

Class:PlayerInfoCardHUDController
Assembly:PlayerInfoCardHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/PlayerInfoCardHUD/PlayerInfoCardHUDController.cs
Covered lines:85
Uncovered lines:40
Coverable lines:125
Total lines:288
Line coverage:68% (85 of 125)
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%2100%
CancelInvitation()0%2100%
AcceptFriendRequest()0%2100%
RejectFriendRequest()0%2100%
OnCurrentPlayerIdChanged(...)0%5.164058.33%
SetUserProfile(...)0%220100%
SetVisibility(...)0%440100%
BlockPlayer()0%6200%
UnblockPlayer()0%6200%
ReportPlayer()0%2100%
Dispose()0%990100%
OnFriendStatusUpdated(...)0%6200%
UpdateFriendshipInteraction()0%2.262060%
CanBeFriends()0%330100%
LoadAndShowWearables(...)0%110100%
IsBlocked(...)0%220100%
FilterName(...)0%220100%
FilterDescription(...)0%220100%
IsProfanityFilteringEnabled()0%110100%

File(s)

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

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using DCL;
 4using DCL.Interface;
 5using UnityEngine;
 6using UnityEngine.Assertions;
 7
 8public class PlayerInfoCardHUDController : IHUD
 9{
 10    internal readonly PlayerInfoCardHUDView view;
 11    internal readonly StringVariable currentPlayerId;
 12    internal UserProfile currentUserProfile;
 13
 14    private UserProfile viewingUserProfile;
 6415    private UserProfile ownUserProfile => userProfileBridge.GetOwn();
 16
 17    private readonly IFriendsController friendsController;
 18    private readonly InputAction_Trigger toggleFriendsTrigger;
 19    private readonly InputAction_Trigger closeWindowTrigger;
 20    private readonly InputAction_Trigger toggleWorldChatTrigger;
 21    private readonly IUserProfileBridge userProfileBridge;
 22    private readonly IWearableCatalogBridge wearableCatalogBridge;
 23    private readonly RegexProfanityFilter profanityFilter;
 24    private readonly DataStore dataStore;
 1925    private readonly List<string> loadedWearables = new List<string>();
 26
 1927    public PlayerInfoCardHUDController(IFriendsController friendsController,
 28        StringVariable currentPlayerIdData,
 29        IUserProfileBridge userProfileBridge,
 30        IWearableCatalogBridge wearableCatalogBridge,
 31        RegexProfanityFilter profanityFilter,
 32        DataStore dataStore)
 33    {
 1934        this.friendsController = friendsController;
 1935        view = PlayerInfoCardHUDView.CreateView();
 1936        view.Initialize(() => OnCloseButtonPressed(),
 37            ReportPlayer, BlockPlayer, UnblockPlayer,
 38            AddPlayerAsFriend, CancelInvitation, AcceptFriendRequest, RejectFriendRequest);
 1939        currentPlayerId = currentPlayerIdData;
 1940        this.userProfileBridge = userProfileBridge;
 1941        this.wearableCatalogBridge = wearableCatalogBridge;
 1942        this.profanityFilter = profanityFilter;
 1943        this.dataStore = dataStore;
 1944        currentPlayerId.OnChange += OnCurrentPlayerIdChanged;
 1945        OnCurrentPlayerIdChanged(currentPlayerId, null);
 46
 1947        toggleFriendsTrigger = Resources.Load<InputAction_Trigger>("ToggleFriends");
 1948        toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 1949        toggleFriendsTrigger.OnTriggered += OnCloseButtonPressed;
 50
 1951        closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow");
 1952        closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 1953        closeWindowTrigger.OnTriggered += OnCloseButtonPressed;
 54
 1955        toggleWorldChatTrigger = Resources.Load<InputAction_Trigger>("ToggleWorldChat");
 1956        toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 1957        toggleWorldChatTrigger.OnTriggered += OnCloseButtonPressed;
 58
 1959        friendsController.OnUpdateFriendship -= OnFriendStatusUpdated;
 1960        friendsController.OnUpdateFriendship += OnFriendStatusUpdated;
 1961    }
 62
 63    public void CloseCard()
 64    {
 065        currentPlayerId.Set(null);
 066    }
 67
 68    private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow)
 69    {
 070        CloseCard();
 071    }
 72
 73    private void AddPlayerAsFriend()
 74    {
 75        // Add fake action to avoid waiting for kernel
 076        userProfileBridge.AddUserProfileToCatalog(new UserProfileModel
 77        {
 78            userId = currentPlayerId,
 79            name = currentPlayerId
 80        });
 081        friendsController.RequestFriendship(currentPlayerId);
 82
 083        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage
 84        {
 85            userId = currentPlayerId, action = FriendshipAction.REQUESTED_TO
 86        });
 087    }
 88
 89    private void CancelInvitation()
 90    {
 91        // Add fake action to avoid waiting for kernel
 092        friendsController.CancelRequest(currentPlayerId);
 93
 094        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 95        {
 96            userId = currentPlayerId, action = FriendshipAction.CANCELLED
 97        });
 098    }
 99
 100    private void AcceptFriendRequest()
 101    {
 102        // Add fake action to avoid waiting for kernel
 0103        friendsController.AcceptFriendship(currentPlayerId);
 104
 0105        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 106        {
 107            userId = currentPlayerId, action = FriendshipAction.APPROVED
 108        });
 0109    }
 110
 111    private void RejectFriendRequest()
 112    {
 113        // Add fake action to avoid waiting for kernel
 0114        friendsController.RejectFriendship(currentPlayerId);
 115
 0116        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 117        {
 118            userId = currentPlayerId, action = FriendshipAction.REJECTED
 119        });
 0120    }
 121
 122    private void OnCurrentPlayerIdChanged(string current, string previous)
 123    {
 19124        if (currentUserProfile != null)
 0125            currentUserProfile.OnUpdate -= SetUserProfile;
 126
 19127        currentUserProfile = string.IsNullOrEmpty(current)
 128            ? null
 129            : userProfileBridge.Get(current);
 130
 19131        if (currentUserProfile == null)
 132        {
 0133            view.SetCardActive(false);
 0134            wearableCatalogBridge.RemoveWearablesInUse(loadedWearables);
 0135            loadedWearables.Clear();
 0136        }
 137        else
 138        {
 19139            currentUserProfile.OnUpdate += SetUserProfile;
 19140            SetUserProfile(currentUserProfile);
 19141            view.SetCardActive(true);
 142        }
 19143    }
 144
 145    private void SetUserProfile(UserProfile userProfile)
 146    {
 32147        Assert.IsTrue(userProfile != null, "userProfile can't be null");
 148
 32149        view.SetName(FilterName(userProfile));
 32150        view.SetDescription(FilterDescription(userProfile));
 32151        view.ClearCollectibles();
 32152        view.SetIsBlocked(IsBlocked(userProfile.userId));
 32153        LoadAndShowWearables(userProfile);
 32154        UpdateFriendshipInteraction();
 155
 32156        if (viewingUserProfile != null)
 13157            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 32158        userProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 32159        viewingUserProfile = userProfile;
 32160    }
 161
 162    public void SetVisibility(bool visible)
 163    {
 1164        view.SetVisibility(visible);
 165
 1166        if (viewingUserProfile != null)
 1167            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 168
 1169        if (visible)
 170        {
 1171            if (viewingUserProfile != null)
 1172                viewingUserProfile.snapshotObserver.AddListener(view.SetFaceSnapshot);
 173        }
 1174    }
 175
 176    private void BlockPlayer()
 177    {
 0178        if (ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0179        ownUserProfile.Block(currentUserProfile.userId);
 0180        view.SetIsBlocked(true);
 0181        WebInterface.SendBlockPlayer(currentUserProfile.userId);
 0182    }
 183
 184    private void UnblockPlayer()
 185    {
 0186        if (!ownUserProfile.IsBlocked(currentUserProfile.userId)) return;
 0187        ownUserProfile.Unblock(currentUserProfile.userId);
 0188        view.SetIsBlocked(false);
 0189        WebInterface.SendUnblockPlayer(currentUserProfile.userId);
 0190    }
 191
 192    private void ReportPlayer()
 193    {
 0194        WebInterface.SendReportPlayer(currentPlayerId);
 0195    }
 196
 197    public void Dispose()
 198    {
 19199        if (currentUserProfile != null)
 19200            currentUserProfile.OnUpdate -= SetUserProfile;
 201
 19202        if (currentPlayerId != null)
 19203            currentPlayerId.OnChange -= OnCurrentPlayerIdChanged;
 204
 19205        if (closeWindowTrigger != null)
 19206            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 207
 19208        if (closeWindowTrigger != null)
 19209            closeWindowTrigger.OnTriggered -= OnCloseButtonPressed;
 210
 19211        if (toggleWorldChatTrigger != null)
 19212            toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed;
 213
 19214        if (toggleFriendsTrigger != null)
 19215            toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed;
 216
 19217        if (viewingUserProfile != null)
 19218            viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot);
 219
 19220        if (view != null)
 19221            Object.Destroy(view.gameObject);
 19222    }
 223
 224    private void OnFriendStatusUpdated(string userId, FriendshipAction action)
 225    {
 0226        if (currentUserProfile == null)
 0227            return;
 228
 0229        UpdateFriendshipInteraction();
 0230    }
 231
 232    private void UpdateFriendshipInteraction()
 233    {
 32234        if (currentUserProfile == null)
 235        {
 0236            view.HideFriendshipInteraction();
 0237            return;
 238        }
 239
 32240        view.UpdateFriendshipInteraction(CanBeFriends(),
 241            friendsController.GetUserStatus(currentUserProfile.userId));
 32242    }
 243
 244    private bool CanBeFriends()
 245    {
 32246        return friendsController != null && friendsController.isInitialized && currentUserProfile.hasConnectedWeb3;
 247    }
 248
 249    private void LoadAndShowWearables(UserProfile userProfile)
 250    {
 32251        wearableCatalogBridge.RequestOwnedWearables(userProfile.userId)
 252            .Then(wearables =>
 253            {
 181254                var wearableIds = wearables.Select(x => x.id).ToArray();
 31255                userProfile.SetInventory(wearableIds);
 31256                loadedWearables.AddRange(wearableIds);
 31257                var containedWearables = wearables
 258                    // this makes any sense?
 150259                    .Where(wearable => wearableCatalogBridge.IsValidWearable(wearable.id));
 31260                view.SetWearables(containedWearables);
 31261            })
 262            .Catch(Debug.LogError);
 32263    }
 264
 265    private bool IsBlocked(string userId)
 266    {
 32267        return ownUserProfile != null && ownUserProfile.IsBlocked(userId);
 268    }
 269
 270    private string FilterName(UserProfile userProfile)
 271    {
 32272        return IsProfanityFilteringEnabled()
 273            ? profanityFilter.Filter(userProfile.userName)
 274            : userProfile.userName;
 275    }
 276
 277    private string FilterDescription(UserProfile userProfile)
 278    {
 32279        return IsProfanityFilteringEnabled()
 280            ? profanityFilter.Filter(userProfile.description)
 281            : userProfile.description;
 282    }
 283
 284    private bool IsProfanityFilteringEnabled()
 285    {
 64286        return dataStore.settings.profanityChatFilteringEnabled.Get();
 287    }
 288}