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