< 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:279
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
 20    [SerializeField] ShowHideAnimator jumpInButtonAnimator;
 21
 22    [Header("Crowd")]
 23    [SerializeField] GameObject crowdCountContainer;
 24
 25    [SerializeField] TextMeshProUGUI crowdCount;
 26
 27    [Header("Events")]
 28    [SerializeField] GameObject eventsContainer;
 29
 30    [Header("Friends")]
 31    [SerializeField] ExploreFriendsView friendsView;
 32
 33    [SerializeField] GameObject friendsContainer;
 34
 35    [Header("Scene")]
 36    [SerializeField] TextMeshProUGUI sceneName;
 37
 38    [SerializeField] internal RawImageFillParent thumbnailImage;
 39    [SerializeField] UIHoverCallback sceneInfoButton;
 40
 41    [Header("UI")]
 42    [SerializeField] UIHoverCallback jumpInHoverArea;
 43
 44    [SerializeField] Button_OnPointerDown jumpIn;
 45    [SerializeField] Sprite errorThumbnail;
 46
 47    public delegate void JumpInDelegate(Vector2Int coords, string serverName, string layerName);
 48
 49    public static event JumpInDelegate OnJumpIn;
 50
 51    public static event Action<HotSceneCellView> OnInfoButtonPointerDown;
 52    public static event Action OnInfoButtonPointerExit;
 53
 54    public event Action<Texture2D> OnThumbnailSet;
 55
 056    public MapInfoHandler mapInfoHandler { private set; get; }
 057    public FriendsHandler friendsHandler { private set; get; }
 058    public ThumbnailHandler thumbnailHandler { private set; get; }
 059    public AnimationHandler animationHandler { private set; get; }
 60
 61    private ViewPool<ExploreFriendsView> friendPool;
 62    private Dictionary<string, ExploreFriendsView> friendViewById;
 63    private bool isLoaded = false;
 64    private bool isInitialized = false;
 4465    private Queue<HotScenesController.HotSceneInfo.Realm> nextMostPopulatedRealms = new Queue<HotScenesController.HotSce
 4466    private JumpInPayload lastJumpInTried = new JumpInPayload();
 67
 68    HotScenesController.HotSceneInfo hotSceneInfo;
 69
 70    protected void Awake()
 71    {
 872        crowdCountContainer.SetActive(false);
 873        eventsContainer.SetActive(false);
 74
 875        jumpInHoverArea.OnPointerEnter += () =>
 76        {
 077            jumpInButtonAnimator.gameObject.SetActive(true);
 078            jumpInButtonAnimator.Show();
 079        };
 880        jumpInHoverArea.OnPointerExit += () => jumpInButtonAnimator.Hide();
 881        sceneInfoButton.OnPointerDown += () => jumpInButtonAnimator.Hide(true);
 82
 83        // NOTE: we don't use the pointer down callback to avoid being mistakenly pressed while dragging
 884        jumpIn.onClick.AddListener(JumpInPressed);
 85
 886        sceneInfoButton.OnPointerDown += () => OnInfoButtonPointerDown?.Invoke(this);
 887        sceneInfoButton.OnPointerExit += () => OnInfoButtonPointerExit?.Invoke();
 88
 889        Initialize();
 890    }
 91
 92    public void Initialize()
 93    {
 1494        if (isInitialized)
 695            return;
 96
 897        isInitialized = true;
 98
 899        friendPool = new ViewPool<ExploreFriendsView>(friendsView, 0);
 8100        friendViewById = new Dictionary<string, ExploreFriendsView>();
 101
 8102        mapInfoHandler = new MapInfoHandler();
 103
 8104        friendsHandler = new FriendsHandler(mapInfoHandler);
 8105        friendsHandler.OnFriendAddedEvent += OnFriendAdded;
 8106        friendsHandler.OnFriendRemovedEvent += OnFriendRemoved;
 107
 8108        thumbnailHandler = new ThumbnailHandler();
 8109        animationHandler = new AnimationHandler(viewAnimator);
 8110    }
 111
 112    public void Setup(HotScenesController.HotSceneInfo sceneInfo)
 113    {
 6114        hotSceneInfo = sceneInfo;
 6115        mapInfoHandler.SetMinimapSceneInfo(sceneInfo);
 116
 6117        OnCrowdInfoUpdated(sceneInfo);
 6118        OnMapInfoUpdated(sceneInfo);
 6119    }
 120
 121    public void Clear()
 122    {
 0123        thumbnailImage.texture = null;
 0124        thumbnailHandler.Dispose();
 0125        thumbnailImage.gameObject.SetActive(false);
 0126        isLoaded = false;
 0127    }
 128
 129    public void JumpInPressed()
 130    {
 0131        HotScenesController.HotSceneInfo.Realm realm = new HotScenesController.HotSceneInfo.Realm() { layer = null, serv
 0132        hotSceneInfo.realms = hotSceneInfo.realms.OrderByDescending(x => x.usersCount).ToArray();
 0133        nextMostPopulatedRealms.Clear();
 0134        for (int i = 0; i < hotSceneInfo.realms.Length; i++)
 135        {
 0136            bool isArchipelagoRealm = string.IsNullOrEmpty(hotSceneInfo.realms[i].layer);
 137
 0138            if (isArchipelagoRealm || hotSceneInfo.realms[i].usersCount < hotSceneInfo.realms[i].maxUsers)
 139            {
 0140                realm = hotSceneInfo.realms[i];
 0141                if (i < hotSceneInfo.realms.Length - 1)
 142                {
 0143                    nextMostPopulatedRealms = new Queue<HotScenesController.HotSceneInfo.Realm>(hotSceneInfo.realms.ToLi
 144                }
 145
 0146                break;
 147            }
 148        }
 149
 0150        if (!string.IsNullOrEmpty(realm.serverName))
 151        {
 0152            RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 0153            RealmsInfoBridge.OnRealmConnectionSuccess += OnRealmConnectionSuccess;
 0154            RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 0155            RealmsInfoBridge.OnRealmConnectionFailed += OnRealmConnectionFailed;
 156        }
 157
 0158        SetLastJumpInTried(hotSceneInfo.baseCoords, realm.serverName, realm.layer);
 0159        OnJumpIn?.Invoke(hotSceneInfo.baseCoords, realm.serverName, realm.layer);
 0160    }
 161
 162    private void SetLastJumpInTried(Vector2 position, string serverName, string layer)
 163    {
 0164        lastJumpInTried.gridPosition = position;
 0165        lastJumpInTried.realm.serverName = serverName;
 0166        lastJumpInTried.realm.layer = layer;
 0167    }
 168
 169    private void OnRealmConnectionSuccess(JumpInPayload successRealm)
 170    {
 0171        bool isArchipelagoRealm = string.IsNullOrEmpty(successRealm.realm.layer);
 172
 0173        if (successRealm.gridPosition != lastJumpInTried.gridPosition ||
 174            successRealm.realm.serverName != lastJumpInTried.realm.serverName ||
 175            (!isArchipelagoRealm && successRealm.realm.layer != lastJumpInTried.realm.layer))
 0176            return;
 177
 0178        RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 0179        RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 0180    }
 181
 182    private void OnRealmConnectionFailed(JumpInPayload failedRealm)
 183    {
 0184        bool isArchipelagoRealm = string.IsNullOrEmpty(failedRealm.realm.layer);
 185
 0186        if (failedRealm.gridPosition != lastJumpInTried.gridPosition ||
 187            failedRealm.realm.serverName != lastJumpInTried.realm.serverName ||
 188            (!isArchipelagoRealm && failedRealm.realm.layer != lastJumpInTried.realm.layer))
 0189            return;
 190
 0191        if (nextMostPopulatedRealms.Count > 0)
 192        {
 0193            WebInterface.NotifyStatusThroughChat("Trying to connect to the next more populated realm...");
 0194            HotScenesController.HotSceneInfo.Realm nextRealmToTry = nextMostPopulatedRealms.Dequeue();
 0195            SetLastJumpInTried(hotSceneInfo.baseCoords, nextRealmToTry.serverName, nextRealmToTry.layer);
 0196            OnJumpIn?.Invoke(hotSceneInfo.baseCoords, nextRealmToTry.serverName, nextRealmToTry.layer);
 0197        }
 198        else
 199        {
 0200            WebInterface.NotifyStatusThroughChat("You'll stay in your current realm.");
 0201            RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 0202            RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 0203            OnJumpIn?.Invoke(hotSceneInfo.baseCoords, null, null);
 204        }
 0205    }
 206
 207    private void OnDestroy()
 208    {
 8209        friendPool.Dispose();
 8210        thumbnailHandler.Dispose();
 211
 8212        friendsHandler.OnFriendAddedEvent -= OnFriendAdded;
 8213        friendsHandler.OnFriendRemovedEvent -= OnFriendRemoved;
 214
 8215        RealmsInfoBridge.OnRealmConnectionFailed -= OnRealmConnectionFailed;
 8216        RealmsInfoBridge.OnRealmConnectionSuccess -= OnRealmConnectionSuccess;
 8217    }
 218
 219    private void OnEnable()
 220    {
 10221        jumpInButtonAnimator.gameObject.SetActive(false);
 10222        jumpInHoverArea.enabled = isLoaded;
 10223        thumbnailImage.gameObject.SetActive(isLoaded);
 224
 10225        if (isLoaded)
 226        {
 0227            animationHandler.SetLoaded();
 228        }
 10229    }
 230
 231    private void OnCrowdInfoUpdated(HotScenesController.HotSceneInfo info)
 232    {
 6233        crowdCount.text = info.usersTotalCount.ToString();
 6234        crowdCountContainer.SetActive(info.usersTotalCount > 0);
 6235    }
 236
 237    private void OnFriendAdded(UserProfile profile, Color backgroundColor)
 238    {
 2239        var view = friendPool.GetView();
 2240        view.SetUserProfile(profile, backgroundColor);
 2241        friendViewById.Add(profile.userId, view);
 2242    }
 243
 244    private void OnFriendRemoved(UserProfile profile)
 245    {
 2246        if (friendViewById.TryGetValue(profile.userId, out ExploreFriendsView view))
 247        {
 2248            friendPool.PoolView(view);
 2249            friendViewById.Remove(profile.userId);
 250        }
 2251    }
 252
 253    private void OnMapInfoUpdated(HotScenesController.HotSceneInfo sceneInfo)
 254    {
 6255        sceneName.text = sceneInfo.name;
 256
 6257        FetchThumbnail(sceneInfo.thumbnail,
 6258            onFail: () => FetchThumbnail(MapUtils.GetMarketPlaceThumbnailUrl(sceneInfo.parcels, THMBL_MARKETPLACE_WIDTH,
 0259                onFail: () => SetThumbnail(errorThumbnail.texture)));
 6260    }
 261
 24262    private void FetchThumbnail(string url, Action onFail) { thumbnailHandler.FetchThumbnail(url, SetThumbnail, onFail);
 263
 264    private void SetThumbnail(Texture2D texture)
 265    {
 0266        thumbnailImage.texture = texture;
 0267        thumbnailImage.gameObject.SetActive(true);
 0268        OnThumbnailSet?.Invoke(texture);
 269
 0270        SetLoaded();
 0271    }
 272
 273    private void SetLoaded()
 274    {
 0275        isLoaded = true;
 0276        animationHandler.SetLoaded();
 0277        jumpInHoverArea.enabled = true;
 0278    }
 279}