| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | | using DCL.Interface; |
| | 7 | |
|
| | 8 | | internal class HighlightScenesController : MonoBehaviour |
| | 9 | | { |
| | 10 | | const float SCENES_UPDATE_INTERVAL = 60; |
| | 11 | | private const float SCENE_FIRST_LOAD_DELAY = 0.1f; |
| | 12 | |
|
| | 13 | | [SerializeField] HotSceneCellView hotsceneBaseCellView; |
| | 14 | | [SerializeField] ScrollRect scrollRect; |
| | 15 | |
|
| 8 | 16 | | Dictionary<Vector2Int, HotSceneCellView> cachedHotScenes = new Dictionary<Vector2Int, HotSceneCellView>(); |
| 8 | 17 | | Dictionary<Vector2Int, HotSceneCellView> activeHotSceneViews = new Dictionary<Vector2Int, HotSceneCellView>(); |
| | 18 | |
|
| | 19 | | FriendTrackerController friendsController; |
| | 20 | |
|
| | 21 | | ViewPool<HotSceneCellView> hotScenesViewPool; |
| | 22 | |
|
| | 23 | | float lastTimeRefreshed = 0; |
| | 24 | | private Coroutine firstLoadAnimRoutine = null; |
| | 25 | |
|
| | 26 | | public void Initialize(FriendTrackerController friendsController) |
| | 27 | | { |
| 4 | 28 | | this.friendsController = friendsController; |
| 4 | 29 | | hotScenesViewPool = new ViewPool<HotSceneCellView>(hotsceneBaseCellView, 9); |
| 4 | 30 | | } |
| | 31 | |
|
| | 32 | | public void RefreshIfNeeded() |
| | 33 | | { |
| 3 | 34 | | bool isFirstTimeLoad = cachedHotScenes.Count == 0; |
| 3 | 35 | | if (isFirstTimeLoad && !ExploreHUDController.isTest) |
| | 36 | | { |
| 0 | 37 | | firstLoadAnimRoutine = StartCoroutine(FirstTimeLoadingRoutine()); |
| 0 | 38 | | } |
| 3 | 39 | | else if (HotScenesController.i.timeSinceLastUpdate >= SCENES_UPDATE_INTERVAL) |
| | 40 | | { |
| 2 | 41 | | FetchHotScenes(); |
| 2 | 42 | | } |
| 1 | 43 | | else if ((Time.realtimeSinceStartup - lastTimeRefreshed) >= SCENES_UPDATE_INTERVAL) |
| | 44 | | { |
| 1 | 45 | | ProcessHotScenes(); |
| | 46 | | } |
| | 47 | |
|
| 3 | 48 | | scrollRect.verticalNormalizedPosition = 1; |
| 3 | 49 | | } |
| | 50 | |
|
| | 51 | | void FetchHotScenes() |
| | 52 | | { |
| 2 | 53 | | WebInterface.FetchHotScenes(); |
| | 54 | |
|
| 2 | 55 | | HotScenesController.i.OnHotSceneListFinishUpdating -= OnFetchHotScenes; |
| 2 | 56 | | HotScenesController.i.OnHotSceneListFinishUpdating += OnFetchHotScenes; |
| 2 | 57 | | } |
| | 58 | |
|
| | 59 | | void OnFetchHotScenes() |
| | 60 | | { |
| 1 | 61 | | HotScenesController.i.OnHotSceneListFinishUpdating -= OnFetchHotScenes; |
| 1 | 62 | | ProcessHotScenes(); |
| 1 | 63 | | } |
| | 64 | |
|
| | 65 | | void ProcessHotScenes() |
| | 66 | | { |
| 2 | 67 | | lastTimeRefreshed = Time.realtimeSinceStartup; |
| | 68 | |
|
| 2 | 69 | | List<Vector2Int> cellsToHide = new List<Vector2Int>(activeHotSceneViews.Keys); |
| | 70 | |
|
| 16 | 71 | | for (int i = 0; i < HotScenesController.i.hotScenesList.Count; i++) |
| | 72 | | { |
| 6 | 73 | | cellsToHide.Remove(HotScenesController.i.hotScenesList[i].baseCoords); |
| 6 | 74 | | ProcessReceivedHotScene(HotScenesController.i.hotScenesList[i], i); |
| | 75 | | } |
| | 76 | |
|
| 4 | 77 | | for (int i = 0; i < cellsToHide.Count; i++) |
| | 78 | | { |
| 0 | 79 | | RemoveActiveHotSceneCell(cellsToHide[i]); |
| | 80 | | } |
| 2 | 81 | | } |
| | 82 | |
|
| | 83 | | void ProcessReceivedHotScene(HotScenesController.HotSceneInfo hotSceneInfo, int priority) |
| | 84 | | { |
| 6 | 85 | | Vector2Int baseCoords = hotSceneInfo.baseCoords; |
| 6 | 86 | | HotSceneCellView hotSceneView = null; |
| | 87 | |
|
| 6 | 88 | | if (cachedHotScenes.ContainsKey(baseCoords)) |
| | 89 | | { |
| 0 | 90 | | hotSceneView = cachedHotScenes[baseCoords]; |
| 0 | 91 | | if (!hotSceneView) |
| 0 | 92 | | return; |
| | 93 | | } |
| | 94 | | else |
| | 95 | | { |
| 6 | 96 | | hotSceneView = hotScenesViewPool.GetView(); |
| 6 | 97 | | hotSceneView.Initialize(); |
| 6 | 98 | | cachedHotScenes.Add(baseCoords, hotSceneView); |
| | 99 | | } |
| | 100 | |
|
| 6 | 101 | | hotSceneView.transform.SetSiblingIndex(priority); |
| | 102 | |
|
| 6 | 103 | | if (!hotSceneView.gameObject.activeSelf) |
| | 104 | | { |
| 6 | 105 | | hotSceneView.gameObject.SetActive(true); |
| | 106 | | } |
| | 107 | |
|
| 6 | 108 | | if (!IsHotSceneCellActive(baseCoords)) |
| | 109 | | { |
| 6 | 110 | | AddActiveHotSceneCell(baseCoords, hotSceneView); |
| | 111 | | } |
| | 112 | |
|
| 6 | 113 | | hotSceneView.Setup(hotSceneInfo); |
| 6 | 114 | | friendsController.AddHandler(hotSceneView.friendsHandler); |
| 6 | 115 | | } |
| | 116 | |
|
| 12 | 117 | | void AddActiveHotSceneCell(Vector2Int coords, HotSceneCellView view) { activeHotSceneViews.Add(coords, view); } |
| | 118 | |
|
| 6 | 119 | | bool IsHotSceneCellActive(Vector2Int coords) { return activeHotSceneViews.ContainsKey(coords); } |
| | 120 | |
|
| | 121 | | void RemoveActiveHotSceneCell(Vector2Int coords) |
| | 122 | | { |
| 0 | 123 | | if (activeHotSceneViews.TryGetValue(coords, out HotSceneCellView view)) |
| | 124 | | { |
| 0 | 125 | | view.gameObject.SetActive(false); |
| 0 | 126 | | view.Clear(); |
| 0 | 127 | | friendsController.RemoveHandler(view.friendsHandler); |
| | 128 | | } |
| | 129 | |
|
| 0 | 130 | | activeHotSceneViews.Remove(coords); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | private void OnDisable() |
| | 134 | | { |
| 4 | 135 | | if (!(firstLoadAnimRoutine is null)) |
| | 136 | | { |
| 0 | 137 | | StopCoroutine(firstLoadAnimRoutine); |
| 0 | 138 | | firstLoadAnimRoutine = null; |
| | 139 | | } |
| 4 | 140 | | } |
| | 141 | |
|
| | 142 | | void OnDestroy() |
| | 143 | | { |
| 4 | 144 | | HotScenesController.i.OnHotSceneListFinishUpdating -= OnFetchHotScenes; |
| 4 | 145 | | hotScenesViewPool?.Dispose(); |
| 4 | 146 | | } |
| | 147 | |
|
| | 148 | | IEnumerator FirstTimeLoadingRoutine() |
| | 149 | | { |
| 0 | 150 | | using (var iterator = hotScenesViewPool.GetEnumerator()) |
| | 151 | | { |
| 0 | 152 | | while (iterator.MoveNext()) |
| | 153 | | { |
| 0 | 154 | | if (iterator.Current is null) |
| | 155 | | continue; |
| | 156 | |
|
| 0 | 157 | | iterator.Current.gameObject.SetActive(true); |
| 0 | 158 | | yield return new WaitForSeconds(SCENE_FIRST_LOAD_DELAY); |
| | 159 | | } |
| 0 | 160 | | } |
| 0 | 161 | | FetchHotScenes(); |
| 0 | 162 | | } |
| | 163 | | } |