| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using DCL.ProfanityFiltering; |
| | 6 | | using DCL.Social.Friends; |
| | 7 | | using DCL.Tasks; |
| | 8 | | using DCLServices.WearablesCatalogService; |
| | 9 | | using SocialFeaturesAnalytics; |
| | 10 | | using System; |
| | 11 | | using System.Collections.Generic; |
| | 12 | | using System.Linq; |
| | 13 | | using System.Threading; |
| | 14 | | using UnityEngine; |
| | 15 | | using UnityEngine.Assertions; |
| | 16 | | using Object = UnityEngine.Object; |
| | 17 | |
|
| | 18 | | public class PlayerInfoCardHUDController : IHUD |
| | 19 | | { |
| | 20 | | internal readonly PlayerInfoCardHUDView view; |
| | 21 | | internal readonly StringVariable currentPlayerId; |
| | 22 | | internal UserProfile currentUserProfile; |
| | 23 | |
|
| | 24 | | private UserProfile viewingUserProfile; |
| 62 | 25 | | private UserProfile ownUserProfile => userProfileBridge.GetOwn(); |
| | 26 | |
|
| | 27 | | private readonly IFriendsController friendsController; |
| | 28 | | private readonly InputAction_Trigger toggleFriendsTrigger; |
| | 29 | | private readonly InputAction_Trigger closeWindowTrigger; |
| | 30 | | private readonly InputAction_Trigger toggleWorldChatTrigger; |
| | 31 | | private readonly IUserProfileBridge userProfileBridge; |
| | 32 | | private readonly IWearablesCatalogService wearablesCatalogService; |
| | 33 | | private readonly IProfanityFilter profanityFilter; |
| | 34 | | private readonly DataStore dataStore; |
| | 35 | | private readonly BooleanVariable playerInfoCardVisibleState; |
| 19 | 36 | | private readonly List<string> loadedWearables = new (); |
| | 37 | | private readonly ISocialAnalytics socialAnalytics; |
| | 38 | |
|
| 19 | 39 | | private CancellationTokenSource friendOperationsCancellationToken = new (); |
| 19 | 40 | | private CancellationTokenSource setUserProfileCancellationToken = new (); |
| | 41 | | private double passportOpenStartTime; |
| 31 | 42 | | private bool isFriendsEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled("friends_enabled"); |
| | 43 | |
|
| 19 | 44 | | public PlayerInfoCardHUDController(IFriendsController friendsController, |
| | 45 | | StringVariable currentPlayerIdData, |
| | 46 | | IUserProfileBridge userProfileBridge, |
| | 47 | | IWearablesCatalogService wearablesCatalogService, |
| | 48 | | ISocialAnalytics socialAnalytics, |
| | 49 | | IProfanityFilter profanityFilter, |
| | 50 | | DataStore dataStore, |
| | 51 | | BooleanVariable playerInfoCardVisibleState) |
| | 52 | | { |
| 19 | 53 | | this.friendsController = friendsController; |
| 19 | 54 | | view = PlayerInfoCardHUDView.CreateView(); |
| | 55 | |
|
| 19 | 56 | | view.Initialize(() => OnCloseButtonPressed(), |
| | 57 | | ReportPlayer, BlockPlayer, UnblockPlayer, |
| | 58 | | AddPlayerAsFriend, CancelInvitation, AcceptFriendRequest, RejectFriendRequest); |
| | 59 | |
|
| 19 | 60 | | currentPlayerId = currentPlayerIdData; |
| 19 | 61 | | this.userProfileBridge = userProfileBridge; |
| 19 | 62 | | this.wearablesCatalogService = wearablesCatalogService; |
| 19 | 63 | | this.socialAnalytics = socialAnalytics; |
| 19 | 64 | | this.profanityFilter = profanityFilter; |
| 19 | 65 | | this.dataStore = dataStore; |
| 19 | 66 | | this.playerInfoCardVisibleState = playerInfoCardVisibleState; |
| 19 | 67 | | currentPlayerId.OnSame += OnCurrentPlayerIdUpdated; |
| 19 | 68 | | currentPlayerId.OnChange += OnCurrentPlayerIdChanged; |
| 19 | 69 | | OnCurrentPlayerIdUpdated(currentPlayerId); |
| | 70 | |
|
| 19 | 71 | | toggleFriendsTrigger = Resources.Load<InputAction_Trigger>("ToggleFriends"); |
| 19 | 72 | | toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed; |
| 19 | 73 | | toggleFriendsTrigger.OnTriggered += OnCloseButtonPressed; |
| | 74 | |
|
| 19 | 75 | | closeWindowTrigger = Resources.Load<InputAction_Trigger>("CloseWindow"); |
| 19 | 76 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| 19 | 77 | | closeWindowTrigger.OnTriggered += OnCloseButtonPressed; |
| | 78 | |
|
| 19 | 79 | | toggleWorldChatTrigger = Resources.Load<InputAction_Trigger>("ToggleWorldChat"); |
| 19 | 80 | | toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed; |
| 19 | 81 | | toggleWorldChatTrigger.OnTriggered += OnCloseButtonPressed; |
| | 82 | |
|
| 19 | 83 | | friendsController.OnUpdateFriendship -= OnFriendStatusUpdated; |
| 19 | 84 | | friendsController.OnUpdateFriendship += OnFriendStatusUpdated; |
| 19 | 85 | | } |
| | 86 | |
|
| | 87 | | private void CloseCard() |
| | 88 | | { |
| 0 | 89 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| 0 | 90 | | currentPlayerId.Set(null); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | private void OnCloseButtonPressed(DCLAction_Trigger action = DCLAction_Trigger.CloseWindow) |
| | 94 | | { |
| 0 | 95 | | CloseCard(); |
| 0 | 96 | | } |
| | 97 | |
|
| | 98 | | private void AddPlayerAsFriend() |
| | 99 | | { |
| 0 | 100 | | dataStore.HUDs.sendFriendRequest.Set(currentPlayerId, true); |
| 0 | 101 | | dataStore.HUDs.sendFriendRequestSource.Set((int)PlayerActionSource.Passport); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private void CancelInvitation() |
| | 105 | | { |
| 0 | 106 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| 0 | 107 | | CancelInvitationAsync(friendOperationsCancellationToken.Token).Forget(); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | private async UniTaskVoid CancelInvitationAsync(CancellationToken cancellationToken) |
| | 111 | | { |
| | 112 | | try |
| | 113 | | { |
| 0 | 114 | | FriendRequest request = await friendsController.CancelRequestByUserIdAsync(currentPlayerId, |
| | 115 | | cancellationToken); |
| | 116 | |
|
| 0 | 117 | | socialAnalytics.SendFriendRequestCancelled(request.From, request.To, PlayerActionSource.Passport.ToString(), |
| 0 | 118 | | } |
| 0 | 119 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 120 | | { |
| 0 | 121 | | e.ReportFriendRequestErrorToAnalyticsByUserId(currentPlayerId, PlayerActionSource.Passport.ToString(), |
| | 122 | | friendsController, socialAnalytics); |
| | 123 | |
|
| 0 | 124 | | throw; |
| | 125 | | } |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private void AcceptFriendRequest() |
| | 129 | | { |
| 0 | 130 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| 0 | 131 | | AcceptFriendRequestAsync(friendOperationsCancellationToken.Token).Forget(); |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | private async UniTaskVoid AcceptFriendRequestAsync(CancellationToken cancellationToken) |
| | 135 | | { |
| | 136 | | try |
| | 137 | | { |
| 0 | 138 | | FriendRequest request = friendsController.GetAllocatedFriendRequestByUser(currentPlayerId); |
| | 139 | |
|
| 0 | 140 | | request = await friendsController.AcceptFriendshipAsync(request.FriendRequestId, |
| | 141 | | cancellationToken); |
| | 142 | |
|
| 0 | 143 | | socialAnalytics.SendFriendRequestApproved(request.From, request.To, PlayerActionSource.Passport.ToString(), |
| | 144 | | request.HasBodyMessage, request.FriendRequestId); |
| 0 | 145 | | } |
| 0 | 146 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 147 | | { |
| 0 | 148 | | e.ReportFriendRequestErrorToAnalyticsByUserId(currentPlayerId, PlayerActionSource.Passport.ToString(), |
| | 149 | | friendsController, socialAnalytics); |
| | 150 | |
|
| 0 | 151 | | throw; |
| | 152 | | } |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | private void RejectFriendRequest() |
| | 156 | | { |
| 0 | 157 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| 0 | 158 | | RejectFriendRequestAsync(friendOperationsCancellationToken.Token).Forget(); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | private async UniTaskVoid RejectFriendRequestAsync(CancellationToken cancellationToken) |
| | 162 | | { |
| | 163 | | try |
| | 164 | | { |
| 0 | 165 | | FriendRequest request = friendsController.GetAllocatedFriendRequestByUser(currentPlayerId); |
| | 166 | |
|
| 0 | 167 | | request = await friendsController.RejectFriendshipAsync(request.FriendRequestId, |
| | 168 | | cancellationToken); |
| | 169 | |
|
| 0 | 170 | | socialAnalytics.SendFriendRequestRejected(request.From, request.To, |
| | 171 | | PlayerActionSource.Passport.ToString(), request.HasBodyMessage, request.FriendRequestId); |
| 0 | 172 | | } |
| 0 | 173 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 174 | | { |
| 0 | 175 | | e.ReportFriendRequestErrorToAnalyticsByUserId(currentPlayerId, PlayerActionSource.Passport.ToString(), |
| | 176 | | friendsController, socialAnalytics); |
| | 177 | |
|
| 0 | 178 | | throw; |
| | 179 | | } |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | private void OnCurrentPlayerIdChanged(string current, string previous) |
| | 183 | | { |
| 25 | 184 | | OnCurrentPlayerIdUpdated(current); |
| 25 | 185 | | } |
| | 186 | |
|
| | 187 | | private void OnCurrentPlayerIdUpdated(string current) |
| | 188 | | { |
| 44 | 189 | | if (currentUserProfile != null) |
| 4 | 190 | | currentUserProfile.OnUpdate -= SetUserProfile; |
| | 191 | |
|
| 44 | 192 | | currentUserProfile = string.IsNullOrEmpty(current) |
| | 193 | | ? null |
| | 194 | | : userProfileBridge.Get(current); |
| | 195 | |
|
| 44 | 196 | | if (currentUserProfile == null) |
| | 197 | | { |
| 22 | 198 | | if (playerInfoCardVisibleState.Get()) |
| 3 | 199 | | socialAnalytics.SendPassportClose(current, Time.realtimeSinceStartup - passportOpenStartTime); |
| | 200 | |
|
| 22 | 201 | | CommonScriptableObjects.playerInfoCardVisibleState.Set(false); |
| 22 | 202 | | view.SetCardActive(false); |
| 22 | 203 | | wearablesCatalogService.RemoveWearablesInUse(loadedWearables); |
| 22 | 204 | | loadedWearables.Clear(); |
| | 205 | | } |
| | 206 | | else |
| | 207 | | { |
| 22 | 208 | | currentUserProfile.OnUpdate += SetUserProfile; |
| | 209 | |
|
| 22 | 210 | | setUserProfileCancellationToken = setUserProfileCancellationToken.SafeRestart(); |
| | 211 | |
|
| 22 | 212 | | TaskUtils.Run(async () => |
| | 213 | | { |
| 22 | 214 | | await AsyncSetUserProfile(currentUserProfile, setUserProfileCancellationToken.Token); |
| 22 | 215 | | CommonScriptableObjects.playerInfoCardVisibleState.Set(true); |
| 22 | 216 | | view.SetCardActive(true); |
| 22 | 217 | | socialAnalytics.SendPassportOpen(current); |
| 22 | 218 | | }) |
| | 219 | | .Forget(); |
| | 220 | |
|
| 22 | 221 | | passportOpenStartTime = Time.realtimeSinceStartup; |
| | 222 | | } |
| 22 | 223 | | } |
| | 224 | |
|
| | 225 | | private void SetUserProfile(UserProfile userProfile) |
| | 226 | | { |
| 9 | 227 | | Assert.IsTrue(userProfile != null, "userProfile can't be null"); |
| | 228 | |
|
| 9 | 229 | | setUserProfileCancellationToken = setUserProfileCancellationToken.SafeRestart(); |
| | 230 | |
|
| 18 | 231 | | TaskUtils.Run(async () => await AsyncSetUserProfile(userProfile, setUserProfileCancellationToken.Token)).Forget( |
| 9 | 232 | | } |
| | 233 | |
|
| | 234 | | private async UniTask AsyncSetUserProfile(UserProfile userProfile, CancellationToken cancellationToken = default) |
| | 235 | | { |
| | 236 | | // TODO: pass cancellation tokens to profanity filtering |
| 31 | 237 | | string filterName = await FilterName(userProfile); |
| 31 | 238 | | string filterDescription = await FilterDescription(userProfile); |
| 31 | 239 | | await UniTask.SwitchToMainThread(cancellationToken); |
| | 240 | |
|
| 31 | 241 | | view.SetName(filterName); |
| 31 | 242 | | view.SetDescription(filterDescription); |
| 31 | 243 | | view.ClearCollectibles(); |
| 31 | 244 | | view.SetIsBlocked(IsBlocked(userProfile.userId)); |
| 31 | 245 | | LoadAndShowWearables(userProfile, cancellationToken).Forget(); |
| 31 | 246 | | UpdateFriendshipInteraction(); |
| | 247 | |
|
| 31 | 248 | | if (viewingUserProfile != null) |
| 13 | 249 | | viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot); |
| | 250 | |
|
| 31 | 251 | | userProfile.snapshotObserver.AddListener(view.SetFaceSnapshot); |
| 31 | 252 | | viewingUserProfile = userProfile; |
| 31 | 253 | | } |
| | 254 | |
|
| | 255 | | public void SetVisibility(bool visible) |
| | 256 | | { |
| 0 | 257 | | view.SetVisibility(visible); |
| | 258 | |
|
| 0 | 259 | | if (viewingUserProfile != null) |
| 0 | 260 | | viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot); |
| | 261 | |
|
| 0 | 262 | | if (visible) |
| | 263 | | { |
| 0 | 264 | | if (viewingUserProfile != null) |
| 0 | 265 | | viewingUserProfile.snapshotObserver.AddListener(view.SetFaceSnapshot); |
| | 266 | | } |
| 0 | 267 | | } |
| | 268 | |
|
| | 269 | | private void BlockPlayer() |
| | 270 | | { |
| 0 | 271 | | if (ownUserProfile.IsBlocked(currentUserProfile.userId)) return; |
| | 272 | |
|
| 0 | 273 | | dataStore.notifications.GenericConfirmation.Set(GenericConfirmationNotificationData.CreateBlockUserData( |
| | 274 | | userProfileBridge.Get(currentPlayerId)?.userName, |
| | 275 | | () => |
| | 276 | | { |
| 0 | 277 | | ownUserProfile.Block(currentUserProfile.userId); |
| 0 | 278 | | view.SetIsBlocked(true); |
| 0 | 279 | | WebInterface.SendBlockPlayer(currentUserProfile.userId); |
| 0 | 280 | | socialAnalytics.SendPlayerBlocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSou |
| 0 | 281 | | }), true); |
| 0 | 282 | | } |
| | 283 | |
|
| | 284 | | private void UnblockPlayer() |
| | 285 | | { |
| 0 | 286 | | if (!ownUserProfile.IsBlocked(currentUserProfile.userId)) return; |
| 0 | 287 | | ownUserProfile.Unblock(currentUserProfile.userId); |
| 0 | 288 | | view.SetIsBlocked(false); |
| 0 | 289 | | WebInterface.SendUnblockPlayer(currentUserProfile.userId); |
| 0 | 290 | | socialAnalytics.SendPlayerUnblocked(friendsController.IsFriend(currentUserProfile.userId), PlayerActionSource.Pa |
| 0 | 291 | | } |
| | 292 | |
|
| | 293 | | private void ReportPlayer() |
| | 294 | | { |
| 0 | 295 | | WebInterface.SendReportPlayer(currentPlayerId, currentUserProfile?.name); |
| 0 | 296 | | socialAnalytics.SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.Passport, currentPlayerId); |
| 0 | 297 | | } |
| | 298 | |
|
| | 299 | | public void Dispose() |
| | 300 | | { |
| 19 | 301 | | friendOperationsCancellationToken.SafeCancelAndDispose(); |
| 19 | 302 | | setUserProfileCancellationToken.SafeCancelAndDispose(); |
| | 303 | |
|
| 19 | 304 | | if (currentUserProfile != null) |
| 18 | 305 | | currentUserProfile.OnUpdate -= SetUserProfile; |
| | 306 | |
|
| 19 | 307 | | if (currentPlayerId != null) |
| | 308 | | { |
| 19 | 309 | | currentPlayerId.OnSame -= OnCurrentPlayerIdUpdated; |
| 19 | 310 | | currentPlayerId.OnChange -= OnCurrentPlayerIdChanged; |
| | 311 | | } |
| | 312 | |
|
| 19 | 313 | | if (closeWindowTrigger != null) |
| 19 | 314 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 315 | |
|
| 19 | 316 | | if (closeWindowTrigger != null) |
| 19 | 317 | | closeWindowTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 318 | |
|
| 19 | 319 | | if (toggleWorldChatTrigger != null) |
| 19 | 320 | | toggleWorldChatTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 321 | |
|
| 19 | 322 | | if (toggleFriendsTrigger != null) |
| 19 | 323 | | toggleFriendsTrigger.OnTriggered -= OnCloseButtonPressed; |
| | 324 | |
|
| 19 | 325 | | if (viewingUserProfile != null) |
| 18 | 326 | | viewingUserProfile.snapshotObserver.RemoveListener(view.SetFaceSnapshot); |
| | 327 | |
|
| 19 | 328 | | if (view != null) |
| 19 | 329 | | Object.Destroy(view.gameObject); |
| 19 | 330 | | } |
| | 331 | |
|
| | 332 | | private void OnFriendStatusUpdated(string userId, FriendshipAction action) |
| | 333 | | { |
| 0 | 334 | | if (currentUserProfile == null) |
| 0 | 335 | | return; |
| | 336 | |
|
| 0 | 337 | | UpdateFriendshipInteraction(); |
| 0 | 338 | | } |
| | 339 | |
|
| | 340 | | private void UpdateFriendshipInteraction() |
| | 341 | | { |
| 31 | 342 | | if (currentUserProfile == null) |
| | 343 | | { |
| 0 | 344 | | view.HideFriendshipInteraction(); |
| 0 | 345 | | return; |
| | 346 | | } |
| | 347 | |
|
| 31 | 348 | | view.UpdateFriendshipInteraction(CanBeFriends(), |
| | 349 | | friendsController.GetUserStatus(currentUserProfile.userId)); |
| 31 | 350 | | } |
| | 351 | |
|
| | 352 | | private bool CanBeFriends() |
| | 353 | | { |
| 31 | 354 | | return friendsController != null && friendsController.IsInitialized && currentUserProfile.hasConnectedWeb3 && is |
| | 355 | | } |
| | 356 | |
|
| | 357 | | private async UniTaskVoid LoadAndShowWearables(UserProfile userProfile, CancellationToken cancellationToken) |
| | 358 | | { |
| | 359 | | try |
| | 360 | | { |
| 31 | 361 | | var ownedWearables = await wearablesCatalogService.RequestOwnedWearablesAsync( |
| | 362 | | userProfile.userId, |
| | 363 | | 1, |
| | 364 | | int.MaxValue, |
| | 365 | | true, |
| | 366 | | cancellationToken); |
| | 367 | |
|
| 217 | 368 | | string[] wearableIds = ownedWearables.wearables.Select(x => x.id).ToArray(); |
| 31 | 369 | | userProfile.SetInventory(wearableIds); |
| 31 | 370 | | loadedWearables.AddRange(wearableIds); |
| | 371 | |
|
| 31 | 372 | | var containedWearables = ownedWearables.wearables |
| | 373 | | // this makes any sense? |
| 186 | 374 | | .Where(wearable => wearablesCatalogService.IsValidWearable(wearable.id)); |
| | 375 | |
|
| 31 | 376 | | view.SetWearables(containedWearables); |
| 31 | 377 | | } |
| 0 | 378 | | catch (Exception) |
| | 379 | | { |
| | 380 | | // Exception was ignored in the previous version |
| | 381 | | // Debug.LogException(e); |
| 0 | 382 | | } |
| 31 | 383 | | } |
| | 384 | |
|
| | 385 | | private bool IsBlocked(string userId) |
| | 386 | | { |
| 31 | 387 | | return ownUserProfile != null && ownUserProfile.IsBlocked(userId); |
| | 388 | | } |
| | 389 | |
|
| | 390 | | // TODO: support cancellation tokens on profanity filtering |
| | 391 | | private async UniTask<string> FilterName(UserProfile userProfile) |
| | 392 | | { |
| 31 | 393 | | return IsProfanityFilteringEnabled() |
| | 394 | | ? await profanityFilter.Filter(userProfile.userName) |
| | 395 | | : userProfile.userName; |
| 31 | 396 | | } |
| | 397 | |
|
| | 398 | | // TODO: support cancellation tokens on profanity filtering |
| | 399 | | private async UniTask<string> FilterDescription(UserProfile userProfile) |
| | 400 | | { |
| 31 | 401 | | return IsProfanityFilteringEnabled() |
| | 402 | | ? await profanityFilter.Filter(userProfile.description) |
| | 403 | | : userProfile.description; |
| 31 | 404 | | } |
| | 405 | |
|
| | 406 | | private bool IsProfanityFilteringEnabled() |
| | 407 | | { |
| 62 | 408 | | return dataStore.settings.profanityChatFilteringEnabled.Get(); |
| | 409 | | } |
| | 410 | | } |