< 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:20
Coverable lines:212
Total lines:412
Line coverage:90.5% (192 of 212)
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%330100%
SetVisiblePanelList(...)0%220100%
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 UnityEngine;
 9using static DCL.SettingsCommon.GeneralSettings;
 10
 11public class VoiceChatWindowController : IHUD
 12{
 13    internal const string VOICE_CHAT_FEATURE_FLAG = "voice_chat";
 14    internal const float MUTE_STATUS_UPDATE_INTERVAL = 0.3f;
 15    internal const string TALKING_MESSAGE_YOU = "You";
 16    internal const string TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT = "No one else is here";
 17    internal const string TALKING_MESSAGE_NOBODY_TALKING = "Nobody is talking";
 18    internal const string TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING = "Several people talking";
 19
 020    public IVoiceChatWindowComponentView VoiceChatWindowView => voiceChatWindowView;
 021    public IVoiceChatBarComponentView VoiceChatBarView => voiceChatBarView;
 22
 3923    private bool isVoiceChatFFEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled(VOICE_CHAT_FEATURE_FLAG);
 624    internal BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 8025    private UserProfile ownProfile => userProfileBridge.GetOwn();
 26
 27    private IVoiceChatWindowComponentView voiceChatWindowView;
 28    private IVoiceChatBarComponentView voiceChatBarView;
 29    private IUserProfileBridge userProfileBridge;
 30    private IFriendsController friendsController;
 31    private ISocialAnalytics socialAnalytics;
 32    private IMouseCatcher mouseCatcher;
 33    private DataStore dataStore;
 34    private Settings settings;
 3935    internal HashSet<string> trackedUsersHashSet = new HashSet<string>();
 3936    internal readonly List<string> usersToMute = new List<string>();
 3937    internal readonly List<string> usersToUnmute = new List<string>();
 38    internal bool isOwnPLayerTalking = false;
 39    private Coroutine updateMuteStatusRoutine = null;
 40    internal bool isMuteAll = false;
 41    internal bool isJoined = false;
 42
 43    public event Action OnCloseView;
 44
 7645    public VoiceChatWindowController() { }
 46
 147    public VoiceChatWindowController(
 48        IUserProfileBridge userProfileBridge,
 49        IFriendsController friendsController,
 50        ISocialAnalytics socialAnalytics,
 51        DataStore dataStore,
 52        Settings settings,
 53        IMouseCatcher mouseCatcher)
 54    {
 155        Initialize(userProfileBridge, friendsController, socialAnalytics, dataStore, settings, mouseCatcher);
 156    }
 57
 58    public void Initialize(
 59        IUserProfileBridge userProfileBridge,
 60        IFriendsController friendsController,
 61        ISocialAnalytics socialAnalytics,
 62        DataStore dataStore,
 63        Settings settings,
 64        IMouseCatcher mouseCatcher)
 65    {
 3966        this.userProfileBridge = userProfileBridge;
 3967        this.friendsController = friendsController;
 3968        this.socialAnalytics = socialAnalytics;
 3969        this.dataStore = dataStore;
 3970        this.settings = settings;
 3971        this.mouseCatcher = mouseCatcher;
 72
 3973        if (!isVoiceChatFFEnabled)
 174            return;
 75
 3876        if(mouseCatcher != null)
 3877            mouseCatcher.OnMouseLock += CloseView;
 78
 3879        voiceChatWindowView = CreateVoiceChatWindowView();
 3880        voiceChatWindowView.Hide(instant: true);
 81
 3882        voiceChatWindowView.OnClose += CloseView;
 3883        voiceChatWindowView.OnJoinVoiceChat += RequestJoinVoiceChat;
 3884        voiceChatWindowView.OnGoToCrowd += GoToCrowd;
 3885        voiceChatWindowView.OnMuteAll += OnMuteAllToggled;
 3886        voiceChatWindowView.OnMuteUser += MuteUser;
 3887        voiceChatWindowView.OnAllowUsersFilterChange += ChangeAllowUsersFilter;
 3888        voiceChatWindowView.SetNumberOfPlayers(0);
 89
 3890        voiceChatBarView = CreateVoiceChatBatView();
 3891        voiceChatBarView.SetAsJoined(false);
 3892        voiceChatBarView.OnJoinVoiceChat += RequestJoinVoiceChat;
 93
 3894        dataStore.voiceChat.isJoinedToVoiceChat.OnChange += OnVoiceChatStatusUpdated;
 3895        OnVoiceChatStatusUpdated(dataStore.voiceChat.isJoinedToVoiceChat.Get(), false);
 96
 3897        dataStore.player.otherPlayers.OnAdded += OnOtherPlayersStatusAdded;
 3898        dataStore.player.otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved;
 3899        ownProfile.OnUpdate += OnUserProfileUpdated;
 38100        friendsController.OnUpdateFriendship += OnUpdateFriendship;
 101
 38102        settings.generalSettings.OnChanged += OnSettingsChanged;
 38103        SetAllowUsersOption(settings.generalSettings.Data.voiceChatAllow);
 104
 38105        CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 38106        RendererState_OnChange(CommonScriptableObjects.rendererState.Get(), false);
 38107    }
 108
 109    public void SetVisibility(bool visible)
 110    {
 4111        if (voiceChatWindowView == null)
 1112            return;
 113
 3114        SetVisiblePanelList(visible);
 3115        if (visible)
 1116            voiceChatWindowView.Show();
 117        else
 2118            voiceChatWindowView.Hide();
 2119    }
 120
 121    private void SetVisiblePanelList(bool visible)
 122    {
 3123        HashSet<string> newSet = visibleTaskbarPanels.Get();
 3124        if (visible)
 1125            newSet.Add("VoiceChatWindow");
 126        else
 2127            newSet.Remove("VoiceChatWindow");
 128
 3129        visibleTaskbarPanels.Set(newSet, true);
 3130    }
 131
 132    public void SetUsersMuted(string[] usersId, bool isMuted)
 133    {
 16134        for (int i = 0; i < usersId.Length; i++)
 135        {
 6136            voiceChatWindowView.SetPlayerMuted(usersId[i], isMuted);
 137        }
 2138    }
 139
 140    public void SetUserRecording(string userId, bool isRecording)
 141    {
 2142        voiceChatWindowView.SetPlayerRecording(userId, isRecording);
 2143        SetWhichPlayerIsTalking();
 2144    }
 145
 146    public void SetVoiceChatRecording(bool recording)
 147    {
 2148        voiceChatBarView.PlayVoiceChatRecordingAnimation(recording);
 2149        isOwnPLayerTalking = recording;
 2150        SetWhichPlayerIsTalking();
 2151    }
 152
 153    public void SetVoiceChatEnabledByScene(bool enabled)
 154    {
 2155        if (voiceChatBarView == null)
 0156            return;
 157
 2158        voiceChatBarView.SetVoiceChatEnabledByScene(enabled);
 2159    }
 160
 161    public void Dispose()
 162    {
 39163        ReportMuteStatuses();
 164
 39165        if (updateMuteStatusRoutine != null)
 2166            CoroutineStarter.Stop(updateMuteStatusRoutine);
 167
 39168        if (voiceChatWindowView != null)
 169        {
 38170            voiceChatWindowView.OnClose -= CloseView;
 38171            voiceChatWindowView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 38172            voiceChatWindowView.OnGoToCrowd -= GoToCrowd;
 38173            voiceChatWindowView.OnMuteAll -= OnMuteAllToggled;
 38174            voiceChatWindowView.OnMuteUser -= MuteUser;
 38175            voiceChatWindowView.OnAllowUsersFilterChange -= ChangeAllowUsersFilter;
 176        }
 177
 39178        if (voiceChatBarView != null)
 38179            voiceChatBarView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 180
 39181        dataStore.voiceChat.isJoinedToVoiceChat.OnChange -= OnVoiceChatStatusUpdated;
 39182        dataStore.player.otherPlayers.OnAdded -= OnOtherPlayersStatusAdded;
 39183        dataStore.player.otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved;
 39184        ownProfile.OnUpdate -= OnUserProfileUpdated;
 39185        friendsController.OnUpdateFriendship -= OnUpdateFriendship;
 39186        settings.generalSettings.OnChanged -= OnSettingsChanged;
 39187        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 39188    }
 189
 190    internal void CloseView()
 191    {
 1192        OnCloseView?.Invoke();
 1193        SetVisibility(false);
 1194    }
 195
 196    internal void RequestJoinVoiceChat(bool isJoined)
 197    {
 2198        if (isJoined)
 199        {
 1200            WebInterface.JoinVoiceChat();
 1201        }
 202        else
 203        {
 1204            if (dataStore.voiceChat.isRecording.Get().Key)
 1205                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 206
 1207            WebInterface.LeaveVoiceChat();
 208        }
 1209    }
 210
 211    internal void OnVoiceChatStatusUpdated(bool isJoined, bool previous)
 212    {
 40213        voiceChatWindowView.SetAsJoined(isJoined);
 40214        voiceChatBarView.SetAsJoined(isJoined);
 215
 40216        if (isJoined)
 217        {
 1218            socialAnalytics.SendVoiceChannelConnection(voiceChatWindowView.numberOfPlayers);
 1219            SetWhichPlayerIsTalking();
 1220        }
 221        else
 222        {
 39223            socialAnalytics.SendVoiceChannelDisconnection();
 39224            isOwnPLayerTalking = false;
 225
 39226            if (dataStore.voiceChat.isRecording.Get().Key)
 3227                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 228        }
 39229    }
 230
 231    internal void GoToCrowd()
 232    {
 0233        DCL.Environment.i.world.teleportController.GoToCrowd();
 0234    }
 235
 236    internal void OnOtherPlayersStatusAdded(string userId, Player player)
 237    {
 1238        var otherProfile = userProfileBridge.Get(player.id);
 239
 1240        if (otherProfile == null)
 0241            return;
 242
 1243        voiceChatWindowView.AddOrUpdatePlayer(otherProfile);
 244
 1245        if (!trackedUsersHashSet.Contains(userId))
 246        {
 1247            trackedUsersHashSet.Add(userId);
 248
 1249            bool isMuted = ownProfile.muted.Contains(userId);
 1250            voiceChatWindowView.SetPlayerMuted(userId, isMuted);
 1251            voiceChatWindowView.SetPlayerBlocked(userId, ownProfile.blocked != null ? ownProfile.blocked.Contains(userId
 1252            voiceChatWindowView.SetPlayerAsFriend(userId, friendsController.ContainsStatus(userId, FriendshipStatus.FRIE
 1253            voiceChatWindowView.SetPlayerAsJoined(userId, dataStore.voiceChat.isJoinedToVoiceChat.Get());
 254
 1255            if (isMuteAll && !isMuted)
 0256                MuteUser(userId, true);
 257        }
 258
 1259        SetWhichPlayerIsTalking();
 1260    }
 261
 262    internal void OnOtherPlayerStatusRemoved(string userId, Player player)
 263    {
 1264        if (trackedUsersHashSet.Contains(userId))
 0265            trackedUsersHashSet.Remove(userId);
 266
 1267        voiceChatWindowView.RemoveUser(userId);
 1268        SetWhichPlayerIsTalking();
 1269    }
 270
 271    internal void OnMuteAllToggled(bool isMute)
 272    {
 2273        isMuteAll = isMute;
 274
 2275        if (!dataStore.voiceChat.isJoinedToVoiceChat.Get())
 2276            return;
 277
 0278        MuteAll(isMute);
 0279    }
 280
 281    internal void MuteAll(bool isMute)
 282    {
 2283        isMuteAll = isMute;
 284
 2285        if (isMute)
 1286            usersToUnmute.Clear();
 287        else
 1288            usersToMute.Clear();
 289
 2290        using (var iterator = trackedUsersHashSet.GetEnumerator())
 291        {
 2292            while (iterator.MoveNext())
 293            {
 0294                MuteUser(iterator.Current, isMute);
 295            }
 2296        }
 2297    }
 298
 299    internal void MuteUser(string userId, bool isMuted)
 300    {
 2301        var list = isMuted ? usersToMute : usersToUnmute;
 2302        list.Add(userId);
 303
 2304        if (updateMuteStatusRoutine == null)
 2305            updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine());
 306
 2307        if (isMuted)
 1308            socialAnalytics.SendPlayerMuted(userId);
 309        else
 1310            socialAnalytics.SendPlayerUnmuted(userId);
 1311    }
 312
 313    internal IEnumerator MuteStateUpdateRoutine()
 314    {
 2315        yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL);
 0316        ReportMuteStatuses();
 0317        updateMuteStatusRoutine = null;
 0318    }
 319
 320    internal void ReportMuteStatuses()
 321    {
 39322        if (usersToUnmute.Count > 0)
 2323            WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false);
 324
 39325        if (usersToMute.Count > 0)
 2326            WebInterface.SetMuteUsers(usersToMute.ToArray(), true);
 327
 39328        usersToUnmute.Clear();
 39329        usersToMute.Clear();
 39330    }
 331
 332    internal void ChangeAllowUsersFilter(string optionId)
 333    {
 3334        var newSettings = settings.generalSettings.Data;
 335
 3336        if (optionId == VoiceChatAllow.ALL_USERS.ToString())
 1337            newSettings.voiceChatAllow = VoiceChatAllow.ALL_USERS;
 2338        else if (optionId == VoiceChatAllow.VERIFIED_ONLY.ToString())
 1339            newSettings.voiceChatAllow = VoiceChatAllow.VERIFIED_ONLY;
 1340        else if (optionId == VoiceChatAllow.FRIENDS_ONLY.ToString())
 1341            newSettings.voiceChatAllow = VoiceChatAllow.FRIENDS_ONLY;
 342
 3343        settings.generalSettings.Apply(newSettings);
 3344    }
 345
 346    internal void OnUserProfileUpdated(UserProfile profile)
 347    {
 1348        using (var iterator = trackedUsersHashSet.GetEnumerator())
 349        {
 2350            while (iterator.MoveNext())
 351            {
 1352                voiceChatWindowView.SetPlayerBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contain
 353            }
 1354        }
 1355    }
 356
 4357    internal void OnUpdateFriendship(string userId, FriendshipAction action) { voiceChatWindowView.SetPlayerAsFriend(use
 358
 359    internal void OnSettingsChanged(GeneralSettings settings)
 360    {
 5361        SetAllowUsersOption(settings.voiceChatAllow);
 5362        socialAnalytics.SendVoiceChatPreferencesChanged(settings.voiceChatAllow);
 5363    }
 364
 365    private void SetAllowUsersOption(VoiceChatAllow option)
 366    {
 367        switch (option)
 368        {
 369            case VoiceChatAllow.ALL_USERS:
 9370                voiceChatWindowView.SelectAllowUsersOption(0);
 9371                break;
 372            case VoiceChatAllow.VERIFIED_ONLY:
 31373                voiceChatWindowView.SelectAllowUsersOption(1);
 31374                break;
 375            case VoiceChatAllow.FRIENDS_ONLY:
 3376                voiceChatWindowView.SelectAllowUsersOption(2);
 377                break;
 378        }
 3379    }
 380
 381    internal void RendererState_OnChange(bool current, bool previous)
 382    {
 38383        if (!current)
 38384            return;
 385
 0386        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 0387        RequestJoinVoiceChat(true);
 0388    }
 389
 390    internal void SetWhichPlayerIsTalking()
 391    {
 12392        if (isOwnPLayerTalking)
 3393            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_YOU);
 9394        else if (voiceChatWindowView.numberOfPlayers == 0)
 6395            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT);
 3396        else if (voiceChatWindowView.numberOfPlayersTalking == 0)
 1397            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_NOBODY_TALKING);
 2398        else if (voiceChatWindowView.numberOfPlayersTalking == 1)
 399        {
 1400            UserProfile userProfile = userProfileBridge.Get(voiceChatWindowView.GetUserTalkingByIndex(0));
 1401            voiceChatBarView.SetTalkingMessage(true, userProfile != null ? userProfile.userName : voiceChatWindowView.Ge
 1402        }
 403        else
 1404            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING);
 1405    }
 406
 0407    protected internal virtual IVoiceChatWindowComponentView CreateVoiceChatWindowView() => VoiceChatWindowComponentView
 408
 0409    protected internal virtual IVoiceChatBarComponentView CreateVoiceChatBatView() => VoiceChatBarComponentView.Create()
 410
 0411    protected internal virtual IVoiceChatPlayerComponentView CreateVoiceChatPlayerView() => VoiceChatPlayerComponentView
 412}