< 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:56
Coverable lines:113
Total lines:255
Line coverage:50.4% (57 of 113)
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%42600%
OnRealmConnectionSuccess(...)0%20400%
OnRealmConnectionFailed(...)0%56700%
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;
 9
 10internal class HotSceneCellView : MonoBehaviour
 11{
 12    const int THMBL_MARKETPLACE_WIDTH = 196;
 13    const int THMBL_MARKETPLACE_HEIGHT = 143;
 14    const int THMBL_MARKETPLACE_SIZEFACTOR = 50;
 15
 16    [Header("Animators")]
 17    [SerializeField] Animator viewAnimator;
 18    [SerializeField] ShowHideAnimator jumpInButtonAnimator;
 19
 20    [Header("Crowd")]
 21    [SerializeField] GameObject crowdCountContainer;
 22    [SerializeField] TextMeshProUGUI crowdCount;
 23
 24    [Header("Events")]
 25    [SerializeField] GameObject eventsContainer;
 26
 27    [Header("Friends")]
 28    [SerializeField] ExploreFriendsView friendsView;
 29    [SerializeField] GameObject friendsContainer;
 30
 31    [Header("Scene")]
 32    [SerializeField] TextMeshProUGUI sceneName;
 33    [SerializeField] internal RawImageFillParent thumbnailImage;
 34    [SerializeField] UIHoverCallback sceneInfoButton;
 35
 36    [Header("UI")]
 37    [SerializeField] UIHoverCallback jumpInHoverArea;
 38    [SerializeField] Button_OnPointerDown jumpIn;
 39    [SerializeField] Sprite errorThumbnail;
 40
 41    public delegate void JumpInDelegate(Vector2Int coords, string serverName, string layerName);
 42    public static event JumpInDelegate OnJumpIn;
 43
 44    public static event Action<HotSceneCellView> OnInfoButtonPointerDown;
 45    public static event Action OnInfoButtonPointerExit;
 46
 47    public event Action<Texture2D> OnThumbnailSet;
 48
 049    public MapInfoHandler mapInfoHandler { private set; get; }
 050    public FriendsHandler friendsHandler { private set; get; }
 051    public ThumbnailHandler thumbnailHandler { private set; get; }
 052    public AnimationHandler animationHandler { private set; get; }
 53
 54    private ViewPool<ExploreFriendsView> friendPool;
 55    private Dictionary<string, ExploreFriendsView> friendViewById;
 56    private bool isLoaded = false;
 57    private bool isInitialized = false;
 4458    private Queue<HotScenesController.HotSceneInfo.Realm> nextMostPopulatedRealms = new Queue<HotScenesController.HotSce
 4459    private JumpInPayload lastJumpInTried = new JumpInPayload();
 60
 61    HotScenesController.HotSceneInfo hotSceneInfo;
 62
 63    protected void Awake()
 64    {
 865        crowdCountContainer.SetActive(false);
 866        eventsContainer.SetActive(false);
 67
 868        jumpInHoverArea.OnPointerEnter += () =>
 69        {
 070            jumpInButtonAnimator.gameObject.SetActive(true);
 071            jumpInButtonAnimator.Show();
 072        };
 873        jumpInHoverArea.OnPointerExit += () => jumpInButtonAnimator.Hide();
 874        sceneInfoButton.OnPointerDown += () => jumpInButtonAnimator.Hide(true);
 75
 76        // NOTE: we don't use the pointer down callback to avoid being mistakenly pressed while dragging
 877        jumpIn.onClick.AddListener(JumpInPressed);
 78
 879        sceneInfoButton.OnPointerDown += () => OnInfoButtonPointerDown?.Invoke(this);
 880        sceneInfoButton.OnPointerExit += () => OnInfoButtonPointerExit?.Invoke();
 81
 882        Initialize();
 883    }
 84
 85    public void Initialize()
 86    {
 1487        if (isInitialized)
 688            return;
 89
 890        isInitialized = true;
 91
 892        friendPool = new ViewPool<ExploreFriendsView>(friendsView, 0);
 893        friendViewById = new Dictionary<string, ExploreFriendsView>();
 94
 895        mapInfoHandler = new MapInfoHandler();
 96
 897        friendsHandler = new FriendsHandler(mapInfoHandler);
 898        friendsHandler.onFriendAdded += OnFriendAdded;
 899        friendsHandler.onFriendRemoved += OnFriendRemoved;
 100
 8101        thumbnailHandler = new ThumbnailHandler();
 8102        animationHandler = new AnimationHandler(viewAnimator);
 8103    }
 104
 105    public void Setup(HotScenesController.HotSceneInfo sceneInfo)
 106    {
 6107        hotSceneInfo = sceneInfo;
 6108        mapInfoHandler.SetMinimapSceneInfo(sceneInfo);
 109
 6110        OnCrowdInfoUpdated(sceneInfo);
 6111        OnMapInfoUpdated(sceneInfo);
 6112    }
 113
 114    public void Clear()
 115    {
 0116        thumbnailImage.texture = null;
 0117        thumbnailHandler.Dispose();
 0118        thumbnailImage.gameObject.SetActive(false);
 0119        isLoaded = false;
 0120    }
 121
 122    public void JumpInPressed()
 123    {
 0124        HotScenesController.HotSceneInfo.Realm realm = new HotScenesController.HotSceneInfo.Realm() { layer = null, serv
 0125        hotSceneInfo.realms = hotSceneInfo.realms.OrderByDescending(x => x.usersCount).ToArray();
 0126        nextMostPopulatedRealms.Clear();
 0127        for (int i = 0; i < hotSceneInfo.realms.Length; i++)
 128        {
 0129            if (hotSceneInfo.realms[i].usersCount < hotSceneInfo.realms[i].usersMax)
 130            {
 0131                realm = hotSceneInfo.realms[i];
 0132                if (i < hotSceneInfo.realms.Length - 1)
 133                {
 0134                    nextMostPopulatedRealms = new Queue<HotScenesController.HotSceneInfo.Realm>(hotSceneInfo.realms.ToLi
 135                }
 0136                break;
 137            }
 138        }
 139
 0140        RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 0141        RealmsInfoBridge.OnRealmConnectionSuccess += OnRealmConnectionSuccess;
 0142        RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 0143        RealmsInfoBridge.OnRealmConnectionFailed += OnRealmConnectionFailed;
 144
 0145        lastJumpInTried.gridPosition = hotSceneInfo.baseCoords;
 0146        lastJumpInTried.realm.serverName = realm.serverName;
 0147        lastJumpInTried.realm.layer = realm.layer;
 148
 0149        OnJumpIn?.Invoke(hotSceneInfo.baseCoords, realm.serverName, realm.layer);
 0150    }
 151
 152    private void OnRealmConnectionSuccess(JumpInPayload successRealm)
 153    {
 0154        if (successRealm.gridPosition != lastJumpInTried.gridPosition ||
 155            successRealm.realm.serverName != lastJumpInTried.realm.serverName ||
 156            successRealm.realm.layer != lastJumpInTried.realm.layer)
 0157            return;
 158
 0159        RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 0160        RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 0161    }
 162
 163    private void OnRealmConnectionFailed(JumpInPayload failedRealm)
 164    {
 0165        if (failedRealm.gridPosition != lastJumpInTried.gridPosition ||
 166            failedRealm.realm.serverName != lastJumpInTried.realm.serverName ||
 167            failedRealm.realm.layer != lastJumpInTried.realm.layer)
 0168            return;
 169
 0170        if (nextMostPopulatedRealms.Count > 0)
 171        {
 0172            HotScenesController.HotSceneInfo.Realm nextRealmToTry = nextMostPopulatedRealms.Dequeue();
 0173            OnJumpIn?.Invoke(hotSceneInfo.baseCoords, nextRealmToTry.serverName, nextRealmToTry.layer);
 0174        }
 175        else
 176        {
 0177            RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 0178            RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 0179            OnJumpIn?.Invoke(hotSceneInfo.baseCoords, null, null);
 180        }
 0181    }
 182
 183    private void OnDestroy()
 184    {
 8185        friendPool.Dispose();
 8186        thumbnailHandler.Dispose();
 187
 8188        friendsHandler.onFriendAdded -= OnFriendAdded;
 8189        friendsHandler.onFriendRemoved -= OnFriendRemoved;
 190
 8191        RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 8192        RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 8193    }
 194
 195    private void OnEnable()
 196    {
 10197        jumpInButtonAnimator.gameObject.SetActive(false);
 10198        jumpInHoverArea.enabled = isLoaded;
 10199        thumbnailImage.gameObject.SetActive(isLoaded);
 200
 10201        if (isLoaded)
 202        {
 0203            animationHandler.SetLoaded();
 204        }
 10205    }
 206
 207    private void OnCrowdInfoUpdated(HotScenesController.HotSceneInfo info)
 208    {
 6209        crowdCount.text = info.usersTotalCount.ToString();
 6210        crowdCountContainer.SetActive(info.usersTotalCount > 0);
 6211    }
 212
 213    private void OnFriendAdded(UserProfile profile, Color backgroundColor)
 214    {
 2215        var view = friendPool.GetView();
 2216        view.SetUserProfile(profile, backgroundColor);
 2217        friendViewById.Add(profile.userId, view);
 2218    }
 219
 220    private void OnFriendRemoved(UserProfile profile)
 221    {
 2222        if (friendViewById.TryGetValue(profile.userId, out ExploreFriendsView view))
 223        {
 2224            friendPool.PoolView(view);
 2225            friendViewById.Remove(profile.userId);
 226        }
 2227    }
 228
 229    private void OnMapInfoUpdated(HotScenesController.HotSceneInfo sceneInfo)
 230    {
 6231        sceneName.text = sceneInfo.name;
 232
 6233        FetchThumbnail(sceneInfo.thumbnail,
 6234            onFail: () => FetchThumbnail(MapUtils.GetMarketPlaceThumbnailUrl(sceneInfo.parcels, THMBL_MARKETPLACE_WIDTH,
 0235                onFail: () => SetThumbnail(errorThumbnail.texture)));
 6236    }
 237
 24238    private void FetchThumbnail(string url, Action onFail) { thumbnailHandler.FetchThumbnail(url, SetThumbnail, onFail);
 239
 240    private void SetThumbnail(Texture2D texture)
 241    {
 0242        thumbnailImage.texture = texture;
 0243        thumbnailImage.gameObject.SetActive(true);
 0244        OnThumbnailSet?.Invoke(texture);
 245
 0246        SetLoaded();
 0247    }
 248
 249    private void SetLoaded()
 250    {
 0251        isLoaded = true;
 0252        animationHandler.SetLoaded();
 0253        jumpInHoverArea.enabled = true;
 0254    }
 255}