| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using DCLServices.QuestsService; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.Quests |
| | 11 | | { |
| | 12 | | public class QuestLogController : IDisposable |
| | 13 | | { |
| | 14 | | private readonly IUserProfileBridge userProfileBridge; |
| | 15 | | private readonly IQuestLogComponentView questLogComponentView; |
| | 16 | | private readonly IQuestsService questsService; |
| | 17 | |
|
| | 18 | | public event Action<string, bool> OnPinChange; |
| | 19 | | public event Action<Vector2Int> OnJumpIn; |
| | 20 | | public event Action<string> OnQuestAbandon; |
| | 21 | | private CancellationTokenSource disposeCts; |
| | 22 | |
|
| 0 | 23 | | public QuestLogController( |
| | 24 | | IQuestLogComponentView questLogComponentView, |
| | 25 | | IUserProfileBridge userProfileBridge, |
| | 26 | | IQuestsService questsService) |
| | 27 | | { |
| 0 | 28 | | disposeCts = new CancellationTokenSource(); |
| 0 | 29 | | this.questLogComponentView = questLogComponentView; |
| 0 | 30 | | this.userProfileBridge = userProfileBridge; |
| 0 | 31 | | this.questsService = questsService; |
| | 32 | |
|
| 0 | 33 | | questLogComponentView.OnPinChange += (id, isPinned) => OnPinChange?.Invoke(id, isPinned); |
| 0 | 34 | | questLogComponentView.OnQuestAbandon += (id) => OnQuestAbandon?.Invoke(id); |
| 0 | 35 | | questLogComponentView.OnJumpIn += (coords) => OnJumpIn?.Invoke(coords); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | public async UniTaskVoid AddActiveQuest(QuestDetailsComponentModel activeQuest, CancellationToken ct = default) |
| | 39 | | { |
| 0 | 40 | | string creatorName = await GetUsername(activeQuest.questCreator, CancellationTokenSource.CreateLinkedTokenSo |
| 0 | 41 | | List<QuestRewardComponentModel> questRewards = new List<QuestRewardComponentModel>(); |
| | 42 | |
|
| 0 | 43 | | foreach (QuestReward questReward in await questsService.GetQuestRewards(activeQuest.questDefinitionId, ct)) |
| | 44 | | { |
| 0 | 45 | | questRewards.Add(new QuestRewardComponentModel() |
| | 46 | | { |
| | 47 | | imageUri = questReward.image_link, |
| | 48 | | name = questReward.name |
| | 49 | | }); |
| 0 | 50 | | activeQuest.questRewards = questRewards; |
| | 51 | | } |
| | 52 | |
|
| 0 | 53 | | Debug.Log(activeQuest.questName + " " + activeQuest.questRewards.Count); |
| 0 | 54 | | questLogComponentView.AddActiveQuest(activeQuest, creatorName); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public async UniTaskVoid AddCompletedQuest(QuestDetailsComponentModel completedQuest, CancellationToken ct = def |
| | 58 | | { |
| 0 | 59 | | string creatorName = await GetUsername(completedQuest.questCreator, CancellationTokenSource.CreateLinkedToke |
| 0 | 60 | | List<QuestRewardComponentModel> questRewards = new List<QuestRewardComponentModel>(); |
| | 61 | |
|
| 0 | 62 | | foreach (QuestReward questReward in await questsService.GetQuestRewards(completedQuest.questDefinitionId, ct |
| | 63 | | { |
| 0 | 64 | | questRewards.Add(new QuestRewardComponentModel() |
| | 65 | | { |
| | 66 | | imageUri = questReward.image_link, |
| | 67 | | name = questReward.name |
| | 68 | | }); |
| 0 | 69 | | completedQuest.questRewards = questRewards; |
| | 70 | | } |
| | 71 | |
|
| 0 | 72 | | questLogComponentView.AddCompletedQuest(completedQuest, creatorName); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void RemoveQuestIfExists(string questId) => |
| 0 | 76 | | questLogComponentView.RemoveQuestIfExists(questId); |
| | 77 | |
|
| | 78 | | public void SetAsFullScreenMenuMode(Transform parentTransform) => |
| 0 | 79 | | questLogComponentView.SetAsFullScreenMenuMode(parentTransform); |
| | 80 | |
|
| | 81 | | public void SetIsGuest(bool isGuest) => |
| 0 | 82 | | questLogComponentView.SetIsGuest(isGuest); |
| | 83 | |
|
| | 84 | | private async UniTask<string> GetUsername(string userId, CancellationToken cancellationToken) |
| | 85 | | { |
| 0 | 86 | | var profile = userProfileBridge.Get(userId); |
| 0 | 87 | | if (profile != null) |
| 0 | 88 | | return profile.userName.ToUpper(); |
| | 89 | |
|
| 0 | 90 | | profile ??= await userProfileBridge.RequestFullUserProfileAsync(userId, cancellationToken); |
| 0 | 91 | | return profile.userName.ToUpper(); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | public void Dispose() => |
| 0 | 95 | | disposeCts?.SafeCancelAndDispose(); |
| | 96 | | } |
| | 97 | | } |