| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Interface; |
| | 5 | | using SocialFeaturesAnalytics; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public class UsersAroundListHUDController : IHUD |
| | 9 | | { |
| | 10 | | const float MUTE_STATUS_UPDATE_INTERVAL = 0.3f; |
| | 11 | |
|
| | 12 | | internal IUsersAroundListHUDButtonView usersButtonView; |
| | 13 | | internal IUsersAroundListHUDListView usersListView; |
| | 14 | | internal ISocialAnalytics socialAnalytics; |
| | 15 | |
|
| | 16 | | private bool isVisible = false; |
| 3 | 17 | | private readonly HashSet<string> trackedUsersHashSet = new HashSet<string>(); |
| 15 | 18 | | private UserProfile profile => UserProfile.GetOwnUserProfile(); |
| | 19 | |
|
| 3 | 20 | | private readonly List<string> usersToMute = new List<string>(); |
| 3 | 21 | | private readonly List<string> usersToUnmute = new List<string>(); |
| | 22 | | private bool isMuteAll = false; |
| | 23 | | private Coroutine updateMuteStatusRoutine = null; |
| | 24 | |
|
| 12 | 25 | | private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers; |
| | 26 | |
|
| | 27 | | public event System.Action OnOpen; |
| | 28 | |
|
| 3 | 29 | | public UsersAroundListHUDController(ISocialAnalytics socialAnalytics) |
| | 30 | | { |
| 3 | 31 | | UsersAroundListHUDListView view = Object.Instantiate(Resources.Load<GameObject>("UsersAroundListHUD")).GetCompon |
| 3 | 32 | | view.name = "_UsersAroundListHUD"; |
| 3 | 33 | | view.gameObject.SetActive(false); |
| 3 | 34 | | Initialize(view, socialAnalytics); |
| 3 | 35 | | } |
| | 36 | |
|
| 0 | 37 | | public UsersAroundListHUDController(IUsersAroundListHUDListView usersListView, ISocialAnalytics socialAnalytics) { I |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Dispose HUD controller |
| | 41 | | /// </summary> |
| | 42 | | public void Dispose() |
| | 43 | | { |
| 3 | 44 | | ReportMuteStatuses(); |
| | 45 | |
|
| 3 | 46 | | if (updateMuteStatusRoutine != null) |
| | 47 | | { |
| 0 | 48 | | CoroutineStarter.Stop(updateMuteStatusRoutine); |
| | 49 | | } |
| | 50 | |
|
| 3 | 51 | | otherPlayers.OnAdded -= OnOtherPlayersStatusAdded; |
| 3 | 52 | | otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved; |
| | 53 | |
|
| 3 | 54 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged; |
| 3 | 55 | | profile.OnUpdate -= OnUserProfileUpdate; |
| | 56 | |
|
| 3 | 57 | | if (usersListView != null) |
| | 58 | | { |
| 3 | 59 | | usersListView.OnRequestMuteUser -= OnMuteUser; |
| 3 | 60 | | usersListView.OnRequestMuteGlobal -= OnMuteAll; |
| 3 | 61 | | usersListView.OnGoToCrowdPressed -= OnGoToCrowd; |
| 3 | 62 | | usersListView.OnOpen -= OnListOpen; |
| | 63 | |
|
| 3 | 64 | | usersListView.Dispose(); |
| | 65 | | } |
| | 66 | |
|
| 3 | 67 | | if (usersButtonView != null) |
| | 68 | | { |
| 0 | 69 | | usersButtonView.OnClick -= ToggleVisibility; |
| | 70 | | } |
| 3 | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Set HUD's visibility |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="visible"></param> |
| | 77 | | public void SetVisibility(bool visible) |
| | 78 | | { |
| 0 | 79 | | isVisible = visible; |
| 0 | 80 | | usersListView.SetVisibility(visible); |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Set button to toggle HUD visibility and display users count |
| | 85 | | /// </summary> |
| | 86 | | /// <param name="view">Button view</param> |
| | 87 | | public void SetButtonView(IUsersAroundListHUDButtonView view) |
| | 88 | | { |
| 0 | 89 | | usersButtonView = view; |
| 0 | 90 | | usersButtonView.OnClick += ToggleVisibility; |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Set mute status for users' id |
| | 95 | | /// </summary> |
| | 96 | | /// <param name="usersId">Array of user ids</param> |
| | 97 | | /// <param name="isMuted">Set if users should be mute or unmute</param> |
| | 98 | | public void SetUsersMuted(string[] usersId, bool isMuted) |
| | 99 | | { |
| 0 | 100 | | for (int i = 0; i < usersId.Length; i++) |
| | 101 | | { |
| 0 | 102 | | usersListView.SetUserMuted(usersId[i], isMuted); |
| | 103 | | } |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Set user status as "talking" |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="userId">User's id</param> |
| | 110 | | /// <param name="isRecording">Set user status as "talking" or "not talking"</param> |
| 0 | 111 | | public void SetUserRecording(string userId, bool isRecording) { usersListView.SetUserRecording(userId, isRecording); |
| | 112 | |
|
| | 113 | | void Initialize(IUsersAroundListHUDListView view, ISocialAnalytics socialAnalytics) |
| | 114 | | { |
| 3 | 115 | | usersListView = view; |
| 3 | 116 | | this.socialAnalytics = socialAnalytics; |
| | 117 | |
|
| 3 | 118 | | usersListView.OnRequestMuteUser += OnMuteUser; |
| 3 | 119 | | usersListView.OnRequestMuteGlobal += OnMuteAll; |
| 3 | 120 | | usersListView.OnGoToCrowdPressed += OnGoToCrowd; |
| 3 | 121 | | usersListView.OnOpen += OnListOpen; |
| | 122 | |
|
| 3 | 123 | | otherPlayers.OnAdded += OnOtherPlayersStatusAdded; |
| 3 | 124 | | otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved; |
| | 125 | |
|
| 3 | 126 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged; |
| 3 | 127 | | profile.OnUpdate += OnUserProfileUpdate; |
| 3 | 128 | | } |
| | 129 | |
|
| | 130 | | void OnOtherPlayersStatusAdded(string userId, Player player) |
| | 131 | | { |
| 3 | 132 | | usersListView.AddOrUpdatePlayer(player); |
| | 133 | |
|
| 3 | 134 | | if (!trackedUsersHashSet.Contains(userId)) |
| | 135 | | { |
| 3 | 136 | | trackedUsersHashSet.Add(userId); |
| | 137 | |
|
| 3 | 138 | | bool isMuted = profile.muted.Contains(userId); |
| 3 | 139 | | bool isBlocked = profile.blocked != null ? profile.blocked.Contains(userId) : false; |
| | 140 | |
|
| 3 | 141 | | usersListView.SetUserMuted(userId, isMuted); |
| 3 | 142 | | usersListView.SetUserBlocked(userId, isBlocked); |
| | 143 | |
|
| 3 | 144 | | if (isMuteAll && !isMuted) |
| | 145 | | { |
| 0 | 146 | | OnMuteUser(userId, true); |
| | 147 | | } |
| | 148 | | } |
| | 149 | |
|
| 3 | 150 | | usersButtonView?.SetUsersCount(trackedUsersHashSet.Count); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | void OnOtherPlayerStatusRemoved(string userId, Player player) |
| | 154 | | { |
| 2 | 155 | | if (trackedUsersHashSet.Contains(userId)) |
| | 156 | | { |
| 2 | 157 | | trackedUsersHashSet.Remove(userId); |
| 2 | 158 | | usersButtonView?.SetUsersCount(trackedUsersHashSet.Count); |
| | 159 | | } |
| 2 | 160 | | usersListView.RemoveUser(userId); |
| 2 | 161 | | } |
| | 162 | |
|
| | 163 | | void ToggleVisibility() |
| | 164 | | { |
| 0 | 165 | | bool setVisible = !isVisible; |
| 0 | 166 | | SetVisibility(setVisible); |
| 0 | 167 | | } |
| | 168 | |
|
| | 169 | | void OnMuteUser(string userId, bool mute) |
| | 170 | | { |
| 0 | 171 | | var list = mute ? usersToMute : usersToUnmute; |
| 0 | 172 | | list.Add(userId); |
| | 173 | |
|
| 0 | 174 | | if (updateMuteStatusRoutine == null) |
| | 175 | | { |
| 0 | 176 | | updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine()); |
| | 177 | | } |
| | 178 | |
|
| 0 | 179 | | if (mute) |
| 0 | 180 | | socialAnalytics.SendPlayerMuted(userId); |
| | 181 | | else |
| 0 | 182 | | socialAnalytics.SendPlayerUnmuted(userId); |
| 0 | 183 | | } |
| | 184 | |
|
| | 185 | | void OnMuteUsers(IEnumerable<string> usersId, bool mute) |
| | 186 | | { |
| 0 | 187 | | using (var iterator = usersId.GetEnumerator()) |
| | 188 | | { |
| 0 | 189 | | while (iterator.MoveNext()) |
| | 190 | | { |
| 0 | 191 | | OnMuteUser(iterator.Current, mute); |
| | 192 | | } |
| 0 | 193 | | } |
| 0 | 194 | | } |
| | 195 | |
|
| | 196 | | void OnMuteAll(bool mute) |
| | 197 | | { |
| 0 | 198 | | isMuteAll = mute; |
| | 199 | |
|
| 0 | 200 | | if (mute) |
| | 201 | | { |
| 0 | 202 | | usersToUnmute.Clear(); |
| 0 | 203 | | } |
| | 204 | | else |
| | 205 | | { |
| 0 | 206 | | usersToMute.Clear(); |
| | 207 | | } |
| 0 | 208 | | OnMuteUsers(trackedUsersHashSet, mute); |
| 0 | 209 | | } |
| | 210 | |
|
| 0 | 211 | | void OnGoToCrowd() { WebInterface.GoToCrowd(); } |
| | 212 | |
|
| 0 | 213 | | void OnListOpen() { OnOpen?.Invoke(); } |
| | 214 | |
|
| | 215 | | private void OnRendererStateChanged(bool isEnable, bool prevState) |
| | 216 | | { |
| 0 | 217 | | if (isEnable || !isVisible) |
| 0 | 218 | | return; |
| | 219 | |
|
| 0 | 220 | | SetVisibility(false); |
| 0 | 221 | | } |
| | 222 | |
|
| | 223 | | private void OnUserProfileUpdate(UserProfile profile) |
| | 224 | | { |
| 0 | 225 | | using (var iterator = trackedUsersHashSet.GetEnumerator()) |
| | 226 | | { |
| 0 | 227 | | while (iterator.MoveNext()) |
| | 228 | | { |
| 0 | 229 | | usersListView.SetUserBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contains(iterat |
| | 230 | | } |
| 0 | 231 | | } |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | void ReportMuteStatuses() |
| | 235 | | { |
| 3 | 236 | | if (usersToUnmute.Count > 0) |
| | 237 | | { |
| 0 | 238 | | WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false); |
| | 239 | | } |
| 3 | 240 | | if (usersToMute.Count > 0) |
| | 241 | | { |
| 0 | 242 | | WebInterface.SetMuteUsers(usersToMute.ToArray(), true); |
| | 243 | | } |
| 3 | 244 | | usersToUnmute.Clear(); |
| 3 | 245 | | usersToMute.Clear(); |
| 3 | 246 | | } |
| | 247 | |
|
| | 248 | | IEnumerator MuteStateUpdateRoutine() |
| | 249 | | { |
| 0 | 250 | | yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL); |
| 0 | 251 | | ReportMuteStatuses(); |
| 0 | 252 | | updateMuteStatusRoutine = null; |
| 0 | 253 | | } |
| | 254 | | public void ToggleUsersCount(bool isEnabled) |
| | 255 | | { |
| 0 | 256 | | usersButtonView.ToggleUsersCount(isEnabled); |
| 0 | 257 | | } |
| | 258 | | } |