| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL.Interface; |
| | 5 | | using MainScripts.DCL.Controllers.HotScenes; |
| | 6 | | using System.Collections; |
| | 7 | | using UpdateMode = DCL.MapGlobalUsersPositionMarkerController.UpdateMode; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Handle the fetch of hot scenes at intervals. |
| | 13 | | /// Interval time may change accordingly if set to update in background or foregraound |
| | 14 | | /// </summary> |
| | 15 | | [Obsolete ("Use IHotScenesFetcher instead")] |
| | 16 | | internal class FetchScenesHandler : IDisposable |
| | 17 | | { |
| | 18 | | public event Action<List<IHotScenesController.HotSceneInfo>> OnScenesFetched; |
| | 19 | |
|
| | 20 | | float initialIntevalTime; |
| | 21 | | float backgroundIntervalTime; |
| | 22 | | float foregroundIntervalTime; |
| | 23 | |
|
| | 24 | | Coroutine updateCoroutine; |
| | 25 | | UpdateMode updateMode; |
| | 26 | |
|
| | 27 | | internal bool isFirstFetch; |
| | 28 | | internal float updateInterval; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Constructor |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="initialIntevalTime">seconds for interval until the first populated scenes are fetched</param> |
| | 34 | | /// <param name="foregroundIntervalTime">seconds for interval when update mode is in FOREGROUND</param> |
| | 35 | | /// <param name="backgroundIntervalTime">seconds for interval when update mode is in BACKGROUND</param> |
| 1 | 36 | | public FetchScenesHandler(float initialIntevalTime, float foregroundIntervalTime, float backgroundIntervalTime) |
| | 37 | | { |
| 1 | 38 | | this.initialIntevalTime = initialIntevalTime; |
| 1 | 39 | | this.backgroundIntervalTime = backgroundIntervalTime; |
| 1 | 40 | | this.foregroundIntervalTime = foregroundIntervalTime; |
| 1 | 41 | | this.updateInterval = initialIntevalTime; |
| 1 | 42 | | this.isFirstFetch = true; |
| 1 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Initialize fetch intervals |
| | 47 | | /// </summary> |
| | 48 | | public void Init() |
| | 49 | | { |
| 0 | 50 | | if (updateCoroutine != null) |
| 0 | 51 | | return; |
| | 52 | |
|
| 0 | 53 | | updateCoroutine = CoroutineStarter.Start(UpdateCoroutine()); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Set update mode. Scene's fetch intervals will smaller when updating in FOREGROUND than when updating in BACK |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="mode">update mode</param> |
| | 60 | | public void SetUpdateMode(UpdateMode mode) |
| | 61 | | { |
| 3 | 62 | | updateMode = mode; |
| 3 | 63 | | if (isFirstFetch) |
| 1 | 64 | | return; |
| | 65 | |
|
| 2 | 66 | | switch (updateMode) |
| | 67 | | { |
| | 68 | | case UpdateMode.BACKGROUND: |
| 1 | 69 | | updateInterval = backgroundIntervalTime; |
| 1 | 70 | | break; |
| | 71 | | case UpdateMode.FOREGROUND: |
| 1 | 72 | | updateInterval = foregroundIntervalTime; |
| | 73 | | break; |
| | 74 | | } |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | public void Dispose() |
| | 78 | | { |
| 0 | 79 | | CoroutineStarter.Stop(updateCoroutine); |
| | 80 | |
|
| 0 | 81 | | if (HotScenesController.i) |
| 0 | 82 | | HotScenesController.i.OnHotSceneListFinishUpdating -= OnHotSceneListFinishUpdating; |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | private IEnumerator UpdateCoroutine() |
| | 86 | | { |
| 0 | 87 | | while (true) |
| | 88 | | { |
| 0 | 89 | | float time = Time.realtimeSinceStartup; |
| | 90 | |
|
| 0 | 91 | | while (Time.realtimeSinceStartup - time < updateInterval) |
| | 92 | | { |
| 0 | 93 | | yield return null; |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | if(HotScenesController.i == null) yield break; |
| | 97 | |
|
| 0 | 98 | | if (HotScenesController.i.timeSinceLastUpdate > updateInterval) |
| | 99 | | { |
| 0 | 100 | | HotScenesController.i.OnHotSceneListFinishUpdating += OnHotSceneListFinishUpdating; |
| 0 | 101 | | WebInterface.FetchHotScenes(); |
| | 102 | | } |
| | 103 | | else |
| | 104 | | { |
| 0 | 105 | | OnHotSceneListFinishUpdating(); |
| | 106 | | } |
| | 107 | | } |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | private void OnHotSceneListFinishUpdating() { OnHotScenesFetched(HotScenesController.i.hotScenesList); } |
| | 111 | |
|
| | 112 | | private void OnHotScenesFetched(List<IHotScenesController.HotSceneInfo> scenes) |
| | 113 | | { |
| 0 | 114 | | HotScenesController.i.OnHotSceneListFinishUpdating -= OnHotSceneListFinishUpdating; |
| | 115 | |
|
| 0 | 116 | | bool fetchSuccess = scenes.Count > 0 && scenes[0].usersTotalCount > 0; |
| | 117 | |
|
| 0 | 118 | | if (!fetchSuccess) |
| 0 | 119 | | return; |
| | 120 | |
|
| 0 | 121 | | if (isFirstFetch) |
| | 122 | | { |
| 0 | 123 | | isFirstFetch = false; |
| 0 | 124 | | SetUpdateMode(updateMode); |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | OnScenesFetched?.Invoke(scenes); |
| 0 | 128 | | } |
| | 129 | | } |
| | 130 | | } |