< 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:37
Uncovered lines:2
Coverable lines:39
Total lines:109
Line coverage:94.8% (37 of 39)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%22090%
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 TMPro;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7/// <summary>
 8/// This button lets the player jump to the current location of a friend.
 9/// To initialize this control, use UpdateInfo().
 10/// </summary>
 11public class JumpInButton : MonoBehaviour
 12{
 13    public Button button;
 14    public TextMeshProUGUI playerLocationText;
 15
 16    private IFriendsController currentFriendsController;
 17    private string currentUserId;
 18    private FriendsController.UserStatus currentUserStatus;
 19
 20    internal Vector2 currentCoords;
 21    internal string currentRealmServerName;
 22    internal string currentRealmLayerName;
 23    internal PresenceStatus currentPresenceStatus;
 24
 25    public event Action OnClick;
 26
 27    /// <summary>
 28    /// Prepares the JumpIn button for listening to a specific user.
 29    /// </summary>
 30    /// <param name="friendsController">Friends Controller to be listened</param>
 31    /// <param name="userId">User ID to listen to</param>
 32    public void Initialize(IFriendsController friendsController, string userId)
 33    {
 2634        if (friendsController == null)
 035            return;
 36
 2637        currentFriendsController = friendsController;
 2638        currentUserId = userId;
 39
 2640        button.onClick.RemoveAllListeners();
 2741        button.onClick.AddListener(() => JumpIn());
 42
 2643        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 2644        currentFriendsController.OnUpdateUserStatus += FriendsController_OnUpdateUserStatus;
 45
 2646        SearchUserStatus(currentUserId);
 2647    }
 48
 49    private void OnDestroy()
 50    {
 5651        if (currentFriendsController == null)
 3052            return;
 53
 2654        button.onClick.RemoveAllListeners();
 2655        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 2656    }
 57
 58    private void FriendsController_OnUpdateUserStatus(string userId, FriendsController.UserStatus userStatus)
 59    {
 360        if (userId != currentUserId)
 061            return;
 62
 363        UpdateInfo(userStatus.position, userStatus.realm?.serverName, userStatus.realm?.layer, userStatus.presence);
 364    }
 65
 66    private void SearchUserStatus(string userId)
 67    {
 2668        if (currentFriendsController.GetFriends().TryGetValue(userId, out currentUserStatus))
 69        {
 470            UpdateInfo(
 71                currentUserStatus.position,
 72                currentUserStatus.realm != null ? currentUserStatus.realm.serverName : string.Empty,
 73                currentUserStatus.realm != null ? currentUserStatus.realm.layer : string.Empty,
 74                currentUserStatus.presence);
 75        }
 2676    }
 77
 78    private void UpdateInfo(Vector2 coords, string realmServerName, string realmLayerName, PresenceStatus status)
 79    {
 780        currentCoords = coords;
 781        currentRealmServerName = realmServerName;
 782        currentRealmLayerName = realmLayerName;
 783        currentPresenceStatus = status;
 84
 785        RefreshInfo();
 786    }
 87
 88    private void RefreshInfo()
 89    {
 790        if (currentPresenceStatus == PresenceStatus.ONLINE &&
 91            !string.IsNullOrEmpty(currentRealmServerName))
 92        {
 693            string realmText = string.Format("{0}{1}", currentRealmServerName, !string.IsNullOrEmpty(currentRealmLayerNa
 694            playerLocationText.text = $"{realmText} {(int)currentCoords.x}, {(int)currentCoords.y}";
 695            this.gameObject.SetActive(true);
 696        }
 97        else
 98        {
 199            this.gameObject.SetActive(false);
 1100            playerLocationText.text = string.Empty;
 101        }
 1102    }
 103
 104    private void JumpIn()
 105    {
 1106        OnClick?.Invoke();
 1107        WebInterface.JumpIn((int)currentCoords.x, (int)currentCoords.y, currentRealmServerName, currentRealmLayerName);
 1108    }
 109}