| | 1 | | using DCL.Helpers; |
| | 2 | | using Newtonsoft.Json; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.QuestsController |
| | 9 | | { |
| | 10 | | public delegate void NewQuest(string questId); |
| | 11 | |
|
| | 12 | | public delegate void QuestUpdated(string questId, bool hasProgress); |
| | 13 | |
|
| | 14 | | public delegate void QuestCompleted(string questId); |
| | 15 | |
|
| | 16 | | public delegate void RewardObtained(string questId, string rewardId); |
| | 17 | |
|
| | 18 | | public interface IQuestsController : IDisposable |
| | 19 | | { |
| | 20 | | event NewQuest OnNewQuest; |
| | 21 | | event QuestUpdated OnQuestUpdated; |
| | 22 | | event RewardObtained OnRewardObtained; |
| | 23 | |
|
| | 24 | | void InitializeQuests(List<QuestModel> parsedQuests); |
| | 25 | | void UpdateQuestProgress(QuestModel progressedQuest); |
| | 26 | | void RemoveQuest(QuestModel quest); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public class QuestsController : IQuestsController |
| | 30 | | { |
| | 31 | | private const string PINNED_QUESTS_KEY = "PinnedQuests"; |
| | 32 | |
|
| 53 | 33 | | public static IQuestsController i { get; internal set; } |
| | 34 | |
|
| | 35 | | public event NewQuest OnNewQuest; |
| | 36 | | public event QuestUpdated OnQuestUpdated; |
| | 37 | | public event RewardObtained OnRewardObtained; |
| | 38 | |
|
| 141 | 39 | | private static BaseCollection<string> pinnedQuests => DataStore.i.Quests.pinnedQuests; |
| 15 | 40 | | private static BaseDictionary<string, QuestModel> quests => DataStore.i.Quests.quests; |
| | 41 | |
|
| 2 | 42 | | static QuestsController() { i = new QuestsController(); } |
| | 43 | |
|
| 10 | 44 | | public QuestsController() |
| | 45 | | { |
| 10 | 46 | | var savedPinnedQuests = PlayerPrefs.GetString(PINNED_QUESTS_KEY, null); |
| 10 | 47 | | if (!string.IsNullOrEmpty(savedPinnedQuests)) |
| | 48 | | { |
| 5 | 49 | | pinnedQuests.Set(Utils.ParseJsonArray<string[]>(savedPinnedQuests)); |
| | 50 | | } |
| | 51 | |
|
| 10 | 52 | | pinnedQuests.OnAdded += OnPinnedQuestUpdated; |
| 10 | 53 | | pinnedQuests.OnRemoved += OnPinnedQuestUpdated; |
| 10 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Bulk initialization of quests |
| | 58 | | /// </summary> |
| | 59 | | /// <param name="parsedQuests"></param> |
| | 60 | | public void InitializeQuests(List<QuestModel> parsedQuests) |
| | 61 | | { |
| 11 | 62 | | List<QuestModel> filteredQuests = parsedQuests.Where(x => x.sections != null && x.sections.Length > 0).ToLis |
| 4 | 63 | | if (filteredQuests.Count == 0) //We ignore quests without sections/tasks |
| 1 | 64 | | return; |
| | 65 | |
|
| 11 | 66 | | var questsToUnpin = parsedQuests.Where(x => !x.canBePinned).Select(x => x.id); |
| 12 | 67 | | foreach (string questId in questsToUnpin) |
| | 68 | | { |
| 3 | 69 | | pinnedQuests.Remove(questId); |
| | 70 | | } |
| | 71 | |
|
| 3 | 72 | | filteredQuests.ForEach(RestoreProgressFlags); |
| 8 | 73 | | quests.Set(filteredQuests.Select(x => (x.id, x))); |
| 3 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Update progress in a quest |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="progressedQuest"></param> |
| | 80 | | public void UpdateQuestProgress(QuestModel progressedQuest) |
| | 81 | | { |
| 6 | 82 | | if (!progressedQuest.canBePinned) |
| 3 | 83 | | pinnedQuests.Remove(progressedQuest.id); |
| | 84 | |
|
| | 85 | | //Alex: Edge case. Quests has no sections/tasks, we ignore the UpdateQuestProgress and remove the cached one |
| 6 | 86 | | if (progressedQuest.sections == null || progressedQuest.sections.Length == 0) |
| | 87 | | { |
| 0 | 88 | | quests.Remove(progressedQuest.id); |
| 0 | 89 | | return; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | //Alex: Edge case. Progressed quest was not included in the initialization. We dont invoke quests events |
| 6 | 93 | | if (!quests.TryGetValue(progressedQuest.id, out QuestModel oldQuest)) |
| | 94 | | { |
| 3 | 95 | | RestoreProgressFlags(progressedQuest); |
| 3 | 96 | | quests.Add(progressedQuest.id, progressedQuest); |
| 3 | 97 | | if (!progressedQuest.isCompleted) |
| | 98 | | { |
| 2 | 99 | | OnNewQuest?.Invoke(progressedQuest.id); |
| 2 | 100 | | QuestsControllerAnalytics.SendQuestDiscovered(progressedQuest); |
| | 101 | | } |
| | 102 | |
|
| 3 | 103 | | return; |
| | 104 | | } |
| | 105 | |
|
| 3 | 106 | | quests[progressedQuest.id] = progressedQuest; |
| 3 | 107 | | progressedQuest.oldProgress = oldQuest.progress; |
| | 108 | |
|
| 12 | 109 | | for (int index = 0; index < progressedQuest.sections.Length; index++) |
| | 110 | | { |
| 3 | 111 | | QuestSection newQuestSection = progressedQuest.sections[index]; |
| | 112 | |
|
| 3 | 113 | | bool oldQuestSectionFound = oldQuest.TryGetSection(newQuestSection.id, out QuestSection oldQuestSection) |
| | 114 | |
|
| 12 | 115 | | for (int index2 = 0; index2 < newQuestSection.tasks.Length; index2++) |
| | 116 | | { |
| 3 | 117 | | QuestTask currentTask = newQuestSection.tasks[index2]; |
| 3 | 118 | | if (oldQuestSectionFound) |
| | 119 | | { |
| 3 | 120 | | bool oldTaskFound = oldQuestSection.TryGetTask(currentTask.id, out QuestTask oldTask); |
| 3 | 121 | | currentTask.justProgressed = !oldTaskFound || currentTask.progress != oldTask.progress; |
| 3 | 122 | | if (currentTask.justProgressed) |
| 2 | 123 | | QuestsControllerAnalytics.SendTaskProgressed(progressedQuest, newQuestSection, currentTask); |
| | 124 | |
|
| 3 | 125 | | currentTask.justUnlocked = !oldTaskFound || (currentTask.status != QuestsLiterals.Status.BLOCKED |
| 3 | 126 | | currentTask.oldProgress = oldTaskFound ? oldTask.progress : 0; |
| 3 | 127 | | if (oldTaskFound && oldTask.status != QuestsLiterals.Status.COMPLETED && currentTask.status == Q |
| 1 | 128 | | QuestsControllerAnalytics.SendTaskCompleted(progressedQuest, newQuestSection, currentTask); |
| 1 | 129 | | } |
| | 130 | | else |
| | 131 | | { |
| 0 | 132 | | currentTask.justProgressed = false; |
| 0 | 133 | | currentTask.justUnlocked = false; |
| 0 | 134 | | currentTask.oldProgress = 0; |
| | 135 | | } |
| | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| | 139 | |
|
| | 140 | | // If quest is not blocked anymore or being secret has been just started, we call NewQuest event. |
| 3 | 141 | | if (!progressedQuest.isCompleted && |
| | 142 | | ((oldQuest.status == QuestsLiterals.Status.BLOCKED && progressedQuest.status != QuestsLiterals.Status.BL |
| | 143 | | (progressedQuest.visibility == QuestsLiterals.Visibility.SECRET && oldQuest.status == QuestsLiterals.St |
| | 144 | | { |
| 0 | 145 | | OnNewQuest?.Invoke(progressedQuest.id); |
| 0 | 146 | | QuestsControllerAnalytics.SendQuestDiscovered(progressedQuest); |
| | 147 | | } |
| | 148 | |
|
| 3 | 149 | | OnQuestUpdated?.Invoke(progressedQuest.id, HasProgressed(progressedQuest, oldQuest)); |
| 3 | 150 | | if (!oldQuest.isCompleted && progressedQuest.isCompleted) |
| 1 | 151 | | QuestsControllerAnalytics.SendQuestCompleted(progressedQuest); |
| | 152 | |
|
| 3 | 153 | | if (progressedQuest.rewards == null) |
| 0 | 154 | | progressedQuest.rewards = new QuestReward[0]; |
| | 155 | |
|
| 12 | 156 | | for (int index = 0; index < progressedQuest.rewards.Length; index++) |
| | 157 | | { |
| 3 | 158 | | QuestReward newReward = progressedQuest.rewards[index]; |
| | 159 | |
|
| 3 | 160 | | bool rewardObtained = false; |
| 3 | 161 | | if (oldQuest.TryGetReward(newReward.id, out QuestReward oldReward)) |
| | 162 | | { |
| 2 | 163 | | rewardObtained = newReward.status != oldReward.status && newReward.status == QuestsLiterals.RewardSt |
| 2 | 164 | | } |
| | 165 | | else |
| | 166 | | { |
| | 167 | | //Alex: Edge case. New quest reported contains a reward that was previously not contained. |
| | 168 | | // If it's completed, we call the RewardObtained event |
| 1 | 169 | | rewardObtained = newReward.status == QuestsLiterals.RewardStatus.OK; |
| | 170 | | } |
| | 171 | |
|
| 3 | 172 | | if (rewardObtained) |
| | 173 | | { |
| 1 | 174 | | OnRewardObtained?.Invoke(progressedQuest.id, newReward.id); |
| 1 | 175 | | QuestsControllerAnalytics.SendRewardObtained(progressedQuest, newReward); |
| | 176 | | } |
| | 177 | | } |
| | 178 | |
|
| 3 | 179 | | RestoreProgressFlags(progressedQuest); |
| 3 | 180 | | } |
| | 181 | |
|
| | 182 | | private void RestoreProgressFlags(QuestModel progressedQuest) |
| | 183 | | { |
| 11 | 184 | | progressedQuest.oldProgress = progressedQuest.progress; |
| 44 | 185 | | for (int index = 0; index < progressedQuest.sections.Length; index++) |
| | 186 | | { |
| 11 | 187 | | QuestSection section = progressedQuest.sections[index]; |
| 44 | 188 | | for (var index2 = 0; index2 < section.tasks.Length; index2++) |
| | 189 | | { |
| 11 | 190 | | section.tasks[index2].justProgressed = false; |
| 11 | 191 | | section.tasks[index2].justUnlocked = false; |
| 11 | 192 | | section.tasks[index2].oldProgress = section.tasks[index2].progress; |
| | 193 | | } |
| | 194 | | } |
| 11 | 195 | | } |
| | 196 | |
|
| 0 | 197 | | public void RemoveQuest(QuestModel quest) { quests.Remove(quest.id); } |
| | 198 | |
|
| 16 | 199 | | private void OnPinnedQuestUpdated(string questId) { PlayerPrefs.SetString(PINNED_QUESTS_KEY, JsonConvert.Seriali |
| | 200 | |
|
| | 201 | | public void Dispose() |
| | 202 | | { |
| 51 | 203 | | pinnedQuests.OnAdded -= OnPinnedQuestUpdated; |
| 51 | 204 | | pinnedQuests.OnRemoved -= OnPinnedQuestUpdated; |
| 51 | 205 | | } |
| | 206 | |
|
| | 207 | | private bool HasProgressed(QuestModel newQuest, QuestModel oldQuest) |
| | 208 | | { |
| 3 | 209 | | if (newQuest.rewards != null) |
| | 210 | | { |
| 11 | 211 | | foreach (QuestReward newQuestReward in newQuest.rewards) |
| | 212 | | { |
| 3 | 213 | | if (!oldQuest.TryGetReward(newQuestReward.id, out var oldReward)) |
| | 214 | | continue; |
| | 215 | |
|
| 2 | 216 | | if (newQuestReward.status != oldReward.status) |
| 1 | 217 | | return true; |
| | 218 | | } |
| | 219 | | } |
| | 220 | |
|
| 7 | 221 | | foreach (QuestSection newQuestSection in newQuest.sections) |
| | 222 | | { |
| 2 | 223 | | if (!oldQuest.TryGetSection(newQuestSection.id, out var oldSection)) |
| | 224 | | continue; |
| | 225 | |
|
| 2 | 226 | | if (Math.Abs(newQuestSection.progress - oldSection.progress) > Mathf.Epsilon) |
| 1 | 227 | | return true; |
| | 228 | | } |
| | 229 | |
|
| 1 | 230 | | return false; |
| | 231 | | } |
| | 232 | | } |
| | 233 | | } |