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