| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL.Interface; |
| | 4 | | using SocialFeaturesAnalytics; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.EventSystems; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Contextual menu with different options about an user. |
| | 12 | | /// </summary> |
| | 13 | | [RequireComponent(typeof(RectTransform))] |
| | 14 | | public class UserContextMenu : MonoBehaviour |
| | 15 | | { |
| | 16 | | internal const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId"; |
| | 17 | |
|
| | 18 | | const string BLOCK_BTN_BLOCK_TEXT = "Block"; |
| | 19 | | const string BLOCK_BTN_UNBLOCK_TEXT = "Unblock"; |
| | 20 | | const string DELETE_MSG_PATTERN = "Are you sure you want to delete {0} as a friend?"; |
| | 21 | |
|
| | 22 | | [System.Flags] |
| | 23 | | public enum MenuConfigFlags |
| | 24 | | { |
| | 25 | | Name = 1, |
| | 26 | | Friendship = 2, |
| | 27 | | Message = 4, |
| | 28 | | Passport = 8, |
| | 29 | | Block = 16, |
| | 30 | | Report = 32 |
| | 31 | | } |
| | 32 | |
|
| | 33 | | const MenuConfigFlags headerFlags = MenuConfigFlags.Name | MenuConfigFlags.Friendship; |
| | 34 | | const MenuConfigFlags usesFriendsApiFlags = MenuConfigFlags.Friendship | MenuConfigFlags.Message; |
| | 35 | |
|
| | 36 | | [Header("Optional: Set Confirmation Dialog")] |
| | 37 | | [SerializeField] internal UserContextConfirmationDialog confirmationDialog; |
| | 38 | |
|
| | 39 | | [Header("Enable Actions")] |
| 196 | 40 | | [SerializeField] internal MenuConfigFlags menuConfigFlags = MenuConfigFlags.Passport | MenuConfigFlags.Block | MenuC |
| | 41 | |
|
| | 42 | | [Header("Containers")] |
| | 43 | | [SerializeField] internal GameObject headerContainer; |
| | 44 | | [SerializeField] internal GameObject bodyContainer; |
| | 45 | | [SerializeField] internal GameObject friendshipContainer; |
| | 46 | | [SerializeField] internal GameObject friendAddContainer; |
| | 47 | | [SerializeField] internal GameObject friendRemoveContainer; |
| | 48 | | [SerializeField] internal GameObject friendRequestedContainer; |
| | 49 | |
|
| | 50 | | [Header("Texts")] |
| | 51 | | [SerializeField] internal TextMeshProUGUI userName; |
| | 52 | | [SerializeField] internal TextMeshProUGUI blockText; |
| | 53 | |
|
| | 54 | | [Header("Buttons")] |
| | 55 | | [SerializeField] internal Button passportButton; |
| | 56 | | [SerializeField] internal Button blockButton; |
| | 57 | | [SerializeField] internal Button reportButton; |
| | 58 | | [SerializeField] internal Button addFriendButton; |
| | 59 | | [SerializeField] internal Button cancelFriendButton; |
| | 60 | | [SerializeField] internal Button deleteFriendButton; |
| | 61 | | [SerializeField] internal Button messageButton; |
| | 62 | |
|
| | 63 | | public static event System.Action<string> OnOpenPrivateChatRequest; |
| | 64 | |
|
| 0 | 65 | | public bool isVisible => gameObject.activeSelf; |
| | 66 | |
|
| | 67 | | public event System.Action OnShowMenu; |
| | 68 | | public event System.Action<string> OnPassport; |
| | 69 | | public event System.Action<string> OnReport; |
| | 70 | | public event System.Action<string, bool> OnBlock; |
| | 71 | | public event System.Action<string> OnUnfriend; |
| | 72 | | public event System.Action<string> OnAddFriend; |
| | 73 | | public event System.Action<string> OnCancelFriend; |
| | 74 | | public event System.Action<string> OnMessage; |
| | 75 | |
|
| | 76 | | private static StringVariable currentPlayerId = null; |
| | 77 | | private RectTransform rectTransform; |
| | 78 | | private string userId; |
| | 79 | | private bool isBlocked; |
| | 80 | | private MenuConfigFlags currentConfigFlags; |
| | 81 | | private IConfirmationDialog currentConfirmationDialog; |
| | 82 | | internal ISocialAnalytics socialAnalytics; |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Show context menu |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="userId"> user id</param> |
| 10 | 88 | | public void Show(string userId) { Show(userId, menuConfigFlags); } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Show context menu |
| | 92 | | /// </summary> |
| | 93 | | /// <param name="userId"> user id</param> |
| | 94 | | /// <param name="configFlags">set buttons to enable in menu</param> |
| | 95 | | public void Show(string userId, MenuConfigFlags configFlags) |
| | 96 | | { |
| 7 | 97 | | this.userId = userId; |
| 7 | 98 | | ProcessActiveElements(configFlags); |
| 7 | 99 | | Setup(userId, configFlags); |
| 7 | 100 | | if (currentConfirmationDialog == null && confirmationDialog != null) |
| | 101 | | { |
| 0 | 102 | | SetConfirmationDialog(confirmationDialog); |
| | 103 | | } |
| 7 | 104 | | gameObject.SetActive(true); |
| 7 | 105 | | OnShowMenu?.Invoke(); |
| 1 | 106 | | } |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// Set confirmation popup to reference use |
| | 110 | | /// </summary> |
| | 111 | | /// <param name="confirmationPopup">confirmation popup reference</param> |
| 0 | 112 | | public void SetConfirmationDialog(IConfirmationDialog confirmationPopup) { this.currentConfirmationDialog = confirma |
| | 113 | |
|
| | 114 | | /// <summary> |
| | 115 | | /// Hides the context menu. |
| | 116 | | /// </summary> |
| 40 | 117 | | public void Hide() { gameObject.SetActive(false); } |
| | 118 | |
|
| | 119 | | private void Awake() |
| | 120 | | { |
| 8 | 121 | | if (!currentPlayerId) |
| | 122 | | { |
| 1 | 123 | | currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID); |
| | 124 | | } |
| | 125 | |
|
| 8 | 126 | | rectTransform = GetComponent<RectTransform>(); |
| | 127 | |
|
| 8 | 128 | | passportButton.onClick.AddListener(OnPassportButtonPressed); |
| 8 | 129 | | blockButton.onClick.AddListener(OnBlockUserButtonPressed); |
| 8 | 130 | | reportButton.onClick.AddListener(OnReportUserButtonPressed); |
| 8 | 131 | | deleteFriendButton.onClick.AddListener(OnDeleteUserButtonPressed); |
| 8 | 132 | | addFriendButton.onClick.AddListener(OnAddFriendButtonPressed); |
| 8 | 133 | | cancelFriendButton.onClick.AddListener(OnCancelFriendRequestButtonPressed); |
| 8 | 134 | | messageButton.onClick.AddListener(OnMessageButtonPressed); |
| 8 | 135 | | } |
| | 136 | |
|
| 0 | 137 | | private void Update() { HideIfClickedOutside(); } |
| | 138 | |
|
| | 139 | | private void OnDisable() |
| | 140 | | { |
| 8 | 141 | | if (FriendsController.i) |
| | 142 | | { |
| 7 | 143 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| | 144 | | } |
| 8 | 145 | | } |
| | 146 | |
|
| 0 | 147 | | public void ClickReportButton() => reportButton.onClick.Invoke(); |
| | 148 | |
|
| | 149 | | private void OnPassportButtonPressed() |
| | 150 | | { |
| 1 | 151 | | OnPassport?.Invoke(userId); |
| 1 | 152 | | currentPlayerId.Set(userId); |
| 1 | 153 | | Hide(); |
| | 154 | |
|
| 1 | 155 | | AudioScriptableObjects.dialogOpen.Play(true); |
| 1 | 156 | | } |
| | 157 | |
|
| | 158 | | private void OnReportUserButtonPressed() |
| | 159 | | { |
| 1 | 160 | | OnReport?.Invoke(userId); |
| 1 | 161 | | WebInterface.SendReportPlayer(userId, UserProfileController.userProfilesCatalog.Get(userId)?.userName); |
| 1 | 162 | | GetSocialAnalytics().SendPlayerReport(PlayerReportIssueType.None, 0, PlayerActionSource.ProfileContextMenu); |
| 1 | 163 | | Hide(); |
| 1 | 164 | | } |
| | 165 | |
|
| | 166 | | private void OnDeleteUserButtonPressed() |
| | 167 | | { |
| 0 | 168 | | OnUnfriend?.Invoke(userId); |
| | 169 | |
|
| 0 | 170 | | if (currentConfirmationDialog != null) |
| | 171 | | { |
| 0 | 172 | | currentConfirmationDialog.SetText(string.Format(DELETE_MSG_PATTERN, UserProfileController.userProfilesCatalo |
| 0 | 173 | | currentConfirmationDialog.Show(() => |
| | 174 | | { |
| 0 | 175 | | UnfriendUser(); |
| 0 | 176 | | }); |
| 0 | 177 | | } |
| | 178 | | else |
| | 179 | | { |
| 0 | 180 | | UnfriendUser(); |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | GetSocialAnalytics().SendFriendDeleted(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSource.Profil |
| 0 | 184 | | Hide(); |
| 0 | 185 | | } |
| | 186 | |
|
| | 187 | | private void UnfriendUser() |
| | 188 | | { |
| 0 | 189 | | FriendsController.FriendshipUpdateStatusMessage newFriendshipStatusMessage = new FriendsController.FriendshipUpd |
| | 190 | | { |
| | 191 | | userId = userId, |
| | 192 | | action = FriendshipAction.DELETED |
| | 193 | | }; |
| | 194 | |
|
| 0 | 195 | | FriendsController.i.UpdateFriendshipStatus(newFriendshipStatusMessage); |
| 0 | 196 | | WebInterface.UpdateFriendshipStatus(newFriendshipStatusMessage); |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | private void OnAddFriendButtonPressed() |
| | 200 | | { |
| 0 | 201 | | OnAddFriend?.Invoke(userId); |
| | 202 | |
|
| 0 | 203 | | if (!FriendsController.i) |
| | 204 | | { |
| 0 | 205 | | return; |
| | 206 | | } |
| | 207 | |
|
| | 208 | | // NOTE: if we don't add this, the friend request has strange behaviors |
| 0 | 209 | | UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel() |
| | 210 | | { |
| | 211 | | userId = userId, |
| | 212 | | name = UserProfileController.userProfilesCatalog.Get(userId)?.userName |
| | 213 | | }); |
| | 214 | |
|
| 0 | 215 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 216 | | { |
| | 217 | | userId = userId, |
| | 218 | | action = FriendshipAction.REQUESTED_TO |
| | 219 | | }); |
| | 220 | |
|
| 0 | 221 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 222 | | { |
| | 223 | | userId = userId, action = FriendshipAction.REQUESTED_TO |
| | 224 | | }); |
| | 225 | |
|
| 0 | 226 | | GetSocialAnalytics().SendFriendRequestSent(UserProfile.GetOwnUserProfile().userId, userId, 0, PlayerActionSource |
| 0 | 227 | | } |
| | 228 | |
|
| | 229 | | private void OnCancelFriendRequestButtonPressed() |
| | 230 | | { |
| 0 | 231 | | OnCancelFriend?.Invoke(userId); |
| | 232 | |
|
| 0 | 233 | | if (!FriendsController.i) |
| | 234 | | { |
| 0 | 235 | | return; |
| | 236 | | } |
| | 237 | |
|
| 0 | 238 | | FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 239 | | { |
| | 240 | | userId = userId, |
| | 241 | | action = FriendshipAction.CANCELLED |
| | 242 | | }); |
| | 243 | |
|
| 0 | 244 | | WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage() |
| | 245 | | { |
| | 246 | | userId = userId, action = FriendshipAction.CANCELLED |
| | 247 | | }); |
| | 248 | |
|
| 0 | 249 | | GetSocialAnalytics().SendFriendRequestCancelled(UserProfile.GetOwnUserProfile().userId, userId, PlayerActionSour |
| 0 | 250 | | } |
| | 251 | |
|
| | 252 | | private void OnMessageButtonPressed() |
| | 253 | | { |
| 0 | 254 | | OnMessage?.Invoke(userId); |
| 0 | 255 | | OnOpenPrivateChatRequest?.Invoke(userId); |
| 0 | 256 | | Hide(); |
| 0 | 257 | | } |
| | 258 | |
|
| | 259 | | private void OnBlockUserButtonPressed() |
| | 260 | | { |
| 1 | 261 | | bool blockUser = !isBlocked; |
| 1 | 262 | | OnBlock?.Invoke(userId, blockUser); |
| 1 | 263 | | if (blockUser) |
| | 264 | | { |
| 1 | 265 | | WebInterface.SendBlockPlayer(userId); |
| 1 | 266 | | GetSocialAnalytics().SendPlayerBlocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileConte |
| 1 | 267 | | } |
| | 268 | | else |
| | 269 | | { |
| 0 | 270 | | WebInterface.SendUnblockPlayer(userId); |
| 0 | 271 | | GetSocialAnalytics().SendPlayerUnblocked(FriendsController.i.IsFriend(userId), PlayerActionSource.ProfileCon |
| | 272 | | } |
| 1 | 273 | | Hide(); |
| 1 | 274 | | } |
| | 275 | |
|
| 10 | 276 | | private void UpdateBlockButton() { blockText.text = isBlocked ? BLOCK_BTN_UNBLOCK_TEXT : BLOCK_BTN_BLOCK_TEXT; } |
| | 277 | |
|
| | 278 | | private void HideIfClickedOutside() |
| | 279 | | { |
| 0 | 280 | | if (!Input.GetMouseButtonDown(0)) return; |
| 0 | 281 | | var pointerEventData = new PointerEventData(EventSystem.current) |
| | 282 | | { |
| | 283 | | position = Input.mousePosition |
| | 284 | | }; |
| 0 | 285 | | var raycastResults = new List<RaycastResult>(); |
| 0 | 286 | | EventSystem.current.RaycastAll(pointerEventData, raycastResults); |
| | 287 | |
|
| 0 | 288 | | if (raycastResults.All(result => result.gameObject != gameObject)) |
| 0 | 289 | | Hide(); |
| 0 | 290 | | } |
| | 291 | |
|
| | 292 | | private void ProcessActiveElements(MenuConfigFlags flags) |
| | 293 | | { |
| 27 | 294 | | headerContainer.SetActive((flags & headerFlags) != 0); |
| 27 | 295 | | userName.gameObject.SetActive((flags & MenuConfigFlags.Name) != 0); |
| 27 | 296 | | friendshipContainer.SetActive((flags & MenuConfigFlags.Friendship) != 0); |
| 27 | 297 | | deleteFriendButton.gameObject.SetActive((flags & MenuConfigFlags.Friendship) != 0); |
| 27 | 298 | | passportButton.gameObject.SetActive((flags & MenuConfigFlags.Passport) != 0); |
| 27 | 299 | | blockButton.gameObject.SetActive((flags & MenuConfigFlags.Block) != 0); |
| 27 | 300 | | reportButton.gameObject.SetActive((flags & MenuConfigFlags.Report) != 0); |
| 27 | 301 | | messageButton.gameObject.SetActive((flags & MenuConfigFlags.Message) != 0); |
| 27 | 302 | | } |
| | 303 | |
|
| | 304 | | private void Setup(string userId, MenuConfigFlags configFlags) |
| | 305 | | { |
| 7 | 306 | | this.userId = userId; |
| | 307 | |
|
| 7 | 308 | | UserProfile profile = UserProfileController.userProfilesCatalog.Get(userId); |
| 7 | 309 | | bool userHasWallet = profile?.hasConnectedWeb3 ?? false; |
| | 310 | |
|
| 7 | 311 | | if (!userHasWallet || !UserProfile.GetOwnUserProfile().hasConnectedWeb3) |
| | 312 | | { |
| 1 | 313 | | configFlags &= ~usesFriendsApiFlags; |
| | 314 | | } |
| | 315 | |
|
| 7 | 316 | | currentConfigFlags = configFlags; |
| 7 | 317 | | ProcessActiveElements(configFlags); |
| | 318 | |
|
| 7 | 319 | | if ((configFlags & MenuConfigFlags.Block) != 0) |
| | 320 | | { |
| 5 | 321 | | isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId); |
| 5 | 322 | | UpdateBlockButton(); |
| | 323 | | } |
| 7 | 324 | | if ((configFlags & MenuConfigFlags.Name) != 0) |
| | 325 | | { |
| 5 | 326 | | string name = profile?.userName; |
| 5 | 327 | | userName.text = name; |
| | 328 | | } |
| 7 | 329 | | if ((configFlags & usesFriendsApiFlags) != 0 && FriendsController.i) |
| | 330 | | { |
| 6 | 331 | | if (FriendsController.i.friends.TryGetValue(userId, out FriendsController.UserStatus status)) |
| | 332 | | { |
| 0 | 333 | | SetupFriendship(status.friendshipStatus); |
| 0 | 334 | | } |
| | 335 | | else |
| | 336 | | { |
| 6 | 337 | | SetupFriendship(FriendshipStatus.NOT_FRIEND); |
| | 338 | | } |
| 6 | 339 | | FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate; |
| 6 | 340 | | FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate; |
| | 341 | | } |
| 7 | 342 | | } |
| | 343 | |
|
| | 344 | | private void SetupFriendship(FriendshipStatus friendshipStatus) |
| | 345 | | { |
| 12 | 346 | | bool friendshipEnabled = (currentConfigFlags & MenuConfigFlags.Friendship) != 0; |
| 12 | 347 | | bool messageEnabled = (currentConfigFlags & MenuConfigFlags.Message) != 0; |
| | 348 | |
|
| 12 | 349 | | if (friendshipStatus == FriendshipStatus.FRIEND) |
| | 350 | | { |
| 2 | 351 | | if (friendshipEnabled) |
| | 352 | | { |
| 1 | 353 | | friendAddContainer.SetActive(false); |
| 1 | 354 | | friendRemoveContainer.SetActive(true); |
| 1 | 355 | | friendRequestedContainer.SetActive(false); |
| 1 | 356 | | deleteFriendButton.gameObject.SetActive(true); |
| | 357 | | } |
| 2 | 358 | | if (messageEnabled) |
| | 359 | | { |
| 1 | 360 | | messageButton.gameObject.SetActive(true); |
| | 361 | | } |
| 1 | 362 | | } |
| 10 | 363 | | else if (friendshipStatus == FriendshipStatus.REQUESTED_TO) |
| | 364 | | { |
| 2 | 365 | | if (friendshipEnabled) |
| | 366 | | { |
| 1 | 367 | | friendAddContainer.SetActive(false); |
| 1 | 368 | | friendRemoveContainer.SetActive(false); |
| 1 | 369 | | friendRequestedContainer.SetActive(true); |
| 1 | 370 | | deleteFriendButton.gameObject.SetActive(false); |
| | 371 | | } |
| 2 | 372 | | if (messageEnabled) |
| | 373 | | { |
| 1 | 374 | | messageButton.gameObject.SetActive(false); |
| | 375 | | } |
| 1 | 376 | | } |
| 8 | 377 | | else if (friendshipStatus == FriendshipStatus.NOT_FRIEND) |
| | 378 | | { |
| 8 | 379 | | if (friendshipEnabled) |
| | 380 | | { |
| 6 | 381 | | friendAddContainer.SetActive(true); |
| 6 | 382 | | friendRemoveContainer.SetActive(false); |
| 6 | 383 | | friendRequestedContainer.SetActive(false); |
| 6 | 384 | | deleteFriendButton.gameObject.SetActive(false); |
| | 385 | | } |
| 8 | 386 | | if (messageEnabled) |
| | 387 | | { |
| 6 | 388 | | messageButton.gameObject.SetActive(false); |
| | 389 | | } |
| 6 | 390 | | } |
| 0 | 391 | | else if (friendshipStatus == FriendshipStatus.REQUESTED_FROM) |
| | 392 | | { |
| 0 | 393 | | if (friendshipEnabled) |
| | 394 | | { |
| 0 | 395 | | friendAddContainer.SetActive(true); |
| 0 | 396 | | friendRemoveContainer.SetActive(false); |
| 0 | 397 | | friendRequestedContainer.SetActive(false); |
| 0 | 398 | | deleteFriendButton.gameObject.SetActive(false); |
| | 399 | | } |
| 0 | 400 | | if (messageEnabled) |
| | 401 | | { |
| 0 | 402 | | messageButton.gameObject.SetActive(false); |
| | 403 | | } |
| | 404 | | } |
| 4 | 405 | | } |
| | 406 | |
|
| | 407 | | private void OnFriendActionUpdate(string userId, FriendshipAction action) |
| | 408 | | { |
| 6 | 409 | | if (this.userId != userId) |
| | 410 | | { |
| 0 | 411 | | return; |
| | 412 | | } |
| | 413 | |
|
| 6 | 414 | | if (action == FriendshipAction.APPROVED) |
| | 415 | | { |
| 2 | 416 | | SetupFriendship(FriendshipStatus.FRIEND); |
| 2 | 417 | | } |
| 4 | 418 | | else if (action == FriendshipAction.REQUESTED_TO) |
| | 419 | | { |
| 2 | 420 | | SetupFriendship(FriendshipStatus.REQUESTED_TO); |
| 2 | 421 | | } |
| 2 | 422 | | else if (action == FriendshipAction.DELETED || action == FriendshipAction.CANCELLED || action == FriendshipActio |
| | 423 | | { |
| 2 | 424 | | SetupFriendship(FriendshipStatus.NOT_FRIEND); |
| | 425 | | } |
| 2 | 426 | | } |
| | 427 | |
|
| | 428 | | private ISocialAnalytics GetSocialAnalytics() |
| | 429 | | { |
| 2 | 430 | | if (socialAnalytics == null) |
| | 431 | | { |
| 0 | 432 | | socialAnalytics = new SocialAnalytics( |
| | 433 | | DCL.Environment.i.platform.serviceProviders.analytics, |
| | 434 | | new UserProfileWebInterfaceBridge()); |
| | 435 | | } |
| | 436 | |
|
| 2 | 437 | | return socialAnalytics; |
| | 438 | | } |
| | 439 | |
|
| | 440 | | #if UNITY_EDITOR |
| | 441 | | //This is just to process buttons and container visibility on editor |
| | 442 | | private void OnValidate() |
| | 443 | | { |
| 13 | 444 | | if (headerContainer == null) |
| 0 | 445 | | return; |
| 13 | 446 | | ProcessActiveElements(menuConfigFlags); |
| 13 | 447 | | } |
| | 448 | | #endif |
| | 449 | | } |