< 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:39
Uncovered lines:1
Coverable lines:40
Total lines:116
Line coverage:97.5% (39 of 40)
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 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    {
 1539        if (friendsController == null)
 340            return;
 41
 1242        currentFriendsController = friendsController;
 1243        currentUserId = userId;
 1244        this.socialAnalytics = socialAnalytics;
 45
 1246        button.onClick.RemoveAllListeners();
 1347        button.onClick.AddListener(() => JumpIn());
 48
 1249        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 1250        currentFriendsController.OnUpdateUserStatus += FriendsController_OnUpdateUserStatus;
 51
 1252        SearchUserStatus(currentUserId);
 1253    }
 54
 55    private void OnDestroy()
 56    {
 2757        if (currentFriendsController == null)
 2158            return;
 59
 660        button.onClick.RemoveAllListeners();
 661        currentFriendsController.OnUpdateUserStatus -= FriendsController_OnUpdateUserStatus;
 662    }
 63
 64    private void FriendsController_OnUpdateUserStatus(string userId, UserStatus userStatus)
 65    {
 366        if (userId != currentUserId)
 067            return;
 68
 369        UpdateInfo(userStatus.position, userStatus.realm?.serverName, userStatus.realm?.layer, userStatus.presence);
 370    }
 71
 72    private void SearchUserStatus(string userId)
 73    {
 1274        if (currentFriendsController.GetAllocatedFriends().TryGetValue(userId, out currentUserStatus))
 75        {
 476            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        }
 1282    }
 83
 84    private void UpdateInfo(Vector2 coords, string realmServerName, string realmLayerName, PresenceStatus status)
 85    {
 786        currentCoords = coords;
 787        currentRealmServerName = realmServerName;
 788        currentRealmLayerName = realmLayerName;
 789        currentPresenceStatus = status;
 90
 791        RefreshInfo();
 792    }
 93
 94    private void RefreshInfo()
 95    {
 796        if (currentPresenceStatus == PresenceStatus.ONLINE &&
 97            !string.IsNullOrEmpty(currentRealmServerName))
 98        {
 699            string realmText = string.Format("{0}{1}", currentRealmServerName, !string.IsNullOrEmpty(currentRealmLayerNa
 6100            playerLocationText.text = $"{realmText} {(int)currentCoords.x}, {(int)currentCoords.y}";
 6101            this.gameObject.SetActive(true);
 102        }
 103        else
 104        {
 1105            this.gameObject.SetActive(false);
 1106            playerLocationText.text = string.Empty;
 107        }
 1108    }
 109
 110    private void JumpIn()
 111    {
 1112        OnClick?.Invoke();
 1113        Environment.i.world.teleportController.JumpIn((int)currentCoords.x, (int)currentCoords.y, currentRealmServerName
 1114        socialAnalytics.SendPlayerJoin(PlayerActionSource.Conversation);
 1115    }
 116}