< Summary

Class:VoiceChatWindowController
Assembly:VoiceChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/VoiceChatHUD/VoiceChatWindowController.cs
Covered lines:192
Uncovered lines:22
Coverable lines:214
Total lines:427
Line coverage:89.7% (192 of 214)
Covered branches:0
Total branches:0
Covered methods:34
Total methods:39
Method coverage:87.1% (34 of 39)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
VoiceChatWindowController()0%110100%
VoiceChatWindowController(...)0%110100%
Initialize(...)0%330100%
SetVisibility(...)0%4.134080%
SetVisiblePanelList(...)0%2.022083.33%
SetUsersMuted(...)0%220100%
SetUserRecording(...)0%110100%
SetVoiceChatRecording(...)0%110100%
SetVoiceChatEnabledByScene(...)0%2.062075%
Dispose()0%440100%
CloseView()0%220100%
RequestJoinVoiceChat(...)0%330100%
OnVoiceChatStatusUpdated(...)0%330100%
GoToCrowd()0%2100%
OnOtherPlayersStatusAdded(...)0%7.127086.67%
OnOtherPlayerStatusRemoved(...)0%2.032080%
OnMuteAllToggled(...)0%2.262060%
MuteAll(...)0%3.013088.89%
MuteUser(...)0%550100%
MuteStateUpdateRoutine()0%6.83025%
ReportMuteStatuses()0%330100%
ChangeAllowUsersFilter(...)0%440100%
OnUserProfileUpdated(...)0%440100%
OnUpdateFriendship(...)0%110100%
OnSettingsChanged(...)0%110100%
SetAllowUsersOption(...)0%440100%
RendererState_OnChange(...)0%2.862040%
SetWhichPlayerIsTalking()0%660100%
CreateVoiceChatWindowView()0%2100%
CreateVoiceChatBatView()0%2100%
CreateVoiceChatPlayerView()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/VoiceChatHUD/VoiceChatWindowController.cs

#LineLine coverage
 1using DCL;
 2using DCL.Interface;
 3using DCL.SettingsCommon;
 4using SocialFeaturesAnalytics;
 5using System;
 6using System.Collections;
 7using System.Collections.Generic;
 8using DCl.Social.Friends;
 9using DCL.Social.Friends;
 10using JetBrains.Annotations;
 11using UnityEngine;
 12using static DCL.SettingsCommon.GeneralSettings;
 13
 14public class VoiceChatWindowController : IHUD
 15{
 16    internal const string VOICE_CHAT_FEATURE_FLAG = "voice_chat";
 17    internal const string VOICE_CHAT_JOINED_ON_START_FEATURE_FLAG = "voice_chat_joined_on_start";
 18    internal const float MUTE_STATUS_UPDATE_INTERVAL = 0.3f;
 19    internal const string TALKING_MESSAGE_YOU = "You";
 20    internal const string TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT = "No one else is here";
 21    internal const string TALKING_MESSAGE_NOBODY_TALKING = "Nobody is talking";
 22    internal const string TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING = "Several people talking";
 23
 1224    public IVoiceChatWindowComponentView VoiceChatWindowView => voiceChatWindowView;
 125    public IVoiceChatBarComponentView VoiceChatBarView => voiceChatBarView;
 26
 3927    private bool isVoiceChatFFEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled(VOICE_CHAT_FEATURE_FLAG);
 028    private bool isVoiceChatJoinedOnStartFFEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled(VOICE_CHAT_JOI
 229    internal BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 8030    private UserProfile ownProfile => userProfileBridge.GetOwn();
 31
 32    private IVoiceChatWindowComponentView voiceChatWindowView;
 33    private IVoiceChatBarComponentView voiceChatBarView;
 34    private IUserProfileBridge userProfileBridge;
 35    private IFriendsController friendsController;
 36    private ISocialAnalytics socialAnalytics;
 37    private IMouseCatcher mouseCatcher;
 38    private DataStore dataStore;
 39    private Settings settings;
 3940    internal HashSet<string> trackedUsersHashSet = new HashSet<string>();
 3941    internal readonly List<string> usersToMute = new List<string>();
 3942    internal readonly List<string> usersToUnmute = new List<string>();
 43    internal bool isOwnPLayerTalking = false;
 44    private Coroutine updateMuteStatusRoutine = null;
 45    internal bool isMuteAll = false;
 46    internal bool isJoined = false;
 47
 548    public bool IsVisible { get; private set; }
 49    public event Action OnCloseView;
 50
 51    [UsedImplicitly] // by NSubstitute
 7652    public VoiceChatWindowController() { }
 53
 154    public VoiceChatWindowController(
 55        IUserProfileBridge userProfileBridge,
 56        IFriendsController friendsController,
 57        ISocialAnalytics socialAnalytics,
 58        DataStore dataStore,
 59        Settings settings,
 60        IMouseCatcher mouseCatcher)
 61    {
 162        Initialize(userProfileBridge, friendsController, socialAnalytics, dataStore, settings, mouseCatcher);
 163    }
 64
 65    public void Initialize(
 66        IUserProfileBridge userProfileBridge,
 67        IFriendsController friendsController,
 68        ISocialAnalytics socialAnalytics,
 69        DataStore dataStore,
 70        Settings settings,
 71        IMouseCatcher mouseCatcher)
 72    {
 3973        this.userProfileBridge = userProfileBridge;
 3974        this.friendsController = friendsController;
 3975        this.socialAnalytics = socialAnalytics;
 3976        this.dataStore = dataStore;
 3977        this.settings = settings;
 3978        this.mouseCatcher = mouseCatcher;
 79
 3980        if (!isVoiceChatFFEnabled)
 181            return;
 82
 3883        if(mouseCatcher != null)
 3884            mouseCatcher.OnMouseLock += CloseView;
 85
 3886        voiceChatWindowView = CreateVoiceChatWindowView();
 3887        voiceChatWindowView.Hide(instant: true);
 88
 3889        voiceChatWindowView.OnClose += CloseView;
 3890        voiceChatWindowView.OnJoinVoiceChat += RequestJoinVoiceChat;
 3891        voiceChatWindowView.OnGoToCrowd += GoToCrowd;
 3892        voiceChatWindowView.OnMuteAll += OnMuteAllToggled;
 3893        voiceChatWindowView.OnMuteUser += MuteUser;
 3894        voiceChatWindowView.OnAllowUsersFilterChange += ChangeAllowUsersFilter;
 3895        voiceChatWindowView.SetNumberOfPlayers(0);
 96
 3897        voiceChatBarView = CreateVoiceChatBatView();
 3898        voiceChatBarView.SetAsJoined(false);
 3899        voiceChatBarView.OnJoinVoiceChat += RequestJoinVoiceChat;
 100
 38101        dataStore.voiceChat.isJoinedToVoiceChat.OnChange += OnVoiceChatStatusUpdated;
 38102        OnVoiceChatStatusUpdated(dataStore.voiceChat.isJoinedToVoiceChat.Get(), false);
 103
 38104        dataStore.player.otherPlayers.OnAdded += OnOtherPlayersStatusAdded;
 38105        dataStore.player.otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved;
 38106        ownProfile.OnUpdate += OnUserProfileUpdated;
 38107        friendsController.OnUpdateFriendship += OnUpdateFriendship;
 108
 38109        settings.generalSettings.OnChanged += OnSettingsChanged;
 38110        SetAllowUsersOption(settings.generalSettings.Data.voiceChatAllow);
 111
 38112        CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 38113        RendererState_OnChange(CommonScriptableObjects.rendererState.Get(), false);
 38114    }
 115
 116    public void SetVisibility(bool visible)
 117    {
 4118        if (IsVisible == visible)
 2119            return;
 120
 2121        if (voiceChatWindowView == null)
 1122            return;
 123
 1124        IsVisible = visible;
 125
 1126        SetVisiblePanelList(visible);
 1127        if (visible)
 1128            voiceChatWindowView.Show();
 129        else
 0130            voiceChatWindowView.Hide();
 0131    }
 132
 133    private void SetVisiblePanelList(bool visible)
 134    {
 1135        HashSet<string> newSet = visibleTaskbarPanels.Get();
 1136        if (visible)
 1137            newSet.Add("VoiceChatWindow");
 138        else
 0139            newSet.Remove("VoiceChatWindow");
 140
 1141        visibleTaskbarPanels.Set(newSet, true);
 1142    }
 143
 144    public void SetUsersMuted(string[] usersId, bool isMuted)
 145    {
 16146        for (int i = 0; i < usersId.Length; i++)
 147        {
 6148            voiceChatWindowView.SetPlayerMuted(usersId[i], isMuted);
 149        }
 2150    }
 151
 152    public void SetUserRecording(string userId, bool isRecording)
 153    {
 2154        voiceChatWindowView.SetPlayerRecording(userId, isRecording);
 2155        SetWhichPlayerIsTalking();
 2156    }
 157
 158    public void SetVoiceChatRecording(bool recording)
 159    {
 2160        voiceChatBarView.PlayVoiceChatRecordingAnimation(recording);
 2161        isOwnPLayerTalking = recording;
 2162        SetWhichPlayerIsTalking();
 2163    }
 164
 165    public void SetVoiceChatEnabledByScene(bool enabled)
 166    {
 2167        if (voiceChatBarView == null)
 0168            return;
 169
 2170        voiceChatBarView.SetVoiceChatEnabledByScene(enabled);
 2171    }
 172
 173    public void Dispose()
 174    {
 39175        ReportMuteStatuses();
 176
 39177        if (updateMuteStatusRoutine != null)
 2178            CoroutineStarter.Stop(updateMuteStatusRoutine);
 179
 39180        if (voiceChatWindowView != null)
 181        {
 38182            voiceChatWindowView.OnClose -= CloseView;
 38183            voiceChatWindowView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 38184            voiceChatWindowView.OnGoToCrowd -= GoToCrowd;
 38185            voiceChatWindowView.OnMuteAll -= OnMuteAllToggled;
 38186            voiceChatWindowView.OnMuteUser -= MuteUser;
 38187            voiceChatWindowView.OnAllowUsersFilterChange -= ChangeAllowUsersFilter;
 188        }
 189
 39190        if (voiceChatBarView != null)
 38191            voiceChatBarView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 192
 39193        dataStore.voiceChat.isJoinedToVoiceChat.OnChange -= OnVoiceChatStatusUpdated;
 39194        dataStore.player.otherPlayers.OnAdded -= OnOtherPlayersStatusAdded;
 39195        dataStore.player.otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved;
 39196        ownProfile.OnUpdate -= OnUserProfileUpdated;
 39197        friendsController.OnUpdateFriendship -= OnUpdateFriendship;
 39198        settings.generalSettings.OnChanged -= OnSettingsChanged;
 39199        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 39200    }
 201
 202    internal void CloseView()
 203    {
 1204        OnCloseView?.Invoke();
 1205        SetVisibility(false);
 1206    }
 207
 208    internal void RequestJoinVoiceChat(bool isJoined)
 209    {
 2210        if (isJoined)
 211        {
 1212            WebInterface.JoinVoiceChat();
 213        }
 214        else
 215        {
 1216            if (dataStore.voiceChat.isRecording.Get().Key)
 1217                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 218
 1219            WebInterface.LeaveVoiceChat();
 220        }
 1221    }
 222
 223    internal void OnVoiceChatStatusUpdated(bool isJoined, bool previous)
 224    {
 40225        voiceChatWindowView.SetAsJoined(isJoined);
 40226        voiceChatBarView.SetAsJoined(isJoined);
 227
 40228        if (isJoined)
 229        {
 1230            socialAnalytics.SendVoiceChannelConnection(voiceChatWindowView.numberOfPlayers);
 1231            SetWhichPlayerIsTalking();
 232        }
 233        else
 234        {
 39235            socialAnalytics.SendVoiceChannelDisconnection();
 39236            isOwnPLayerTalking = false;
 237
 39238            if (dataStore.voiceChat.isRecording.Get().Key)
 3239                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 240        }
 39241    }
 242
 243    internal void GoToCrowd()
 244    {
 245        // Requested temporally by product team since the "go to crowd" approach was always redirecting to the casino.
 246        //DCL.Environment.i.world.teleportController.GoToCrowd();
 0247        DCL.Environment.i.world.teleportController.Teleport(0, 0);
 0248    }
 249
 250    internal void OnOtherPlayersStatusAdded(string userId, Player player)
 251    {
 1252        var otherProfile = userProfileBridge.Get(player.id);
 253
 1254        if (otherProfile == null)
 0255            return;
 256
 1257        voiceChatWindowView.AddOrUpdatePlayer(otherProfile);
 258
 1259        if (!trackedUsersHashSet.Contains(userId))
 260        {
 1261            trackedUsersHashSet.Add(userId);
 262
 1263            bool isMuted = ownProfile.muted.Contains(userId);
 1264            voiceChatWindowView.SetPlayerMuted(userId, isMuted);
 1265            voiceChatWindowView.SetPlayerBlocked(userId, ownProfile.blocked != null ? ownProfile.blocked.Contains(userId
 1266            voiceChatWindowView.SetPlayerAsFriend(userId, friendsController.ContainsStatus(userId, FriendshipStatus.FRIE
 1267            voiceChatWindowView.SetPlayerAsJoined(userId, dataStore.voiceChat.isJoinedToVoiceChat.Get());
 268
 1269            if (isMuteAll && !isMuted)
 0270                MuteUser(userId, true);
 271        }
 272
 1273        SetWhichPlayerIsTalking();
 1274    }
 275
 276    internal void OnOtherPlayerStatusRemoved(string userId, Player player)
 277    {
 1278        if (trackedUsersHashSet.Contains(userId))
 0279            trackedUsersHashSet.Remove(userId);
 280
 1281        voiceChatWindowView.RemoveUser(userId);
 1282        SetWhichPlayerIsTalking();
 1283    }
 284
 285    internal void OnMuteAllToggled(bool isMute)
 286    {
 2287        isMuteAll = isMute;
 288
 2289        if (!dataStore.voiceChat.isJoinedToVoiceChat.Get())
 2290            return;
 291
 0292        MuteAll(isMute);
 0293    }
 294
 295    internal void MuteAll(bool isMute)
 296    {
 2297        isMuteAll = isMute;
 298
 2299        if (isMute)
 1300            usersToUnmute.Clear();
 301        else
 1302            usersToMute.Clear();
 303
 2304        using (var iterator = trackedUsersHashSet.GetEnumerator())
 305        {
 2306            while (iterator.MoveNext())
 307            {
 0308                MuteUser(iterator.Current, isMute);
 309            }
 2310        }
 2311    }
 312
 313    internal void MuteUser(string userId, bool isMuted)
 314    {
 2315        var list = isMuted ? usersToMute : usersToUnmute;
 2316        list.Add(userId);
 317
 2318        if (updateMuteStatusRoutine == null)
 2319            updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine());
 320
 2321        if (isMuted)
 1322            socialAnalytics.SendPlayerMuted(userId);
 323        else
 1324            socialAnalytics.SendPlayerUnmuted(userId);
 1325    }
 326
 327    internal IEnumerator MuteStateUpdateRoutine()
 328    {
 2329        yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL);
 0330        ReportMuteStatuses();
 0331        updateMuteStatusRoutine = null;
 0332    }
 333
 334    internal void ReportMuteStatuses()
 335    {
 39336        if (usersToUnmute.Count > 0)
 2337            WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false);
 338
 39339        if (usersToMute.Count > 0)
 2340            WebInterface.SetMuteUsers(usersToMute.ToArray(), true);
 341
 39342        usersToUnmute.Clear();
 39343        usersToMute.Clear();
 39344    }
 345
 346    internal void ChangeAllowUsersFilter(string optionId)
 347    {
 3348        var newSettings = settings.generalSettings.Data;
 349
 3350        if (optionId == VoiceChatAllow.ALL_USERS.ToString())
 1351            newSettings.voiceChatAllow = VoiceChatAllow.ALL_USERS;
 2352        else if (optionId == VoiceChatAllow.VERIFIED_ONLY.ToString())
 1353            newSettings.voiceChatAllow = VoiceChatAllow.VERIFIED_ONLY;
 1354        else if (optionId == VoiceChatAllow.FRIENDS_ONLY.ToString())
 1355            newSettings.voiceChatAllow = VoiceChatAllow.FRIENDS_ONLY;
 356
 3357        settings.generalSettings.Apply(newSettings);
 3358    }
 359
 360    internal void OnUserProfileUpdated(UserProfile profile)
 361    {
 1362        using (var iterator = trackedUsersHashSet.GetEnumerator())
 363        {
 2364            while (iterator.MoveNext())
 365            {
 1366                voiceChatWindowView.SetPlayerBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contain
 367            }
 1368        }
 1369    }
 370
 4371    internal void OnUpdateFriendship(string userId, FriendshipAction action) { voiceChatWindowView.SetPlayerAsFriend(use
 372
 373    internal void OnSettingsChanged(GeneralSettings settings)
 374    {
 5375        SetAllowUsersOption(settings.voiceChatAllow);
 5376        socialAnalytics.SendVoiceChatPreferencesChanged(settings.voiceChatAllow);
 5377    }
 378
 379    private void SetAllowUsersOption(VoiceChatAllow option)
 380    {
 381        switch (option)
 382        {
 383            case VoiceChatAllow.ALL_USERS:
 9384                voiceChatWindowView.SelectAllowUsersOption(0);
 9385                break;
 386            case VoiceChatAllow.VERIFIED_ONLY:
 31387                voiceChatWindowView.SelectAllowUsersOption(1);
 31388                break;
 389            case VoiceChatAllow.FRIENDS_ONLY:
 3390                voiceChatWindowView.SelectAllowUsersOption(2);
 391                break;
 392        }
 3393    }
 394
 395    internal void RendererState_OnChange(bool current, bool previous)
 396    {
 38397        if (!current)
 38398            return;
 399
 0400        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 401
 0402        RequestJoinVoiceChat(isVoiceChatJoinedOnStartFFEnabled);
 0403    }
 404
 405    internal void SetWhichPlayerIsTalking()
 406    {
 12407        if (isOwnPLayerTalking)
 3408            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_YOU);
 9409        else if (voiceChatWindowView.numberOfPlayers == 0)
 6410            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT);
 3411        else if (voiceChatWindowView.numberOfPlayersTalking == 0)
 1412            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_NOBODY_TALKING);
 2413        else if (voiceChatWindowView.numberOfPlayersTalking == 1)
 414        {
 1415            UserProfile userProfile = userProfileBridge.Get(voiceChatWindowView.GetUserTalkingByIndex(0));
 1416            voiceChatBarView.SetTalkingMessage(true, userProfile != null ? userProfile.userName : voiceChatWindowView.Ge
 417        }
 418        else
 1419            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING);
 1420    }
 421
 0422    protected internal virtual IVoiceChatWindowComponentView CreateVoiceChatWindowView() => VoiceChatWindowComponentView
 423
 0424    protected internal virtual IVoiceChatBarComponentView CreateVoiceChatBatView() => VoiceChatBarComponentView.Create()
 425
 0426    protected internal virtual IVoiceChatPlayerComponentView CreateVoiceChatPlayerView() => VoiceChatPlayerComponentView
 427}

Methods/Properties

VoiceChatWindowView()
VoiceChatBarView()
isVoiceChatFFEnabled()
isVoiceChatJoinedOnStartFFEnabled()
visibleTaskbarPanels()
ownProfile()
VoiceChatWindowController()
VoiceChatWindowController(IUserProfileBridge, DCL.Social.Friends.IFriendsController, SocialFeaturesAnalytics.ISocialAnalytics, DCL.DataStore, DCL.SettingsCommon.Settings, DCL.IMouseCatcher)
IsVisible()
IsVisible(System.Boolean)
Initialize(IUserProfileBridge, DCL.Social.Friends.IFriendsController, SocialFeaturesAnalytics.ISocialAnalytics, DCL.DataStore, DCL.SettingsCommon.Settings, DCL.IMouseCatcher)
SetVisibility(System.Boolean)
SetVisiblePanelList(System.Boolean)
SetUsersMuted(System.String[], System.Boolean)
SetUserRecording(System.String, System.Boolean)
SetVoiceChatRecording(System.Boolean)
SetVoiceChatEnabledByScene(System.Boolean)
Dispose()
CloseView()
RequestJoinVoiceChat(System.Boolean)
OnVoiceChatStatusUpdated(System.Boolean, System.Boolean)
GoToCrowd()
OnOtherPlayersStatusAdded(System.String, Player)
OnOtherPlayerStatusRemoved(System.String, Player)
OnMuteAllToggled(System.Boolean)
MuteAll(System.Boolean)
MuteUser(System.String, System.Boolean)
MuteStateUpdateRoutine()
ReportMuteStatuses()
ChangeAllowUsersFilter(System.String)
OnUserProfileUpdated(UserProfile)
OnUpdateFriendship(System.String, DCL.Social.Friends.FriendshipAction)
OnSettingsChanged(DCL.SettingsCommon.GeneralSettings)
SetAllowUsersOption(DCL.SettingsCommon.GeneralSettings/VoiceChatAllow)
RendererState_OnChange(System.Boolean, System.Boolean)
SetWhichPlayerIsTalking()
CreateVoiceChatWindowView()
CreateVoiceChatBatView()
CreateVoiceChatPlayerView()