| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Interface; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class FriendsHUDController : IHUD |
| | 7 | | { |
| | 8 | | internal const string PLAYER_PREFS_SEEN_FRIEND_COUNT = "SeenFriendsCount"; |
| 0 | 9 | | public FriendsHUDView view { get; private set; } |
| | 10 | |
|
| | 11 | | IFriendsController friendsController; |
| | 12 | | public event System.Action<string> OnPressWhisper; |
| | 13 | | public event System.Action OnFriendsOpened; |
| | 14 | | public event System.Action OnFriendsClosed; |
| | 15 | |
|
| | 16 | | UserProfile ownUserProfile; |
| | 17 | |
|
| | 18 | | public void Initialize(IFriendsController friendsController, UserProfile ownUserProfile) |
| | 19 | | { |
| 22 | 20 | | view = FriendsHUDView.Create(this); |
| 22 | 21 | | this.friendsController = friendsController; |
| | 22 | |
|
| 22 | 23 | | if (this.friendsController != null) |
| | 24 | | { |
| 12 | 25 | | this.friendsController.OnUpdateFriendship += OnUpdateFriendship; |
| 12 | 26 | | this.friendsController.OnUpdateUserStatus += OnUpdateUserStatus; |
| 12 | 27 | | this.friendsController.OnFriendNotFound += OnFriendNotFound; |
| | 28 | | } |
| | 29 | |
|
| 22 | 30 | | view.friendRequestsList.OnFriendRequestApproved += Entry_OnRequestAccepted; |
| 22 | 31 | | view.friendRequestsList.OnCancelConfirmation += Entry_OnRequestCancelled; |
| 22 | 32 | | view.friendRequestsList.OnRejectConfirmation += Entry_OnRequestRejected; |
| 22 | 33 | | view.friendRequestsList.OnFriendRequestSent += Entry_OnRequestSent; |
| | 34 | |
|
| 22 | 35 | | view.friendsList.OnWhisper += Entry_OnWhisper; |
| | 36 | |
|
| 22 | 37 | | view.friendsList.OnDeleteConfirmation += Entry_OnDelete; |
| | 38 | |
|
| 22 | 39 | | if (ownUserProfile != null) |
| | 40 | | { |
| 12 | 41 | | this.ownUserProfile = ownUserProfile; |
| 12 | 42 | | ownUserProfile.OnUpdate += OnUserProfileUpdate; |
| | 43 | | } |
| | 44 | |
|
| 22 | 45 | | if (friendsController != null) |
| | 46 | | { |
| 12 | 47 | | if (friendsController.isInitialized) |
| | 48 | | { |
| 11 | 49 | | view.HideSpinner(); |
| 11 | 50 | | } |
| | 51 | | else |
| | 52 | | { |
| 1 | 53 | | view.ShowSpinner(); |
| 1 | 54 | | friendsController.OnInitialized -= FriendsController_OnInitialized; |
| 1 | 55 | | friendsController.OnInitialized += FriendsController_OnInitialized; |
| | 56 | | } |
| | 57 | | } |
| 11 | 58 | | } |
| | 59 | |
|
| | 60 | | private void FriendsController_OnInitialized() |
| | 61 | | { |
| 0 | 62 | | friendsController.OnInitialized -= FriendsController_OnInitialized; |
| 0 | 63 | | view.HideSpinner(); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void OnUserProfileUpdate(UserProfile profile) |
| | 67 | | { |
| | 68 | | //NOTE(Brian): HashSet to check Contains quicker. |
| | 69 | | HashSet<string> allBlockedUsers; |
| | 70 | |
|
| 0 | 71 | | if (profile.blocked != null) |
| 0 | 72 | | allBlockedUsers = new HashSet<string>(profile.blocked); |
| | 73 | | else |
| 0 | 74 | | allBlockedUsers = new HashSet<string>(); |
| | 75 | |
|
| 0 | 76 | | var entries = view.GetAllEntries(); |
| 0 | 77 | | int entriesCount = entries.Count; |
| | 78 | |
|
| 0 | 79 | | for (int i = 0; i < entriesCount; i++) |
| | 80 | | { |
| 0 | 81 | | entries[i].model.blocked = allBlockedUsers.Contains(entries[i].userId); |
| 0 | 82 | | entries[i].Populate(entries[i].model); |
| | 83 | | } |
| 0 | 84 | | } |
| | 85 | |
|
| 2 | 86 | | private void Entry_OnRequestSent(string userId) { WebInterface.UpdateFriendshipStatus(new FriendsController.Friendsh |
| | 87 | |
|
| | 88 | | private void OnUpdateUserStatus(string userId, FriendsController.UserStatus newStatus) |
| | 89 | | { |
| 0 | 90 | | var model = new FriendEntry.Model(); |
| | 91 | |
|
| 0 | 92 | | FriendEntryBase entry = view.friendsList.GetEntry(userId) ?? view.friendRequestsList.GetEntry(userId); |
| | 93 | |
|
| 0 | 94 | | if (entry != null) |
| 0 | 95 | | model = entry.model; |
| | 96 | |
|
| 0 | 97 | | model.status = newStatus.presence; |
| 0 | 98 | | model.coords = newStatus.position; |
| | 99 | |
|
| 0 | 100 | | if (newStatus.realm != null) |
| | 101 | | { |
| 0 | 102 | | model.realm = $"{newStatus.realm.serverName.ToUpperFirst()} {newStatus.realm.layer.ToUpperFirst()}"; |
| 0 | 103 | | model.realmServerName = newStatus.realm.serverName; |
| 0 | 104 | | model.realmLayerName = newStatus.realm.layer; |
| 0 | 105 | | } |
| | 106 | | else |
| | 107 | | { |
| 0 | 108 | | model.realm = string.Empty; |
| 0 | 109 | | model.realmServerName = string.Empty; |
| 0 | 110 | | model.realmLayerName = string.Empty; |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | view.friendsList.UpdateEntry(userId, model); |
| 0 | 114 | | view.friendRequestsList.UpdateEntry(userId, model); |
| 0 | 115 | | } |
| | 116 | |
|
| 2 | 117 | | void OnFriendNotFound(string name) { view.friendRequestsList.DisplayFriendUserNotFound(); } |
| | 118 | |
|
| | 119 | | private void OnUpdateFriendship(string userId, FriendshipAction friendshipAction) |
| | 120 | | { |
| 18 | 121 | | UserProfile userProfile = UserProfileController.userProfilesCatalog.Get(userId); |
| | 122 | |
|
| 18 | 123 | | if (userProfile == null) |
| | 124 | | { |
| 0 | 125 | | Debug.LogError($"UserProfile is null for {userId}! ... friendshipAction {friendshipAction}"); |
| 0 | 126 | | return; |
| | 127 | | } |
| | 128 | |
|
| 18 | 129 | | FriendEntryBase.Model friendEntryModel = new FriendEntry.Model(); |
| | 130 | |
|
| 18 | 131 | | FriendEntryBase entry = view.friendsList.GetEntry(userId) ?? view.friendRequestsList.GetEntry(userId); |
| | 132 | |
|
| 18 | 133 | | if (entry != null) |
| 6 | 134 | | friendEntryModel = entry.model; |
| | 135 | |
|
| 18 | 136 | | friendEntryModel.userName = userProfile.userName; |
| 18 | 137 | | friendEntryModel.avatarSnapshotObserver = userProfile.snapshotObserver; |
| | 138 | |
|
| 18 | 139 | | if (ownUserProfile != null && ownUserProfile.blocked != null) |
| 18 | 140 | | friendEntryModel.blocked = ownUserProfile.blocked.Contains(userId); |
| | 141 | |
|
| | 142 | | switch (friendshipAction) |
| | 143 | | { |
| | 144 | | case FriendshipAction.NONE: |
| 0 | 145 | | view.friendRequestsList.RemoveEntry(userId); |
| 0 | 146 | | view.friendsList.RemoveEntry(userId); |
| 0 | 147 | | break; |
| | 148 | | case FriendshipAction.APPROVED: |
| 11 | 149 | | view.friendRequestsList.RemoveEntry(userId); |
| 11 | 150 | | view.friendsList.CreateOrUpdateEntryDeferred(userId, friendEntryModel); |
| 11 | 151 | | break; |
| | 152 | | case FriendshipAction.REJECTED: |
| 1 | 153 | | view.friendRequestsList.RemoveEntry(userId); |
| 1 | 154 | | break; |
| | 155 | | case FriendshipAction.CANCELLED: |
| 1 | 156 | | view.friendRequestsList.RemoveEntry(userId); |
| 1 | 157 | | break; |
| | 158 | | case FriendshipAction.REQUESTED_FROM: |
| 3 | 159 | | view.friendRequestsList.CreateOrUpdateEntry(userId, friendEntryModel, true); |
| 3 | 160 | | break; |
| | 161 | | case FriendshipAction.REQUESTED_TO: |
| 1 | 162 | | view.friendRequestsList.CreateOrUpdateEntry(userId, friendEntryModel, false); |
| 1 | 163 | | break; |
| | 164 | | case FriendshipAction.DELETED: |
| 1 | 165 | | view.friendRequestsList.RemoveEntry(userId); |
| 1 | 166 | | view.friendsList.RemoveEntry(userId); |
| | 167 | | break; |
| | 168 | | } |
| | 169 | |
|
| 18 | 170 | | UpdateNotificationsCounter(); |
| 18 | 171 | | } |
| | 172 | |
|
| | 173 | | private void UpdateNotificationsCounter() |
| | 174 | | { |
| | 175 | | //NOTE(Brian): If friends tab is already active, update and save this value instantly |
| 22 | 176 | | if (view.friendsList.gameObject.activeInHierarchy) |
| | 177 | | { |
| 16 | 178 | | PlayerPrefsUtils.SetInt(PLAYER_PREFS_SEEN_FRIEND_COUNT, friendsController.friendCount); |
| 16 | 179 | | PlayerPrefsUtils.Save(); |
| | 180 | | } |
| | 181 | |
|
| 22 | 182 | | var pendingFriendRequestsSO = NotificationScriptableObjects.pendingFriendRequests; |
| 22 | 183 | | int receivedRequestsCount = view.friendRequestsList.receivedRequestsList.Count(); |
| | 184 | |
|
| 22 | 185 | | if (pendingFriendRequestsSO != null) |
| | 186 | | { |
| 22 | 187 | | pendingFriendRequestsSO.Set(receivedRequestsCount); |
| | 188 | | } |
| | 189 | |
|
| 22 | 190 | | int seenFriendsCount = PlayerPrefs.GetInt(PLAYER_PREFS_SEEN_FRIEND_COUNT, 0); |
| 22 | 191 | | int friendsCount = friendsController.friendCount; |
| | 192 | |
|
| 22 | 193 | | int newFriends = friendsCount - seenFriendsCount; |
| | 194 | |
|
| | 195 | | //NOTE(Brian): If someone deletes you, don't show badge notification |
| 22 | 196 | | if (newFriends < 0) |
| 1 | 197 | | newFriends = 0; |
| | 198 | |
|
| 22 | 199 | | var newApprovedFriendsSO = NotificationScriptableObjects.newApprovedFriends; |
| | 200 | |
|
| 22 | 201 | | if (newApprovedFriendsSO != null) |
| | 202 | | { |
| 22 | 203 | | newApprovedFriendsSO.Set(newFriends); |
| | 204 | | } |
| 22 | 205 | | } |
| | 206 | |
|
| 2 | 207 | | private void Entry_OnWhisper(FriendEntry entry) { OnPressWhisper?.Invoke(entry.userId); } |
| | 208 | |
|
| | 209 | | private void Entry_OnDelete(string userId) |
| | 210 | | { |
| 1 | 211 | | WebInterface.UpdateFriendshipStatus( |
| | 212 | | new FriendsController.FriendshipUpdateStatusMessage() |
| | 213 | | { |
| | 214 | | action = FriendshipAction.DELETED, |
| | 215 | | userId = userId |
| | 216 | | }); |
| 1 | 217 | | } |
| | 218 | |
|
| | 219 | | private void Entry_OnRequestRejected(FriendRequestEntry entry) |
| | 220 | | { |
| 1 | 221 | | WebInterface.UpdateFriendshipStatus( |
| | 222 | | new FriendsController.FriendshipUpdateStatusMessage() |
| | 223 | | { |
| | 224 | | action = FriendshipAction.REJECTED, |
| | 225 | | userId = entry.userId |
| | 226 | | }); |
| 1 | 227 | | } |
| | 228 | |
|
| | 229 | | private void Entry_OnRequestCancelled(FriendRequestEntry entry) |
| | 230 | | { |
| 1 | 231 | | WebInterface.UpdateFriendshipStatus( |
| | 232 | | new FriendsController.FriendshipUpdateStatusMessage() |
| | 233 | | { |
| | 234 | | action = FriendshipAction.CANCELLED, |
| | 235 | | userId = entry.userId |
| | 236 | | }); |
| 1 | 237 | | } |
| | 238 | |
|
| | 239 | | private void Entry_OnRequestAccepted(FriendRequestEntry entry) |
| | 240 | | { |
| 0 | 241 | | WebInterface.UpdateFriendshipStatus( |
| | 242 | | new FriendsController.FriendshipUpdateStatusMessage() |
| | 243 | | { |
| | 244 | | action = FriendshipAction.APPROVED, |
| | 245 | | userId = entry.userId |
| | 246 | | }); |
| 0 | 247 | | } |
| | 248 | |
|
| | 249 | | public void Dispose() |
| | 250 | | { |
| 23 | 251 | | if (this.friendsController != null) |
| | 252 | | { |
| 12 | 253 | | this.friendsController.OnInitialized -= FriendsController_OnInitialized; |
| 12 | 254 | | this.friendsController.OnUpdateFriendship -= OnUpdateFriendship; |
| 12 | 255 | | this.friendsController.OnUpdateUserStatus -= OnUpdateUserStatus; |
| | 256 | | } |
| | 257 | |
|
| 23 | 258 | | if (view != null) |
| | 259 | | { |
| 22 | 260 | | UnityEngine.Object.Destroy(view.gameObject); |
| | 261 | | } |
| | 262 | |
|
| 23 | 263 | | if (this.ownUserProfile != null) |
| 12 | 264 | | ownUserProfile.OnUpdate -= OnUserProfileUpdate; |
| 23 | 265 | | } |
| | 266 | |
|
| | 267 | | public void SetVisibility(bool visible) |
| | 268 | | { |
| 7 | 269 | | view.gameObject.SetActive(visible); |
| | 270 | |
|
| 7 | 271 | | if (visible) |
| | 272 | | { |
| 4 | 273 | | UpdateNotificationsCounter(); |
| | 274 | |
|
| 4 | 275 | | if (view.friendsButton.interactable) |
| 3 | 276 | | view.friendsButton.onClick.Invoke(); |
| | 277 | |
|
| 4 | 278 | | OnFriendsOpened?.Invoke(); |
| | 279 | |
|
| 4 | 280 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 4 | 281 | | } |
| | 282 | | else |
| | 283 | | { |
| 3 | 284 | | OnFriendsClosed?.Invoke(); |
| | 285 | |
|
| 3 | 286 | | AudioScriptableObjects.dialogClose.Play(true); |
| | 287 | | } |
| 3 | 288 | | } |
| | 289 | | } |