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