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