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