using System; using System.Collections.Generic; using System.Diagnostics; using CIG; using CIG.Extensions; using SUISS.Core; using SUISSEngine; using UnityEngine; public class CIGDropChestManager : MonoBehaviour { //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event Action StateChangedEvent; private void FireStateChangedEvent() { if (this.StateChangedEvent != null) { this.StateChangedEvent(); } } private void Awake() { if (this._defaultFallbackDropChestLocations.Length == 0) { UnityEngine.Debug.LogErrorFormat(base.gameObject, "({0})CIGDropChestManager.DefaultFallbackDropChestLocations must have atleast 1 grid index!", new object[] { base.gameObject.name }); } } private void OnDestroy() { this.ClearInvokes(); this.StateChangedEvent = null; if (SingletonMonobehaviour.IsAvailable) { SingletonMonobehaviour.Instance.UnlockedEvent -= this.OnVideoWatchingUnlocked; } } public float RemainingTimeUntilDrop { get { return Mathf.Max((float)this._dropTime.Subtract(DateTime.UtcNow).TotalSeconds, 0f); } } public CIGDropChestManager.ChestState State { get { return this._state; } } public void DropChest() { this.ClearInvokes(); GridIndex index = this.FindChestDropLocation(this._expansions, this._grid); this._builder.BuildAt(this._dropChestPrefab, index, false, true); this._state = CIGDropChestManager.ChestState.Dropped; this._serializing.Serialize(); this.FireStateChangedEvent(); } private void SetChestReady() { this.ClearInvokes(); this._state = CIGDropChestManager.ChestState.Ready; this._serializing.Serialize(); this.FireStateChangedEvent(); } private void StartChestDrop() { this.ClearInvokes(); this._dropTime = DateTime.UtcNow + TimeSpan.FromHours((double)this._droppingTimerHours); this._state = CIGDropChestManager.ChestState.Timer; this._serializing.Serialize(); this.FireStateChangedEvent(); this.Invoke(new Action(this.SetChestReady), this.RemainingTimeUntilDrop, true); } private void ClearInvokes() { this.CancelInvoke(new Action(this.SetChestReady)); } private GridIndex FindChestDropLocation(CIGExpansions expansions, IsometricGrid grid) { List list = new List(); List list2 = new List(); for (int i = 0; i < grid.sizeU; i++) { for (int j = 0; j < grid.sizeV; j++) { GridElement gridElement = grid[i, j]; if (gridElement.Type > 0 && gridElement.Type != 3 && gridElement.Tile == null) { GridIndex gridIndex = new GridIndex(i, j); if (gridElement.Unlocked) { list2.Add(gridIndex); } else if (expansions.HasBuySign(gridIndex)) { list.Add(gridIndex); } } } } int count = list.Count; int count2 = list2.Count; if (count > 0) { return list[UnityEngine.Random.Range(0, count)]; } if (count2 > 0) { return list2[UnityEngine.Random.Range(0, count2)]; } return this._defaultFallbackDropChestLocations[UnityEngine.Random.Range(0, this._defaultFallbackDropChestLocations.Length)]; } private void OnSerialize(Dictionary values) { values["state"] = (int)this._state; values["dropTime"] = this._dropTime.ToBinary(); } private void OnDeserialize(Dictionary values) { object obj; if (values.TryGetValue("state", out obj) && obj is int) { this._state = (CIGDropChestManager.ChestState)((int)obj); } else { this._state = CIGDropChestManager.ChestState.Disabled; } object obj2; if (values.TryGetValue("dropTime", out obj2) && obj2 is long) { this._dropTime = DateTime.FromBinary((long)obj2); } else { this._dropTime = DateTime.UtcNow; } } private void OnDeserialized() { if (this._state == CIGDropChestManager.ChestState.Disabled) { if (SingletonMonobehaviour.IsAvailable) { VideoAds1Manager instance = SingletonMonobehaviour.Instance; if (instance.IsUnlocked) { this.StartChestDrop(); } else { instance.UnlockedEvent += this.OnVideoWatchingUnlocked; } } } else if (this._state == CIGDropChestManager.ChestState.Timer) { this.CancelInvoke(new Action(this.SetChestReady)); if (this.RemainingTimeUntilDrop > 0f) { this.Invoke(new Action(this.SetChestReady), this.RemainingTimeUntilDrop, true); } else { this.SetChestReady(); } } this.FireStateChangedEvent(); } private void OnVideoWatchingUnlocked() { this.StartChestDrop(); } [SerializeField] private IsometricGrid _grid; [SerializeField] private CIGExpansions _expansions; [SerializeField] private CIGBuilder _builder; [SerializeField] private GameObject _dropChestPrefab; [SerializeField] private Serializing _serializing; [SerializeField] private float _droppingTimerHours; [SerializeField] private GridIndex[] _defaultFallbackDropChestLocations; private CIGDropChestManager.ChestState _state; private DateTime _dropTime = DateTime.UtcNow; private const string StateKey = "state"; private const string DropTimeKey = "dropTime"; public enum ChestState { Disabled, Timer, Ready, Dropped } }