< Summary

Class:VoiceChatWindowController
Assembly:VoiceChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/VoiceChatHUD/VoiceChatWindowController.cs
Covered lines:179
Uncovered lines:19
Coverable lines:198
Total lines:381
Line coverage:90.4% (179 of 198)
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%
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.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 = "No one else is here";
 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
 3922    private bool isVoiceChatFFEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled(VOICE_CHAT_FEATURE_FLAG);
 8023    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;
 3932    internal HashSet<string> trackedUsersHashSet = new HashSet<string>();
 3933    internal readonly List<string> usersToMute = new List<string>();
 3934    internal readonly List<string> usersToUnmute = new List<string>();
 35    internal bool isOwnPLayerTalking = false;
 36    private Coroutine updateMuteStatusRoutine = null;
 37    internal bool isMuteAll = false;
 38
 7639    public VoiceChatWindowController() { }
 40
 141    public VoiceChatWindowController(
 42        IUserProfileBridge userProfileBridge,
 43        IFriendsController friendsController,
 44        ISocialAnalytics socialAnalytics,
 45        DataStore dataStore,
 46        Settings settings)
 47    {
 148        Initialize(userProfileBridge, friendsController, socialAnalytics, dataStore, settings);
 149    }
 50
 51    public void Initialize(
 52        IUserProfileBridge userProfileBridge,
 53        IFriendsController friendsController,
 54        ISocialAnalytics socialAnalytics,
 55        DataStore dataStore,
 56        Settings settings)
 57    {
 3958        this.userProfileBridge = userProfileBridge;
 3959        this.friendsController = friendsController;
 3960        this.socialAnalytics = socialAnalytics;
 3961        this.dataStore = dataStore;
 3962        this.settings = settings;
 63
 3964        if (!isVoiceChatFFEnabled)
 165            return;
 66
 3867        voiceChatWindowView = CreateVoiceChatWindowView();
 3868        voiceChatWindowView.Hide(instant: true);
 69
 3870        voiceChatWindowView.OnClose += CloseView;
 3871        voiceChatWindowView.OnJoinVoiceChat += RequestJoinVoiceChat;
 3872        voiceChatWindowView.OnGoToCrowd += GoToCrowd;
 3873        voiceChatWindowView.OnMuteAll += OnMuteAllToggled;
 3874        voiceChatWindowView.OnMuteUser += MuteUser;
 3875        voiceChatWindowView.OnAllowUsersFilterChange += ChangeAllowUsersFilter;
 3876        voiceChatWindowView.SetNumberOfPlayers(0);
 77
 3878        voiceChatBarView = CreateVoiceChatBatView();
 3879        voiceChatBarView.SetAsJoined(false);
 3880        voiceChatBarView.OnJoinVoiceChat += RequestJoinVoiceChat;
 81
 3882        dataStore.voiceChat.isJoinedToVoiceChat.OnChange += OnVoiceChatStatusUpdated;
 3883        OnVoiceChatStatusUpdated(dataStore.voiceChat.isJoinedToVoiceChat.Get(), false);
 84
 3885        dataStore.player.otherPlayers.OnAdded += OnOtherPlayersStatusAdded;
 3886        dataStore.player.otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved;
 3887        ownProfile.OnUpdate += OnUserProfileUpdated;
 3888        friendsController.OnUpdateFriendship += OnUpdateFriendship;
 89
 3890        settings.generalSettings.OnChanged += OnSettingsChanged;
 3891        SetAllowUsersOption(settings.generalSettings.Data.voiceChatAllow);
 92
 3893        CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 3894        RendererState_OnChange(CommonScriptableObjects.rendererState.Get(), false);
 3895    }
 96
 97    public void SetVisibility(bool visible)
 98    {
 499        if (voiceChatWindowView == null)
 1100            return;
 101
 3102        if (visible)
 1103            voiceChatWindowView.Show();
 104        else
 2105            voiceChatWindowView.Hide();
 2106    }
 107
 108    public void SetUsersMuted(string[] usersId, bool isMuted)
 109    {
 16110        for (int i = 0; i < usersId.Length; i++)
 111        {
 6112            voiceChatWindowView.SetPlayerMuted(usersId[i], isMuted);
 113        }
 2114    }
 115
 116    public void SetUserRecording(string userId, bool isRecording)
 117    {
 2118        voiceChatWindowView.SetPlayerRecording(userId, isRecording);
 2119        SetWhichPlayerIsTalking();
 2120    }
 121
 122    public void SetVoiceChatRecording(bool recording)
 123    {
 2124        voiceChatBarView.PlayVoiceChatRecordingAnimation(recording);
 2125        isOwnPLayerTalking = recording;
 2126        SetWhichPlayerIsTalking();
 2127    }
 128
 129    public void SetVoiceChatEnabledByScene(bool enabled)
 130    {
 2131        if (voiceChatBarView == null)
 0132            return;
 133
 2134        voiceChatBarView.SetVoiceChatEnabledByScene(enabled);
 2135    }
 136
 137    public void Dispose()
 138    {
 39139        ReportMuteStatuses();
 140
 39141        if (updateMuteStatusRoutine != null)
 2142            CoroutineStarter.Stop(updateMuteStatusRoutine);
 143
 39144        if (voiceChatWindowView != null)
 145        {
 38146            voiceChatWindowView.OnClose -= CloseView;
 38147            voiceChatWindowView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 38148            voiceChatWindowView.OnGoToCrowd -= GoToCrowd;
 38149            voiceChatWindowView.OnMuteAll -= OnMuteAllToggled;
 38150            voiceChatWindowView.OnMuteUser -= MuteUser;
 38151            voiceChatWindowView.OnAllowUsersFilterChange -= ChangeAllowUsersFilter;
 152        }
 153
 39154        if (voiceChatBarView != null)
 38155            voiceChatBarView.OnJoinVoiceChat -= RequestJoinVoiceChat;
 156
 39157        dataStore.voiceChat.isJoinedToVoiceChat.OnChange -= OnVoiceChatStatusUpdated;
 39158        dataStore.player.otherPlayers.OnAdded -= OnOtherPlayersStatusAdded;
 39159        dataStore.player.otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved;
 39160        ownProfile.OnUpdate -= OnUserProfileUpdated;
 39161        friendsController.OnUpdateFriendship -= OnUpdateFriendship;
 39162        settings.generalSettings.OnChanged -= OnSettingsChanged;
 39163        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 39164    }
 165
 2166    internal void CloseView() { SetVisibility(false); }
 167
 168    internal void RequestJoinVoiceChat(bool isJoined)
 169    {
 2170        if (isJoined)
 171        {
 1172            WebInterface.JoinVoiceChat();
 1173        }
 174        else
 175        {
 1176            if (dataStore.voiceChat.isRecording.Get().Key)
 1177                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 178
 1179            WebInterface.LeaveVoiceChat();
 180        }
 1181    }
 182
 183    internal void OnVoiceChatStatusUpdated(bool isJoined, bool previous)
 184    {
 40185        voiceChatWindowView.SetAsJoined(isJoined);
 40186        voiceChatBarView.SetAsJoined(isJoined);
 187
 40188        if (isJoined)
 189        {
 1190            socialAnalytics.SendVoiceChannelConnection(voiceChatWindowView.numberOfPlayers);
 1191            SetWhichPlayerIsTalking();
 1192        }
 193        else
 194        {
 39195            socialAnalytics.SendVoiceChannelDisconnection();
 39196            isOwnPLayerTalking = false;
 197
 39198            if (dataStore.voiceChat.isRecording.Get().Key)
 3199                dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true);
 200        }
 39201    }
 202
 0203    internal void GoToCrowd() { WebInterface.GoToCrowd(); }
 204
 205    internal void OnOtherPlayersStatusAdded(string userId, Player player)
 206    {
 1207        var otherProfile = userProfileBridge.Get(player.id);
 208
 1209        if (otherProfile == null)
 0210            return;
 211
 1212        voiceChatWindowView.AddOrUpdatePlayer(otherProfile);
 213
 1214        if (!trackedUsersHashSet.Contains(userId))
 215        {
 1216            trackedUsersHashSet.Add(userId);
 217
 1218            bool isMuted = ownProfile.muted.Contains(userId);
 1219            voiceChatWindowView.SetPlayerMuted(userId, isMuted);
 1220            voiceChatWindowView.SetPlayerBlocked(userId, ownProfile.blocked != null ? ownProfile.blocked.Contains(userId
 1221            voiceChatWindowView.SetPlayerAsFriend(userId, friendsController.ContainsStatus(userId, FriendshipStatus.FRIE
 1222            voiceChatWindowView.SetPlayerAsJoined(userId, dataStore.voiceChat.isJoinedToVoiceChat.Get());
 223
 1224            if (isMuteAll && !isMuted)
 0225                MuteUser(userId, true);
 226        }
 227
 1228        SetWhichPlayerIsTalking();
 1229    }
 230
 231    internal void OnOtherPlayerStatusRemoved(string userId, Player player)
 232    {
 1233        if (trackedUsersHashSet.Contains(userId))
 0234            trackedUsersHashSet.Remove(userId);
 235
 1236        voiceChatWindowView.RemoveUser(userId);
 1237        SetWhichPlayerIsTalking();
 1238    }
 239
 240    internal void OnMuteAllToggled(bool isMute)
 241    {
 2242        isMuteAll = isMute;
 243
 2244        if (!dataStore.voiceChat.isJoinedToVoiceChat.Get())
 2245            return;
 246
 0247        MuteAll(isMute);
 0248    }
 249
 250    internal void MuteAll(bool isMute)
 251    {
 2252        isMuteAll = isMute;
 253
 2254        if (isMute)
 1255            usersToUnmute.Clear();
 256        else
 1257            usersToMute.Clear();
 258
 2259        using (var iterator = trackedUsersHashSet.GetEnumerator())
 260        {
 2261            while (iterator.MoveNext())
 262            {
 0263                MuteUser(iterator.Current, isMute);
 264            }
 2265        }
 2266    }
 267
 268    internal void MuteUser(string userId, bool isMuted)
 269    {
 2270        var list = isMuted ? usersToMute : usersToUnmute;
 2271        list.Add(userId);
 272
 2273        if (updateMuteStatusRoutine == null)
 2274            updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine());
 275
 2276        if (isMuted)
 1277            socialAnalytics.SendPlayerMuted(userId);
 278        else
 1279            socialAnalytics.SendPlayerUnmuted(userId);
 1280    }
 281
 282    internal IEnumerator MuteStateUpdateRoutine()
 283    {
 2284        yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL);
 0285        ReportMuteStatuses();
 0286        updateMuteStatusRoutine = null;
 0287    }
 288
 289    internal void ReportMuteStatuses()
 290    {
 39291        if (usersToUnmute.Count > 0)
 2292            WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false);
 293
 39294        if (usersToMute.Count > 0)
 2295            WebInterface.SetMuteUsers(usersToMute.ToArray(), true);
 296
 39297        usersToUnmute.Clear();
 39298        usersToMute.Clear();
 39299    }
 300
 301    internal void ChangeAllowUsersFilter(string optionId)
 302    {
 3303        var newSettings = settings.generalSettings.Data;
 304
 3305        if (optionId == VoiceChatAllow.ALL_USERS.ToString())
 1306            newSettings.voiceChatAllow = VoiceChatAllow.ALL_USERS;
 2307        else if (optionId == VoiceChatAllow.VERIFIED_ONLY.ToString())
 1308            newSettings.voiceChatAllow = VoiceChatAllow.VERIFIED_ONLY;
 1309        else if (optionId == VoiceChatAllow.FRIENDS_ONLY.ToString())
 1310            newSettings.voiceChatAllow = VoiceChatAllow.FRIENDS_ONLY;
 311
 3312        settings.generalSettings.Apply(newSettings);
 3313    }
 314
 315    internal void OnUserProfileUpdated(UserProfile profile)
 316    {
 1317        using (var iterator = trackedUsersHashSet.GetEnumerator())
 318        {
 2319            while (iterator.MoveNext())
 320            {
 1321                voiceChatWindowView.SetPlayerBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contain
 322            }
 1323        }
 1324    }
 325
 4326    internal void OnUpdateFriendship(string userId, FriendshipAction action) { voiceChatWindowView.SetPlayerAsFriend(use
 327
 328    internal void OnSettingsChanged(GeneralSettings settings)
 329    {
 5330        SetAllowUsersOption(settings.voiceChatAllow);
 5331        socialAnalytics.SendVoiceChatPreferencesChanged(settings.voiceChatAllow);
 5332    }
 333
 334    private void SetAllowUsersOption(VoiceChatAllow option)
 335    {
 336        switch (option)
 337        {
 338            case VoiceChatAllow.ALL_USERS:
 9339                voiceChatWindowView.SelectAllowUsersOption(0);
 9340                break;
 341            case VoiceChatAllow.VERIFIED_ONLY:
 31342                voiceChatWindowView.SelectAllowUsersOption(1);
 31343                break;
 344            case VoiceChatAllow.FRIENDS_ONLY:
 3345                voiceChatWindowView.SelectAllowUsersOption(2);
 346                break;
 347        }
 3348    }
 349
 350    internal void RendererState_OnChange(bool current, bool previous)
 351    {
 38352        if (!current)
 38353            return;
 354
 0355        CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange;
 0356        RequestJoinVoiceChat(true);
 0357    }
 358
 359    internal void SetWhichPlayerIsTalking()
 360    {
 12361        if (isOwnPLayerTalking)
 3362            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_YOU);
 9363        else if (voiceChatWindowView.numberOfPlayers == 0)
 6364            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT);
 3365        else if (voiceChatWindowView.numberOfPlayersTalking == 0)
 1366            voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_NOBODY_TALKING);
 2367        else if (voiceChatWindowView.numberOfPlayersTalking == 1)
 368        {
 1369            UserProfile userProfile = userProfileBridge.Get(voiceChatWindowView.GetUserTalkingByIndex(0));
 1370            voiceChatBarView.SetTalkingMessage(true, userProfile != null ? userProfile.userName : voiceChatWindowView.Ge
 1371        }
 372        else
 1373            voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING);
 1374    }
 375
 0376    protected internal virtual IVoiceChatWindowComponentView CreateVoiceChatWindowView() => VoiceChatWindowComponentView
 377
 0378    protected internal virtual IVoiceChatBarComponentView CreateVoiceChatBatView() => VoiceChatBarComponentView.Create()
 379
 0380    protected internal virtual IVoiceChatPlayerComponentView CreateVoiceChatPlayerView() => VoiceChatPlayerComponentView
 381}