using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using SUISS.Core; using SUISS.Core.Utilities; using SUISS.Storage; namespace SUISSEngine { public sealed class Daily : Singleton { public Daily() { this._calendar = new GregorianCalendar(GregorianCalendarTypes.USEnglish); this.CheckCurrentDay(); if (SingletonMonobehaviour.IsAvailable) { SingletonMonobehaviour.Instance.CreateClockTimer(DateTimeKind.Local, new TimeSpan(3, 0, 0), new TimeSpan(1, 0, 0, 0), delegate() { this.CheckCurrentDay(); }); } Storage.NewGameEvent += this.OnNewGame; } //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event Daily.CurrentDayChangedEventHandler CurrentDayChanged; public Daily.Day CurrentDay { get { return this._currentDay; } } private void FireCurrentDayChangedEvent(Daily.Day previousDay, Daily.Day newDay) { if (this.CurrentDayChanged != null) { this.CurrentDayChanged(previousDay, newDay); } } private void CheckCurrentDay() { DateTime time = DateTime.Now; if (this._calendar.GetHour(time) < 3) { time = this._calendar.AddHours(time, -3); } Daily.Day day = new Daily.Day(this._calendar.GetYear(time), this._calendar.GetMonth(time), this._calendar.GetDayOfMonth(time)); Daily.Day day2 = (Daily.Day)Storage.Get(StorageLifecycle.Game).Root.Get("Daily.PreviousDay", null); if (day2 != null && day.CompareTo(day2) < 0) { day = day2; } if (!object.Equals(day, this._currentDay)) { Daily.Day currentDay = this._currentDay; this._currentDay = day; Storage.Get(StorageLifecycle.Game).Root["Daily.PreviousDay"] = this._currentDay; this.FireCurrentDayChangedEvent(currentDay, this._currentDay); } } private void OnNewGame() { this.CheckCurrentDay(); } public const int NewDayHourOffset = 3; private const string PreviousDayStorageKey = "Daily.PreviousDay"; private Calendar _calendar; private Daily.Day _currentDay; public delegate void CurrentDayChangedEventHandler(Daily.Day previousDay, Daily.Day newDay); public class Day : IStorable, IComparable { public Day() { this._year = 1970; this._month = 1; this._dayOfMonth = 1; } public Day(int year, int month, int dayOfMonth) { this._year = year; this._month = month; this._dayOfMonth = dayOfMonth; } void IStorable.FromStorage(IDictionary dict) { this._year = dict.GetInt("Year", 1970); this._month = dict.GetInt("Month", 1); this._dayOfMonth = dict.GetInt("DayOfMonth", 1); } IDictionary IStorable.ToStorage() { Dictionary dictionary = new Dictionary(); dictionary["Year"] = this._year; dictionary["Month"] = this._month; dictionary["DayOfMonth"] = this._dayOfMonth; return dictionary; } public int Year { get { return this._year; } } public int Month { get { return this._month; } } public int DayOfMonth { get { return this._dayOfMonth; } } public DateTime Date { get { return DateTimeExtensionsWS.NewDateTime(this._year, this._month, this._dayOfMonth, 0, 0, 0, 0, Singleton.Instance._calendar, DateTimeKind.Local); } } public DateTime UtcDate { get { return DateTimeExtensionsWS.NewDateTime(this._year, this._month, this._dayOfMonth, 0, 0, 0, 0, Singleton.Instance._calendar, DateTimeKind.Utc); } } public int CalculateTimeRemaining() { DateTime dateTime = this.Date.Add(new TimeSpan(1, 3, 0, 0)); DateTime now = DateTime.Now; return (int)Math.Ceiling(dateTime.Subtract(now).TotalSeconds); } public int CompareTo(Daily.Day other) { int num = this._year - other._year; if (num == 0) { num = this._month - other._month; if (num == 0) { num = this._dayOfMonth - other._dayOfMonth; } } return num; } public override string ToString() { return string.Format(CultureInfo.InvariantCulture, "{0:D4}-{1:D2}-{2:D2}", new object[] { this._year, this._month, this._dayOfMonth }); } public override int GetHashCode() { return this._year * 10000 + this._month * 100 + this._dayOfMonth; } public override bool Equals(object obj) { return obj is Daily.Day && ((Daily.Day)obj)._year == this._year && ((Daily.Day)obj)._month == this._month && ((Daily.Day)obj)._dayOfMonth == this._dayOfMonth; } private int _year; private int _month; private int _dayOfMonth; } } }