| | 1 | | using DCL; |
| | 2 | | using DCL.Interface; |
| | 3 | | using DCL.SettingsCommon; |
| | 4 | | using SocialFeaturesAnalytics; |
| | 5 | | using System; |
| | 6 | | using System.Collections; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using UnityEngine; |
| | 9 | | using static DCL.SettingsCommon.GeneralSettings; |
| | 10 | |
|
| | 11 | | public 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 | |
|
| 0 | 20 | | public IVoiceChatWindowComponentView VoiceChatWindowView => voiceChatWindowView; |
| 0 | 21 | | public IVoiceChatBarComponentView VoiceChatBarView => voiceChatBarView; |
| | 22 | |
|
| 39 | 23 | | private bool isVoiceChatFFEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled(VOICE_CHAT_FEATURE_FLAG); |
| 6 | 24 | | internal BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| 80 | 25 | | 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; |
| 39 | 35 | | internal HashSet<string> trackedUsersHashSet = new HashSet<string>(); |
| 39 | 36 | | internal readonly List<string> usersToMute = new List<string>(); |
| 39 | 37 | | 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 | |
|
| 76 | 45 | | public VoiceChatWindowController() { } |
| | 46 | |
|
| 1 | 47 | | public VoiceChatWindowController( |
| | 48 | | IUserProfileBridge userProfileBridge, |
| | 49 | | IFriendsController friendsController, |
| | 50 | | ISocialAnalytics socialAnalytics, |
| | 51 | | DataStore dataStore, |
| | 52 | | Settings settings, |
| | 53 | | IMouseCatcher mouseCatcher) |
| | 54 | | { |
| 1 | 55 | | Initialize(userProfileBridge, friendsController, socialAnalytics, dataStore, settings, mouseCatcher); |
| 1 | 56 | | } |
| | 57 | |
|
| | 58 | | public void Initialize( |
| | 59 | | IUserProfileBridge userProfileBridge, |
| | 60 | | IFriendsController friendsController, |
| | 61 | | ISocialAnalytics socialAnalytics, |
| | 62 | | DataStore dataStore, |
| | 63 | | Settings settings, |
| | 64 | | IMouseCatcher mouseCatcher) |
| | 65 | | { |
| 39 | 66 | | this.userProfileBridge = userProfileBridge; |
| 39 | 67 | | this.friendsController = friendsController; |
| 39 | 68 | | this.socialAnalytics = socialAnalytics; |
| 39 | 69 | | this.dataStore = dataStore; |
| 39 | 70 | | this.settings = settings; |
| 39 | 71 | | this.mouseCatcher = mouseCatcher; |
| | 72 | |
|
| 39 | 73 | | if (!isVoiceChatFFEnabled) |
| 1 | 74 | | return; |
| | 75 | |
|
| 38 | 76 | | if(mouseCatcher != null) |
| 38 | 77 | | mouseCatcher.OnMouseLock += CloseView; |
| | 78 | |
|
| 38 | 79 | | voiceChatWindowView = CreateVoiceChatWindowView(); |
| 38 | 80 | | voiceChatWindowView.Hide(instant: true); |
| | 81 | |
|
| 38 | 82 | | voiceChatWindowView.OnClose += CloseView; |
| 38 | 83 | | voiceChatWindowView.OnJoinVoiceChat += RequestJoinVoiceChat; |
| 38 | 84 | | voiceChatWindowView.OnGoToCrowd += GoToCrowd; |
| 38 | 85 | | voiceChatWindowView.OnMuteAll += OnMuteAllToggled; |
| 38 | 86 | | voiceChatWindowView.OnMuteUser += MuteUser; |
| 38 | 87 | | voiceChatWindowView.OnAllowUsersFilterChange += ChangeAllowUsersFilter; |
| 38 | 88 | | voiceChatWindowView.SetNumberOfPlayers(0); |
| | 89 | |
|
| 38 | 90 | | voiceChatBarView = CreateVoiceChatBatView(); |
| 38 | 91 | | voiceChatBarView.SetAsJoined(false); |
| 38 | 92 | | voiceChatBarView.OnJoinVoiceChat += RequestJoinVoiceChat; |
| | 93 | |
|
| 38 | 94 | | dataStore.voiceChat.isJoinedToVoiceChat.OnChange += OnVoiceChatStatusUpdated; |
| 38 | 95 | | OnVoiceChatStatusUpdated(dataStore.voiceChat.isJoinedToVoiceChat.Get(), false); |
| | 96 | |
|
| 38 | 97 | | dataStore.player.otherPlayers.OnAdded += OnOtherPlayersStatusAdded; |
| 38 | 98 | | dataStore.player.otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved; |
| 38 | 99 | | ownProfile.OnUpdate += OnUserProfileUpdated; |
| 38 | 100 | | friendsController.OnUpdateFriendship += OnUpdateFriendship; |
| | 101 | |
|
| 38 | 102 | | settings.generalSettings.OnChanged += OnSettingsChanged; |
| 38 | 103 | | SetAllowUsersOption(settings.generalSettings.Data.voiceChatAllow); |
| | 104 | |
|
| 38 | 105 | | CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange; |
| 38 | 106 | | RendererState_OnChange(CommonScriptableObjects.rendererState.Get(), false); |
| 38 | 107 | | } |
| | 108 | |
|
| | 109 | | public void SetVisibility(bool visible) |
| | 110 | | { |
| 4 | 111 | | if (voiceChatWindowView == null) |
| 1 | 112 | | return; |
| | 113 | |
|
| 3 | 114 | | SetVisiblePanelList(visible); |
| 3 | 115 | | if (visible) |
| 1 | 116 | | voiceChatWindowView.Show(); |
| | 117 | | else |
| 2 | 118 | | voiceChatWindowView.Hide(); |
| 2 | 119 | | } |
| | 120 | |
|
| | 121 | | private void SetVisiblePanelList(bool visible) |
| | 122 | | { |
| 3 | 123 | | HashSet<string> newSet = visibleTaskbarPanels.Get(); |
| 3 | 124 | | if (visible) |
| 1 | 125 | | newSet.Add("VoiceChatWindow"); |
| | 126 | | else |
| 2 | 127 | | newSet.Remove("VoiceChatWindow"); |
| | 128 | |
|
| 3 | 129 | | visibleTaskbarPanels.Set(newSet, true); |
| 3 | 130 | | } |
| | 131 | |
|
| | 132 | | public void SetUsersMuted(string[] usersId, bool isMuted) |
| | 133 | | { |
| 16 | 134 | | for (int i = 0; i < usersId.Length; i++) |
| | 135 | | { |
| 6 | 136 | | voiceChatWindowView.SetPlayerMuted(usersId[i], isMuted); |
| | 137 | | } |
| 2 | 138 | | } |
| | 139 | |
|
| | 140 | | public void SetUserRecording(string userId, bool isRecording) |
| | 141 | | { |
| 2 | 142 | | voiceChatWindowView.SetPlayerRecording(userId, isRecording); |
| 2 | 143 | | SetWhichPlayerIsTalking(); |
| 2 | 144 | | } |
| | 145 | |
|
| | 146 | | public void SetVoiceChatRecording(bool recording) |
| | 147 | | { |
| 2 | 148 | | voiceChatBarView.PlayVoiceChatRecordingAnimation(recording); |
| 2 | 149 | | isOwnPLayerTalking = recording; |
| 2 | 150 | | SetWhichPlayerIsTalking(); |
| 2 | 151 | | } |
| | 152 | |
|
| | 153 | | public void SetVoiceChatEnabledByScene(bool enabled) |
| | 154 | | { |
| 2 | 155 | | if (voiceChatBarView == null) |
| 0 | 156 | | return; |
| | 157 | |
|
| 2 | 158 | | voiceChatBarView.SetVoiceChatEnabledByScene(enabled); |
| 2 | 159 | | } |
| | 160 | |
|
| | 161 | | public void Dispose() |
| | 162 | | { |
| 39 | 163 | | ReportMuteStatuses(); |
| | 164 | |
|
| 39 | 165 | | if (updateMuteStatusRoutine != null) |
| 2 | 166 | | CoroutineStarter.Stop(updateMuteStatusRoutine); |
| | 167 | |
|
| 39 | 168 | | if (voiceChatWindowView != null) |
| | 169 | | { |
| 38 | 170 | | voiceChatWindowView.OnClose -= CloseView; |
| 38 | 171 | | voiceChatWindowView.OnJoinVoiceChat -= RequestJoinVoiceChat; |
| 38 | 172 | | voiceChatWindowView.OnGoToCrowd -= GoToCrowd; |
| 38 | 173 | | voiceChatWindowView.OnMuteAll -= OnMuteAllToggled; |
| 38 | 174 | | voiceChatWindowView.OnMuteUser -= MuteUser; |
| 38 | 175 | | voiceChatWindowView.OnAllowUsersFilterChange -= ChangeAllowUsersFilter; |
| | 176 | | } |
| | 177 | |
|
| 39 | 178 | | if (voiceChatBarView != null) |
| 38 | 179 | | voiceChatBarView.OnJoinVoiceChat -= RequestJoinVoiceChat; |
| | 180 | |
|
| 39 | 181 | | dataStore.voiceChat.isJoinedToVoiceChat.OnChange -= OnVoiceChatStatusUpdated; |
| 39 | 182 | | dataStore.player.otherPlayers.OnAdded -= OnOtherPlayersStatusAdded; |
| 39 | 183 | | dataStore.player.otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved; |
| 39 | 184 | | ownProfile.OnUpdate -= OnUserProfileUpdated; |
| 39 | 185 | | friendsController.OnUpdateFriendship -= OnUpdateFriendship; |
| 39 | 186 | | settings.generalSettings.OnChanged -= OnSettingsChanged; |
| 39 | 187 | | CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange; |
| 39 | 188 | | } |
| | 189 | |
|
| | 190 | | internal void CloseView() |
| | 191 | | { |
| 1 | 192 | | OnCloseView?.Invoke(); |
| 1 | 193 | | SetVisibility(false); |
| 1 | 194 | | } |
| | 195 | |
|
| | 196 | | internal void RequestJoinVoiceChat(bool isJoined) |
| | 197 | | { |
| 2 | 198 | | if (isJoined) |
| | 199 | | { |
| 1 | 200 | | WebInterface.JoinVoiceChat(); |
| 1 | 201 | | } |
| | 202 | | else |
| | 203 | | { |
| 1 | 204 | | if (dataStore.voiceChat.isRecording.Get().Key) |
| 1 | 205 | | dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true); |
| | 206 | |
|
| 1 | 207 | | WebInterface.LeaveVoiceChat(); |
| | 208 | | } |
| 1 | 209 | | } |
| | 210 | |
|
| | 211 | | internal void OnVoiceChatStatusUpdated(bool isJoined, bool previous) |
| | 212 | | { |
| 40 | 213 | | voiceChatWindowView.SetAsJoined(isJoined); |
| 40 | 214 | | voiceChatBarView.SetAsJoined(isJoined); |
| | 215 | |
|
| 40 | 216 | | if (isJoined) |
| | 217 | | { |
| 1 | 218 | | socialAnalytics.SendVoiceChannelConnection(voiceChatWindowView.numberOfPlayers); |
| 1 | 219 | | SetWhichPlayerIsTalking(); |
| 1 | 220 | | } |
| | 221 | | else |
| | 222 | | { |
| 39 | 223 | | socialAnalytics.SendVoiceChannelDisconnection(); |
| 39 | 224 | | isOwnPLayerTalking = false; |
| | 225 | |
|
| 39 | 226 | | if (dataStore.voiceChat.isRecording.Get().Key) |
| 3 | 227 | | dataStore.voiceChat.isRecording.Set(new KeyValuePair<bool, bool>(false, false), true); |
| | 228 | | } |
| 39 | 229 | | } |
| | 230 | |
|
| | 231 | | internal void GoToCrowd() |
| | 232 | | { |
| 0 | 233 | | DCL.Environment.i.world.teleportController.GoToCrowd(); |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | internal void OnOtherPlayersStatusAdded(string userId, Player player) |
| | 237 | | { |
| 1 | 238 | | var otherProfile = userProfileBridge.Get(player.id); |
| | 239 | |
|
| 1 | 240 | | if (otherProfile == null) |
| 0 | 241 | | return; |
| | 242 | |
|
| 1 | 243 | | voiceChatWindowView.AddOrUpdatePlayer(otherProfile); |
| | 244 | |
|
| 1 | 245 | | if (!trackedUsersHashSet.Contains(userId)) |
| | 246 | | { |
| 1 | 247 | | trackedUsersHashSet.Add(userId); |
| | 248 | |
|
| 1 | 249 | | bool isMuted = ownProfile.muted.Contains(userId); |
| 1 | 250 | | voiceChatWindowView.SetPlayerMuted(userId, isMuted); |
| 1 | 251 | | voiceChatWindowView.SetPlayerBlocked(userId, ownProfile.blocked != null ? ownProfile.blocked.Contains(userId |
| 1 | 252 | | voiceChatWindowView.SetPlayerAsFriend(userId, friendsController.ContainsStatus(userId, FriendshipStatus.FRIE |
| 1 | 253 | | voiceChatWindowView.SetPlayerAsJoined(userId, dataStore.voiceChat.isJoinedToVoiceChat.Get()); |
| | 254 | |
|
| 1 | 255 | | if (isMuteAll && !isMuted) |
| 0 | 256 | | MuteUser(userId, true); |
| | 257 | | } |
| | 258 | |
|
| 1 | 259 | | SetWhichPlayerIsTalking(); |
| 1 | 260 | | } |
| | 261 | |
|
| | 262 | | internal void OnOtherPlayerStatusRemoved(string userId, Player player) |
| | 263 | | { |
| 1 | 264 | | if (trackedUsersHashSet.Contains(userId)) |
| 0 | 265 | | trackedUsersHashSet.Remove(userId); |
| | 266 | |
|
| 1 | 267 | | voiceChatWindowView.RemoveUser(userId); |
| 1 | 268 | | SetWhichPlayerIsTalking(); |
| 1 | 269 | | } |
| | 270 | |
|
| | 271 | | internal void OnMuteAllToggled(bool isMute) |
| | 272 | | { |
| 2 | 273 | | isMuteAll = isMute; |
| | 274 | |
|
| 2 | 275 | | if (!dataStore.voiceChat.isJoinedToVoiceChat.Get()) |
| 2 | 276 | | return; |
| | 277 | |
|
| 0 | 278 | | MuteAll(isMute); |
| 0 | 279 | | } |
| | 280 | |
|
| | 281 | | internal void MuteAll(bool isMute) |
| | 282 | | { |
| 2 | 283 | | isMuteAll = isMute; |
| | 284 | |
|
| 2 | 285 | | if (isMute) |
| 1 | 286 | | usersToUnmute.Clear(); |
| | 287 | | else |
| 1 | 288 | | usersToMute.Clear(); |
| | 289 | |
|
| 2 | 290 | | using (var iterator = trackedUsersHashSet.GetEnumerator()) |
| | 291 | | { |
| 2 | 292 | | while (iterator.MoveNext()) |
| | 293 | | { |
| 0 | 294 | | MuteUser(iterator.Current, isMute); |
| | 295 | | } |
| 2 | 296 | | } |
| 2 | 297 | | } |
| | 298 | |
|
| | 299 | | internal void MuteUser(string userId, bool isMuted) |
| | 300 | | { |
| 2 | 301 | | var list = isMuted ? usersToMute : usersToUnmute; |
| 2 | 302 | | list.Add(userId); |
| | 303 | |
|
| 2 | 304 | | if (updateMuteStatusRoutine == null) |
| 2 | 305 | | updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine()); |
| | 306 | |
|
| 2 | 307 | | if (isMuted) |
| 1 | 308 | | socialAnalytics.SendPlayerMuted(userId); |
| | 309 | | else |
| 1 | 310 | | socialAnalytics.SendPlayerUnmuted(userId); |
| 1 | 311 | | } |
| | 312 | |
|
| | 313 | | internal IEnumerator MuteStateUpdateRoutine() |
| | 314 | | { |
| 2 | 315 | | yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL); |
| 0 | 316 | | ReportMuteStatuses(); |
| 0 | 317 | | updateMuteStatusRoutine = null; |
| 0 | 318 | | } |
| | 319 | |
|
| | 320 | | internal void ReportMuteStatuses() |
| | 321 | | { |
| 39 | 322 | | if (usersToUnmute.Count > 0) |
| 2 | 323 | | WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false); |
| | 324 | |
|
| 39 | 325 | | if (usersToMute.Count > 0) |
| 2 | 326 | | WebInterface.SetMuteUsers(usersToMute.ToArray(), true); |
| | 327 | |
|
| 39 | 328 | | usersToUnmute.Clear(); |
| 39 | 329 | | usersToMute.Clear(); |
| 39 | 330 | | } |
| | 331 | |
|
| | 332 | | internal void ChangeAllowUsersFilter(string optionId) |
| | 333 | | { |
| 3 | 334 | | var newSettings = settings.generalSettings.Data; |
| | 335 | |
|
| 3 | 336 | | if (optionId == VoiceChatAllow.ALL_USERS.ToString()) |
| 1 | 337 | | newSettings.voiceChatAllow = VoiceChatAllow.ALL_USERS; |
| 2 | 338 | | else if (optionId == VoiceChatAllow.VERIFIED_ONLY.ToString()) |
| 1 | 339 | | newSettings.voiceChatAllow = VoiceChatAllow.VERIFIED_ONLY; |
| 1 | 340 | | else if (optionId == VoiceChatAllow.FRIENDS_ONLY.ToString()) |
| 1 | 341 | | newSettings.voiceChatAllow = VoiceChatAllow.FRIENDS_ONLY; |
| | 342 | |
|
| 3 | 343 | | settings.generalSettings.Apply(newSettings); |
| 3 | 344 | | } |
| | 345 | |
|
| | 346 | | internal void OnUserProfileUpdated(UserProfile profile) |
| | 347 | | { |
| 1 | 348 | | using (var iterator = trackedUsersHashSet.GetEnumerator()) |
| | 349 | | { |
| 2 | 350 | | while (iterator.MoveNext()) |
| | 351 | | { |
| 1 | 352 | | voiceChatWindowView.SetPlayerBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contain |
| | 353 | | } |
| 1 | 354 | | } |
| 1 | 355 | | } |
| | 356 | |
|
| 4 | 357 | | internal void OnUpdateFriendship(string userId, FriendshipAction action) { voiceChatWindowView.SetPlayerAsFriend(use |
| | 358 | |
|
| | 359 | | internal void OnSettingsChanged(GeneralSettings settings) |
| | 360 | | { |
| 5 | 361 | | SetAllowUsersOption(settings.voiceChatAllow); |
| 5 | 362 | | socialAnalytics.SendVoiceChatPreferencesChanged(settings.voiceChatAllow); |
| 5 | 363 | | } |
| | 364 | |
|
| | 365 | | private void SetAllowUsersOption(VoiceChatAllow option) |
| | 366 | | { |
| | 367 | | switch (option) |
| | 368 | | { |
| | 369 | | case VoiceChatAllow.ALL_USERS: |
| 9 | 370 | | voiceChatWindowView.SelectAllowUsersOption(0); |
| 9 | 371 | | break; |
| | 372 | | case VoiceChatAllow.VERIFIED_ONLY: |
| 31 | 373 | | voiceChatWindowView.SelectAllowUsersOption(1); |
| 31 | 374 | | break; |
| | 375 | | case VoiceChatAllow.FRIENDS_ONLY: |
| 3 | 376 | | voiceChatWindowView.SelectAllowUsersOption(2); |
| | 377 | | break; |
| | 378 | | } |
| 3 | 379 | | } |
| | 380 | |
|
| | 381 | | internal void RendererState_OnChange(bool current, bool previous) |
| | 382 | | { |
| 38 | 383 | | if (!current) |
| 38 | 384 | | return; |
| | 385 | |
|
| 0 | 386 | | CommonScriptableObjects.rendererState.OnChange -= RendererState_OnChange; |
| 0 | 387 | | RequestJoinVoiceChat(true); |
| 0 | 388 | | } |
| | 389 | |
|
| | 390 | | internal void SetWhichPlayerIsTalking() |
| | 391 | | { |
| 12 | 392 | | if (isOwnPLayerTalking) |
| 3 | 393 | | voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_YOU); |
| 9 | 394 | | else if (voiceChatWindowView.numberOfPlayers == 0) |
| 6 | 395 | | voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_JUST_YOU_IN_THE_VOICE_CHAT); |
| 3 | 396 | | else if (voiceChatWindowView.numberOfPlayersTalking == 0) |
| 1 | 397 | | voiceChatBarView.SetTalkingMessage(false, TALKING_MESSAGE_NOBODY_TALKING); |
| 2 | 398 | | else if (voiceChatWindowView.numberOfPlayersTalking == 1) |
| | 399 | | { |
| 1 | 400 | | UserProfile userProfile = userProfileBridge.Get(voiceChatWindowView.GetUserTalkingByIndex(0)); |
| 1 | 401 | | voiceChatBarView.SetTalkingMessage(true, userProfile != null ? userProfile.userName : voiceChatWindowView.Ge |
| 1 | 402 | | } |
| | 403 | | else |
| 1 | 404 | | voiceChatBarView.SetTalkingMessage(true, TALKING_MESSAGE_SEVERAL_PEOPLE_TALKING); |
| 1 | 405 | | } |
| | 406 | |
|
| 0 | 407 | | protected internal virtual IVoiceChatWindowComponentView CreateVoiceChatWindowView() => VoiceChatWindowComponentView |
| | 408 | |
|
| 0 | 409 | | protected internal virtual IVoiceChatBarComponentView CreateVoiceChatBatView() => VoiceChatBarComponentView.Create() |
| | 410 | |
|
| 0 | 411 | | protected internal virtual IVoiceChatPlayerComponentView CreateVoiceChatPlayerView() => VoiceChatPlayerComponentView |
| | 412 | | } |