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