< Summary

Class:UsersAroundListHUDController
Assembly:UsersAroundListHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/UsersAroundListHUD/UsersAroundListHUDController.cs
Covered lines:55
Uncovered lines:57
Coverable lines:112
Total lines:261
Line coverage:49.1% (55 of 112)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UsersAroundListHUDController(...)0%110100%
UsersAroundListHUDController(...)0%2100%
Dispose()0%4.034087.5%
SetVisibility(...)0%2100%
SetButtonView(...)0%2100%
SetUsersMuted(...)0%6200%
SetUserRecording(...)0%2100%
Initialize(...)0%110100%
OnOtherPlayersStatusAdded(...)0%7.297081.82%
OnOtherPlayerStatusRemoved(...)0%330100%
ToggleVisibility()0%2100%
OnMuteUser(...)0%30500%
OnMuteUsers(...)0%12300%
OnMuteAll(...)0%6200%
OnGoToCrowd()0%2100%
OnListOpen()0%6200%
OnRendererStateChanged(...)0%12300%
OnUserProfileUpdate(...)0%20400%
ReportMuteStatuses()0%3.213071.43%
MuteStateUpdateRoutine()0%12300%
ToggleUsersCount(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/UsersAroundListHUD/UsersAroundListHUDController.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using DCL;
 4using DCL.Interface;
 5using SocialFeaturesAnalytics;
 6using UnityEngine;
 7
 8public class UsersAroundListHUDController : IHUD
 9{
 10    const float MUTE_STATUS_UPDATE_INTERVAL = 0.3f;
 11
 12    internal IUsersAroundListHUDButtonView usersButtonView;
 13    internal IUsersAroundListHUDListView usersListView;
 14    internal ISocialAnalytics socialAnalytics;
 15
 16    private bool isVisible = false;
 317    private readonly HashSet<string> trackedUsersHashSet = new HashSet<string>();
 1518    private UserProfile profile => UserProfile.GetOwnUserProfile();
 19
 320    private readonly List<string> usersToMute = new List<string>();
 321    private readonly List<string> usersToUnmute = new List<string>();
 22    private bool isMuteAll = false;
 23    private Coroutine updateMuteStatusRoutine = null;
 24
 1225    private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers;
 26
 27    public event System.Action OnOpen;
 28
 329    public UsersAroundListHUDController(ISocialAnalytics socialAnalytics)
 30    {
 331        UsersAroundListHUDListView view = Object.Instantiate(Resources.Load<GameObject>("UsersAroundListHUD")).GetCompon
 332        view.name = "_UsersAroundListHUD";
 333        view.gameObject.SetActive(false);
 334        Initialize(view, socialAnalytics);
 335    }
 36
 037    public UsersAroundListHUDController(IUsersAroundListHUDListView usersListView, ISocialAnalytics socialAnalytics) { I
 38
 39    /// <summary>
 40    /// Dispose HUD controller
 41    /// </summary>
 42    public void Dispose()
 43    {
 344        ReportMuteStatuses();
 45
 346        if (updateMuteStatusRoutine != null)
 47        {
 048            CoroutineStarter.Stop(updateMuteStatusRoutine);
 49        }
 50
 351        otherPlayers.OnAdded -= OnOtherPlayersStatusAdded;
 352        otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved;
 53
 354        CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged;
 355        profile.OnUpdate -= OnUserProfileUpdate;
 56
 357        if (usersListView != null)
 58        {
 359            usersListView.OnRequestMuteUser -= OnMuteUser;
 360            usersListView.OnRequestMuteGlobal -= OnMuteAll;
 361            usersListView.OnGoToCrowdPressed -= OnGoToCrowd;
 362            usersListView.OnOpen -= OnListOpen;
 63
 364            usersListView.Dispose();
 65        }
 66
 367        if (usersButtonView != null)
 68        {
 069            usersButtonView.OnClick -= ToggleVisibility;
 70        }
 371    }
 72
 73    /// <summary>
 74    /// Set HUD's visibility
 75    /// </summary>
 76    /// <param name="visible"></param>
 77    public void SetVisibility(bool visible)
 78    {
 079        isVisible = visible;
 080        usersListView.SetVisibility(visible);
 081    }
 82
 83    /// <summary>
 84    /// Set button to toggle HUD visibility and display users count
 85    /// </summary>
 86    /// <param name="view">Button view</param>
 87    public void SetButtonView(IUsersAroundListHUDButtonView view)
 88    {
 089        usersButtonView = view;
 090        usersButtonView.OnClick += ToggleVisibility;
 091    }
 92
 93    /// <summary>
 94    /// Set mute status for users' id
 95    /// </summary>
 96    /// <param name="usersId">Array of user ids</param>
 97    /// <param name="isMuted">Set if users should be mute or unmute</param>
 98    public void SetUsersMuted(string[] usersId, bool isMuted)
 99    {
 0100        for (int i = 0; i < usersId.Length; i++)
 101        {
 0102            usersListView.SetUserMuted(usersId[i], isMuted);
 103        }
 0104    }
 105
 106    /// <summary>
 107    /// Set user status as "talking"
 108    /// </summary>
 109    /// <param name="userId">User's id</param>
 110    /// <param name="isRecording">Set user status as "talking" or "not talking"</param>
 0111    public void SetUserRecording(string userId, bool isRecording) { usersListView.SetUserRecording(userId, isRecording);
 112
 113    void Initialize(IUsersAroundListHUDListView view, ISocialAnalytics socialAnalytics)
 114    {
 3115        usersListView = view;
 3116        this.socialAnalytics = socialAnalytics;
 117
 3118        usersListView.OnRequestMuteUser += OnMuteUser;
 3119        usersListView.OnRequestMuteGlobal += OnMuteAll;
 3120        usersListView.OnGoToCrowdPressed += OnGoToCrowd;
 3121        usersListView.OnOpen += OnListOpen;
 122
 3123        otherPlayers.OnAdded += OnOtherPlayersStatusAdded;
 3124        otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved;
 125
 3126        CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged;
 3127        profile.OnUpdate += OnUserProfileUpdate;
 3128    }
 129
 130    void OnOtherPlayersStatusAdded(string userId, Player player)
 131    {
 3132        usersListView.AddOrUpdatePlayer(player);
 133
 3134        if (!trackedUsersHashSet.Contains(userId))
 135        {
 3136            trackedUsersHashSet.Add(userId);
 137
 3138            bool isMuted = profile.muted.Contains(userId);
 3139            bool isBlocked = profile.blocked != null ? profile.blocked.Contains(userId) : false;
 140
 3141            usersListView.SetUserMuted(userId, isMuted);
 3142            usersListView.SetUserBlocked(userId, isBlocked);
 143
 3144            if (isMuteAll && !isMuted)
 145            {
 0146                OnMuteUser(userId, true);
 147            }
 148        }
 149
 3150        usersButtonView?.SetUsersCount(trackedUsersHashSet.Count);
 0151    }
 152
 153    void OnOtherPlayerStatusRemoved(string userId, Player player)
 154    {
 2155        if (trackedUsersHashSet.Contains(userId))
 156        {
 2157            trackedUsersHashSet.Remove(userId);
 2158            usersButtonView?.SetUsersCount(trackedUsersHashSet.Count);
 159        }
 2160        usersListView.RemoveUser(userId);
 2161    }
 162
 163    void ToggleVisibility()
 164    {
 0165        bool setVisible = !isVisible;
 0166        SetVisibility(setVisible);
 0167    }
 168
 169    void OnMuteUser(string userId, bool mute)
 170    {
 0171        var list = mute ? usersToMute : usersToUnmute;
 0172        list.Add(userId);
 173
 0174        if (updateMuteStatusRoutine == null)
 175        {
 0176            updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine());
 177        }
 178
 0179        if (mute)
 0180            socialAnalytics.SendPlayerMuted(userId);
 181        else
 0182            socialAnalytics.SendPlayerUnmuted(userId);
 0183    }
 184
 185    void OnMuteUsers(IEnumerable<string> usersId, bool mute)
 186    {
 0187        using (var iterator = usersId.GetEnumerator())
 188        {
 0189            while (iterator.MoveNext())
 190            {
 0191                OnMuteUser(iterator.Current, mute);
 192            }
 0193        }
 0194    }
 195
 196    void OnMuteAll(bool mute)
 197    {
 0198        isMuteAll = mute;
 199
 0200        if (mute)
 201        {
 0202            usersToUnmute.Clear();
 203        }
 204        else
 205        {
 0206            usersToMute.Clear();
 207        }
 0208        OnMuteUsers(trackedUsersHashSet, mute);
 0209    }
 210
 211    void OnGoToCrowd()
 212    {
 0213        DCL.Environment.i.world.teleportController.GoToCrowd();
 0214    }
 215
 0216    void OnListOpen() { OnOpen?.Invoke(); }
 217
 218    private void OnRendererStateChanged(bool isEnable, bool prevState)
 219    {
 0220        if (isEnable || !isVisible)
 0221            return;
 222
 0223        SetVisibility(false);
 0224    }
 225
 226    private void OnUserProfileUpdate(UserProfile profile)
 227    {
 0228        using (var iterator = trackedUsersHashSet.GetEnumerator())
 229        {
 0230            while (iterator.MoveNext())
 231            {
 0232                usersListView.SetUserBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contains(iterat
 233            }
 0234        }
 0235    }
 236
 237    void ReportMuteStatuses()
 238    {
 3239        if (usersToUnmute.Count > 0)
 240        {
 0241            WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false);
 242        }
 3243        if (usersToMute.Count > 0)
 244        {
 0245            WebInterface.SetMuteUsers(usersToMute.ToArray(), true);
 246        }
 3247        usersToUnmute.Clear();
 3248        usersToMute.Clear();
 3249    }
 250
 251    IEnumerator MuteStateUpdateRoutine()
 252    {
 0253        yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL);
 0254        ReportMuteStatuses();
 0255        updateMuteStatusRoutine = null;
 0256    }
 257    public void ToggleUsersCount(bool isEnabled)
 258    {
 0259        usersButtonView.ToggleUsersCount(isEnabled);
 0260    }
 261}