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