< 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:113
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;
 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>
 12public class JumpInButton : MonoBehaviour
 13{
 14    public Button button;
 15    public TextMeshProUGUI playerLocationText;
 16
 17    private IFriendsController currentFriendsController;
 18    private string currentUserId;
 19    private 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    {
 1536        if (friendsController == null)
 337            return;
 38
 1239        currentFriendsController = friendsController;
 1240        currentUserId = userId;
 1241        this.socialAnalytics = socialAnalytics;
 42
 1243        button.onClick.RemoveAllListeners();
 1344        button.onClick.AddListener(() => JumpIn());
 45
 1246        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 1247        currentFriendsController.OnUpdateUserStatus += FriendsController_OnUpdateUserStatus;
 48
 1249        SearchUserStatus(currentUserId);
 1250    }
 51
 52    private void OnDestroy()
 53    {
 3554        if (currentFriendsController == null)
 2355            return;
 56
 1257        button.onClick.RemoveAllListeners();
 1258        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 1259    }
 60
 61    private void FriendsController_OnUpdateUserStatus(string userId, UserStatus userStatus)
 62    {
 363        if (userId != currentUserId)
 064            return;
 65
 366        UpdateInfo(userStatus.position, userStatus.realm?.serverName, userStatus.realm?.layer, userStatus.presence);
 367    }
 68
 69    private void SearchUserStatus(string userId)
 70    {
 1271        if (currentFriendsController.GetAllocatedFriends().TryGetValue(userId, out currentUserStatus))
 72        {
 473            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        }
 1279    }
 80
 81    private void UpdateInfo(Vector2 coords, string realmServerName, string realmLayerName, PresenceStatus status)
 82    {
 783        currentCoords = coords;
 784        currentRealmServerName = realmServerName;
 785        currentRealmLayerName = realmLayerName;
 786        currentPresenceStatus = status;
 87
 788        RefreshInfo();
 789    }
 90
 91    private void RefreshInfo()
 92    {
 793        if (currentPresenceStatus == PresenceStatus.ONLINE &&
 94            !string.IsNullOrEmpty(currentRealmServerName))
 95        {
 696            string realmText = string.Format("{0}{1}", currentRealmServerName, !string.IsNullOrEmpty(currentRealmLayerNa
 697            playerLocationText.text = $"{realmText} {(int)currentCoords.x}, {(int)currentCoords.y}";
 698            this.gameObject.SetActive(true);
 699        }
 100        else
 101        {
 1102            this.gameObject.SetActive(false);
 1103            playerLocationText.text = string.Empty;
 104        }
 1105    }
 106
 107    private void JumpIn()
 108    {
 1109        OnClick?.Invoke();
 1110        WebInterface.JumpIn((int)currentCoords.x, (int)currentCoords.y, currentRealmServerName, currentRealmLayerName);
 1111        socialAnalytics.SendPlayerJoin(PlayerActionSource.Conversation);
 1112    }
 113}