| | 1 | | using System; |
| | 2 | | using DCL.Interface; |
| | 3 | | using DCl.Social.Friends; |
| | 4 | | using DCL.Social.Friends; |
| | 5 | | using SocialFeaturesAnalytics; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | | using Environment = DCL.Environment; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// This button lets the player jump to the current location of a friend. |
| | 13 | | /// To initialize this control, use UpdateInfo(). |
| | 14 | | /// </summary> |
| | 15 | | public class JumpInButton : MonoBehaviour |
| | 16 | | { |
| | 17 | | public Button button; |
| | 18 | | public TextMeshProUGUI playerLocationText; |
| | 19 | |
|
| | 20 | | private IFriendsController currentFriendsController; |
| | 21 | | private string currentUserId; |
| | 22 | | private UserStatus currentUserStatus; |
| | 23 | |
|
| | 24 | | internal Vector2 currentCoords; |
| | 25 | | internal string currentRealmServerName; |
| | 26 | | internal string currentRealmLayerName; |
| | 27 | | internal PresenceStatus currentPresenceStatus; |
| | 28 | | internal ISocialAnalytics socialAnalytics; |
| | 29 | |
|
| | 30 | | public event Action OnClick; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Prepares the JumpIn button for listening to a specific user. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="friendsController">Friends Controller to be listened</param> |
| | 36 | | /// <param name="userId">User ID to listen to</param> |
| | 37 | | public void Initialize(IFriendsController friendsController, string userId, ISocialAnalytics socialAnalytics) |
| | 38 | | { |
| 2 | 39 | | if (friendsController == null) |
| 0 | 40 | | return; |
| | 41 | |
|
| 2 | 42 | | currentFriendsController = friendsController; |
| 2 | 43 | | currentUserId = userId; |
| 2 | 44 | | this.socialAnalytics = socialAnalytics; |
| | 45 | |
|
| 2 | 46 | | button.onClick.RemoveAllListeners(); |
| 2 | 47 | | button.onClick.AddListener(() => JumpIn()); |
| | 48 | |
|
| 2 | 49 | | currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus; |
| 2 | 50 | | currentFriendsController.OnUpdateUserStatus += FriendsController_OnUpdateUserStatus; |
| | 51 | |
|
| 2 | 52 | | SearchUserStatus(currentUserId); |
| 2 | 53 | | } |
| | 54 | |
|
| | 55 | | private void OnDestroy() |
| | 56 | | { |
| 14 | 57 | | if (currentFriendsController == null) |
| 12 | 58 | | return; |
| | 59 | |
|
| 2 | 60 | | button.onClick.RemoveAllListeners(); |
| 2 | 61 | | currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus; |
| 2 | 62 | | } |
| | 63 | |
|
| | 64 | | private void FriendsController_OnUpdateUserStatus(string userId, UserStatus userStatus) |
| | 65 | | { |
| 0 | 66 | | if (userId != currentUserId) |
| 0 | 67 | | return; |
| | 68 | |
|
| 0 | 69 | | UpdateInfo(userStatus.position, userStatus.realm?.serverName, userStatus.realm?.layer, userStatus.presence); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | private void SearchUserStatus(string userId) |
| | 73 | | { |
| 2 | 74 | | if (currentFriendsController.GetAllocatedFriends().TryGetValue(userId, out currentUserStatus)) |
| | 75 | | { |
| 0 | 76 | | UpdateInfo( |
| | 77 | | currentUserStatus.position, |
| | 78 | | currentUserStatus.realm != null ? currentUserStatus.realm.serverName : string.Empty, |
| | 79 | | currentUserStatus.realm != null ? currentUserStatus.realm.layer : string.Empty, |
| | 80 | | currentUserStatus.presence); |
| | 81 | | } |
| 2 | 82 | | } |
| | 83 | |
|
| | 84 | | private void UpdateInfo(Vector2 coords, string realmServerName, string realmLayerName, PresenceStatus status) |
| | 85 | | { |
| 0 | 86 | | currentCoords = coords; |
| 0 | 87 | | currentRealmServerName = realmServerName; |
| 0 | 88 | | currentRealmLayerName = realmLayerName; |
| 0 | 89 | | currentPresenceStatus = status; |
| | 90 | |
|
| 0 | 91 | | RefreshInfo(); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | private void RefreshInfo() |
| | 95 | | { |
| 0 | 96 | | if (currentPresenceStatus == PresenceStatus.ONLINE && |
| | 97 | | !string.IsNullOrEmpty(currentRealmServerName)) |
| | 98 | | { |
| 0 | 99 | | string realmText = string.Format("{0}{1}", currentRealmServerName, !string.IsNullOrEmpty(currentRealmLayerNa |
| 0 | 100 | | playerLocationText.text = $"{realmText} {(int)currentCoords.x}, {(int)currentCoords.y}"; |
| 0 | 101 | | this.gameObject.SetActive(true); |
| | 102 | | } |
| | 103 | | else |
| | 104 | | { |
| 0 | 105 | | this.gameObject.SetActive(false); |
| 0 | 106 | | playerLocationText.text = string.Empty; |
| | 107 | | } |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | private void JumpIn() |
| | 111 | | { |
| 0 | 112 | | OnClick?.Invoke(); |
| 0 | 113 | | Environment.i.world.teleportController.JumpIn((int)currentCoords.x, (int)currentCoords.y, currentRealmServerName |
| 0 | 114 | | socialAnalytics.SendPlayerJoin(PlayerActionSource.Conversation, currentUserId); |
| 0 | 115 | | } |
| | 116 | | } |