using System; using CIG; using UnityEngine; public abstract class BalloonFactory : ICanSerialize { public BalloonFactory() { this._lastCollectTimestamp = DateTime.MinValue; this._lastShownTimestamp = DateTime.MinValue; } public BalloonFactory(StorageDictionary storage) { this._storage = storage; this._lastCollectTimestamp = this._storage.GetDateTime("LastCollectDateTime", DateTime.MinValue); this._lastShownTimestamp = this._storage.GetDateTime("LastShownDateTime", DateTime.MinValue); } public virtual bool CanProduce() { return (DateTime.UtcNow - this._lastCollectTimestamp).TotalMinutes >= (double)this.CoolDownAfterCollectInMinutes && (DateTime.UtcNow - this._lastShownTimestamp).TotalMinutes >= (double)this.CoolDownAfterShowInMinutes; } public virtual int CoolDownAfterShowInMinutes { get { return 0; } } public virtual int CoolDownAfterCollectInMinutes { get { return 0; } } public Balloon ProduceBalloon() { if (!this.CanProduce()) { UnityEngine.Debug.LogError("Requested to produce balloon while factory can't produce. Did you forget to check CanProduce?"); return null; } Balloon balloon = this.CreateBalloonInstance(); balloon.CollectedEvent += this.OnBalloonCollected; balloon.ExpiredEvent += this.OnBalloonExpired; this.UpdateLastShownBalloonTimestamp(); return balloon; } protected abstract Balloon CreateBalloonInstance(); private void UpdateLastCollectedBalloonTimestamp() { this._lastCollectTimestamp = DateTime.UtcNow; } private void UpdateLastShownBalloonTimestamp() { this._lastShownTimestamp = DateTime.UtcNow; } protected virtual void OnBalloonExpired(Balloon balloon) { balloon.CollectedEvent -= this.OnBalloonCollected; balloon.ExpiredEvent -= this.OnBalloonExpired; } protected virtual void OnBalloonCollected(Balloon balloon) { this.UpdateLastCollectedBalloonTimestamp(); balloon.CollectedEvent -= this.OnBalloonCollected; balloon.ExpiredEvent -= this.OnBalloonExpired; } protected DateTime LastCollectedTimestamp { get { return this._lastCollectTimestamp; } } protected DateTime LastShownTimestamp { get { return this._lastShownTimestamp; } } public virtual StorageDictionary Serialize() { if (this._storage == null) { this._storage = new StorageDictionary(); } this._storage.Set("LastCollectDateTime", this._lastCollectTimestamp); this._storage.Set("LastShownDateTime", this._lastShownTimestamp); return this._storage; } private DateTime _lastCollectTimestamp; private DateTime _lastShownTimestamp; private const string LastCollectDateTimeKey = "LastCollectDateTime"; private const string LastShownDateTimeKey = "LastShownDateTime"; private StorageDictionary _storage; }