< 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:21
Coverable lines:213
Total lines:422
Line coverage:90.1% (192 of 213)
Covered branches:0
Total branches:0

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 float MUTE_STATUS_UPDATE_INTERVAL = 0.3f;
 18    internal const string TALKING_MESSAGE_YOU = "You";
 19    internal const string TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT = "No one else is here";
 20    internal const string TALKING_MESSAGE_NOBODY_TALKING = "Nobody is talking";
 21    internal const string TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING = "Several people talking";
 22
 1223    public IVoiceChatWindowComponentView VoiceChatWindowView => voiceChatWindowView;
 124    public IVoiceChatBarComponentView VoiceChatBarView => voiceChatBarView;
 25
 3926    private bool isVoiceChatFFEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled(VOICE_CHAT_FEATURE_FLAG);
 227    internal BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 8028    private UserProfile ownProfile => userProfileBridge.GetOwn();
 29
 30    private IVoiceChatWindowComponentView voiceChatWindowView;
 31    private IVoiceChatBarComponentView voiceChatBarView;
 32    private IUserProfileBridge userProfileBridge;
 33    private IFriendsController friendsController;
 34    private ISocialAnalytics socialAnalytics;
 35    private IMouseCatcher mouseCatcher;
 36    private DataStore dataStore;
 37    private Settings settings;
 3938    internal HashSet<string> trackedUsersHashSet = new HashSet<string>();
 3939    internal readonly List<string> usersToMute = new List<string>();
 3940    internal readonly List<string> usersToUnmute = new List<string>();
 41    internal bool isOwnPLayerTalking = false;
 42    private Coroutine updateMuteStatusRoutine = null;
 43    internal bool isMuteAll = false;
 44    internal bool isJoined = false;
 45
 546    public bool IsVisible { get; private set; }
 47    public event Action OnCloseView;
 48
 49    [UsedImplicitly] // by NSubstitute
 7650    public VoiceChatWindowController() { }
 51
 152    public VoiceChatWindowController(
 53        IUserProfileBridge userProfileBridge,
 54        IFriendsController friendsController,
 55        ISocialAnalytics socialAnalytics,
 56        DataStore dataStore,
 57        Settings settings,
 58        IMouseCatcher mouseCatcher)
 59    {
 160        Initialize(userProfileBridge, friendsController, socialAnalytics, dataStore, settings, mouseCatcher);
 161    }
 62
 63    public void Initialize(
 64        IUserProfileBridge userProfileBridge,
 65        IFriendsController friendsController,
 66        ISocialAnalytics socialAnalytics,
 67        DataStore dataStore,
 68        Settings settings,
 69        IMouseCatcher mouseCatcher)
 70    {
 3971        this.userProfileBridge = userProfileBridge;
 3972        this.friendsController = friendsController;
 3973        this.socialAnalytics = socialAnalytics;
 3974        this.dataStore = dataStore;
 3975        this.settings = settings;
 3976        this.mouseCatcher = mouseCatcher;
 77
 3978        if (!isVoiceChatFFEnabled)
 179            return;
 80
 3881        if(mouseCatcher != null)
 3882            mouseCatcher.OnMouseLock += CloseView;
 83
 3884        voiceChatWindowView = CreateVoiceChatWindowView();
 3885        voiceChatWindowView.Hide(instant: true);
 86
 3887        voiceChatWindowView.OnClose += CloseView;
 3888        voiceChatWindowView.OnJoinVoiceChat += RequestJoinVoiceChat;
 3889        voiceChatWindowView.OnGoToCrowd += GoToCrowd;
 3890        voiceChatWindowView.OnMuteAll += OnMuteAllToggled;
 3891        voiceChatWindowView.OnMuteUser += MuteUser;
 3892        voiceChatWindowView.OnAllowUsersFilterChange += ChangeAllowUsersFilter;
 3893        voiceChatWindowView.SetNumberOfPlayers(0);
 94
 3895        voiceChatBarView = CreateVoiceChatBatView();
 3896        voiceChatBarView.SetAsJoined(false);
 3897        voiceChatBarView.OnJoinVoiceChat += RequestJoinVoiceChat;
 98
 3899        dataStore.voiceChat.isJoinedToVoiceChat.OnChange += OnVoiceChatStatusUpdated;
 38100        OnVoiceChatStatusUpdated(dataStore.voiceChat.isJoinedToVoiceChat.Get(), false);
 101
 38102        dataStore.player.otherPlayers.OnAdded += OnOtherPlayersStatusAdded;
 38103        dataStore.player.otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved;
 38104        ownProfile.OnUpdate += OnUserProfileUpdated;
 38105        friendsController.OnUpdateFriendship += OnUpdateFriendship;
 106
 38107        settings.generalSettings.OnChanged += OnSettingsChanged;
 38108        SetAllowUsersOption(settings.generalSettings.Data.voiceChatAllow);
 109
 38110        CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 38111        RendererState_OnChange(CommonScriptableObjects.rendererState.Get(), false);
 38112    }
 113
 114    public void SetVisibility(bool visible)
 115    {
 4116        if (IsVisible == visible)
 2117            return;
 118
 2119        if (voiceChatWindowView == null)
 1120            return;
 121
 1122        IsVisible = visible;
 123
 1124        SetVisiblePanelList(visible);
 1125        if (visible)
 1126            voiceChatWindowView.Show();
 127        else
 0128            voiceChatWindowView.Hide();
 0129    }
 130
 131    private void SetVisiblePanelList(bool visible)
 132    {
 1133        HashSet<string> newSet = visibleTaskbarPanels.Get();
 1134        if (visible)
 1135            newSet.Add("VoiceChatWindow");
 136        else
 0137            newSet.Remove("VoiceChatWindow");
 138
 1139        visibleTaskbarPanels.Set(newSet, true);
 1140    }
 141
 142    public void SetUsersMuted(string[] usersId, bool isMuted)
 143    {
 16144        for (int i = 0; i < usersId.Length; i++)
 145        {
 6146            voiceChatWindowView.SetPlayerMuted(usersId[i], isMuted);
 147        }
 2148    }
 149
 150    public void SetUserRecording(string userId, bool isRecording)
 151    {
 2152        voiceChatWindowView.SetPlayerRecording(userId, isRecording);
 2153        SetWhichPlayerIsTalking();
 2154    }
 155
 156    public void SetVoiceChatRecording(bool recording)
 157    {
 2158        voiceChatBarView.PlayVoiceChatRecordingAnimation(recording);
 2159        isOwnPLayerTalking = recording;
 2160        SetWhichPlayerIsTalking();
 2161    }
 162
 163    public void SetVoiceChatEnabledByScene(bool enabled)
 164    {
 2165        if (voiceChatBarView == null)
 0166            return;
 167
 2168        voiceChatBarView.SetVoiceChatEnabledByScene(enabled);
 2169    }
 170
 171    public void Dispose()
 172    {
 39173        ReportMuteStatuses();
 174
 39175        if (updateMuteStatusRoutine != null)
 2176            CoroutineStarter.Stop(updateMuteStatusRoutine);
 177
 39178        if (voiceChatWindowView != null)
 179        {
 38180            voiceChatWindowView.OnClose -= CloseView;
 38181            voiceChatWindowView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 38182            voiceChatWindowView.OnGoToCrowd -= GoToCrowd;
 38183            voiceChatWindowView.OnMuteAll -= OnMuteAllToggled;
 38184            voiceChatWindowView.OnMuteUser -= MuteUser;
 38185            voiceChatWindowView.OnAllowUsersFilterChange -= ChangeAllowUsersFilter;
 186        }
 187
 39188        if (voiceChatBarView != null)
 38189            voiceChatBarView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 190
 39191        dataStore.voiceChat.isJoinedToVoiceChat.OnChange -= OnVoiceChatStatusUpdated;
 39192        dataStore.player.otherPlayers.OnAdded -= OnOtherPlayersStatusAdded;
 39193        dataStore.player.otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved;
 39194        ownProfile.OnUpdate -= OnUserProfileUpdated;
 39195        friendsController.OnUpdateFriendship -= OnUpdateFriendship;
 39196        settings.generalSettings.OnChanged -= OnSettingsChanged;
 39197        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 39198    }
 199
 200    internal void CloseView()
 201    {
 1202        OnCloseView?.Invoke();
 1203        SetVisibility(false);
 1204    }
 205
 206    internal void RequestJoinVoiceChat(bool isJoined)
 207    {
 2208        if (isJoined)
 209        {
 1210            WebInterface.JoinVoiceChat();
 211        }
 212        else
 213        {
 1214            if (dataStore.voiceChat.isRecording.Get().Key)
 1215                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 216
 1217            WebInterface.LeaveVoiceChat();
 218        }
 1219    }
 220
 221    internal void OnVoiceChatStatusUpdated(bool isJoined, bool previous)
 222    {
 40223        voiceChatWindowView.SetAsJoined(isJoined);
 40224        voiceChatBarView.SetAsJoined(isJoined);
 225
 40226        if (isJoined)
 227        {
 1228            socialAnalytics.SendVoiceChannelConnection(voiceChatWindowView.numberOfPlayers);
 1229            SetWhichPlayerIsTalking();
 230        }
 231        else
 232        {
 39233            socialAnalytics.SendVoiceChannelDisconnection();
 39234            isOwnPLayerTalking = false;
 235
 39236            if (dataStore.voiceChat.isRecording.Get().Key)
 3237                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 238        }
 39239    }
 240
 241    internal void GoToCrowd()
 242    {
 0243        DCL.Environment.i.world.teleportController.GoToCrowd();
 0244    }
 245
 246    internal void OnOtherPlayersStatusAdded(string userId, Player player)
 247    {
 1248        var otherProfile = userProfileBridge.Get(player.id);
 249
 1250        if (otherProfile == null)
 0251            return;
 252
 1253        voiceChatWindowView.AddOrUpdatePlayer(otherProfile);
 254
 1255        if (!trackedUsersHashSet.Contains(userId))
 256        {
 1257            trackedUsersHashSet.Add(userId);
 258
 1259            bool isMuted = ownProfile.muted.Contains(userId);
 1260            voiceChatWindowView.SetPlayerMuted(userId, isMuted);
 1261            voiceChatWindowView.SetPlayerBlocked(userId, ownProfile.blocked != null ? ownProfile.blocked.Contains(userId
 1262            voiceChatWindowView.SetPlayerAsFriend(userId, friendsController.ContainsStatus(userId, FriendshipStatus.FRIE
 1263            voiceChatWindowView.SetPlayerAsJoined(userId, dataStore.voiceChat.isJoinedToVoiceChat.Get());
 264
 1265            if (isMuteAll && !isMuted)
 0266                MuteUser(userId, true);
 267        }
 268
 1269        SetWhichPlayerIsTalking();
 1270    }
 271
 272    internal void OnOtherPlayerStatusRemoved(string userId, Player player)
 273    {
 1274        if (trackedUsersHashSet.Contains(userId))
 0275            trackedUsersHashSet.Remove(userId);
 276
 1277        voiceChatWindowView.RemoveUser(userId);
 1278        SetWhichPlayerIsTalking();
 1279    }
 280
 281    internal void OnMuteAllToggled(bool isMute)
 282    {
 2283        isMuteAll = isMute;
 284
 2285        if (!dataStore.voiceChat.isJoinedToVoiceChat.Get())
 2286            return;
 287
 0288        MuteAll(isMute);
 0289    }
 290
 291    internal void MuteAll(bool isMute)
 292    {
 2293        isMuteAll = isMute;
 294
 2295        if (isMute)
 1296            usersToUnmute.Clear();
 297        else
 1298            usersToMute.Clear();
 299
 2300        using (var iterator = trackedUsersHashSet.GetEnumerator())
 301        {
 2302            while (iterator.MoveNext())
 303            {
 0304                MuteUser(iterator.Current, isMute);
 305            }
 2306        }
 2307    }
 308
 309    internal void MuteUser(string userId, bool isMuted)
 310    {
 2311        var list = isMuted ? usersToMute : usersToUnmute;
 2312        list.Add(userId);
 313
 2314        if (updateMuteStatusRoutine == null)
 2315            updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine());
 316
 2317        if (isMuted)
 1318            socialAnalytics.SendPlayerMuted(userId);
 319        else
 1320            socialAnalytics.SendPlayerUnmuted(userId);
 1321    }
 322
 323    internal IEnumerator MuteStateUpdateRoutine()
 324    {
 2325        yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL);
 0326        ReportMuteStatuses();
 0327        updateMuteStatusRoutine = null;
 0328    }
 329
 330    internal void ReportMuteStatuses()
 331    {
 39332        if (usersToUnmute.Count > 0)
 2333            WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false);
 334
 39335        if (usersToMute.Count > 0)
 2336            WebInterface.SetMuteUsers(usersToMute.ToArray(), true);
 337
 39338        usersToUnmute.Clear();
 39339        usersToMute.Clear();
 39340    }
 341
 342    internal void ChangeAllowUsersFilter(string optionId)
 343    {
 3344        var newSettings = settings.generalSettings.Data;
 345
 3346        if (optionId == VoiceChatAllow.ALL_USERS.ToString())
 1347            newSettings.voiceChatAllow = VoiceChatAllow.ALL_USERS;
 2348        else if (optionId == VoiceChatAllow.VERIFIED_ONLY.ToString())
 1349            newSettings.voiceChatAllow = VoiceChatAllow.VERIFIED_ONLY;
 1350        else if (optionId == VoiceChatAllow.FRIENDS_ONLY.ToString())
 1351            newSettings.voiceChatAllow = VoiceChatAllow.FRIENDS_ONLY;
 352
 3353        settings.generalSettings.Apply(newSettings);
 3354    }
 355
 356    internal void OnUserProfileUpdated(UserProfile profile)
 357    {
 1358        using (var iterator = trackedUsersHashSet.GetEnumerator())
 359        {
 2360            while (iterator.MoveNext())
 361            {
 1362                voiceChatWindowView.SetPlayerBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contain
 363            }
 1364        }
 1365    }
 366
 4367    internal void OnUpdateFriendship(string userId, FriendshipAction action) { voiceChatWindowView.SetPlayerAsFriend(use
 368
 369    internal void OnSettingsChanged(GeneralSettings settings)
 370    {
 5371        SetAllowUsersOption(settings.voiceChatAllow);
 5372        socialAnalytics.SendVoiceChatPreferencesChanged(settings.voiceChatAllow);
 5373    }
 374
 375    private void SetAllowUsersOption(VoiceChatAllow option)
 376    {
 377        switch (option)
 378        {
 379            case VoiceChatAllow.ALL_USERS:
 9380                voiceChatWindowView.SelectAllowUsersOption(0);
 9381                break;
 382            case VoiceChatAllow.VERIFIED_ONLY:
 31383                voiceChatWindowView.SelectAllowUsersOption(1);
 31384                break;
 385            case VoiceChatAllow.FRIENDS_ONLY:
 3386                voiceChatWindowView.SelectAllowUsersOption(2);
 387                break;
 388        }
 3389    }
 390
 391    internal void RendererState_OnChange(bool current, bool previous)
 392    {
 38393        if (!current)
 38394            return;
 395
 0396        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 0397        RequestJoinVoiceChat(true);
 0398    }
 399
 400    internal void SetWhichPlayerIsTalking()
 401    {
 12402        if (isOwnPLayerTalking)
 3403            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_YOU);
 9404        else if (voiceChatWindowView.numberOfPlayers == 0)
 6405            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT);
 3406        else if (voiceChatWindowView.numberOfPlayersTalking == 0)
 1407            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_NOBODY_TALKING);
 2408        else if (voiceChatWindowView.numberOfPlayersTalking == 1)
 409        {
 1410            UserProfile userProfile = userProfileBridge.Get(voiceChatWindowView.GetUserTalkingByIndex(0));
 1411            voiceChatBarView.SetTalkingMessage(true, userProfile != null ? userProfile.userName : voiceChatWindowView.Ge
 412        }
 413        else
 1414            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING);
 1415    }
 416
 0417    protected internal virtual IVoiceChatWindowComponentView CreateVoiceChatWindowView() => VoiceChatWindowComponentView
 418
 0419    protected internal virtual IVoiceChatBarComponentView CreateVoiceChatBatView() => VoiceChatBarComponentView.Create()
 420
 0421    protected internal virtual IVoiceChatPlayerComponentView CreateVoiceChatPlayerView() => VoiceChatPlayerComponentView
 422}

Methods/Properties

VoiceChatWindowView()
VoiceChatBarView()
isVoiceChatFFEnabled()
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()