| | 1 | | using DCL; |
| | 2 | | using DCL.Interface; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | | using static HotScenesController; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Utils related to the places management in ExploreV2. |
| | 11 | | /// </summary> |
| | 12 | | public 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 | | { |
| 25 | 24 | | PlaceCardComponentView placeModal = null; |
| | 25 | |
|
| 25 | 26 | | GameObject existingModal = GameObject.Find(PLACE_CARD_MODAL_ID); |
| 25 | 27 | | if (existingModal != null) |
| 17 | 28 | | placeModal = existingModal.GetComponent<PlaceCardComponentView>(); |
| | 29 | | else |
| | 30 | | { |
| 8 | 31 | | placeModal = GameObject.Instantiate(placeCardModalPrefab); |
| 8 | 32 | | placeModal.name = PLACE_CARD_MODAL_ID; |
| | 33 | | } |
| | 34 | |
|
| 25 | 35 | | placeModal.Hide(true); |
| | 36 | |
|
| 25 | 37 | | 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 | | { |
| 38 | 49 | | pool = PoolManager.i.GetPool(poolName); |
| 38 | 50 | | if (pool == null) |
| | 51 | | { |
| 3 | 52 | | pool = PoolManager.i.AddPool( |
| | 53 | | poolName, |
| | 54 | | GameObject.Instantiate(placeCardPrefab).gameObject, |
| | 55 | | maxPrewarmCount: maxPrewarmCount, |
| | 56 | | isPersistent: true); |
| | 57 | |
|
| 3 | 58 | | pool.ForcePrewarm(); |
| | 59 | | } |
| 38 | 60 | | } |
| | 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 | | { |
| 4 | 78 | | List<BaseComponentView> instantiatedPlaces = new List<BaseComponentView>(); |
| | 79 | |
|
| 24 | 80 | | foreach (PlaceCardComponentModel placeInfo in places) |
| | 81 | | { |
| 8 | 82 | | PlaceCardComponentView placeGO = pool.Get().gameObject.GetComponent<PlaceCardComponentView>(); |
| 8 | 83 | | ConfigurePlaceCard(placeGO, placeInfo, OnPlaceInfoClicked, OnPlaceJumpInClicked); |
| 8 | 84 | | OnFriendHandlerAdded?.Invoke(placeGO.friendsHandler); |
| 8 | 85 | | instantiatedPlaces.Add(placeGO); |
| | 86 | | } |
| | 87 | |
|
| 4 | 88 | | 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 | | { |
| 11 | 104 | | placeCard.Configure(placeInfo); |
| 11 | 105 | | placeCard.onInfoClick?.RemoveAllListeners(); |
| 11 | 106 | | placeCard.onInfoClick?.AddListener(() => OnPlaceInfoClicked?.Invoke(placeInfo)); |
| 11 | 107 | | placeCard.onJumpInClick?.RemoveAllListeners(); |
| 11 | 108 | | placeCard.onJumpInClick?.AddListener(() => OnPlaceJumpInClicked?.Invoke(placeInfo.hotSceneInfo)); |
| 11 | 109 | | } |
| | 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 | | { |
| 9 | 118 | | PlaceCardComponentModel placeCardModel = new PlaceCardComponentModel(); |
| 9 | 119 | | placeCardModel.placePictureUri = placeFromAPI.thumbnail; |
| 9 | 120 | | placeCardModel.placeName = placeFromAPI.name; |
| 9 | 121 | | placeCardModel.placeDescription = FormatDescription(placeFromAPI); |
| 9 | 122 | | placeCardModel.placeAuthor = FormatAuthorName(placeFromAPI); |
| 9 | 123 | | placeCardModel.numberOfUsers = placeFromAPI.usersTotalCount; |
| 9 | 124 | | placeCardModel.parcels = placeFromAPI.parcels; |
| 9 | 125 | | placeCardModel.coords = placeFromAPI.baseCoords; |
| 9 | 126 | | placeCardModel.hotSceneInfo = placeFromAPI; |
| | 127 | |
|
| 9 | 128 | | return placeCardModel; |
| | 129 | | } |
| | 130 | |
|
| 10 | 131 | | internal static string FormatDescription(HotSceneInfo placeFromAPI) { return string.IsNullOrEmpty(placeFromAPI.descr |
| | 132 | |
|
| 10 | 133 | | 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 | | { |
| 2 | 141 | | HotScenesController.HotSceneInfo.Realm realm = new HotScenesController.HotSceneInfo.Realm() { layer = null, serv |
| 4 | 142 | | placeFromAPI.realms = placeFromAPI.realms.OrderByDescending(x => x.usersCount).ToArray(); |
| | 143 | |
|
| 4 | 144 | | for (int i = 0; i < placeFromAPI.realms.Length; i++) |
| | 145 | | { |
| 2 | 146 | | bool isArchipelagoRealm = string.IsNullOrEmpty(placeFromAPI.realms[i].layer); |
| | 147 | |
|
| 2 | 148 | | if (isArchipelagoRealm || placeFromAPI.realms[i].usersCount < placeFromAPI.realms[i].maxUsers) |
| | 149 | | { |
| 2 | 150 | | realm = placeFromAPI.realms[i]; |
| 2 | 151 | | break; |
| | 152 | | } |
| | 153 | | } |
| | 154 | |
|
| 2 | 155 | | if (string.IsNullOrEmpty(realm.serverName)) |
| 0 | 156 | | WebInterface.GoTo(placeFromAPI.baseCoords.x, placeFromAPI.baseCoords.y); |
| | 157 | | else |
| 2 | 158 | | WebInterface.JumpIn(placeFromAPI.baseCoords.x, placeFromAPI.baseCoords.y, realm.serverName, realm.layer); |
| 2 | 159 | | } |
| | 160 | | } |