< 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:17
Uncovered lines:23
Coverable lines:40
Total lines:116
Line coverage:42.5% (17 of 40)
Covered branches:0
Total branches:0
Covered methods:3
Total methods:7
Method coverage:42.8% (3 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%22090.91%
OnDestroy()0%220100%
FriendsController_OnUpdateUserStatus(...)0%42600%
SearchUserStatus(...)0%7.336066.67%
UpdateInfo(...)0%2100%
RefreshInfo()0%30500%
JumpIn()0%6200%

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 DCl.Social.Friends;
 4using DCL.Social.Friends;
 5using SocialFeaturesAnalytics;
 6using TMPro;
 7using UnityEngine;
 8using UnityEngine.UI;
 9using Environment = DCL.Environment;
 10
 11/// <summary>
 12/// This button lets the player jump to the current location of a friend.
 13/// To initialize this control, use UpdateInfo().
 14/// </summary>
 15public class JumpInButton : MonoBehaviour
 16{
 17    public Button button;
 18    public TextMeshProUGUI playerLocationText;
 19
 20    private IFriendsController currentFriendsController;
 21    private string currentUserId;
 22    private UserStatus currentUserStatus;
 23
 24    internal Vector2 currentCoords;
 25    internal string currentRealmServerName;
 26    internal string currentRealmLayerName;
 27    internal PresenceStatus currentPresenceStatus;
 28    internal ISocialAnalytics socialAnalytics;
 29
 30    public event Action OnClick;
 31
 32    /// <summary>
 33    /// Prepares the JumpIn button for listening to a specific user.
 34    /// </summary>
 35    /// <param name="friendsController">Friends Controller to be listened</param>
 36    /// <param name="userId">User ID to listen to</param>
 37    public void Initialize(IFriendsController friendsController, string userId, ISocialAnalytics socialAnalytics)
 38    {
 239        if (friendsController == null)
 040            return;
 41
 242        currentFriendsController = friendsController;
 243        currentUserId = userId;
 244        this.socialAnalytics = socialAnalytics;
 45
 246        button.onClick.RemoveAllListeners();
 247        button.onClick.AddListener(() => JumpIn());
 48
 249        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 250        currentFriendsController.OnUpdateUserStatus += FriendsController_OnUpdateUserStatus;
 51
 252        SearchUserStatus(currentUserId);
 253    }
 54
 55    private void OnDestroy()
 56    {
 1457        if (currentFriendsController == null)
 1258            return;
 59
 260        button.onClick.RemoveAllListeners();
 261        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 262    }
 63
 64    private void FriendsController_OnUpdateUserStatus(string userId, UserStatus userStatus)
 65    {
 066        if (userId != currentUserId)
 067            return;
 68
 069        UpdateInfo(userStatus.position, userStatus.realm?.serverName, userStatus.realm?.layer, userStatus.presence);
 070    }
 71
 72    private void SearchUserStatus(string userId)
 73    {
 274        if (currentFriendsController.GetAllocatedFriends().TryGetValue(userId, out currentUserStatus))
 75        {
 076            UpdateInfo(
 77                currentUserStatus.position,
 78                currentUserStatus.realm != null ? currentUserStatus.realm.serverName : string.Empty,
 79                currentUserStatus.realm != null ? currentUserStatus.realm.layer : string.Empty,
 80                currentUserStatus.presence);
 81        }
 282    }
 83
 84    private void UpdateInfo(Vector2 coords, string realmServerName, string realmLayerName, PresenceStatus status)
 85    {
 086        currentCoords = coords;
 087        currentRealmServerName = realmServerName;
 088        currentRealmLayerName = realmLayerName;
 089        currentPresenceStatus = status;
 90
 091        RefreshInfo();
 092    }
 93
 94    private void RefreshInfo()
 95    {
 096        if (currentPresenceStatus == PresenceStatus.ONLINE &&
 97            !string.IsNullOrEmpty(currentRealmServerName))
 98        {
 099            string realmText = string.Format("{0}{1}", currentRealmServerName, !string.IsNullOrEmpty(currentRealmLayerNa
 0100            playerLocationText.text = $"{realmText} {(int)currentCoords.x}, {(int)currentCoords.y}";
 0101            this.gameObject.SetActive(true);
 102        }
 103        else
 104        {
 0105            this.gameObject.SetActive(false);
 0106            playerLocationText.text = string.Empty;
 107        }
 0108    }
 109
 110    private void JumpIn()
 111    {
 0112        OnClick?.Invoke();
 0113        Environment.i.world.teleportController.JumpIn((int)currentCoords.x, (int)currentCoords.y, currentRealmServerName
 0114        socialAnalytics.SendPlayerJoin(PlayerActionSource.Conversation, currentUserId);
 0115    }
 116}