< 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:45
Uncovered lines:5
Coverable lines:50
Total lines:167
Line coverage:90% (45 of 50)
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%6200%
InstantiateConfiguredPlaceCard(...)0%220100%
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;
 8using Environment = DCL.Environment;
 9
 10/// <summary>
 11/// Utils related to the places management in ExploreV2.
 12/// </summary>
 13public static class ExplorePlacesUtils
 14{
 15    internal const string PLACE_CARD_MODAL_ID = "PlaceCard_Modal";
 16    internal const string NO_PLACE_DESCRIPTION_WRITTEN = "The author hasn't written a description yet.";
 17
 18    /// <summary>
 19    /// Instantiates (if does not already exists) a place card modal from the given prefab.
 20    /// </summary>
 21    /// <param name="placeCardModalPrefab">Prefab to instantiate.</param>
 22    /// <returns>An instance of a place card modal.</returns>
 23    public static PlaceCardComponentView ConfigurePlaceCardModal(PlaceCardComponentView placeCardModalPrefab)
 24    {
 12025        PlaceCardComponentView placeModal = null;
 26
 12027        GameObject existingModal = GameObject.Find(PLACE_CARD_MODAL_ID);
 12028        if (existingModal != null)
 9929            placeModal = existingModal.GetComponent<PlaceCardComponentView>();
 30        else
 31        {
 2132            placeModal = GameObject.Instantiate(placeCardModalPrefab);
 2133            placeModal.name = PLACE_CARD_MODAL_ID;
 34        }
 35
 12036        placeModal.Hide(true);
 37
 12038        return placeModal;
 39    }
 40
 41    /// <summary>
 42    /// Creates and configures a pool for place cards.
 43    /// </summary>
 44    /// <param name="pool">Pool to configure.</param>
 45    /// <param name="poolName">Name of the pool.</param>
 46    /// <param name="placeCardPrefab">Place card prefab to use by the pool.</param>
 47    /// <param name="maxPrewarmCount">Max number of pre-created cards.</param>
 48    public static void ConfigurePlaceCardsPool(out Pool pool, string poolName, PlaceCardComponentView placeCardPrefab, i
 49    {
 5650        pool = PoolManager.i.GetPool(poolName);
 5651        if (pool == null)
 52        {
 353            pool = PoolManager.i.AddPool(
 54                poolName,
 55                GameObject.Instantiate(placeCardPrefab).gameObject,
 56                maxPrewarmCount: maxPrewarmCount,
 57                isPersistent: true);
 58
 359            pool.ForcePrewarm();
 60        }
 5661    }
 62
 63    /// <summary>
 64    /// Instantiates and configures a given list of places.
 65    /// </summary>
 66    /// <param name="places">List of places data.</param>
 67    /// <param name="pool">Pool to use.</param>
 68    /// <param name="OnFriendHandlerAdded">Action to inform about the addition of a new friend handler.</param>
 69    /// <param name="OnPlaceInfoClicked">Action to inform when the Info button has been clicked.</param>
 70    /// <param name="OnPlaceJumpInClicked">Action to inform when the JumpIn button has been clicked.</param>
 71    /// <returns>A list of instances of places.</returns>
 72    public static List<BaseComponentView> InstantiateAndConfigurePlaceCards(
 73        List<PlaceCardComponentModel> places,
 74        Pool pool,
 75        Action<FriendsHandler> OnFriendHandlerAdded,
 76        Action<PlaceCardComponentModel> OnPlaceInfoClicked,
 77        Action<HotScenesController.HotSceneInfo> OnPlaceJumpInClicked)
 78    {
 079        List<BaseComponentView> instantiatedPlaces = new List<BaseComponentView>();
 80
 081        foreach (PlaceCardComponentModel placeInfo in places)
 082            instantiatedPlaces.Add(
 83                InstantiateConfiguredPlaceCard(placeInfo, pool, OnFriendHandlerAdded, OnPlaceInfoClicked, OnPlaceJumpInC
 84                );
 85
 086        return instantiatedPlaces;
 87    }
 88
 89    public static BaseComponentView InstantiateConfiguredPlaceCard(PlaceCardComponentModel placeInfo, Pool pool,
 90        Action<FriendsHandler> OnFriendHandlerAdded, Action<PlaceCardComponentModel> OnPlaceInfoClicked, Action<HotScene
 91    {
 3892        PlaceCardComponentView placeGO = pool.Get().gameObject.GetComponent<PlaceCardComponentView>();
 3893        ConfigurePlaceCard(placeGO, placeInfo, OnPlaceInfoClicked, OnPlaceJumpInClicked);
 3894        OnFriendHandlerAdded?.Invoke(placeGO.friendsHandler);
 3895        return placeGO;
 96    }
 97
 98    /// <summary>
 99    /// Configure a place card with the given model.
 100    /// </summary>
 101    /// <param name="placeCard">Place card to configure.</param>
 102    /// <param name="placeInfo">Model to apply.</param>
 103    /// <param name="OnPlaceInfoClicked">Action to inform when the Info button has been clicked.</param>
 104    /// <param name="OnPlaceJumpInClicked">Action to inform when the JumpIn button has been clicked.</param>
 105    public static void ConfigurePlaceCard(
 106        PlaceCardComponentView placeCard,
 107        PlaceCardComponentModel placeInfo,
 108        Action<PlaceCardComponentModel> OnPlaceInfoClicked,
 109        Action<HotScenesController.HotSceneInfo> OnPlaceJumpInClicked)
 110    {
 41111        placeCard.Configure(placeInfo);
 41112        placeCard.onInfoClick?.RemoveAllListeners();
 41113        placeCard.onInfoClick?.AddListener(() => OnPlaceInfoClicked?.Invoke(placeInfo));
 41114        placeCard.onJumpInClick?.RemoveAllListeners();
 41115        placeCard.onJumpInClick?.AddListener(() => OnPlaceJumpInClicked?.Invoke(placeInfo.hotSceneInfo));
 41116    }
 117
 118    /// <summary>
 119    /// Returs a place card model from the given API data.
 120    /// </summary>
 121    /// <param name="placeFromAPI">Data received from the API.</param>
 122    /// <returns>A place card model.</returns>
 123    public static PlaceCardComponentModel CreatePlaceCardModelFromAPIPlace(HotSceneInfo placeFromAPI)
 124    {
 9125        PlaceCardComponentModel placeCardModel = new PlaceCardComponentModel();
 9126        placeCardModel.placePictureUri = placeFromAPI.thumbnail;
 9127        placeCardModel.placeName = placeFromAPI.name;
 9128        placeCardModel.placeDescription = FormatDescription(placeFromAPI);
 9129        placeCardModel.placeAuthor = FormatAuthorName(placeFromAPI);
 9130        placeCardModel.numberOfUsers = placeFromAPI.usersTotalCount;
 9131        placeCardModel.parcels = placeFromAPI.parcels;
 9132        placeCardModel.coords = placeFromAPI.baseCoords;
 9133        placeCardModel.hotSceneInfo = placeFromAPI;
 134
 9135        return placeCardModel;
 136    }
 137
 10138    internal static string FormatDescription(HotSceneInfo placeFromAPI) { return string.IsNullOrEmpty(placeFromAPI.descr
 139
 10140    internal static string FormatAuthorName(HotSceneInfo placeFromAPI) { return $"Author <b>{placeFromAPI.creator}</b>";
 141
 142    /// <summary>
 143    /// Makes a jump in to the place defined by the given place data from API.
 144    /// </summary>
 145    /// <param name="placeFromAPI">Place data from API.</param>
 146    public static void JumpInToPlace(HotSceneInfo placeFromAPI)
 147    {
 2148        HotScenesController.HotSceneInfo.Realm realm = new HotScenesController.HotSceneInfo.Realm() { layer = null, serv
 4149        placeFromAPI.realms = placeFromAPI.realms.OrderByDescending(x => x.usersCount).ToArray();
 150
 4151        for (int i = 0; i < placeFromAPI.realms.Length; i++)
 152        {
 2153            bool isArchipelagoRealm = string.IsNullOrEmpty(placeFromAPI.realms[i].layer);
 154
 2155            if (isArchipelagoRealm || placeFromAPI.realms[i].usersCount < placeFromAPI.realms[i].maxUsers)
 156            {
 2157                realm = placeFromAPI.realms[i];
 2158                break;
 159            }
 160        }
 161
 2162        if (string.IsNullOrEmpty(realm.serverName))
 0163            Environment.i.world.teleportController.Teleport(placeFromAPI.baseCoords.x, placeFromAPI.baseCoords.y);
 164        else
 2165            Environment.i.world.teleportController.JumpIn(placeFromAPI.baseCoords.x, placeFromAPI.baseCoords.y, realm.se
 2166    }
 167}