using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; using System.Globalization; using CIG; using Engine.DependencyTree; using SUISS.Core; using SUISS.Storage; using SUISSEngine; using UnityEngine; public sealed class CIGDailyQuestManager : SingletonMonobehaviour { //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event CIGDailyQuestManager.DailyQuestUpdated UpdatedEvent; private void FireUpdatedEvent() { if (this.UpdatedEvent != null) { this.UpdatedEvent(); } } //[DebuggerBrowsable(DebuggerBrowsableState.Never)] public event CIGDailyQuestManager.AllDailyQuestAchieved AllDailyQuestAchievedEvent; private void FireAllDailyQuestAchievedEvent() { if (this.AllDailyQuestAchievedEvent != null) { this.AllDailyQuestAchievedEvent(); } } private void Start() { Dictionary root = Storage.Get(StorageLifecycle.Game).Root; if (root.ContainsKey("RWDailyQuestManager.ActiveQuests") && !root.ContainsKey("RWDailyQuestManager.DaysPassed")) { this._daysPassed = -1; } else { this._daysPassed = (int)root.Get("RWDailyQuestManager.DaysPassed", 0); } List list = (List)root.Get("RWDailyQuestManager.ActiveQuests", new List()); this.ClearActiveQuests(); Dependency currentDayDependency = this.GetCurrentDayDependency(); int count = currentDayDependency.Children.Count; for (int i = 0; i < count; i++) { Dependency dependency = currentDayDependency.Children[i]; if (list.Contains(dependency.Identifier)) { dependency.AchievedChangedEvent += this.OnQuestAchieved; this._activeQuests.Add(dependency); dependency.IsActive = true; } else { dependency.IsActive = false; } } this.FireUpdatedEvent(); if (list.Count > this._dailyQuestCount) { this._forceReset = true; } this.ResetQuestsIfNecessary(); Singleton.Instance.CurrentDayChanged += this.OnCurrentDayChanged; } protected override void OnDestroy() { if (Singleton.IsAvailable) { Singleton.Instance.CurrentDayChanged -= this.OnCurrentDayChanged; } base.OnDestroy(); } public void ForceReset() { this._forceReset = true; this.ResetQuestsIfNecessary(); } public int ActiveQuestsAmount { get { return this._activeQuests.Count; } } public Dependency GetActiveQuestAt(int index) { if (index >= 0 && index < this.ActiveQuestsAmount) { return this._activeQuests[index]; } return null; } public bool IsActiveDailyQuest(Dependency quest) { return this._activeQuests.Contains(quest); } private IEnumerator ResetQuestsIfNecessaryRoutine() { yield return new WaitForSeconds(0.5f); this.ResetQuestsIfNecessary(); yield break; } private Dependency GetCurrentDayDependency() { string identifier = string.Format("daily_quests_day_{0}", this._daysPassed + 1); if (!SingletonMonobehaviour.Instance.ContainsDependency(identifier)) { identifier = "daily_quests"; } return SingletonMonobehaviour.Instance[identifier]; } private void ResetQuestsIfNecessary() { Dictionary root = Storage.Get(StorageLifecycle.Game).Root; Daily.Day day = (Daily.Day)root.Get("RWDailyQuestManager.PreviousDay", null); Daily.Day currentDay = Singleton.Instance.CurrentDay; bool flag = !object.Equals(day, currentDay); if (flag || this._forceReset) { this._forceReset = false; if (flag && day != null) { this._daysPassed++; CIGGameStats instance = SingletonMonobehaviour.Instance; if ((currentDay.UtcDate - day.UtcDate).TotalDays > 1.0) { instance.ResetDaysPlayedStreak(); } else { instance.AddDaysPlayedStreak(1); } } Dependency currentDayDependency = this.GetCurrentDayDependency(); List list = new List(currentDayDependency.Children); List list2 = new List(list.Count); float num = 0f; int count = list.Count; for (int i = 0; i < count; i++) { Dependency dependency = list[i]; string property = dependency.GetProperty("weight"); float num2; if (string.IsNullOrEmpty(property) || !float.TryParse(property, NumberStyles.Float, CultureInfo.InvariantCulture, out num2)) { num2 = 1f; } list2.Add(num2); num += num2; } this.ClearActiveQuests(); int num3 = 0; while (num3 < this._dailyQuestCount && count > 0) { float num4 = UnityEngine.Random.Range(0f, num); float num5 = 0f; int index = count - 1; for (int j = 0; j < count; j++) { num5 += list2[j]; if (num4 < num5) { index = j; break; } } Dependency dependency2 = list[index]; dependency2.AchievedChangedEvent += this.OnQuestAchieved; this._activeQuests.Add(dependency2); num -= list2[index]; list2.RemoveAt(index); list.RemoveAt(index); count = list.Count; num3++; } SingletonMonobehaviour.Instance.BeginMutating(); count = list.Count; for (int k = 0; k < count; k++) { list[k].IsActive = false; } int count2 = this._activeQuests.Count; Dictionary dictionary = new Dictionary(); for (int l = 0; l < count2; l++) { Dependency dependency2 = this._activeQuests[l]; Dependency dependency3 = dependency2.Children[0]; dictionary.Clear(); string property2 = dependency3.GetProperty("possible-values"); if (!string.IsNullOrEmpty(property2)) { string[] array = property2.Split(new char[] { '|' }); int num6 = UnityEngine.Random.Range(0, array.Length); dictionary["min"] = array[num6]; string property3 = dependency3.GetProperty("possible-rewards"); if (!string.IsNullOrEmpty(property3)) { string[] array2 = property3.Split(new char[] { '|' }); if (array2.Length == array.Length) { dictionary["reward"] = array2[num6]; } else { UnityEngine.Debug.LogErrorFormat("Possible Reward and Value lists of '{0}' have different sizes ({1} vs {2})!", new object[] { dependency3.Identifier, property3.Length, array2.Length }); } } } dependency3.Reset(dictionary); dependency2.Reset(null); dependency2.IsActive = true; } SingletonMonobehaviour.Instance.EndMutating(); count2 = this._activeQuests.Count; List list3 = new List(count2); for (int m = 0; m < count2; m++) { list3.Add(this._activeQuests[m].Identifier); } root["RWDailyQuestManager.ActiveQuests"] = list3; root["RWDailyQuestManager.PreviousDay"] = currentDay; root["RWDailyQuestManager.DaysPassed"] = this._daysPassed; this.FireUpdatedEvent(); } } private void OnCurrentDayChanged(Daily.Day previousDay, Daily.Day newDay) { base.StartCoroutine(this.ResetQuestsIfNecessaryRoutine()); } private void ClearActiveQuests() { int count = this._activeQuests.Count; for (int i = 0; i < count; i++) { this._activeQuests[i].AchievedChangedEvent -= this.OnQuestAchieved; } this._activeQuests.Clear(); } private void OnQuestAchieved(Dependency quest, bool achieved) { if (achieved) { SingletonMonobehaviour.Instance.AddQuestCompleted(); quest.AchievedChangedEvent -= this.OnQuestAchieved; Dependency currentDayDependency = this.GetCurrentDayDependency(); if (currentDayDependency != null) { ReadOnlyCollection children = currentDayDependency.Children; int num = 0; bool flag = true; int count = children.Count; for (int i = 0; i < count; i++) { Dependency dependency = children[i]; if (dependency.IsActive) { if (dependency.IsAchieved) { num++; } else { flag = false; } } } if (flag && num > 0) { SingletonMonobehaviour.Instance.IncrementAllDailyQuestsAchieved(); this.FireAllDailyQuestAchievedEvent(); } } } } private const string PreviousDayKey = "RWDailyQuestManager.PreviousDay"; private const string ActiveQuestsKey = "RWDailyQuestManager.ActiveQuests"; private const string DaysPassedKey = "RWDailyQuestManager.DaysPassed"; private const string DailyQuestsDependencyIdentifier = "daily_quests"; private const string FixedDailyQuestsDependencyIdentifierTemplate = "daily_quests_day_{0}"; [SerializeField] private int _dailyQuestCount = 3; private CIGGameStats _gameStats; private bool _forceReset; private int _daysPassed; private List _activeQuests = new List(); public delegate void DailyQuestUpdated(); public delegate void AllDailyQuestAchieved(); }