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