< Summary

Class:VoiceChatWindowController
Assembly:VoiceChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/VoiceChatHUD/VoiceChatWindowController.cs
Covered lines:191
Uncovered lines:18
Coverable lines:209
Total lines:414
Line coverage:91.3% (191 of 209)
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 DCl.Social.Friends;
 9using DCL.Social.Friends;
 10using UnityEngine;
 11using static DCL.SettingsCommon.GeneralSettings;
 12
 13public class VoiceChatWindowController : IHUD
 14{
 15    internal const string VOICE_CHAT_FEATURE_FLAG = "voice_chat";
 16    internal const float MUTE_STATUS_UPDATE_INTERVAL = 0.3f;
 17    internal const string TALKING_MESSAGE_YOU = "You";
 18    internal const string TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT = "No one else is here";
 19    internal const string TALKING_MESSAGE_NOBODY_TALKING = "Nobody is talking";
 20    internal const string TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING = "Several people talking";
 21
 1222    public IVoiceChatWindowComponentView VoiceChatWindowView => voiceChatWindowView;
 123    public IVoiceChatBarComponentView VoiceChatBarView => voiceChatBarView;
 24
 3925    private bool isVoiceChatFFEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled(VOICE_CHAT_FEATURE_FLAG);
 626    internal BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels;
 8027    private UserProfile ownProfile => userProfileBridge.GetOwn();
 28
 29    private IVoiceChatWindowComponentView voiceChatWindowView;
 30    private IVoiceChatBarComponentView voiceChatBarView;
 31    private IUserProfileBridge userProfileBridge;
 32    private IFriendsController friendsController;
 33    private ISocialAnalytics socialAnalytics;
 34    private IMouseCatcher mouseCatcher;
 35    private DataStore dataStore;
 36    private Settings settings;
 3937    internal HashSet<string> trackedUsersHashSet = new HashSet<string>();
 3938    internal readonly List<string> usersToMute = new List<string>();
 3939    internal readonly List<string> usersToUnmute = new List<string>();
 40    internal bool isOwnPLayerTalking = false;
 41    private Coroutine updateMuteStatusRoutine = null;
 42    internal bool isMuteAll = false;
 43    internal bool isJoined = false;
 44
 45    public event Action OnCloseView;
 46
 7647    public VoiceChatWindowController() { }
 48
 149    public VoiceChatWindowController(
 50        IUserProfileBridge userProfileBridge,
 51        IFriendsController friendsController,
 52        ISocialAnalytics socialAnalytics,
 53        DataStore dataStore,
 54        Settings settings,
 55        IMouseCatcher mouseCatcher)
 56    {
 157        Initialize(userProfileBridge, friendsController, socialAnalytics, dataStore, settings, mouseCatcher);
 158    }
 59
 60    public void Initialize(
 61        IUserProfileBridge userProfileBridge,
 62        IFriendsController friendsController,
 63        ISocialAnalytics socialAnalytics,
 64        DataStore dataStore,
 65        Settings settings,
 66        IMouseCatcher mouseCatcher)
 67    {
 3968        this.userProfileBridge = userProfileBridge;
 3969        this.friendsController = friendsController;
 3970        this.socialAnalytics = socialAnalytics;
 3971        this.dataStore = dataStore;
 3972        this.settings = settings;
 3973        this.mouseCatcher = mouseCatcher;
 74
 3975        if (!isVoiceChatFFEnabled)
 176            return;
 77
 3878        if(mouseCatcher != null)
 3879            mouseCatcher.OnMouseLock += CloseView;
 80
 3881        voiceChatWindowView = CreateVoiceChatWindowView();
 3882        voiceChatWindowView.Hide(instant: true);
 83
 3884        voiceChatWindowView.OnClose += CloseView;
 3885        voiceChatWindowView.OnJoinVoiceChat += RequestJoinVoiceChat;
 3886        voiceChatWindowView.OnGoToCrowd += GoToCrowd;
 3887        voiceChatWindowView.OnMuteAll += OnMuteAllToggled;
 3888        voiceChatWindowView.OnMuteUser += MuteUser;
 3889        voiceChatWindowView.OnAllowUsersFilterChange += ChangeAllowUsersFilter;
 3890        voiceChatWindowView.SetNumberOfPlayers(0);
 91
 3892        voiceChatBarView = CreateVoiceChatBatView();
 3893        voiceChatBarView.SetAsJoined(false);
 3894        voiceChatBarView.OnJoinVoiceChat += RequestJoinVoiceChat;
 95
 3896        dataStore.voiceChat.isJoinedToVoiceChat.OnChange += OnVoiceChatStatusUpdated;
 3897        OnVoiceChatStatusUpdated(dataStore.voiceChat.isJoinedToVoiceChat.Get(), false);
 98
 3899        dataStore.player.otherPlayers.OnAdded += OnOtherPlayersStatusAdded;
 38100        dataStore.player.otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved;
 38101        ownProfile.OnUpdate += OnUserProfileUpdated;
 38102        friendsController.OnUpdateFriendship += OnUpdateFriendship;
 103
 38104        settings.generalSettings.OnChanged += OnSettingsChanged;
 38105        SetAllowUsersOption(settings.generalSettings.Data.voiceChatAllow);
 106
 38107        CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 38108        RendererState_OnChange(CommonScriptableObjects.rendererState.Get(), false);
 38109    }
 110
 111    public void SetVisibility(bool visible)
 112    {
 4113        if (voiceChatWindowView == null)
 1114            return;
 115
 3116        SetVisiblePanelList(visible);
 3117        if (visible)
 1118            voiceChatWindowView.Show();
 119        else
 2120            voiceChatWindowView.Hide();
 2121    }
 122
 123    private void SetVisiblePanelList(bool visible)
 124    {
 3125        HashSet<string> newSet = visibleTaskbarPanels.Get();
 3126        if (visible)
 1127            newSet.Add("VoiceChatWindow");
 128        else
 2129            newSet.Remove("VoiceChatWindow");
 130
 3131        visibleTaskbarPanels.Set(newSet, true);
 3132    }
 133
 134    public void SetUsersMuted(string[] usersId, bool isMuted)
 135    {
 16136        for (int i = 0; i < usersId.Length; i++)
 137        {
 6138            voiceChatWindowView.SetPlayerMuted(usersId[i], isMuted);
 139        }
 2140    }
 141
 142    public void SetUserRecording(string userId, bool isRecording)
 143    {
 2144        voiceChatWindowView.SetPlayerRecording(userId, isRecording);
 2145        SetWhichPlayerIsTalking();
 2146    }
 147
 148    public void SetVoiceChatRecording(bool recording)
 149    {
 2150        voiceChatBarView.PlayVoiceChatRecordingAnimation(recording);
 2151        isOwnPLayerTalking = recording;
 2152        SetWhichPlayerIsTalking();
 2153    }
 154
 155    public void SetVoiceChatEnabledByScene(bool enabled)
 156    {
 2157        if (voiceChatBarView == null)
 0158            return;
 159
 2160        voiceChatBarView.SetVoiceChatEnabledByScene(enabled);
 2161    }
 162
 163    public void Dispose()
 164    {
 39165        ReportMuteStatuses();
 166
 39167        if (updateMuteStatusRoutine != null)
 2168            CoroutineStarter.Stop(updateMuteStatusRoutine);
 169
 39170        if (voiceChatWindowView != null)
 171        {
 38172            voiceChatWindowView.OnClose -= CloseView;
 38173            voiceChatWindowView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 38174            voiceChatWindowView.OnGoToCrowd -= GoToCrowd;
 38175            voiceChatWindowView.OnMuteAll -= OnMuteAllToggled;
 38176            voiceChatWindowView.OnMuteUser -= MuteUser;
 38177            voiceChatWindowView.OnAllowUsersFilterChange -= ChangeAllowUsersFilter;
 178        }
 179
 39180        if (voiceChatBarView != null)
 38181            voiceChatBarView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 182
 39183        dataStore.voiceChat.isJoinedToVoiceChat.OnChange -= OnVoiceChatStatusUpdated;
 39184        dataStore.player.otherPlayers.OnAdded -= OnOtherPlayersStatusAdded;
 39185        dataStore.player.otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved;
 39186        ownProfile.OnUpdate -= OnUserProfileUpdated;
 39187        friendsController.OnUpdateFriendship -= OnUpdateFriendship;
 39188        settings.generalSettings.OnChanged -= OnSettingsChanged;
 39189        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 39190    }
 191
 192    internal void CloseView()
 193    {
 1194        OnCloseView?.Invoke();
 1195        SetVisibility(false);
 1196    }
 197
 198    internal void RequestJoinVoiceChat(bool isJoined)
 199    {
 2200        if (isJoined)
 201        {
 1202            WebInterface.JoinVoiceChat();
 203        }
 204        else
 205        {
 1206            if (dataStore.voiceChat.isRecording.Get().Key)
 1207                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 208
 1209            WebInterface.LeaveVoiceChat();
 210        }
 1211    }
 212
 213    internal void OnVoiceChatStatusUpdated(bool isJoined, bool previous)
 214    {
 40215        voiceChatWindowView.SetAsJoined(isJoined);
 40216        voiceChatBarView.SetAsJoined(isJoined);
 217
 40218        if (isJoined)
 219        {
 1220            socialAnalytics.SendVoiceChannelConnection(voiceChatWindowView.numberOfPlayers);
 1221            SetWhichPlayerIsTalking();
 222        }
 223        else
 224        {
 39225            socialAnalytics.SendVoiceChannelDisconnection();
 39226            isOwnPLayerTalking = false;
 227
 39228            if (dataStore.voiceChat.isRecording.Get().Key)
 3229                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 230        }
 39231    }
 232
 233    internal void GoToCrowd()
 234    {
 0235        DCL.Environment.i.world.teleportController.GoToCrowd();
 0236    }
 237
 238    internal void OnOtherPlayersStatusAdded(string userId, Player player)
 239    {
 1240        var otherProfile = userProfileBridge.Get(player.id);
 241
 1242        if (otherProfile == null)
 0243            return;
 244
 1245        voiceChatWindowView.AddOrUpdatePlayer(otherProfile);
 246
 1247        if (!trackedUsersHashSet.Contains(userId))
 248        {
 1249            trackedUsersHashSet.Add(userId);
 250
 1251            bool isMuted = ownProfile.muted.Contains(userId);
 1252            voiceChatWindowView.SetPlayerMuted(userId, isMuted);
 1253            voiceChatWindowView.SetPlayerBlocked(userId, ownProfile.blocked != null ? ownProfile.blocked.Contains(userId
 1254            voiceChatWindowView.SetPlayerAsFriend(userId, friendsController.ContainsStatus(userId, FriendshipStatus.FRIE
 1255            voiceChatWindowView.SetPlayerAsJoined(userId, dataStore.voiceChat.isJoinedToVoiceChat.Get());
 256
 1257            if (isMuteAll && !isMuted)
 0258                MuteUser(userId, true);
 259        }
 260
 1261        SetWhichPlayerIsTalking();
 1262    }
 263
 264    internal void OnOtherPlayerStatusRemoved(string userId, Player player)
 265    {
 1266        if (trackedUsersHashSet.Contains(userId))
 0267            trackedUsersHashSet.Remove(userId);
 268
 1269        voiceChatWindowView.RemoveUser(userId);
 1270        SetWhichPlayerIsTalking();
 1271    }
 272
 273    internal void OnMuteAllToggled(bool isMute)
 274    {
 2275        isMuteAll = isMute;
 276
 2277        if (!dataStore.voiceChat.isJoinedToVoiceChat.Get())
 2278            return;
 279
 0280        MuteAll(isMute);
 0281    }
 282
 283    internal void MuteAll(bool isMute)
 284    {
 2285        isMuteAll = isMute;
 286
 2287        if (isMute)
 1288            usersToUnmute.Clear();
 289        else
 1290            usersToMute.Clear();
 291
 2292        using (var iterator = trackedUsersHashSet.GetEnumerator())
 293        {
 2294            while (iterator.MoveNext())
 295            {
 0296                MuteUser(iterator.Current, isMute);
 297            }
 2298        }
 2299    }
 300
 301    internal void MuteUser(string userId, bool isMuted)
 302    {
 2303        var list = isMuted ? usersToMute : usersToUnmute;
 2304        list.Add(userId);
 305
 2306        if (updateMuteStatusRoutine == null)
 2307            updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine());
 308
 2309        if (isMuted)
 1310            socialAnalytics.SendPlayerMuted(userId);
 311        else
 1312            socialAnalytics.SendPlayerUnmuted(userId);
 1313    }
 314
 315    internal IEnumerator MuteStateUpdateRoutine()
 316    {
 2317        yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL);
 0318        ReportMuteStatuses();
 0319        updateMuteStatusRoutine = null;
 0320    }
 321
 322    internal void ReportMuteStatuses()
 323    {
 39324        if (usersToUnmute.Count > 0)
 2325            WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false);
 326
 39327        if (usersToMute.Count > 0)
 2328            WebInterface.SetMuteUsers(usersToMute.ToArray(), true);
 329
 39330        usersToUnmute.Clear();
 39331        usersToMute.Clear();
 39332    }
 333
 334    internal void ChangeAllowUsersFilter(string optionId)
 335    {
 3336        var newSettings = settings.generalSettings.Data;
 337
 3338        if (optionId == VoiceChatAllow.ALL_USERS.ToString())
 1339            newSettings.voiceChatAllow = VoiceChatAllow.ALL_USERS;
 2340        else if (optionId == VoiceChatAllow.VERIFIED_ONLY.ToString())
 1341            newSettings.voiceChatAllow = VoiceChatAllow.VERIFIED_ONLY;
 1342        else if (optionId == VoiceChatAllow.FRIENDS_ONLY.ToString())
 1343            newSettings.voiceChatAllow = VoiceChatAllow.FRIENDS_ONLY;
 344
 3345        settings.generalSettings.Apply(newSettings);
 3346    }
 347
 348    internal void OnUserProfileUpdated(UserProfile profile)
 349    {
 1350        using (var iterator = trackedUsersHashSet.GetEnumerator())
 351        {
 2352            while (iterator.MoveNext())
 353            {
 1354                voiceChatWindowView.SetPlayerBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contain
 355            }
 1356        }
 1357    }
 358
 4359    internal void OnUpdateFriendship(string userId, FriendshipAction action) { voiceChatWindowView.SetPlayerAsFriend(use
 360
 361    internal void OnSettingsChanged(GeneralSettings settings)
 362    {
 5363        SetAllowUsersOption(settings.voiceChatAllow);
 5364        socialAnalytics.SendVoiceChatPreferencesChanged(settings.voiceChatAllow);
 5365    }
 366
 367    private void SetAllowUsersOption(VoiceChatAllow option)
 368    {
 369        switch (option)
 370        {
 371            case VoiceChatAllow.ALL_USERS:
 9372                voiceChatWindowView.SelectAllowUsersOption(0);
 9373                break;
 374            case VoiceChatAllow.VERIFIED_ONLY:
 31375                voiceChatWindowView.SelectAllowUsersOption(1);
 31376                break;
 377            case VoiceChatAllow.FRIENDS_ONLY:
 3378                voiceChatWindowView.SelectAllowUsersOption(2);
 379                break;
 380        }
 3381    }
 382
 383    internal void RendererState_OnChange(bool current, bool previous)
 384    {
 38385        if (!current)
 38386            return;
 387
 0388        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 0389        RequestJoinVoiceChat(true);
 0390    }
 391
 392    internal void SetWhichPlayerIsTalking()
 393    {
 12394        if (isOwnPLayerTalking)
 3395            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_YOU);
 9396        else if (voiceChatWindowView.numberOfPlayers == 0)
 6397            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT);
 3398        else if (voiceChatWindowView.numberOfPlayersTalking == 0)
 1399            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_NOBODY_TALKING);
 2400        else if (voiceChatWindowView.numberOfPlayersTalking == 1)
 401        {
 1402            UserProfile userProfile = userProfileBridge.Get(voiceChatWindowView.GetUserTalkingByIndex(0));
 1403            voiceChatBarView.SetTalkingMessage(true, userProfile != null ? userProfile.userName : voiceChatWindowView.Ge
 404        }
 405        else
 1406            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING);
 1407    }
 408
 0409    protected internal virtual IVoiceChatWindowComponentView CreateVoiceChatWindowView() => VoiceChatWindowComponentView
 410
 0411    protected internal virtual IVoiceChatBarComponentView CreateVoiceChatBatView() => VoiceChatBarComponentView.Create()
 412
 0413    protected internal virtual IVoiceChatPlayerComponentView CreateVoiceChatPlayerView() => VoiceChatPlayerComponentView
 414}

Methods/Properties

VoiceChatWindowView()
VoiceChatBarView()
isVoiceChatFFEnabled()
visibleTaskbarPanels()
ownProfile()
VoiceChatWindowController()
VoiceChatWindowController(IUserProfileBridge, DCL.Social.Friends.IFriendsController, SocialFeaturesAnalytics.ISocialAnalytics, DCL.DataStore, DCL.SettingsCommon.Settings, DCL.IMouseCatcher)
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()