< Summary

Class:VoiceChatWindowController
Assembly:VoiceChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/VoiceChatHUD/VoiceChatWindowController.cs
Covered lines:173
Uncovered lines:20
Coverable lines:193
Total lines:372
Line coverage:89.6% (173 of 193)
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%220100%
SetVisibility(...)0%330100%
SetUsersMuted(...)0%220100%
SetUserRecording(...)0%110100%
SetVoiceChatRecording(...)0%110100%
SetVoiceChatEnabledByScene(...)0%2.062075%
Dispose()0%440100%
CloseView()0%110100%
JoinVoiceChat(...)0%330100%
LeaveVoiceChat()0%2100%
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%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.Collections;
 6using System.Collections.Generic;
 7using UnityEngine;
 8using static DCL.SettingsCommon.GeneralSettings;
 9
 10public class VoiceChatWindowController : IHUD
 11{
 12    internal const string VOICE_CHAT_FEATURE_FLAG = "voice_chat";
 13    internal const float MUTE_STATUS_UPDATE_INTERVAL = 0.3f;
 14    internal const string TALKING_MESSAGE_YOU = "You";
 15    internal const string TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT = "Just you in the voice chat";
 16    internal const string TALKING_MESSAGE_NOBODY_TALKING = "Nobody is talking";
 17    internal const string TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING = "Several people talking";
 18
 019    public IVoiceChatWindowComponentView VoiceChatWindowView => voiceChatWindowView;
 020    public IVoiceChatBarComponentView VoiceChatBarView => voiceChatBarView;
 21
 3722    private bool isVoiceChatFFEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled(VOICE_CHAT_FEATURE_FLAG);
 7623    private UserProfile ownProfile => userProfileBridge.GetOwn();
 24
 25    private IVoiceChatWindowComponentView voiceChatWindowView;
 26    private IVoiceChatBarComponentView voiceChatBarView;
 27    private IUserProfileBridge userProfileBridge;
 28    private IFriendsController friendsController;
 29    private ISocialAnalytics socialAnalytics;
 30    private DataStore dataStore;
 31    private Settings settings;
 3732    internal HashSet<string> trackedUsersHashSet = new HashSet<string>();
 3733    internal readonly List<string> usersToMute = new List<string>();
 3734    internal readonly List<string> usersToUnmute = new List<string>();
 35    internal bool isOwnPLayerTalking = false;
 36    private Coroutine updateMuteStatusRoutine = null;
 37    internal bool isMuteAll = false;
 38    internal bool isJoined = false;
 39
 7240    public VoiceChatWindowController() { }
 41
 142    public VoiceChatWindowController(
 43        IUserProfileBridge userProfileBridge,
 44        IFriendsController friendsController,
 45        ISocialAnalytics socialAnalytics,
 46        DataStore dataStore,
 47        Settings settings)
 48    {
 149        Initialize(userProfileBridge, friendsController, socialAnalytics, dataStore, settings);
 150    }
 51
 52    public void Initialize(
 53        IUserProfileBridge userProfileBridge,
 54        IFriendsController friendsController,
 55        ISocialAnalytics socialAnalytics,
 56        DataStore dataStore,
 57        Settings settings)
 58    {
 3759        this.userProfileBridge = userProfileBridge;
 3760        this.friendsController = friendsController;
 3761        this.socialAnalytics = socialAnalytics;
 3762        this.dataStore = dataStore;
 3763        this.settings = settings;
 64
 3765        if (!isVoiceChatFFEnabled)
 166            return;
 67
 3668        voiceChatWindowView = CreateVoiceChatWindowView();
 3669        voiceChatWindowView.Hide(instant: true);
 70
 3671        voiceChatWindowView.OnClose += CloseView;
 3672        voiceChatWindowView.OnJoinVoiceChat += JoinVoiceChat;
 3673        voiceChatWindowView.OnGoToCrowd += GoToCrowd;
 3674        voiceChatWindowView.OnMuteAll += OnMuteAllToggled;
 3675        voiceChatWindowView.OnMuteUser += MuteUser;
 3676        voiceChatWindowView.OnAllowUsersFilterChange += ChangeAllowUsersFilter;
 3677        voiceChatWindowView.SetNumberOfPlayers(0);
 78
 3679        voiceChatBarView = CreateVoiceChatBatView();
 3680        voiceChatBarView.Hide(instant: true);
 3681        voiceChatBarView.OnLeaveVoiceChat += LeaveVoiceChat;
 82
 3683        dataStore.player.otherPlayers.OnAdded += OnOtherPlayersStatusAdded;
 3684        dataStore.player.otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved;
 3685        ownProfile.OnUpdate += OnUserProfileUpdated;
 3686        friendsController.OnUpdateFriendship += OnUpdateFriendship;
 87
 3688        settings.generalSettings.OnChanged += OnSettingsChanged;
 89
 3690        CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 3691        RendererState_OnChange(CommonScriptableObjects.rendererState.Get(), false);
 3692    }
 93
 94    public void SetVisibility(bool visible)
 95    {
 496        if (voiceChatWindowView == null)
 197            return;
 98
 399        if (visible)
 1100            voiceChatWindowView.Show();
 101        else
 2102            voiceChatWindowView.Hide();
 2103    }
 104
 105    public void SetUsersMuted(string[] usersId, bool isMuted)
 106    {
 16107        for (int i = 0; i < usersId.Length; i++)
 108        {
 6109            voiceChatWindowView.SetPlayerMuted(usersId[i], isMuted);
 110        }
 2111    }
 112
 113    public void SetUserRecording(string userId, bool isRecording)
 114    {
 2115        voiceChatWindowView.SetPlayerRecording(userId, isRecording);
 2116        SetWhichPlayerIsTalking();
 2117    }
 118
 119    public void SetVoiceChatRecording(bool recording)
 120    {
 2121        voiceChatBarView.PlayVoiceChatRecordingAnimation(recording);
 2122        isOwnPLayerTalking = recording;
 2123        SetWhichPlayerIsTalking();
 2124    }
 125
 126    public void SetVoiceChatEnabledByScene(bool enabled)
 127    {
 2128        if (voiceChatBarView == null)
 0129            return;
 130
 2131        voiceChatBarView.SetVoiceChatEnabledByScene(enabled);
 2132    }
 133
 134    public void Dispose()
 135    {
 37136        ReportMuteStatuses();
 137
 37138        if (updateMuteStatusRoutine != null)
 2139            CoroutineStarter.Stop(updateMuteStatusRoutine);
 140
 37141        if (voiceChatWindowView != null)
 142        {
 36143            voiceChatWindowView.OnClose -= CloseView;
 36144            voiceChatWindowView.OnJoinVoiceChat -= JoinVoiceChat;
 36145            voiceChatWindowView.OnGoToCrowd -= GoToCrowd;
 36146            voiceChatWindowView.OnMuteAll -= OnMuteAllToggled;
 36147            voiceChatWindowView.OnMuteUser -= MuteUser;
 36148            voiceChatWindowView.OnAllowUsersFilterChange -= ChangeAllowUsersFilter;
 149        }
 150
 37151        if (voiceChatBarView != null)
 36152            voiceChatBarView.OnLeaveVoiceChat -= LeaveVoiceChat;
 153
 37154        dataStore.player.otherPlayers.OnAdded -= OnOtherPlayersStatusAdded;
 37155        dataStore.player.otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved;
 37156        ownProfile.OnUpdate -= OnUserProfileUpdated;
 37157        friendsController.OnUpdateFriendship -= OnUpdateFriendship;
 37158        settings.generalSettings.OnChanged -= OnSettingsChanged;
 37159        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 37160    }
 161
 2162    internal void CloseView() { SetVisibility(false); }
 163
 164    internal void JoinVoiceChat(bool isJoined)
 165    {
 2166        voiceChatWindowView.SetAsJoined(isJoined);
 167
 2168        if (isJoined)
 169        {
 1170            voiceChatBarView.Show();
 1171            SetWhichPlayerIsTalking();
 1172        }
 173        else
 174        {
 1175            dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 1176            isOwnPLayerTalking = false;
 1177            voiceChatBarView.Hide();
 178        }
 179
 2180        this.isJoined = isJoined;
 181
 2182        if (!isJoined)
 183        {
 1184            MuteAll(true);
 1185            socialAnalytics.SendVoiceChannelDisconnection();
 1186        }
 187        else
 188        {
 1189            MuteAll(voiceChatWindowView.isMuteAllOn);
 1190            socialAnalytics.SendVoiceChannelConnection(voiceChatWindowView.numberOfPlayers);
 191        }
 192
 2193        dataStore.voiceChat.isJoinedToVoiceChat.Set(isJoined);
 2194    }
 195
 0196    internal void LeaveVoiceChat() { JoinVoiceChat(false); }
 197
 0198    internal void GoToCrowd() { WebInterface.GoToCrowd(); }
 199
 200    internal void OnOtherPlayersStatusAdded(string userId, Player player)
 201    {
 1202        var otherProfile = userProfileBridge.Get(player.id);
 203
 1204        if (otherProfile == null)
 0205            return;
 206
 1207        voiceChatWindowView.AddOrUpdatePlayer(otherProfile);
 208
 1209        if (!trackedUsersHashSet.Contains(userId))
 210        {
 1211            trackedUsersHashSet.Add(userId);
 212
 1213            bool isMuted = ownProfile.muted.Contains(userId);
 1214            voiceChatWindowView.SetPlayerMuted(userId, isMuted);
 1215            voiceChatWindowView.SetPlayerBlocked(userId, ownProfile.blocked != null ? ownProfile.blocked.Contains(userId
 1216            voiceChatWindowView.SetPlayerAsFriend(userId, friendsController.ContainsStatus(userId, FriendshipStatus.FRIE
 1217            voiceChatWindowView.SetPlayerAsJoined(userId, dataStore.voiceChat.isJoinedToVoiceChat.Get());
 218
 1219            if (isMuteAll && !isMuted)
 0220                MuteUser(userId, true);
 221        }
 222
 1223        SetWhichPlayerIsTalking();
 1224    }
 225
 226    internal void OnOtherPlayerStatusRemoved(string userId, Player player)
 227    {
 1228        if (trackedUsersHashSet.Contains(userId))
 0229            trackedUsersHashSet.Remove(userId);
 230
 1231        voiceChatWindowView.RemoveUser(userId);
 1232        SetWhichPlayerIsTalking();
 1233    }
 234
 235    internal void OnMuteAllToggled(bool isMute)
 236    {
 2237        isMuteAll = isMute;
 238
 2239        if (!isJoined)
 2240            return;
 241
 0242        MuteAll(isMute);
 0243    }
 244
 245    internal void MuteAll(bool isMute)
 246    {
 4247        isMuteAll = isMute;
 248
 4249        if (isMute)
 2250            usersToUnmute.Clear();
 251        else
 2252            usersToMute.Clear();
 253
 4254        using (var iterator = trackedUsersHashSet.GetEnumerator())
 255        {
 4256            while (iterator.MoveNext())
 257            {
 0258                MuteUser(iterator.Current, isMute);
 259            }
 4260        }
 4261    }
 262
 263    internal void MuteUser(string userId, bool isMuted)
 264    {
 2265        var list = isMuted ? usersToMute : usersToUnmute;
 2266        list.Add(userId);
 267
 2268        if (updateMuteStatusRoutine == null)
 2269            updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine());
 270
 2271        if (isMuted)
 1272            socialAnalytics.SendPlayerMuted(userId);
 273        else
 1274            socialAnalytics.SendPlayerUnmuted(userId);
 1275    }
 276
 277    internal IEnumerator MuteStateUpdateRoutine()
 278    {
 2279        yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL);
 0280        ReportMuteStatuses();
 0281        updateMuteStatusRoutine = null;
 0282    }
 283
 284    internal void ReportMuteStatuses()
 285    {
 37286        if (usersToUnmute.Count > 0)
 2287            WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false);
 288
 37289        if (usersToMute.Count > 0)
 2290            WebInterface.SetMuteUsers(usersToMute.ToArray(), true);
 291
 37292        usersToUnmute.Clear();
 37293        usersToMute.Clear();
 37294    }
 295
 296    internal void ChangeAllowUsersFilter(string optionId)
 297    {
 3298        var newSettings = settings.generalSettings.Data;
 299
 3300        if (optionId == VoiceChatAllow.ALL_USERS.ToString())
 1301            newSettings.voiceChatAllow = VoiceChatAllow.ALL_USERS;
 2302        else if (optionId == VoiceChatAllow.VERIFIED_ONLY.ToString())
 1303            newSettings.voiceChatAllow = VoiceChatAllow.VERIFIED_ONLY;
 1304        else if (optionId == VoiceChatAllow.FRIENDS_ONLY.ToString())
 1305            newSettings.voiceChatAllow = VoiceChatAllow.FRIENDS_ONLY;
 306
 3307        settings.generalSettings.Apply(newSettings);
 3308    }
 309
 310    internal void OnUserProfileUpdated(UserProfile profile)
 311    {
 1312        using (var iterator = trackedUsersHashSet.GetEnumerator())
 313        {
 2314            while (iterator.MoveNext())
 315            {
 1316                voiceChatWindowView.SetPlayerBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contain
 317            }
 1318        }
 1319    }
 320
 4321    internal void OnUpdateFriendship(string userId, FriendshipAction action) { voiceChatWindowView.SetPlayerAsFriend(use
 322
 323    internal void OnSettingsChanged(GeneralSettings settings)
 324    {
 5325        switch (settings.voiceChatAllow)
 326        {
 327            case VoiceChatAllow.ALL_USERS:
 1328                voiceChatWindowView.SelectAllowUsersOption(0);
 1329                break;
 330            case VoiceChatAllow.VERIFIED_ONLY:
 2331                voiceChatWindowView.SelectAllowUsersOption(1);
 2332                break;
 333            case VoiceChatAllow.FRIENDS_ONLY:
 2334                voiceChatWindowView.SelectAllowUsersOption(2);
 335                break;
 336        }
 337
 5338        socialAnalytics.SendVoiceChatPreferencesChanged(settings.voiceChatAllow);
 5339    }
 340
 341    internal void RendererState_OnChange(bool current, bool previous)
 342    {
 36343        if (!current)
 36344            return;
 345
 0346        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 0347        JoinVoiceChat(true);
 0348    }
 349
 350    internal void SetWhichPlayerIsTalking()
 351    {
 12352        if (isOwnPLayerTalking)
 3353            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_YOU);
 9354        else if (voiceChatWindowView.numberOfPlayers == 0)
 6355            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT);
 3356        else if (voiceChatWindowView.numberOfPlayersTalking == 0)
 1357            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_NOBODY_TALKING);
 2358        else if (voiceChatWindowView.numberOfPlayersTalking == 1)
 359        {
 1360            UserProfile userProfile = userProfileBridge.Get(voiceChatWindowView.GetUserTalkingByIndex(0));
 1361            voiceChatBarView.SetTalkingMessage(true, userProfile != null ? userProfile.userName : voiceChatWindowView.Ge
 1362        }
 363        else
 1364            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING);
 1365    }
 366
 0367    protected internal virtual IVoiceChatWindowComponentView CreateVoiceChatWindowView() => VoiceChatWindowComponentView
 368
 0369    protected internal virtual IVoiceChatBarComponentView CreateVoiceChatBatView() => VoiceChatBarComponentView.Create()
 370
 0371    protected internal virtual IVoiceChatPlayerComponentView CreateVoiceChatPlayerView() => VoiceChatPlayerComponentView
 372}