| | 1 | | using DCL; |
| | 2 | | using DCL.Interface; |
| | 3 | | using ExploreV2Analytics; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | | using static HotScenesController; |
| | 9 | |
|
| | 10 | | public interface IPlacesSubSectionComponentController : IDisposable |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// It will be triggered when the sub-section want to request to close the ExploreV2 main menu. |
| | 14 | | /// </summary> |
| | 15 | | event Action OnCloseExploreV2; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// It will be triggered when any action is executed inside the places sub-section. |
| | 19 | | /// </summary> |
| | 20 | | event Action OnAnyActionExecuted; |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Request all places from the API. |
| | 24 | | /// </summary> |
| | 25 | | void RequestAllPlaces(); |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Load the places with the last requested ones. |
| | 29 | | /// </summary> |
| | 30 | | void LoadPlaces(); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Increment the number of places loaded. |
| | 34 | | /// </summary> |
| | 35 | | void ShowMorePlaces(); |
| | 36 | | } |
| | 37 | |
|
| | 38 | | public class PlacesSubSectionComponentController : IPlacesSubSectionComponentController |
| | 39 | | { |
| | 40 | | public event Action OnCloseExploreV2; |
| | 41 | | public event Action OnAnyActionExecuted; |
| | 42 | | internal event Action OnPlacesFromAPIUpdated; |
| | 43 | |
|
| | 44 | | internal const int INITIAL_NUMBER_OF_ROWS = 4; |
| | 45 | | internal const int SHOW_MORE_ROWS_INCREMENT = 1; |
| | 46 | | internal const string NO_PLACE_DESCRIPTION_WRITTEN = "The author hasn't written a description yet."; |
| | 47 | | internal IPlacesSubSectionComponentView view; |
| | 48 | | internal IPlacesAPIController placesAPIApiController; |
| | 49 | | internal FriendTrackerController friendsTrackerController; |
| 17 | 50 | | internal List<HotSceneInfo> placesFromAPI = new List<HotSceneInfo>(); |
| | 51 | | internal int currentPlacesShowed = 0; |
| | 52 | | internal bool reloadPlaces = false; |
| | 53 | | internal IExploreV2Analytics exploreV2Analytics; |
| | 54 | |
|
| 17 | 55 | | public PlacesSubSectionComponentController(IPlacesSubSectionComponentView view, IPlacesAPIController placesAPI, IFri |
| | 56 | | { |
| 17 | 57 | | this.view = view; |
| 17 | 58 | | this.view.OnReady += FirstLoading; |
| 17 | 59 | | this.view.OnInfoClicked += ShowPlaceDetailedInfo; |
| 17 | 60 | | this.view.OnJumpInClicked += JumpInToPlace; |
| 17 | 61 | | this.view.OnFriendHandlerAdded += View_OnFriendHandlerAdded; |
| 17 | 62 | | this.view.OnShowMorePlacesClicked += ShowMorePlaces; |
| | 63 | |
|
| 17 | 64 | | placesAPIApiController = placesAPI; |
| 17 | 65 | | OnPlacesFromAPIUpdated += OnRequestedPlacesUpdated; |
| | 66 | |
|
| 17 | 67 | | friendsTrackerController = new FriendTrackerController(friendsController, view.currentFriendColors); |
| | 68 | |
|
| 17 | 69 | | this.exploreV2Analytics = exploreV2Analytics; |
| 17 | 70 | | } |
| | 71 | |
|
| | 72 | | internal void FirstLoading() |
| | 73 | | { |
| 1 | 74 | | reloadPlaces = true; |
| 1 | 75 | | RequestAllPlaces(); |
| | 76 | |
|
| 1 | 77 | | view.OnPlacesSubSectionEnable += RequestAllPlaces; |
| 1 | 78 | | DataStore.i.exploreV2.isOpen.OnChange += OnExploreV2Open; |
| 1 | 79 | | } |
| | 80 | |
|
| | 81 | | internal void OnExploreV2Open(bool current, bool previous) |
| | 82 | | { |
| 0 | 83 | | if (current) |
| 0 | 84 | | return; |
| | 85 | |
|
| 0 | 86 | | reloadPlaces = true; |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | public void RequestAllPlaces() |
| | 90 | | { |
| 2 | 91 | | if (!reloadPlaces) |
| 0 | 92 | | return; |
| | 93 | |
|
| 2 | 94 | | currentPlacesShowed = view.currentPlacesPerRow * INITIAL_NUMBER_OF_ROWS; |
| 2 | 95 | | view.RestartScrollViewPosition(); |
| 2 | 96 | | view.SetPlacesAsLoading(true); |
| 2 | 97 | | view.SetShowMorePlacesButtonActive(false); |
| 2 | 98 | | RequestAllPlacesFromAPI(); |
| 2 | 99 | | reloadPlaces = false; |
| 2 | 100 | | } |
| | 101 | |
|
| | 102 | | internal void RequestAllPlacesFromAPI() |
| | 103 | | { |
| 3 | 104 | | placesAPIApiController.GetAllPlaces( |
| | 105 | | (placeList) => |
| | 106 | | { |
| 0 | 107 | | placesFromAPI = placeList; |
| 0 | 108 | | OnPlacesFromAPIUpdated?.Invoke(); |
| 0 | 109 | | }); |
| 3 | 110 | | } |
| | 111 | |
|
| 2 | 112 | | internal void OnRequestedPlacesUpdated() { LoadPlaces(); } |
| | 113 | |
|
| | 114 | | public void LoadPlaces() |
| | 115 | | { |
| 2 | 116 | | friendsTrackerController.RemoveAllHandlers(); |
| | 117 | |
|
| 2 | 118 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| 2 | 119 | | List<HotSceneInfo> placesFiltered = placesFromAPI.Take(currentPlacesShowed).ToList(); |
| 4 | 120 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 121 | | { |
| 0 | 122 | | PlaceCardComponentModel placeCardModel = CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 0 | 123 | | places.Add(placeCardModel); |
| | 124 | | } |
| | 125 | |
|
| 2 | 126 | | view.SetPlaces(places); |
| 2 | 127 | | view.SetShowMorePlacesButtonActive(currentPlacesShowed < placesFromAPI.Count); |
| 2 | 128 | | view.SetPlacesAsLoading(false); |
| 2 | 129 | | } |
| | 130 | |
|
| | 131 | | public void ShowMorePlaces() |
| | 132 | | { |
| 2 | 133 | | List<PlaceCardComponentModel> places = new List<PlaceCardComponentModel>(); |
| 2 | 134 | | List<HotSceneInfo> placesFiltered = new List<HotSceneInfo>(); |
| 2 | 135 | | int numberOfExtraItemsToAdd = ((int)Mathf.Ceil((float)currentPlacesShowed / view.currentPlacesPerRow) * view.cur |
| 2 | 136 | | int numberOfItemsToAdd = view.currentPlacesPerRow * SHOW_MORE_ROWS_INCREMENT + numberOfExtraItemsToAdd; |
| | 137 | |
|
| 2 | 138 | | if (currentPlacesShowed + numberOfItemsToAdd <= placesFromAPI.Count) |
| 2 | 139 | | placesFiltered = placesFromAPI.GetRange(currentPlacesShowed, numberOfItemsToAdd); |
| | 140 | | else |
| 0 | 141 | | placesFiltered = placesFromAPI.GetRange(currentPlacesShowed, placesFromAPI.Count - currentPlacesShowed); |
| | 142 | |
|
| 4 | 143 | | foreach (HotSceneInfo receivedPlace in placesFiltered) |
| | 144 | | { |
| 0 | 145 | | PlaceCardComponentModel placeCardModel = CreatePlaceCardModelFromAPIPlace(receivedPlace); |
| 0 | 146 | | places.Add(placeCardModel); |
| | 147 | | } |
| | 148 | |
|
| 2 | 149 | | view.AddPlaces(places); |
| | 150 | |
|
| 2 | 151 | | currentPlacesShowed += numberOfItemsToAdd; |
| 2 | 152 | | if (currentPlacesShowed > placesFromAPI.Count) |
| 0 | 153 | | currentPlacesShowed = placesFromAPI.Count; |
| | 154 | |
|
| 2 | 155 | | view.SetShowMorePlacesButtonActive(currentPlacesShowed < placesFromAPI.Count); |
| | 156 | |
|
| 2 | 157 | | OnAnyActionExecuted?.Invoke(); |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | public void Dispose() |
| | 161 | | { |
| 17 | 162 | | view.OnReady -= FirstLoading; |
| 17 | 163 | | view.OnInfoClicked -= ShowPlaceDetailedInfo; |
| 17 | 164 | | view.OnJumpInClicked -= JumpInToPlace; |
| 17 | 165 | | view.OnPlacesSubSectionEnable -= RequestAllPlaces; |
| 17 | 166 | | view.OnFriendHandlerAdded -= View_OnFriendHandlerAdded; |
| 17 | 167 | | view.OnShowMorePlacesClicked -= ShowMorePlaces; |
| 17 | 168 | | OnPlacesFromAPIUpdated -= OnRequestedPlacesUpdated; |
| 17 | 169 | | DataStore.i.exploreV2.isOpen.OnChange -= OnExploreV2Open; |
| 17 | 170 | | } |
| | 171 | |
|
| | 172 | | internal PlaceCardComponentModel CreatePlaceCardModelFromAPIPlace(HotSceneInfo placeFromAPI) |
| | 173 | | { |
| 1 | 174 | | PlaceCardComponentModel placeCardModel = new PlaceCardComponentModel(); |
| 1 | 175 | | placeCardModel.placePictureUri = placeFromAPI.thumbnail; |
| 1 | 176 | | placeCardModel.placeName = placeFromAPI.name; |
| 1 | 177 | | placeCardModel.placeDescription = FormatDescription(placeFromAPI); |
| 1 | 178 | | placeCardModel.placeAuthor = FormatAuthorName(placeFromAPI); |
| 1 | 179 | | placeCardModel.numberOfUsers = placeFromAPI.usersTotalCount; |
| 1 | 180 | | placeCardModel.parcels = placeFromAPI.parcels; |
| 1 | 181 | | placeCardModel.coords = placeFromAPI.baseCoords; |
| 1 | 182 | | placeCardModel.hotSceneInfo = placeFromAPI; |
| | 183 | |
|
| 1 | 184 | | return placeCardModel; |
| | 185 | | } |
| | 186 | |
|
| 2 | 187 | | internal string FormatDescription(HotSceneInfo placeFromAPI) { return string.IsNullOrEmpty(placeFromAPI.description) |
| | 188 | |
|
| 2 | 189 | | internal string FormatAuthorName(HotSceneInfo placeFromAPI) { return $"Author <b>{placeFromAPI.creator}</b>"; } |
| | 190 | |
|
| | 191 | | internal void ShowPlaceDetailedInfo(PlaceCardComponentModel placeModel) |
| | 192 | | { |
| 1 | 193 | | view.ShowPlaceModal(placeModel); |
| 1 | 194 | | exploreV2Analytics.SendClickOnPlaceInfo(placeModel.hotSceneInfo.id, placeModel.placeName); |
| 1 | 195 | | OnAnyActionExecuted?.Invoke(); |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | internal void JumpInToPlace(HotSceneInfo placeFromAPI) |
| | 199 | | { |
| 1 | 200 | | HotScenesController.HotSceneInfo.Realm realm = new HotScenesController.HotSceneInfo.Realm() { layer = null, serv |
| 2 | 201 | | placeFromAPI.realms = placeFromAPI.realms.OrderByDescending(x => x.usersCount).ToArray(); |
| | 202 | |
|
| 2 | 203 | | for (int i = 0; i < placeFromAPI.realms.Length; i++) |
| | 204 | | { |
| 1 | 205 | | bool isArchipelagoRealm = string.IsNullOrEmpty(placeFromAPI.realms[i].layer); |
| | 206 | |
|
| 1 | 207 | | if (isArchipelagoRealm || placeFromAPI.realms[i].usersCount < placeFromAPI.realms[i].maxUsers) |
| | 208 | | { |
| 1 | 209 | | realm = placeFromAPI.realms[i]; |
| 1 | 210 | | break; |
| | 211 | | } |
| | 212 | | } |
| | 213 | |
|
| 1 | 214 | | if (string.IsNullOrEmpty(realm.serverName)) |
| 0 | 215 | | WebInterface.GoTo(placeFromAPI.baseCoords.x, placeFromAPI.baseCoords.y); |
| | 216 | | else |
| 1 | 217 | | WebInterface.JumpIn(placeFromAPI.baseCoords.x, placeFromAPI.baseCoords.y, realm.serverName, realm.layer); |
| | 218 | |
|
| 1 | 219 | | view.HidePlaceModal(); |
| 1 | 220 | | OnCloseExploreV2?.Invoke(); |
| 1 | 221 | | OnAnyActionExecuted?.Invoke(); |
| | 222 | |
|
| 1 | 223 | | exploreV2Analytics.SendPlaceTeleport(placeFromAPI.id, placeFromAPI.name, placeFromAPI.baseCoords); |
| 1 | 224 | | } |
| | 225 | |
|
| 0 | 226 | | internal void View_OnFriendHandlerAdded(FriendsHandler friendsHandler) { friendsTrackerController.AddHandler(friends |
| | 227 | | } |