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