| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public class HotScenesController : MonoBehaviour |
| | 7 | | { |
| 0 | 8 | | public static HotScenesController i { get; private set; } |
| | 9 | |
|
| | 10 | | public event Action OnHotSceneListFinishUpdating; |
| | 11 | | public event Action OnHotSceneListChunkUpdated; |
| | 12 | |
|
| 1 | 13 | | public List<HotSceneInfo> hotScenesList { get; private set; } = new List<HotSceneInfo>(); |
| 0 | 14 | | public bool isUpdating { get; private set; } |
| 0 | 15 | | public float timeSinceLastUpdate { get { return Time.realtimeSinceStartup - lastUpdateTime; } } |
| | 16 | |
|
| 1 | 17 | | private float lastUpdateTime = float.MinValue * .5f; |
| | 18 | |
|
| | 19 | | [Serializable] |
| | 20 | | public class HotSceneInfo |
| | 21 | | { |
| | 22 | | [Serializable] |
| | 23 | | public class Realm |
| | 24 | | { |
| | 25 | | public string serverName; |
| | 26 | | public string layer; |
| | 27 | | public int usersCount; |
| | 28 | | public int maxUsers; |
| | 29 | | public Vector2Int[] userParcels; |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public string id; |
| | 33 | | public string name; |
| | 34 | | public string creator; |
| | 35 | | public string description; |
| | 36 | | public string thumbnail; |
| | 37 | | public Vector2Int baseCoords; |
| | 38 | | public Vector2Int[] parcels; |
| | 39 | | public int usersTotalCount; |
| | 40 | | public Realm[] realms; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | [Serializable] |
| | 44 | | internal struct HotScenesUpdatePayload |
| | 45 | | { |
| | 46 | | public int chunkIndex; |
| | 47 | | public int chunksCount; |
| | 48 | | public HotSceneInfo[] scenesInfo; |
| | 49 | | } |
| | 50 | |
|
| 2 | 51 | | void Awake() { i = this; } |
| | 52 | |
|
| | 53 | | public void UpdateHotScenesList(string json) |
| | 54 | | { |
| 1 | 55 | | var updatePayload = Utils.SafeFromJson<HotScenesUpdatePayload>(json); |
| | 56 | |
|
| 1 | 57 | | if (updatePayload.chunkIndex == 0) |
| | 58 | | { |
| 1 | 59 | | isUpdating = true; |
| 1 | 60 | | hotScenesList.Clear(); |
| | 61 | | } |
| | 62 | |
|
| 1 | 63 | | hotScenesList.AddRange(updatePayload.scenesInfo); |
| 1 | 64 | | OnHotSceneListChunkUpdated?.Invoke(); |
| | 65 | |
|
| 1 | 66 | | if (updatePayload.chunkIndex >= updatePayload.chunksCount - 1) |
| | 67 | | { |
| 1 | 68 | | isUpdating = false; |
| 1 | 69 | | lastUpdateTime = Time.realtimeSinceStartup; |
| 1 | 70 | | OnHotSceneListFinishUpdating?.Invoke(); |
| | 71 | | } |
| 0 | 72 | | } |
| | 73 | | } |