| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Linq; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Interface; |
| | 7 | | using UnityEngine; |
| | 8 | | using Variables.RealmsInfo; |
| | 9 | | using Environment = DCL.Environment; |
| | 10 | | using Object = UnityEngine.Object; |
| | 11 | |
|
| | 12 | | public class BuilderProjectsPanelController : IHUD |
| | 13 | | { |
| | 14 | | private const string TESTING_ETH_ADDRESS = "0xDc13378daFca7Fe2306368A16BCFac38c80BfCAD"; |
| | 15 | | private const string TESTING_TLD = "org"; |
| | 16 | | private const string VIEW_PREFAB_PATH = "BuilderProjectsPanel"; |
| | 17 | |
|
| | 18 | | private const float CACHE_TIME_LAND = 5 * 60; |
| | 19 | | private const float CACHE_TIME_SCENES = 1 * 60; |
| | 20 | | private const float REFRESH_INTERVAL = 2 * 60; |
| | 21 | |
|
| | 22 | | internal readonly IBuilderProjectsPanelView view; |
| | 23 | |
|
| | 24 | | private ISectionsController sectionsController; |
| | 25 | | private IScenesViewController scenesViewController; |
| | 26 | | private ILandController landsController; |
| | 27 | | private UnpublishPopupController unpublishPopupController; |
| | 28 | |
|
| | 29 | | private SectionsHandler sectionsHandler; |
| | 30 | | private SceneContextMenuHandler sceneContextMenuHandler; |
| | 31 | | private LeftMenuHandler leftMenuHandler; |
| | 32 | | private LeftMenuSettingsViewHandler leftMenuSettingsViewHandler; |
| | 33 | |
|
| | 34 | | private ITheGraph theGraph; |
| | 35 | | private ICatalyst catalyst; |
| | 36 | |
|
| | 37 | | private bool isInitialized = false; |
| | 38 | | internal bool isFetching = false; |
| | 39 | | private bool sendPlayerOpenPanelEvent = false; |
| | 40 | | private Coroutine fetchDataInterval; |
| | 41 | | private Promise<LandWithAccess[]> fetchLandPromise = null; |
| | 42 | |
|
| | 43 | | public event Action OnJumpInOrEdit; |
| | 44 | |
|
| 16 | 45 | | public BuilderProjectsPanelController() : this( |
| | 46 | | Object.Instantiate(Resources.Load<BuilderProjectsPanelView>(VIEW_PREFAB_PATH))) |
| | 47 | | { |
| 16 | 48 | | } |
| | 49 | |
|
| 16 | 50 | | internal BuilderProjectsPanelController(IBuilderProjectsPanelView view) |
| | 51 | | { |
| 16 | 52 | | this.view = view; |
| 16 | 53 | | view.OnClosePressed += OnClose; |
| 16 | 54 | | } |
| | 55 | |
|
| | 56 | | public void Dispose() |
| | 57 | | { |
| 16 | 58 | | StopFetchInterval(); |
| | 59 | |
|
| 16 | 60 | | DataStore.i.HUDs.builderProjectsPanelVisible.OnChange -= OnVisibilityChanged; |
| 16 | 61 | | DataStore.i.builderInWorld.unpublishSceneResult.OnChange -= OnSceneUnpublished; |
| 16 | 62 | | view.OnClosePressed -= OnClose; |
| | 63 | |
|
| 16 | 64 | | unpublishPopupController?.Dispose(); |
| | 65 | |
|
| 16 | 66 | | fetchLandPromise?.Dispose(); |
| | 67 | |
|
| 16 | 68 | | leftMenuSettingsViewHandler?.Dispose(); |
| 16 | 69 | | sectionsHandler?.Dispose(); |
| 16 | 70 | | sceneContextMenuHandler?.Dispose(); |
| 16 | 71 | | leftMenuHandler?.Dispose(); |
| | 72 | |
|
| 16 | 73 | | sectionsController?.Dispose(); |
| 16 | 74 | | scenesViewController?.Dispose(); |
| | 75 | |
|
| 16 | 76 | | view.Dispose(); |
| 16 | 77 | | } |
| | 78 | |
|
| | 79 | | public void Initialize() |
| | 80 | | { |
| 1 | 81 | | Initialize(new SectionsController(view.GetSectionContainer()), |
| | 82 | | new ScenesViewController(view.GetCardViewPrefab(), view.GetTransform()), |
| | 83 | | new LandController(), |
| | 84 | | Environment.i.platform.serviceProviders.theGraph, |
| | 85 | | Environment.i.platform.serviceProviders.catalyst); |
| 1 | 86 | | } |
| | 87 | |
|
| | 88 | | internal void Initialize(ISectionsController sectionsController, |
| | 89 | | IScenesViewController scenesViewController, ILandController landController, ITheGraph theGraph, ICatalyst cataly |
| | 90 | | { |
| 16 | 91 | | if (isInitialized) |
| 0 | 92 | | return; |
| | 93 | |
|
| 16 | 94 | | isInitialized = true; |
| | 95 | |
|
| 16 | 96 | | this.sectionsController = sectionsController; |
| 16 | 97 | | this.scenesViewController = scenesViewController; |
| 16 | 98 | | this.landsController = landController; |
| | 99 | |
|
| 16 | 100 | | this.theGraph = theGraph; |
| 16 | 101 | | this.catalyst = catalyst; |
| | 102 | |
|
| 16 | 103 | | this.unpublishPopupController = new UnpublishPopupController(view.GetUnpublishPopup()); |
| | 104 | |
|
| | 105 | | // set listeners for sections, setup searchbar for section, handle request for opening a new section |
| 16 | 106 | | sectionsHandler = new SectionsHandler(sectionsController, scenesViewController, landsController, view.GetSearchB |
| | 107 | | // handle if main panel or settings panel should be shown in current section |
| 16 | 108 | | leftMenuHandler = new LeftMenuHandler(view, sectionsController); |
| | 109 | | // handle project scene info on the left menu panel |
| 16 | 110 | | leftMenuSettingsViewHandler = new LeftMenuSettingsViewHandler(view.GetSettingsViewReferences(), scenesViewContro |
| | 111 | | // handle scene's context menu options |
| 16 | 112 | | sceneContextMenuHandler = new SceneContextMenuHandler(view.GetSceneCardViewContextMenu(), sectionsController, sc |
| | 113 | |
|
| 16 | 114 | | SetView(); |
| | 115 | |
|
| 16 | 116 | | sectionsController.OnRequestOpenUrl += OpenUrl; |
| 16 | 117 | | sectionsController.OnRequestGoToCoords += GoToCoords; |
| 16 | 118 | | sectionsController.OnRequestEditSceneAtCoords += OnGoToEditScene; |
| 16 | 119 | | scenesViewController.OnJumpInPressed += GoToCoords; |
| 16 | 120 | | scenesViewController.OnRequestOpenUrl += OpenUrl; |
| 16 | 121 | | scenesViewController.OnEditorPressed += OnGoToEditScene; |
| | 122 | |
|
| 16 | 123 | | DataStore.i.HUDs.builderProjectsPanelVisible.OnChange += OnVisibilityChanged; |
| 16 | 124 | | DataStore.i.builderInWorld.unpublishSceneResult.OnChange += OnSceneUnpublished; |
| 16 | 125 | | } |
| | 126 | |
|
| 16 | 127 | | public void SetVisibility(bool visible) { DataStore.i.HUDs.builderProjectsPanelVisible.Set(visible); } |
| | 128 | |
|
| | 129 | | private void OnVisibilityChanged(bool isVisible, bool prev) |
| | 130 | | { |
| 8 | 131 | | if (isVisible == prev) |
| 0 | 132 | | return; |
| | 133 | |
|
| 8 | 134 | | view.SetVisible(isVisible); |
| | 135 | |
|
| 8 | 136 | | if (isVisible) |
| | 137 | | { |
| 5 | 138 | | if (DataStore.i.builderInWorld.landsWithAccess.Get() != null) |
| 4 | 139 | | PanelOpenEvent(DataStore.i.builderInWorld.landsWithAccess.Get()); |
| | 140 | | else |
| 1 | 141 | | sendPlayerOpenPanelEvent = true; |
| | 142 | |
|
| 5 | 143 | | FetchLandsAndScenes(); |
| 5 | 144 | | StartFetchInterval(); |
| 5 | 145 | | sectionsController.OpenSection(SectionId.SCENES_DEPLOYED); |
| 5 | 146 | | } |
| | 147 | | else |
| | 148 | | { |
| 3 | 149 | | StopFetchInterval(); |
| | 150 | | } |
| 3 | 151 | | } |
| | 152 | |
|
| | 153 | | private void OnClose() |
| | 154 | | { |
| 1 | 155 | | SetVisibility(false); |
| | 156 | |
|
| 1 | 157 | | LandWithAccess[] lands = landsController.GetLands(); |
| 1 | 158 | | if (lands != null) |
| | 159 | | { |
| 1 | 160 | | Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands); |
| 1 | 161 | | BIWAnalytics.PlayerClosesPanel(totalLands.x, totalLands.y); |
| | 162 | | } |
| 1 | 163 | | } |
| | 164 | |
|
| | 165 | | private void PanelOpenEvent(LandWithAccess[] lands) |
| | 166 | | { |
| 4 | 167 | | Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands); |
| 4 | 168 | | BIWAnalytics.PlayerOpenPanel(totalLands.x, totalLands.y); |
| 4 | 169 | | } |
| | 170 | |
|
| | 171 | | /// <summary> |
| | 172 | | /// This counts the amount of lands that the user own and the amount of lands that the user operate |
| | 173 | | /// </summary> |
| | 174 | | /// <param name="lands"></param> |
| | 175 | | /// <returns>Vector2: X = amount of owned lands, Y = amount of operator lands</returns> |
| | 176 | | private Vector2Int GetAmountOfLandsOwnedAndOperator(LandWithAccess[] lands) |
| | 177 | | { |
| 5 | 178 | | int ownedLandsCount = 0; |
| 5 | 179 | | int operatorLandsCount = 0; |
| 18 | 180 | | foreach (var land in lands) |
| | 181 | | { |
| 4 | 182 | | if (land.role == LandRole.OWNER) |
| 4 | 183 | | ownedLandsCount++; |
| | 184 | | else |
| 0 | 185 | | operatorLandsCount++; |
| | 186 | | } |
| | 187 | |
|
| 5 | 188 | | return new Vector2Int(ownedLandsCount, operatorLandsCount); |
| | 189 | | } |
| | 190 | |
|
| | 191 | | private void SetView() |
| | 192 | | { |
| 16 | 193 | | scenesViewController.AddListener((IDeployedSceneListener) view); |
| 16 | 194 | | scenesViewController.AddListener((IProjectSceneListener) view); |
| 16 | 195 | | } |
| | 196 | |
|
| | 197 | | private void FetchLandsAndScenes(float landCacheTime = CACHE_TIME_LAND, float scenesCacheTime = CACHE_TIME_SCENES) |
| | 198 | | { |
| 5 | 199 | | if (isFetching) |
| 0 | 200 | | return; |
| | 201 | |
|
| 5 | 202 | | isFetching = true; |
| | 203 | |
|
| 5 | 204 | | var address = UserProfile.GetOwnUserProfile().ethAddress; |
| 5 | 205 | | var network = KernelConfig.i.Get().network; |
| | 206 | |
|
| | 207 | | #if UNITY_EDITOR |
| | 208 | | // NOTE: to be able to test in editor without getting a profile we hardcode an address here |
| 5 | 209 | | if (string.IsNullOrEmpty(address)) |
| | 210 | | { |
| 0 | 211 | | address = TESTING_ETH_ADDRESS; |
| 0 | 212 | | network = TESTING_TLD; |
| 0 | 213 | | DataStore.i.playerRealm.Set(new CurrentRealmModel() |
| | 214 | | { |
| | 215 | | domain = $"https://peer-lb.decentraland.{TESTING_TLD}", |
| | 216 | | contentServerUrl = $"https://peer-lb.decentraland.{TESTING_TLD}/content", |
| | 217 | | }); |
| | 218 | | } |
| | 219 | | #endif |
| | 220 | |
|
| 5 | 221 | | sectionsController.SetFetchingDataStart(); |
| | 222 | |
|
| 5 | 223 | | fetchLandPromise = DeployedScenesFetcher.FetchLandsFromOwner(catalyst, theGraph, address, network, landCacheTime |
| 5 | 224 | | fetchLandPromise |
| | 225 | | .Then(LandsFetched) |
| | 226 | | .Catch(LandsFetchedError); |
| 5 | 227 | | } |
| | 228 | |
|
| | 229 | | internal void LandsFetchedError(string error) |
| | 230 | | { |
| 1 | 231 | | isFetching = false; |
| 1 | 232 | | sectionsController.SetFetchingDataEnd(); |
| 1 | 233 | | landsController.SetLands(new LandWithAccess[] { }); |
| 1 | 234 | | scenesViewController.SetScenes(new ISceneData[] { }); |
| 1 | 235 | | Debug.LogError(error); |
| 1 | 236 | | } |
| | 237 | |
|
| | 238 | | internal void LandsFetched(LandWithAccess[] lands) |
| | 239 | | { |
| 1 | 240 | | DataStore.i.builderInWorld.landsWithAccess.Set(lands.ToArray(), true); |
| 1 | 241 | | sectionsController.SetFetchingDataEnd(); |
| 1 | 242 | | isFetching = false; |
| | 243 | |
|
| | 244 | | try |
| | 245 | | { |
| 2 | 246 | | var scenes = lands.Where(land => land.scenes != null && land.scenes.Count > 0) |
| 2 | 247 | | .Select(land => land.scenes.Where(scene => !scene.isEmpty).Select(scene => (ISceneData)new SceneData(sce |
| 0 | 248 | | .Aggregate((i, j) => i.Concat(j)) |
| | 249 | | .ToArray(); |
| | 250 | |
|
| 0 | 251 | | if (sendPlayerOpenPanelEvent) PanelOpenEvent(lands); |
| 0 | 252 | | landsController.SetLands(lands); |
| 0 | 253 | | scenesViewController.SetScenes(scenes); |
| 0 | 254 | | } |
| 1 | 255 | | catch (Exception e) |
| | 256 | | { |
| 1 | 257 | | landsController.SetLands(lands); |
| 1 | 258 | | scenesViewController.SetScenes(new ISceneData[] { }); |
| 1 | 259 | | } |
| 1 | 260 | | } |
| | 261 | |
|
| | 262 | | internal void GoToCoords(Vector2Int coords) |
| | 263 | | { |
| 1 | 264 | | WebInterface.GoTo(coords.x, coords.y); |
| 1 | 265 | | SetVisibility(false); |
| 1 | 266 | | OnJumpInOrEdit?.Invoke(); |
| 1 | 267 | | } |
| | 268 | |
|
| 0 | 269 | | private void OpenUrl(string url) { WebInterface.OpenURL(url); } |
| | 270 | |
|
| | 271 | | internal void OnGoToEditScene(Vector2Int coords) |
| | 272 | | { |
| 1 | 273 | | bool isGoingToTeleport = BIWTeleportAndEdit.TeleportAndEdit(coords); |
| 1 | 274 | | if (isGoingToTeleport) |
| | 275 | | { |
| 1 | 276 | | SetVisibility(false); |
| | 277 | | } |
| | 278 | |
|
| 1 | 279 | | OnJumpInOrEdit?.Invoke(); |
| 1 | 280 | | } |
| | 281 | |
|
| | 282 | | private void StartFetchInterval() |
| | 283 | | { |
| 5 | 284 | | if (fetchDataInterval != null) |
| | 285 | | { |
| 0 | 286 | | StopFetchInterval(); |
| | 287 | | } |
| | 288 | |
|
| 5 | 289 | | fetchDataInterval = CoroutineStarter.Start(RefreshDataInterval()); |
| 5 | 290 | | } |
| | 291 | |
|
| | 292 | | private void StopFetchInterval() |
| | 293 | | { |
| 19 | 294 | | CoroutineStarter.Stop(fetchDataInterval); |
| 19 | 295 | | fetchDataInterval = null; |
| 19 | 296 | | } |
| | 297 | |
|
| | 298 | | IEnumerator RefreshDataInterval() |
| | 299 | | { |
| 0 | 300 | | while (true) |
| | 301 | | { |
| 5 | 302 | | yield return WaitForSecondsCache.Get(REFRESH_INTERVAL); |
| 0 | 303 | | FetchLandsAndScenes(); |
| | 304 | | } |
| | 305 | | } |
| | 306 | |
|
| | 307 | | private void OnSceneUnpublished(PublishSceneResultPayload current, PublishSceneResultPayload previous) |
| | 308 | | { |
| 0 | 309 | | if (current.ok) |
| | 310 | | { |
| 0 | 311 | | FetchLandsAndScenes(CACHE_TIME_LAND, 0); |
| | 312 | | } |
| 0 | 313 | | } |
| | 314 | | } |