< Summary

Class:JumpInButton
Assembly:FriendsHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendsHUD/Scripts/JumpInButton.cs
Covered lines:40
Uncovered lines:1
Coverable lines:41
Total lines:114
Line coverage:97.5% (40 of 41)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%220100%
OnDestroy()0%220100%
FriendsController_OnUpdateUserStatus(...)0%6.566075%
SearchUserStatus(...)0%660100%
UpdateInfo(...)0%110100%
RefreshInfo()0%550100%
JumpIn()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/FriendsHUD/Scripts/JumpInButton.cs

#LineLine coverage
 1using System;
 2using DCL.Interface;
 3using SocialFeaturesAnalytics;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7using 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>
 13public 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    {
 1537        if (friendsController == null)
 338            return;
 39
 1240        currentFriendsController = friendsController;
 1241        currentUserId = userId;
 1242        this.socialAnalytics = socialAnalytics;
 43
 1244        button.onClick.RemoveAllListeners();
 1345        button.onClick.AddListener(() => JumpIn());
 46
 1247        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 1248        currentFriendsController.OnUpdateUserStatus += FriendsController_OnUpdateUserStatus;
 49
 1250        SearchUserStatus(currentUserId);
 1251    }
 52
 53    private void OnDestroy()
 54    {
 3355        if (currentFriendsController == null)
 2156            return;
 57
 1258        button.onClick.RemoveAllListeners();
 1259        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 1260    }
 61
 62    private void FriendsController_OnUpdateUserStatus(string userId, UserStatus userStatus)
 63    {
 364        if (userId != currentUserId)
 065            return;
 66
 367        UpdateInfo(userStatus.position, userStatus.realm?.serverName, userStatus.realm?.layer, userStatus.presence);
 368    }
 69
 70    private void SearchUserStatus(string userId)
 71    {
 1272        if (currentFriendsController.GetAllocatedFriends().TryGetValue(userId, out currentUserStatus))
 73        {
 474            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        }
 1280    }
 81
 82    private void UpdateInfo(Vector2 coords, string realmServerName, string realmLayerName, PresenceStatus status)
 83    {
 784        currentCoords = coords;
 785        currentRealmServerName = realmServerName;
 786        currentRealmLayerName = realmLayerName;
 787        currentPresenceStatus = status;
 88
 789        RefreshInfo();
 790    }
 91
 92    private void RefreshInfo()
 93    {
 794        if (currentPresenceStatus == PresenceStatus.ONLINE &&
 95            !string.IsNullOrEmpty(currentRealmServerName))
 96        {
 697            string realmText = string.Format("{0}{1}", currentRealmServerName, !string.IsNullOrEmpty(currentRealmLayerNa
 698            playerLocationText.text = $"{realmText} {(int)currentCoords.x}, {(int)currentCoords.y}";
 699            this.gameObject.SetActive(true);
 6100        }
 101        else
 102        {
 1103            this.gameObject.SetActive(false);
 1104            playerLocationText.text = string.Empty;
 105        }
 1106    }
 107
 108    private void JumpIn()
 109    {
 1110        OnClick?.Invoke();
 1111        Environment.i.world.teleportController.JumpIn((int)currentCoords.x, (int)currentCoords.y, currentRealmServerName
 1112        socialAnalytics.SendPlayerJoin(PlayerActionSource.Conversation);
 1113    }
 114}