| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using DCL; |
| | 6 | | using DCL.Builder; |
| | 7 | | using DCL.Builder.Manifest; |
| | 8 | | using DCL.Helpers; |
| | 9 | | using DCL.Interface; |
| | 10 | | using UnityEngine; |
| | 11 | | using Variables.RealmsInfo; |
| | 12 | | using Environment = DCL.Environment; |
| | 13 | | using Object = UnityEngine.Object; |
| | 14 | |
|
| | 15 | | public class BuilderMainPanelController : IHUD, IBuilderMainPanelController |
| | 16 | | { |
| | 17 | | private const string CREATING_PROJECT_ERROR = "Error creating a new project: "; |
| | 18 | | private const string OBTAIN_PROJECT_ERROR = "Error obtaining the project: "; |
| | 19 | | private const string TESTING_ETH_ADDRESS = "0xDc13378daFca7Fe2306368A16BCFac38c80BfCAD"; |
| | 20 | | private const string TESTING_TLD = "org"; |
| | 21 | | private const string VIEW_PREFAB_PATH = "BuilderProjectsPanel"; |
| | 22 | | private const string VIEW_PREFAB_PATH_DEV = "BuilderProjectsPanelDev"; |
| | 23 | |
|
| | 24 | | private const float CACHE_TIME_LAND = 5 * 60; |
| | 25 | | private const float CACHE_TIME_SCENES = 1 * 60; |
| | 26 | | private const float REFRESH_INTERVAL = 2 * 60; |
| | 27 | |
|
| | 28 | | internal IBuilderMainPanelView view; |
| | 29 | |
|
| | 30 | | private ISectionsController sectionsController; |
| | 31 | | private IProjectsController projectsController; |
| | 32 | | private IScenesViewController scenesViewController; |
| | 33 | | private ILandsController landsesController; |
| | 34 | | private UnpublishPopupController unpublishPopupController; |
| | 35 | |
|
| | 36 | | private INewProjectFlowController newProjectFlowController; |
| | 37 | |
|
| | 38 | | private SectionsHandler sectionsHandler; |
| | 39 | | private SceneContextMenuHandler sceneContextMenuHandler; |
| | 40 | | private LeftMenuHandler leftMenuHandler; |
| | 41 | | private LeftMenuSettingsViewHandler leftMenuSettingsViewHandler; |
| | 42 | |
|
| | 43 | | private ITheGraph theGraph; |
| | 44 | | private ICatalyst catalyst; |
| | 45 | |
|
| | 46 | | private bool isInitialized = false; |
| | 47 | | internal bool isFetchingLands = false; |
| | 48 | | internal bool isFetchingProjects = false; |
| | 49 | | private bool sendPlayerOpenPanelEvent = false; |
| | 50 | | private Coroutine fetchDataInterval; |
| | 51 | | private Promise<LandWithAccess[]> fetchLandPromise = null; |
| | 52 | | private Promise<ProjectData[]> fetchProjectsPromise = null; |
| | 53 | |
|
| | 54 | | public event Action OnJumpInOrEdit; |
| | 55 | |
|
| | 56 | | internal IContext context; |
| | 57 | |
|
| 17 | 58 | | public BuilderMainPanelController() |
| | 59 | | { |
| 17 | 60 | | if (DataStore.i.builderInWorld.isDevBuild.Get()) |
| 0 | 61 | | SetView(Object.Instantiate(Resources.Load<BuilderMainPanelView>(VIEW_PREFAB_PATH_DEV))); |
| | 62 | | else |
| 17 | 63 | | SetView(Object.Instantiate(Resources.Load<BuilderMainPanelView>(VIEW_PREFAB_PATH))); |
| 17 | 64 | | } |
| | 65 | |
|
| | 66 | | internal void SetView(IBuilderMainPanelView view) |
| | 67 | | { |
| 17 | 68 | | this.view = view; |
| 17 | 69 | | view.OnClosePressed += OnClose; |
| 17 | 70 | | view.OnBackPressed += OnBack; |
| 17 | 71 | | } |
| | 72 | |
|
| | 73 | | private void OnBack() |
| | 74 | | { |
| 0 | 75 | | if (newProjectFlowController.IsActive()) |
| 0 | 76 | | newProjectFlowController.Hide(); |
| | 77 | | else |
| 0 | 78 | | OnClose(); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public void Dispose() |
| | 82 | | { |
| 17 | 83 | | StopFetchInterval(); |
| | 84 | |
|
| 17 | 85 | | sectionsController.OnRequestOpenUrl -= OpenUrl; |
| 17 | 86 | | sectionsController.OnRequestGoToCoords -= GoToCoords; |
| 17 | 87 | | sectionsController.OnRequestEditSceneAtCoords -= OnGoToEditScene; |
| 17 | 88 | | sectionsController.OnCreateProjectRequest -= newProjectFlowController.NewProject; |
| | 89 | |
|
| 17 | 90 | | scenesViewController.OnJumpInPressed -= GoToCoords; |
| 17 | 91 | | scenesViewController.OnRequestOpenUrl -= OpenUrl; |
| 17 | 92 | | scenesViewController.OnEditorPressed -= OnGoToEditScene; |
| 17 | 93 | | projectsController.OnEditorPressed -= GetManifestToEdit; |
| | 94 | |
|
| 17 | 95 | | newProjectFlowController.OnNewProjectCrated -= CreateNewProject; |
| | 96 | |
|
| 17 | 97 | | view.OnCreateProjectPressed -= newProjectFlowController.NewProject; |
| | 98 | |
|
| 17 | 99 | | DataStore.i.HUDs.builderProjectsPanelVisible.OnChange -= OnVisibilityChanged; |
| 17 | 100 | | DataStore.i.builderInWorld.unpublishSceneResult.OnChange -= OnSceneUnpublished; |
| 17 | 101 | | view.OnClosePressed -= OnClose; |
| 17 | 102 | | view.OnBackPressed -= OnBack; |
| | 103 | |
|
| 17 | 104 | | unpublishPopupController?.Dispose(); |
| | 105 | |
|
| 17 | 106 | | fetchLandPromise?.Dispose(); |
| 17 | 107 | | fetchProjectsPromise?.Dispose(); |
| | 108 | |
|
| 17 | 109 | | leftMenuSettingsViewHandler?.Dispose(); |
| 17 | 110 | | sectionsHandler?.Dispose(); |
| 17 | 111 | | sceneContextMenuHandler?.Dispose(); |
| 17 | 112 | | leftMenuHandler?.Dispose(); |
| | 113 | |
|
| 17 | 114 | | sectionsController?.Dispose(); |
| 17 | 115 | | scenesViewController?.Dispose(); |
| | 116 | |
|
| 17 | 117 | | newProjectFlowController?.Dispose(); |
| | 118 | |
|
| 17 | 119 | | view.Dispose(); |
| 17 | 120 | | } |
| | 121 | |
|
| | 122 | | public void Initialize(IContext context) |
| | 123 | | { |
| 0 | 124 | | this.context = context; |
| 0 | 125 | | Initialize(new SectionsController(view.GetSectionContainer()), |
| | 126 | | new ScenesViewController(view.GetSceneCardViewPrefab(), view.GetTransform()), |
| | 127 | | new LandsController(), |
| | 128 | | new ProjectsController(view.GetProjectCardView(), view.GetTransform()), |
| | 129 | | new NewProjectFlowController(), |
| | 130 | | Environment.i.platform.serviceProviders.theGraph, |
| | 131 | | Environment.i.platform.serviceProviders.catalyst); |
| 0 | 132 | | } |
| | 133 | |
|
| | 134 | | internal void Initialize(ISectionsController sectionsController, |
| | 135 | | IScenesViewController scenesViewController, ILandsController landsesController, IProjectsController projectsCont |
| | 136 | | { |
| 17 | 137 | | if (isInitialized) |
| 0 | 138 | | return; |
| | 139 | |
|
| 17 | 140 | | isInitialized = true; |
| | 141 | |
|
| 17 | 142 | | this.sectionsController = sectionsController; |
| 17 | 143 | | this.scenesViewController = scenesViewController; |
| 17 | 144 | | this.landsesController = landsesController; |
| 17 | 145 | | this.projectsController = projectsController; |
| | 146 | |
|
| 17 | 147 | | this.newProjectFlowController = newProjectFlowController; |
| | 148 | |
|
| 17 | 149 | | this.theGraph = theGraph; |
| 17 | 150 | | this.catalyst = catalyst; |
| | 151 | |
|
| 17 | 152 | | this.unpublishPopupController = new UnpublishPopupController(view.GetUnpublishPopup()); |
| | 153 | |
|
| | 154 | | // set listeners for sections, setup searchbar for section, handle request for opening a new section |
| 17 | 155 | | sectionsHandler = new SectionsHandler(sectionsController, scenesViewController, landsesController, projectsContr |
| | 156 | | // handle if main panel or settings panel should be shown in current section |
| 17 | 157 | | leftMenuHandler = new LeftMenuHandler(view, sectionsController); |
| | 158 | | // handle project scene info on the left menu panel |
| 17 | 159 | | leftMenuSettingsViewHandler = new LeftMenuSettingsViewHandler(view.GetSettingsViewReferences(), scenesViewContro |
| | 160 | | // handle scene's context menu options |
| 17 | 161 | | sceneContextMenuHandler = new SceneContextMenuHandler(view.GetSceneCardViewContextMenu(), sectionsController, sc |
| | 162 | |
|
| 17 | 163 | | SetView(); |
| | 164 | |
|
| 17 | 165 | | sectionsController.OnRequestOpenUrl += OpenUrl; |
| 17 | 166 | | sectionsController.OnRequestGoToCoords += GoToCoords; |
| 17 | 167 | | sectionsController.OnRequestEditSceneAtCoords += OnGoToEditScene; |
| 17 | 168 | | sectionsController.OnCreateProjectRequest += newProjectFlowController.NewProject; |
| | 169 | |
|
| 17 | 170 | | scenesViewController.OnJumpInPressed += GoToCoords; |
| 17 | 171 | | scenesViewController.OnRequestOpenUrl += OpenUrl; |
| 17 | 172 | | scenesViewController.OnEditorPressed += OnGoToEditScene; |
| 17 | 173 | | newProjectFlowController.OnNewProjectCrated += CreateNewProject; |
| | 174 | |
|
| 17 | 175 | | view.OnCreateProjectPressed += this.newProjectFlowController.NewProject; |
| 17 | 176 | | this.projectsController.OnEditorPressed += GetManifestToEdit; |
| | 177 | |
|
| 17 | 178 | | DataStore.i.HUDs.builderProjectsPanelVisible.OnChange += OnVisibilityChanged; |
| 17 | 179 | | DataStore.i.builderInWorld.unpublishSceneResult.OnChange += OnSceneUnpublished; |
| 17 | 180 | | } |
| | 181 | |
|
| | 182 | | private void GetManifestToEdit(ProjectData data) |
| | 183 | | { |
| 0 | 184 | | Promise<Manifest> manifestPromise = context.builderAPIController.GetManifestById(data.id); |
| 0 | 185 | | manifestPromise.Then( OpenEditorFromManifest); |
| | 186 | |
|
| 0 | 187 | | manifestPromise.Catch( errorString => |
| | 188 | | { |
| 0 | 189 | | BIWUtils.ShowGenericNotification(OBTAIN_PROJECT_ERROR + errorString); |
| 0 | 190 | | }); |
| 0 | 191 | | } |
| | 192 | |
|
| | 193 | | private void CreateNewProject(ProjectData project) |
| | 194 | | { |
| 0 | 195 | | Promise<Manifest> projectPromise = context.builderAPIController.CreateNewProject(project); |
| | 196 | |
|
| 0 | 197 | | projectPromise.Then( OpenEditorFromManifest); |
| | 198 | |
|
| 0 | 199 | | projectPromise.Catch( errorString => |
| | 200 | | { |
| 0 | 201 | | BIWUtils.ShowGenericNotification(CREATING_PROJECT_ERROR + errorString); |
| 0 | 202 | | }); |
| 0 | 203 | | } |
| | 204 | |
|
| 0 | 205 | | private void OpenEditorFromManifest(Manifest manifest) { context.sceneManager.StartEditorFromManifest(manifest); } |
| | 206 | |
|
| 14 | 207 | | public void SetVisibility(bool visible) { DataStore.i.HUDs.builderProjectsPanelVisible.Set(visible); } |
| | 208 | |
|
| | 209 | | private void OnVisibilityChanged(bool isVisible, bool prev) |
| | 210 | | { |
| 7 | 211 | | if (isVisible == prev) |
| 0 | 212 | | return; |
| | 213 | |
|
| 7 | 214 | | view.SetVisible(isVisible); |
| | 215 | |
|
| 7 | 216 | | if (isVisible) |
| | 217 | | { |
| 4 | 218 | | if (DataStore.i.builderInWorld.landsWithAccess.Get() != null) |
| 4 | 219 | | PanelOpenEvent(DataStore.i.builderInWorld.landsWithAccess.Get()); |
| | 220 | | else |
| 0 | 221 | | sendPlayerOpenPanelEvent = true; |
| | 222 | |
|
| 4 | 223 | | FetchPanelInfo(); |
| 4 | 224 | | StartFetchInterval(); |
| 4 | 225 | | if (DataStore.i.builderInWorld.isDevBuild.Get()) |
| 0 | 226 | | sectionsController.OpenSection(SectionId.PROJECTS); |
| | 227 | | else |
| 4 | 228 | | sectionsController.OpenSection(SectionId.SCENES); |
| 4 | 229 | | } |
| | 230 | | else |
| | 231 | | { |
| 3 | 232 | | StopFetchInterval(); |
| | 233 | | } |
| 3 | 234 | | } |
| | 235 | |
|
| | 236 | | private void OnClose() |
| | 237 | | { |
| 1 | 238 | | if (!view.IsVisible()) |
| 0 | 239 | | return; |
| | 240 | |
|
| 1 | 241 | | SetVisibility(false); |
| | 242 | |
|
| 1 | 243 | | LandWithAccess[] lands = landsesController.GetLands(); |
| 1 | 244 | | if (lands != null) |
| | 245 | | { |
| 1 | 246 | | Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands); |
| 1 | 247 | | BIWAnalytics.PlayerClosesPanel(totalLands.x, totalLands.y); |
| | 248 | | } |
| 1 | 249 | | } |
| | 250 | |
|
| | 251 | | private void PanelOpenEvent(LandWithAccess[] lands) |
| | 252 | | { |
| 4 | 253 | | Vector2Int totalLands = GetAmountOfLandsOwnedAndOperator(lands); |
| 4 | 254 | | BIWAnalytics.PlayerOpenPanel(totalLands.x, totalLands.y); |
| 4 | 255 | | } |
| | 256 | |
|
| | 257 | | /// <summary> |
| | 258 | | /// This counts the amount of lands that the user own and the amount of lands that the user operate |
| | 259 | | /// </summary> |
| | 260 | | /// <param name="lands"></param> |
| | 261 | | /// <returns>Vector2: X = amount of owned lands, Y = amount of operator lands</returns> |
| | 262 | | private Vector2Int GetAmountOfLandsOwnedAndOperator(LandWithAccess[] lands) |
| | 263 | | { |
| 5 | 264 | | int ownedLandsCount = 0; |
| 5 | 265 | | int operatorLandsCount = 0; |
| 18 | 266 | | foreach (var land in lands) |
| | 267 | | { |
| 4 | 268 | | if (land.role == LandRole.OWNER) |
| 4 | 269 | | ownedLandsCount++; |
| | 270 | | else |
| 0 | 271 | | operatorLandsCount++; |
| | 272 | | } |
| | 273 | |
|
| 5 | 274 | | return new Vector2Int(ownedLandsCount, operatorLandsCount); |
| | 275 | | } |
| | 276 | |
|
| | 277 | | private void SetView() |
| | 278 | | { |
| 17 | 279 | | scenesViewController.AddListener((ISceneListener) view); |
| 17 | 280 | | projectsController.AddListener((IProjectsListener) view); |
| 17 | 281 | | } |
| | 282 | |
|
| | 283 | | private void FetchPanelInfo(float landCacheTime = CACHE_TIME_LAND, float scenesCacheTime = CACHE_TIME_SCENES) |
| | 284 | | { |
| 4 | 285 | | if (isFetchingLands || isFetchingProjects) |
| 0 | 286 | | return; |
| | 287 | |
|
| 4 | 288 | | isFetchingLands = true; |
| 4 | 289 | | isFetchingProjects = true; |
| | 290 | |
|
| 4 | 291 | | var address = UserProfile.GetOwnUserProfile().ethAddress; |
| 4 | 292 | | var network = KernelConfig.i.Get().network; |
| | 293 | |
|
| | 294 | | #if UNITY_EDITOR |
| | 295 | | // NOTE: to be able to test in editor without getting a profile we hardcode an address here |
| 4 | 296 | | if (string.IsNullOrEmpty(address)) |
| | 297 | | { |
| 0 | 298 | | address = TESTING_ETH_ADDRESS; |
| 0 | 299 | | network = TESTING_TLD; |
| 0 | 300 | | DataStore.i.playerRealm.Set(new CurrentRealmModel() |
| | 301 | | { |
| | 302 | | domain = $"https://peer-lb.decentraland.{TESTING_TLD}", |
| | 303 | | contentServerUrl = $"https://peer-lb.decentraland.{TESTING_TLD}/content", |
| | 304 | | }); |
| | 305 | | } |
| | 306 | | #endif |
| | 307 | |
|
| 4 | 308 | | sectionsController.SetFetchingDataStart(); |
| | 309 | |
|
| 4 | 310 | | fetchLandPromise = DeployedScenesFetcher.FetchLandsFromOwner(catalyst, theGraph, address, network, landCacheTime |
| 4 | 311 | | fetchLandPromise |
| | 312 | | .Then(LandsFetched) |
| | 313 | | .Catch(LandsFetchedError); |
| | 314 | |
|
| 4 | 315 | | if (!DataStore.i.builderInWorld.isDevBuild.Get()) |
| 4 | 316 | | return; |
| 0 | 317 | | fetchProjectsPromise = BuilderPanelDataFetcher.FetchProjectData(context.builderAPIController); |
| 0 | 318 | | fetchProjectsPromise |
| | 319 | | .Then(ProjectsFetched) |
| | 320 | | .Catch(ProjectsFetchedError); |
| 0 | 321 | | } |
| | 322 | |
|
| | 323 | | internal void ProjectsFetched(ProjectData[] data) |
| | 324 | | { |
| 1 | 325 | | DataStore.i.builderInWorld.projectData.Set(data); |
| 1 | 326 | | isFetchingProjects = false; |
| 1 | 327 | | projectsController.SetProjects(data); |
| 1 | 328 | | UpdateProjectsDeploymentStatus(); |
| 1 | 329 | | } |
| | 330 | |
|
| | 331 | | internal void ProjectsFetchedError(string error) |
| | 332 | | { |
| 1 | 333 | | isFetchingProjects = false; |
| 1 | 334 | | sectionsController.SetFetchingDataEnd<SectionProjectController>(); |
| 1 | 335 | | projectsController.SetProjects(new ProjectData[] { }); |
| 1 | 336 | | BIWUtils.ShowGenericNotification(error); |
| 1 | 337 | | } |
| | 338 | |
|
| | 339 | | private void UpdateProjectsDeploymentStatus() |
| | 340 | | { |
| 2 | 341 | | if (isFetchingLands || isFetchingProjects) |
| 0 | 342 | | return; |
| | 343 | |
|
| 2 | 344 | | projectsController.UpdateDeploymentStatus(); |
| 2 | 345 | | } |
| | 346 | |
|
| | 347 | | internal void LandsFetchedError(string error) |
| | 348 | | { |
| 1 | 349 | | isFetchingLands = false; |
| 1 | 350 | | sectionsController.SetFetchingDataEnd<SectionLandController>(); |
| 1 | 351 | | sectionsController.SetFetchingDataEnd<SectionScenesController>(); |
| 1 | 352 | | landsesController.SetLands(new LandWithAccess[] { }); |
| 1 | 353 | | scenesViewController.SetScenes(new ISceneData[] { }); |
| 1 | 354 | | Debug.LogError(error); |
| 1 | 355 | | } |
| | 356 | |
|
| | 357 | | internal void LandsFetched(LandWithAccess[] lands) |
| | 358 | | { |
| 1 | 359 | | DataStore.i.builderInWorld.landsWithAccess.Set(lands.ToArray(), true); |
| 1 | 360 | | sectionsController.SetFetchingDataEnd<SectionLandController>(); |
| 1 | 361 | | isFetchingLands = false; |
| 1 | 362 | | UpdateProjectsDeploymentStatus(); |
| | 363 | |
|
| | 364 | | try |
| | 365 | | { |
| 2 | 366 | | ISceneData[] places = lands.Where(land => land.scenes != null && land.scenes.Count > 0) |
| 2 | 367 | | .Select(land => land.scenes.Where(scene => !scene.isEmpty).Select(scene => (IScen |
| 0 | 368 | | .Aggregate((i, j) => i.Concat(j)) |
| | 369 | | .ToArray(); |
| | 370 | |
|
| 0 | 371 | | if (sendPlayerOpenPanelEvent) |
| 0 | 372 | | PanelOpenEvent(lands); |
| 0 | 373 | | landsesController.SetLands(lands); |
| 0 | 374 | | scenesViewController.SetScenes(places); |
| 0 | 375 | | } |
| 1 | 376 | | catch (Exception e) |
| | 377 | | { |
| 1 | 378 | | landsesController.SetLands(lands); |
| 1 | 379 | | scenesViewController.SetScenes(new ISceneData[] { }); |
| 1 | 380 | | } |
| 1 | 381 | | } |
| | 382 | |
|
| | 383 | | internal void GoToCoords(Vector2Int coords) |
| | 384 | | { |
| 1 | 385 | | WebInterface.GoTo(coords.x, coords.y); |
| 1 | 386 | | SetVisibility(false); |
| 1 | 387 | | OnJumpInOrEdit?.Invoke(); |
| 1 | 388 | | } |
| | 389 | |
|
| 0 | 390 | | private void OpenUrl(string url) { WebInterface.OpenURL(url); } |
| | 391 | |
|
| | 392 | | internal void OnGoToEditScene(Vector2Int coords) |
| | 393 | | { |
| 1 | 394 | | bool isGoingToTeleport = BIWTeleportAndEdit.TeleportAndEdit(coords); |
| 1 | 395 | | if (isGoingToTeleport) |
| | 396 | | { |
| 1 | 397 | | SetVisibility(false); |
| | 398 | | } |
| | 399 | |
|
| 1 | 400 | | OnJumpInOrEdit?.Invoke(); |
| 1 | 401 | | } |
| | 402 | |
|
| | 403 | | private void StartFetchInterval() |
| | 404 | | { |
| 4 | 405 | | if (fetchDataInterval != null) |
| | 406 | | { |
| 0 | 407 | | StopFetchInterval(); |
| | 408 | | } |
| | 409 | |
|
| 4 | 410 | | fetchDataInterval = CoroutineStarter.Start(RefreshDataInterval()); |
| 4 | 411 | | } |
| | 412 | |
|
| | 413 | | private void StopFetchInterval() |
| | 414 | | { |
| 20 | 415 | | CoroutineStarter.Stop(fetchDataInterval); |
| 20 | 416 | | fetchDataInterval = null; |
| 20 | 417 | | } |
| | 418 | |
|
| | 419 | | IEnumerator RefreshDataInterval() |
| | 420 | | { |
| 0 | 421 | | while (true) |
| | 422 | | { |
| 4 | 423 | | yield return WaitForSecondsCache.Get(REFRESH_INTERVAL); |
| 0 | 424 | | FetchPanelInfo(); |
| | 425 | | } |
| | 426 | | } |
| | 427 | |
|
| | 428 | | private void OnSceneUnpublished(PublishSceneResultPayload current, PublishSceneResultPayload previous) |
| | 429 | | { |
| 0 | 430 | | if (current.ok) |
| | 431 | | { |
| 0 | 432 | | FetchPanelInfo(CACHE_TIME_LAND, 0); |
| | 433 | | } |
| 0 | 434 | | } |
| | 435 | | } |