< Summary

Class:HotSceneCellView
Assembly:ExploreHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ExploreHUD/Scripts/HighlightScenes/HotSceneCellView.cs
Covered lines:57
Uncovered lines:65
Coverable lines:122
Total lines:272
Line coverage:46.7% (57 of 122)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
HotSceneCellView()0%110100%
Awake()0%220100%
Initialize()0%220100%
Setup(...)0%110100%
Clear()0%2100%
JumpInPressed()0%72800%
SetLastJumpInTried(...)0%2100%
OnRealmConnectionSuccess(...)0%30500%
OnRealmConnectionFailed(...)0%72800%
OnDestroy()0%110100%
OnEnable()0%2.022083.33%
OnCrowdInfoUpdated(...)0%110100%
OnFriendAdded(...)0%110100%
OnFriendRemoved(...)0%220100%
OnMapInfoUpdated(...)0%110100%
FetchThumbnail(...)0%110100%
SetThumbnail(...)0%6200%
SetLoaded()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ExploreHUD/Scripts/HighlightScenes/HotSceneCellView.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using DCL.Helpers;
 4using UnityEngine;
 5using TMPro;
 6using System.Linq;
 7using DCL;
 8using static DCL.Interface.WebInterface;
 9using DCL.Interface;
 10
 11internal class HotSceneCellView : MonoBehaviour
 12{
 13    const int THMBL_MARKETPLACE_WIDTH = 196;
 14    const int THMBL_MARKETPLACE_HEIGHT = 143;
 15    const int THMBL_MARKETPLACE_SIZEFACTOR = 50;
 16
 17    [Header("Animators")]
 18    [SerializeField] Animator viewAnimator;
 19    [SerializeField] ShowHideAnimator jumpInButtonAnimator;
 20
 21    [Header("Crowd")]
 22    [SerializeField] GameObject crowdCountContainer;
 23    [SerializeField] TextMeshProUGUI crowdCount;
 24
 25    [Header("Events")]
 26    [SerializeField] GameObject eventsContainer;
 27
 28    [Header("Friends")]
 29    [SerializeField] ExploreFriendsView friendsView;
 30    [SerializeField] GameObject friendsContainer;
 31
 32    [Header("Scene")]
 33    [SerializeField] TextMeshProUGUI sceneName;
 34    [SerializeField] internal RawImageFillParent thumbnailImage;
 35    [SerializeField] UIHoverCallback sceneInfoButton;
 36
 37    [Header("UI")]
 38    [SerializeField] UIHoverCallback jumpInHoverArea;
 39    [SerializeField] Button_OnPointerDown jumpIn;
 40    [SerializeField] Sprite errorThumbnail;
 41
 42    public delegate void JumpInDelegate(Vector2Int coords, string serverName, string layerName);
 43    public static event JumpInDelegate OnJumpIn;
 44
 45    public static event Action<HotSceneCellView> OnInfoButtonPointerDown;
 46    public static event Action OnInfoButtonPointerExit;
 47
 48    public event Action<Texture2D> OnThumbnailSet;
 49
 050    public MapInfoHandler mapInfoHandler { private set; get; }
 051    public FriendsHandler friendsHandler { private set; get; }
 052    public ThumbnailHandler thumbnailHandler { private set; get; }
 053    public AnimationHandler animationHandler { private set; get; }
 54
 55    private ViewPool<ExploreFriendsView> friendPool;
 56    private Dictionary<string, ExploreFriendsView> friendViewById;
 57    private bool isLoaded = false;
 58    private bool isInitialized = false;
 4459    private Queue<HotScenesController.HotSceneInfo.Realm> nextMostPopulatedRealms = new Queue<HotScenesController.HotSce
 4460    private JumpInPayload lastJumpInTried = new JumpInPayload();
 61
 62    HotScenesController.HotSceneInfo hotSceneInfo;
 63
 64    protected void Awake()
 65    {
 866        crowdCountContainer.SetActive(false);
 867        eventsContainer.SetActive(false);
 68
 869        jumpInHoverArea.OnPointerEnter += () =>
 70        {
 071            jumpInButtonAnimator.gameObject.SetActive(true);
 072            jumpInButtonAnimator.Show();
 073        };
 874        jumpInHoverArea.OnPointerExit += () => jumpInButtonAnimator.Hide();
 875        sceneInfoButton.OnPointerDown += () => jumpInButtonAnimator.Hide(true);
 76
 77        // NOTE: we don't use the pointer down callback to avoid being mistakenly pressed while dragging
 878        jumpIn.onClick.AddListener(JumpInPressed);
 79
 880        sceneInfoButton.OnPointerDown += () => OnInfoButtonPointerDown?.Invoke(this);
 881        sceneInfoButton.OnPointerExit += () => OnInfoButtonPointerExit?.Invoke();
 82
 883        Initialize();
 884    }
 85
 86    public void Initialize()
 87    {
 1488        if (isInitialized)
 689            return;
 90
 891        isInitialized = true;
 92
 893        friendPool = new ViewPool<ExploreFriendsView>(friendsView, 0);
 894        friendViewById = new Dictionary<string, ExploreFriendsView>();
 95
 896        mapInfoHandler = new MapInfoHandler();
 97
 898        friendsHandler = new FriendsHandler(mapInfoHandler);
 899        friendsHandler.onFriendAdded += OnFriendAdded;
 8100        friendsHandler.onFriendRemoved += OnFriendRemoved;
 101
 8102        thumbnailHandler = new ThumbnailHandler();
 8103        animationHandler = new AnimationHandler(viewAnimator);
 8104    }
 105
 106    public void Setup(HotScenesController.HotSceneInfo sceneInfo)
 107    {
 6108        hotSceneInfo = sceneInfo;
 6109        mapInfoHandler.SetMinimapSceneInfo(sceneInfo);
 110
 6111        OnCrowdInfoUpdated(sceneInfo);
 6112        OnMapInfoUpdated(sceneInfo);
 6113    }
 114
 115    public void Clear()
 116    {
 0117        thumbnailImage.texture = null;
 0118        thumbnailHandler.Dispose();
 0119        thumbnailImage.gameObject.SetActive(false);
 0120        isLoaded = false;
 0121    }
 122
 123    public void JumpInPressed()
 124    {
 0125        HotScenesController.HotSceneInfo.Realm realm = new HotScenesController.HotSceneInfo.Realm() { layer = null, serv
 0126        hotSceneInfo.realms = hotSceneInfo.realms.OrderByDescending(x => x.usersCount).ToArray();
 0127        nextMostPopulatedRealms.Clear();
 0128        for (int i = 0; i < hotSceneInfo.realms.Length; i++)
 129        {
 0130            bool isArchipelagoRealm = string.IsNullOrEmpty(hotSceneInfo.realms[i].layer);
 131
 0132            if (isArchipelagoRealm || hotSceneInfo.realms[i].usersCount < hotSceneInfo.realms[i].maxUsers)
 133            {
 0134                realm = hotSceneInfo.realms[i];
 0135                if (i < hotSceneInfo.realms.Length - 1)
 136                {
 0137                    nextMostPopulatedRealms = new Queue<HotScenesController.HotSceneInfo.Realm>(hotSceneInfo.realms.ToLi
 138                }
 0139                break;
 140            }
 141        }
 142
 0143        if (!string.IsNullOrEmpty(realm.serverName))
 144        {
 0145            RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 0146            RealmsInfoBridge.OnRealmConnectionSuccess += OnRealmConnectionSuccess;
 0147            RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 0148            RealmsInfoBridge.OnRealmConnectionFailed += OnRealmConnectionFailed;
 149        }
 150
 0151        SetLastJumpInTried(hotSceneInfo.baseCoords, realm.serverName, realm.layer);
 0152        OnJumpIn?.Invoke(hotSceneInfo.baseCoords, realm.serverName, realm.layer);
 0153    }
 154
 155    private void SetLastJumpInTried(Vector2 position, string serverName, string layer)
 156    {
 0157        lastJumpInTried.gridPosition = position;
 0158        lastJumpInTried.realm.serverName = serverName;
 0159        lastJumpInTried.realm.layer = layer;
 0160    }
 161
 162    private void OnRealmConnectionSuccess(JumpInPayload successRealm)
 163    {
 0164        bool isArchipelagoRealm = string.IsNullOrEmpty(successRealm.realm.layer);
 165
 0166        if (successRealm.gridPosition != lastJumpInTried.gridPosition ||
 167            successRealm.realm.serverName != lastJumpInTried.realm.serverName ||
 168            (!isArchipelagoRealm && successRealm.realm.layer != lastJumpInTried.realm.layer))
 0169            return;
 170
 0171        RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 0172        RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 0173    }
 174
 175    private void OnRealmConnectionFailed(JumpInPayload failedRealm)
 176    {
 0177        bool isArchipelagoRealm = string.IsNullOrEmpty(failedRealm.realm.layer);
 178
 0179        if (failedRealm.gridPosition != lastJumpInTried.gridPosition ||
 180            failedRealm.realm.serverName != lastJumpInTried.realm.serverName ||
 181            (!isArchipelagoRealm && failedRealm.realm.layer != lastJumpInTried.realm.layer))
 0182            return;
 183
 0184        if (nextMostPopulatedRealms.Count > 0)
 185        {
 0186            WebInterface.NotifyStatusThroughChat("Trying to connect to the next more populated realm...");
 0187            HotScenesController.HotSceneInfo.Realm nextRealmToTry = nextMostPopulatedRealms.Dequeue();
 0188            SetLastJumpInTried(hotSceneInfo.baseCoords, nextRealmToTry.serverName, nextRealmToTry.layer);
 0189            OnJumpIn?.Invoke(hotSceneInfo.baseCoords, nextRealmToTry.serverName, nextRealmToTry.layer);
 0190        }
 191        else
 192        {
 0193            WebInterface.NotifyStatusThroughChat("You'll stay in your current realm.");
 0194            RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 0195            RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 0196            OnJumpIn?.Invoke(hotSceneInfo.baseCoords, null, null);
 197        }
 0198    }
 199
 200    private void OnDestroy()
 201    {
 8202        friendPool.Dispose();
 8203        thumbnailHandler.Dispose();
 204
 8205        friendsHandler.onFriendAdded -= OnFriendAdded;
 8206        friendsHandler.onFriendRemoved -= OnFriendRemoved;
 207
 8208        RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 8209        RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 8210    }
 211
 212    private void OnEnable()
 213    {
 10214        jumpInButtonAnimator.gameObject.SetActive(false);
 10215        jumpInHoverArea.enabled = isLoaded;
 10216        thumbnailImage.gameObject.SetActive(isLoaded);
 217
 10218        if (isLoaded)
 219        {
 0220            animationHandler.SetLoaded();
 221        }
 10222    }
 223
 224    private void OnCrowdInfoUpdated(HotScenesController.HotSceneInfo info)
 225    {
 6226        crowdCount.text = info.usersTotalCount.ToString();
 6227        crowdCountContainer.SetActive(info.usersTotalCount > 0);
 6228    }
 229
 230    private void OnFriendAdded(UserProfile profile, Color backgroundColor)
 231    {
 2232        var view = friendPool.GetView();
 2233        view.SetUserProfile(profile, backgroundColor);
 2234        friendViewById.Add(profile.userId, view);
 2235    }
 236
 237    private void OnFriendRemoved(UserProfile profile)
 238    {
 2239        if (friendViewById.TryGetValue(profile.userId, out ExploreFriendsView view))
 240        {
 2241            friendPool.PoolView(view);
 2242            friendViewById.Remove(profile.userId);
 243        }
 2244    }
 245
 246    private void OnMapInfoUpdated(HotScenesController.HotSceneInfo sceneInfo)
 247    {
 6248        sceneName.text = sceneInfo.name;
 249
 6250        FetchThumbnail(sceneInfo.thumbnail,
 6251            onFail: () => FetchThumbnail(MapUtils.GetMarketPlaceThumbnailUrl(sceneInfo.parcels, THMBL_MARKETPLACE_WIDTH,
 0252                onFail: () => SetThumbnail(errorThumbnail.texture)));
 6253    }
 254
 24255    private void FetchThumbnail(string url, Action onFail) { thumbnailHandler.FetchThumbnail(url, SetThumbnail, onFail);
 256
 257    private void SetThumbnail(Texture2D texture)
 258    {
 0259        thumbnailImage.texture = texture;
 0260        thumbnailImage.gameObject.SetActive(true);
 0261        OnThumbnailSet?.Invoke(texture);
 262
 0263        SetLoaded();
 0264    }
 265
 266    private void SetLoaded()
 267    {
 0268        isLoaded = true;
 0269        animationHandler.SetLoaded();
 0270        jumpInHoverArea.enabled = true;
 0271    }
 272}