You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

333 lines
9.0 KiB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Diagnostics;
  6. using System.Globalization;
  7. using CIG;
  8. using Engine.DependencyTree;
  9. using SUISS.Core;
  10. using SUISS.Storage;
  11. using SUISSEngine;
  12. using UnityEngine;
  13. public sealed class CIGDailyQuestManager : SingletonMonobehaviour<CIGDailyQuestManager>
  14. {
  15. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  16. public event CIGDailyQuestManager.DailyQuestUpdated UpdatedEvent;
  17. private void FireUpdatedEvent()
  18. {
  19. if (this.UpdatedEvent != null)
  20. {
  21. this.UpdatedEvent();
  22. }
  23. }
  24. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  25. public event CIGDailyQuestManager.AllDailyQuestAchieved AllDailyQuestAchievedEvent;
  26. private void FireAllDailyQuestAchievedEvent()
  27. {
  28. if (this.AllDailyQuestAchievedEvent != null)
  29. {
  30. this.AllDailyQuestAchievedEvent();
  31. }
  32. }
  33. private void Start()
  34. {
  35. Dictionary<string, object> root = Storage.Get(StorageLifecycle.Game).Root;
  36. if (root.ContainsKey("RWDailyQuestManager.ActiveQuests") && !root.ContainsKey("RWDailyQuestManager.DaysPassed"))
  37. {
  38. this._daysPassed = -1;
  39. }
  40. else
  41. {
  42. this._daysPassed = (int)root.Get("RWDailyQuestManager.DaysPassed", 0);
  43. }
  44. List<object> list = (List<object>)root.Get("RWDailyQuestManager.ActiveQuests", new List<object>());
  45. this.ClearActiveQuests();
  46. Dependency currentDayDependency = this.GetCurrentDayDependency();
  47. int count = currentDayDependency.Children.Count;
  48. for (int i = 0; i < count; i++)
  49. {
  50. Dependency dependency = currentDayDependency.Children[i];
  51. if (list.Contains(dependency.Identifier))
  52. {
  53. dependency.AchievedChangedEvent += this.OnQuestAchieved;
  54. this._activeQuests.Add(dependency);
  55. dependency.IsActive = true;
  56. }
  57. else
  58. {
  59. dependency.IsActive = false;
  60. }
  61. }
  62. this.FireUpdatedEvent();
  63. if (list.Count > this._dailyQuestCount)
  64. {
  65. this._forceReset = true;
  66. }
  67. this.ResetQuestsIfNecessary();
  68. Singleton<Daily>.Instance.CurrentDayChanged += this.OnCurrentDayChanged;
  69. }
  70. protected override void OnDestroy()
  71. {
  72. if (Singleton<Daily>.IsAvailable)
  73. {
  74. Singleton<Daily>.Instance.CurrentDayChanged -= this.OnCurrentDayChanged;
  75. }
  76. base.OnDestroy();
  77. }
  78. public void ForceReset()
  79. {
  80. this._forceReset = true;
  81. this.ResetQuestsIfNecessary();
  82. }
  83. public int ActiveQuestsAmount
  84. {
  85. get
  86. {
  87. return this._activeQuests.Count;
  88. }
  89. }
  90. public Dependency GetActiveQuestAt(int index)
  91. {
  92. if (index >= 0 && index < this.ActiveQuestsAmount)
  93. {
  94. return this._activeQuests[index];
  95. }
  96. return null;
  97. }
  98. public bool IsActiveDailyQuest(Dependency quest)
  99. {
  100. return this._activeQuests.Contains(quest);
  101. }
  102. private IEnumerator ResetQuestsIfNecessaryRoutine()
  103. {
  104. yield return new WaitForSeconds(0.5f);
  105. this.ResetQuestsIfNecessary();
  106. yield break;
  107. }
  108. private Dependency GetCurrentDayDependency()
  109. {
  110. string identifier = string.Format("daily_quests_day_{0}", this._daysPassed + 1);
  111. if (!SingletonMonobehaviour<DependencyTree>.Instance.ContainsDependency(identifier))
  112. {
  113. identifier = "daily_quests";
  114. }
  115. return SingletonMonobehaviour<DependencyTree>.Instance[identifier];
  116. }
  117. private void ResetQuestsIfNecessary()
  118. {
  119. Dictionary<string, object> root = Storage.Get(StorageLifecycle.Game).Root;
  120. Daily.Day day = (Daily.Day)root.Get("RWDailyQuestManager.PreviousDay", null);
  121. Daily.Day currentDay = Singleton<Daily>.Instance.CurrentDay;
  122. bool flag = !object.Equals(day, currentDay);
  123. if (flag || this._forceReset)
  124. {
  125. this._forceReset = false;
  126. if (flag && day != null)
  127. {
  128. this._daysPassed++;
  129. CIGGameStats instance = SingletonMonobehaviour<CIGGameStats>.Instance;
  130. if ((currentDay.UtcDate - day.UtcDate).TotalDays > 1.0)
  131. {
  132. instance.ResetDaysPlayedStreak();
  133. }
  134. else
  135. {
  136. instance.AddDaysPlayedStreak(1);
  137. }
  138. }
  139. Dependency currentDayDependency = this.GetCurrentDayDependency();
  140. List<Dependency> list = new List<Dependency>(currentDayDependency.Children);
  141. List<float> list2 = new List<float>(list.Count);
  142. float num = 0f;
  143. int count = list.Count;
  144. for (int i = 0; i < count; i++)
  145. {
  146. Dependency dependency = list[i];
  147. string property = dependency.GetProperty("weight");
  148. float num2;
  149. if (string.IsNullOrEmpty(property) || !float.TryParse(property, NumberStyles.Float, CultureInfo.InvariantCulture, out num2))
  150. {
  151. num2 = 1f;
  152. }
  153. list2.Add(num2);
  154. num += num2;
  155. }
  156. this.ClearActiveQuests();
  157. int num3 = 0;
  158. while (num3 < this._dailyQuestCount && count > 0)
  159. {
  160. float num4 = UnityEngine.Random.Range(0f, num);
  161. float num5 = 0f;
  162. int index = count - 1;
  163. for (int j = 0; j < count; j++)
  164. {
  165. num5 += list2[j];
  166. if (num4 < num5)
  167. {
  168. index = j;
  169. break;
  170. }
  171. }
  172. Dependency dependency2 = list[index];
  173. dependency2.AchievedChangedEvent += this.OnQuestAchieved;
  174. this._activeQuests.Add(dependency2);
  175. num -= list2[index];
  176. list2.RemoveAt(index);
  177. list.RemoveAt(index);
  178. count = list.Count;
  179. num3++;
  180. }
  181. SingletonMonobehaviour<DependencyTree>.Instance.BeginMutating();
  182. count = list.Count;
  183. for (int k = 0; k < count; k++)
  184. {
  185. list[k].IsActive = false;
  186. }
  187. int count2 = this._activeQuests.Count;
  188. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  189. for (int l = 0; l < count2; l++)
  190. {
  191. Dependency dependency2 = this._activeQuests[l];
  192. Dependency dependency3 = dependency2.Children[0];
  193. dictionary.Clear();
  194. string property2 = dependency3.GetProperty("possible-values");
  195. if (!string.IsNullOrEmpty(property2))
  196. {
  197. string[] array = property2.Split(new char[]
  198. {
  199. '|'
  200. });
  201. int num6 = UnityEngine.Random.Range(0, array.Length);
  202. dictionary["min"] = array[num6];
  203. string property3 = dependency3.GetProperty("possible-rewards");
  204. if (!string.IsNullOrEmpty(property3))
  205. {
  206. string[] array2 = property3.Split(new char[]
  207. {
  208. '|'
  209. });
  210. if (array2.Length == array.Length)
  211. {
  212. dictionary["reward"] = array2[num6];
  213. }
  214. else
  215. {
  216. UnityEngine.Debug.LogErrorFormat("Possible Reward and Value lists of '{0}' have different sizes ({1} vs {2})!", new object[]
  217. {
  218. dependency3.Identifier,
  219. property3.Length,
  220. array2.Length
  221. });
  222. }
  223. }
  224. }
  225. dependency3.Reset(dictionary);
  226. dependency2.Reset(null);
  227. dependency2.IsActive = true;
  228. }
  229. SingletonMonobehaviour<DependencyTree>.Instance.EndMutating();
  230. count2 = this._activeQuests.Count;
  231. List<object> list3 = new List<object>(count2);
  232. for (int m = 0; m < count2; m++)
  233. {
  234. list3.Add(this._activeQuests[m].Identifier);
  235. }
  236. root["RWDailyQuestManager.ActiveQuests"] = list3;
  237. root["RWDailyQuestManager.PreviousDay"] = currentDay;
  238. root["RWDailyQuestManager.DaysPassed"] = this._daysPassed;
  239. this.FireUpdatedEvent();
  240. }
  241. }
  242. private void OnCurrentDayChanged(Daily.Day previousDay, Daily.Day newDay)
  243. {
  244. base.StartCoroutine(this.ResetQuestsIfNecessaryRoutine());
  245. }
  246. private void ClearActiveQuests()
  247. {
  248. int count = this._activeQuests.Count;
  249. for (int i = 0; i < count; i++)
  250. {
  251. this._activeQuests[i].AchievedChangedEvent -= this.OnQuestAchieved;
  252. }
  253. this._activeQuests.Clear();
  254. }
  255. private void OnQuestAchieved(Dependency quest, bool achieved)
  256. {
  257. if (achieved)
  258. {
  259. SingletonMonobehaviour<CIGGameStats>.Instance.AddQuestCompleted();
  260. quest.AchievedChangedEvent -= this.OnQuestAchieved;
  261. Dependency currentDayDependency = this.GetCurrentDayDependency();
  262. if (currentDayDependency != null)
  263. {
  264. ReadOnlyCollection<Dependency> children = currentDayDependency.Children;
  265. int num = 0;
  266. bool flag = true;
  267. int count = children.Count;
  268. for (int i = 0; i < count; i++)
  269. {
  270. Dependency dependency = children[i];
  271. if (dependency.IsActive)
  272. {
  273. if (dependency.IsAchieved)
  274. {
  275. num++;
  276. }
  277. else
  278. {
  279. flag = false;
  280. }
  281. }
  282. }
  283. if (flag && num > 0)
  284. {
  285. SingletonMonobehaviour<CIGGameStats>.Instance.IncrementAllDailyQuestsAchieved();
  286. this.FireAllDailyQuestAchievedEvent();
  287. }
  288. }
  289. }
  290. }
  291. private const string PreviousDayKey = "RWDailyQuestManager.PreviousDay";
  292. private const string ActiveQuestsKey = "RWDailyQuestManager.ActiveQuests";
  293. private const string DaysPassedKey = "RWDailyQuestManager.DaysPassed";
  294. private const string DailyQuestsDependencyIdentifier = "daily_quests";
  295. private const string FixedDailyQuestsDependencyIdentifierTemplate = "daily_quests_day_{0}";
  296. [SerializeField]
  297. private int _dailyQuestCount = 3;
  298. private CIGGameStats _gameStats;
  299. private bool _forceReset;
  300. private int _daysPassed;
  301. private List<Dependency> _activeQuests = new List<Dependency>();
  302. public delegate void DailyQuestUpdated();
  303. public delegate void AllDailyQuestAchieved();
  304. }