< Summary

Class:ExplorePlacesUtils
Assembly:ExploreV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Common/ExplorePlacesUtils.cs
Covered lines:48
Uncovered lines:1
Coverable lines:49
Total lines:160
Line coverage:97.9% (48 of 49)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ConfigurePlaceCardModal(...)0%220100%
ConfigurePlaceCardsPool(...)0%220100%
InstantiateAndConfigurePlaceCards(...)0%330100%
ConfigurePlaceCard(...)0%550100%
CreatePlaceCardModelFromAPIPlace(...)0%110100%
FormatDescription(...)0%220100%
FormatAuthorName(...)0%110100%
JumpInToPlace(...)0%6.136084.62%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ExploreV2/Scripts/Common/ExplorePlacesUtils.cs

#LineLine coverage
 1using DCL;
 2using DCL.Interface;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using UnityEngine;
 7using static HotScenesController;
 8
 9/// <summary>
 10/// Utils related to the places management in ExploreV2.
 11/// </summary>
 12public static class ExplorePlacesUtils
 13{
 14    internal const string PLACE_CARD_MODAL_ID = "PlaceCard_Modal";
 15    internal const string NO_PLACE_DESCRIPTION_WRITTEN = "The author hasn't written a description yet.";
 16
 17    /// <summary>
 18    /// Instantiates (if does not already exists) a place card modal from the given prefab.
 19    /// </summary>
 20    /// <param name="placeCardModalPrefab">Prefab to instantiate.</param>
 21    /// <returns>An instance of a place card modal.</returns>
 22    public static PlaceCardComponentView ConfigurePlaceCardModal(PlaceCardComponentView placeCardModalPrefab)
 23    {
 2524        PlaceCardComponentView placeModal = null;
 25
 2526        GameObject existingModal = GameObject.Find(PLACE_CARD_MODAL_ID);
 2527        if (existingModal != null)
 1728            placeModal = existingModal.GetComponent<PlaceCardComponentView>();
 29        else
 30        {
 831            placeModal = GameObject.Instantiate(placeCardModalPrefab);
 832            placeModal.name = PLACE_CARD_MODAL_ID;
 33        }
 34
 2535        placeModal.Hide(true);
 36
 2537        return placeModal;
 38    }
 39
 40    /// <summary>
 41    /// Creates and configures a pool for place cards.
 42    /// </summary>
 43    /// <param name="pool">Pool to configure.</param>
 44    /// <param name="poolName">Name of the pool.</param>
 45    /// <param name="placeCardPrefab">Place card prefab to use by the pool.</param>
 46    /// <param name="maxPrewarmCount">Max number of pre-created cards.</param>
 47    public static void ConfigurePlaceCardsPool(out Pool pool, string poolName, PlaceCardComponentView placeCardPrefab, i
 48    {
 3849        pool = PoolManager.i.GetPool(poolName);
 3850        if (pool == null)
 51        {
 352            pool = PoolManager.i.AddPool(
 53                poolName,
 54                GameObject.Instantiate(placeCardPrefab).gameObject,
 55                maxPrewarmCount: maxPrewarmCount,
 56                isPersistent: true);
 57
 358            pool.ForcePrewarm();
 59        }
 3860    }
 61
 62    /// <summary>
 63    /// Instantiates and configures a given list of places.
 64    /// </summary>
 65    /// <param name="places">List of places data.</param>
 66    /// <param name="pool">Pool to use.</param>
 67    /// <param name="OnFriendHandlerAdded">Action to inform about the addition of a new friend handler.</param>
 68    /// <param name="OnPlaceInfoClicked">Action to inform when the Info button has been clicked.</param>
 69    /// <param name="OnPlaceJumpInClicked">Action to inform when the JumpIn button has been clicked.</param>
 70    /// <returns>A list of instances of places.</returns>
 71    public static List<BaseComponentView> InstantiateAndConfigurePlaceCards(
 72        List<PlaceCardComponentModel> places,
 73        Pool pool,
 74        Action<FriendsHandler> OnFriendHandlerAdded,
 75        Action<PlaceCardComponentModel> OnPlaceInfoClicked,
 76        Action<HotScenesController.HotSceneInfo> OnPlaceJumpInClicked)
 77    {
 478        List<BaseComponentView> instantiatedPlaces = new List<BaseComponentView>();
 79
 2480        foreach (PlaceCardComponentModel placeInfo in places)
 81        {
 882            PlaceCardComponentView placeGO = pool.Get().gameObject.GetComponent<PlaceCardComponentView>();
 883            ConfigurePlaceCard(placeGO, placeInfo, OnPlaceInfoClicked, OnPlaceJumpInClicked);
 884            OnFriendHandlerAdded?.Invoke(placeGO.friendsHandler);
 885            instantiatedPlaces.Add(placeGO);
 86        }
 87
 488        return instantiatedPlaces;
 89    }
 90
 91    /// <summary>
 92    /// Configure a place card with the given model.
 93    /// </summary>
 94    /// <param name="placeCard">Place card to configure.</param>
 95    /// <param name="placeInfo">Model to apply.</param>
 96    /// <param name="OnPlaceInfoClicked">Action to inform when the Info button has been clicked.</param>
 97    /// <param name="OnPlaceJumpInClicked">Action to inform when the JumpIn button has been clicked.</param>
 98    public static void ConfigurePlaceCard(
 99        PlaceCardComponentView placeCard,
 100        PlaceCardComponentModel placeInfo,
 101        Action<PlaceCardComponentModel> OnPlaceInfoClicked,
 102        Action<HotScenesController.HotSceneInfo> OnPlaceJumpInClicked)
 103    {
 11104        placeCard.Configure(placeInfo);
 11105        placeCard.onInfoClick?.RemoveAllListeners();
 11106        placeCard.onInfoClick?.AddListener(() => OnPlaceInfoClicked?.Invoke(placeInfo));
 11107        placeCard.onJumpInClick?.RemoveAllListeners();
 11108        placeCard.onJumpInClick?.AddListener(() => OnPlaceJumpInClicked?.Invoke(placeInfo.hotSceneInfo));
 11109    }
 110
 111    /// <summary>
 112    /// Returs a place card model from the given API data.
 113    /// </summary>
 114    /// <param name="placeFromAPI">Data received from the API.</param>
 115    /// <returns>A place card model.</returns>
 116    public static PlaceCardComponentModel CreatePlaceCardModelFromAPIPlace(HotSceneInfo placeFromAPI)
 117    {
 9118        PlaceCardComponentModel placeCardModel = new PlaceCardComponentModel();
 9119        placeCardModel.placePictureUri = placeFromAPI.thumbnail;
 9120        placeCardModel.placeName = placeFromAPI.name;
 9121        placeCardModel.placeDescription = FormatDescription(placeFromAPI);
 9122        placeCardModel.placeAuthor = FormatAuthorName(placeFromAPI);
 9123        placeCardModel.numberOfUsers = placeFromAPI.usersTotalCount;
 9124        placeCardModel.parcels = placeFromAPI.parcels;
 9125        placeCardModel.coords = placeFromAPI.baseCoords;
 9126        placeCardModel.hotSceneInfo = placeFromAPI;
 127
 9128        return placeCardModel;
 129    }
 130
 10131    internal static string FormatDescription(HotSceneInfo placeFromAPI) { return string.IsNullOrEmpty(placeFromAPI.descr
 132
 10133    internal static string FormatAuthorName(HotSceneInfo placeFromAPI) { return $"Author <b>{placeFromAPI.creator}</b>";
 134
 135    /// <summary>
 136    /// Makes a jump in to the place defined by the given place data from API.
 137    /// </summary>
 138    /// <param name="placeFromAPI">Place data from API.</param>
 139    public static void JumpInToPlace(HotSceneInfo placeFromAPI)
 140    {
 2141        HotScenesController.HotSceneInfo.Realm realm = new HotScenesController.HotSceneInfo.Realm() { layer = null, serv
 4142        placeFromAPI.realms = placeFromAPI.realms.OrderByDescending(x => x.usersCount).ToArray();
 143
 4144        for (int i = 0; i < placeFromAPI.realms.Length; i++)
 145        {
 2146            bool isArchipelagoRealm = string.IsNullOrEmpty(placeFromAPI.realms[i].layer);
 147
 2148            if (isArchipelagoRealm || placeFromAPI.realms[i].usersCount < placeFromAPI.realms[i].maxUsers)
 149            {
 2150                realm = placeFromAPI.realms[i];
 2151                break;
 152            }
 153        }
 154
 2155        if (string.IsNullOrEmpty(realm.serverName))
 0156            WebInterface.GoTo(placeFromAPI.baseCoords.x, placeFromAPI.baseCoords.y);
 157        else
 2158            WebInterface.JumpIn(placeFromAPI.baseCoords.x, placeFromAPI.baseCoords.y, realm.serverName, realm.layer);
 2159    }
 160}