| | 1 | | using DCL.QuestsController; |
| | 2 | | using System.Collections.Generic; |
| | 3 | |
|
| | 4 | | namespace DCL.Huds.QuestsTracker |
| | 5 | | { |
| | 6 | | public class QuestsTrackerHUDController : IHUD |
| | 7 | | { |
| 35 | 8 | | private static BaseDictionary<string, QuestModel> quests => DataStore.i.Quests.quests; |
| 36 | 9 | | private static BaseCollection<string> pinnedQuests => DataStore.i.Quests.pinnedQuests; |
| | 10 | |
|
| | 11 | | internal IQuestsTrackerHUDView view; |
| | 12 | | internal IQuestsController questsController; |
| | 13 | |
|
| | 14 | | public void Initialize(IQuestsController controller) |
| | 15 | | { |
| 8 | 16 | | questsController = controller; |
| 8 | 17 | | view = CreateView(); |
| | 18 | |
|
| 8 | 19 | | questsController.OnQuestUpdated += OnQuestUpdated; |
| 8 | 20 | | questsController.OnRewardObtained += AddReward; |
| 8 | 21 | | pinnedQuests.OnAdded += OnPinnedQuest; |
| 8 | 22 | | pinnedQuests.OnRemoved += OnUnpinnedQuest; |
| 8 | 23 | | pinnedQuests.OnSet += OnPinnedQuestsSet; |
| 8 | 24 | | quests.OnSet += OnQuestsSet; |
| 8 | 25 | | quests.OnAdded += OnQuestsAdded; |
| 8 | 26 | | quests.OnRemoved += OnQuestRemoved; |
| | 27 | |
|
| 20 | 28 | | foreach (string questId in pinnedQuests.Get()) |
| | 29 | | { |
| 2 | 30 | | OnPinnedQuest(questId); |
| | 31 | | } |
| 8 | 32 | | } |
| | 33 | |
|
| | 34 | | private void AddReward(string questId, string rewardId) |
| | 35 | | { |
| 1 | 36 | | if (!quests.TryGetValue(questId, out QuestModel model) || model.status == QuestsLiterals.Status.BLOCKED) |
| 0 | 37 | | return; |
| | 38 | |
|
| 1 | 39 | | if (!model.TryGetReward(rewardId, out QuestReward reward)) |
| 0 | 40 | | return; |
| 1 | 41 | | view?.AddReward(questId, reward); |
| 1 | 42 | | } |
| | 43 | |
|
| | 44 | | private void OnQuestsAdded(string questId, QuestModel quest) |
| | 45 | | { |
| 0 | 46 | | if (pinnedQuests.Contains(questId)) |
| 0 | 47 | | view?.PinQuest(questId); |
| 0 | 48 | | } |
| | 49 | |
|
| 2 | 50 | | private void OnQuestsSet(IEnumerable<KeyValuePair<string, QuestModel>> pairs) { OnPinnedQuestsSet(pinnedQuests.G |
| | 51 | |
|
| | 52 | | private void OnPinnedQuestsSet(IEnumerable<string> pinnedQuests) |
| | 53 | | { |
| 2 | 54 | | view?.ClearEntries(); |
| 12 | 55 | | foreach (string questId in pinnedQuests) |
| | 56 | | { |
| 4 | 57 | | OnPinnedQuest(questId); |
| | 58 | | } |
| 2 | 59 | | } |
| | 60 | |
|
| | 61 | | private void OnQuestUpdated(string questId, bool hasProgress) |
| | 62 | | { |
| 1 | 63 | | if (!quests.TryGetValue(questId, out QuestModel model) || model.status == QuestsLiterals.Status.BLOCKED || ( |
| | 64 | | { |
| 0 | 65 | | view?.RemoveEntry(questId); |
| 0 | 66 | | return; |
| | 67 | | } |
| | 68 | |
|
| 1 | 69 | | view?.UpdateQuest(questId, hasProgress); |
| 1 | 70 | | } |
| | 71 | |
|
| | 72 | | private void OnPinnedQuest(string questId) |
| | 73 | | { |
| 7 | 74 | | if (!quests.TryGetValue(questId, out QuestModel model) || model.status == QuestsLiterals.Status.BLOCKED || ( |
| | 75 | | { |
| 2 | 76 | | view?.RemoveEntry(questId); |
| 2 | 77 | | return; |
| | 78 | | } |
| 5 | 79 | | view?.PinQuest(questId); |
| 5 | 80 | | } |
| | 81 | |
|
| 0 | 82 | | private void OnUnpinnedQuest(string questId) { view?.UnpinQuest(questId); } |
| | 83 | |
|
| 2 | 84 | | private void OnQuestRemoved(string questId, QuestModel quest) { view?.RemoveEntry(questId); } |
| | 85 | |
|
| 2 | 86 | | public void SetVisibility(bool visible) { view?.SetVisibility(visible); } |
| | 87 | |
|
| | 88 | | public void Dispose() |
| | 89 | | { |
| 1 | 90 | | view.Dispose(); |
| 1 | 91 | | if (questsController != null) |
| | 92 | | { |
| 1 | 93 | | questsController.OnQuestUpdated -= OnQuestUpdated; |
| 1 | 94 | | questsController.OnRewardObtained -= AddReward; |
| | 95 | | } |
| 1 | 96 | | pinnedQuests.OnAdded -= OnPinnedQuest; |
| 1 | 97 | | pinnedQuests.OnRemoved -= OnUnpinnedQuest; |
| 1 | 98 | | pinnedQuests.OnSet -= OnPinnedQuestsSet; |
| 1 | 99 | | quests.OnSet -= OnQuestsSet; |
| 1 | 100 | | quests.OnRemoved -= OnQuestRemoved; |
| 1 | 101 | | } |
| | 102 | |
|
| 1 | 103 | | internal virtual IQuestsTrackerHUDView CreateView() => QuestsTrackerHUDView.Create(); |
| | 104 | | } |
| | 105 | | } |