| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Tasks; |
| | 4 | | using Decentraland.Quests; |
| | 5 | | using Google.Protobuf.WellKnownTypes; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using DCL; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.Networking; |
| | 11 | | using DCLServices.QuestsService; |
| | 12 | |
|
| | 13 | | namespace DCLServices.QuestsService |
| | 14 | | { |
| | 15 | | /* TODO Alex: |
| | 16 | | - Add service to ServiceLocator |
| | 17 | | - Find a good place to call QuestsService.SetUserId |
| | 18 | | All these requirements are only needed to launch quests, |
| | 19 | | in the meantime the service is ready to be tested by mocking a ClientQuestService (look at the test scene) |
| | 20 | | */ |
| | 21 | | public class QuestsService : IQuestsService |
| | 22 | | { |
| | 23 | | private const bool VERBOSE = false; |
| | 24 | |
|
| 0 | 25 | | public IAsyncEnumerableWithEvent<QuestInstance> QuestStarted => questStarted; |
| 0 | 26 | | public IAsyncEnumerableWithEvent<QuestInstance> QuestUpdated => questUpdated; |
| | 27 | |
|
| 0 | 28 | | private readonly AsyncEnumerableWithEvent<QuestInstance> questStarted = new (); |
| 0 | 29 | | private readonly AsyncEnumerableWithEvent<QuestInstance> questUpdated = new (); |
| | 30 | |
|
| 0 | 31 | | public IReadOnlyDictionary<string, QuestInstance> QuestInstances => questInstances; |
| 0 | 32 | | public IReadOnlyDictionary<string, object> QuestRewards { get; } |
| | 33 | |
|
| 0 | 34 | | internal readonly Dictionary<string, QuestInstance> questInstances = new (); |
| | 35 | |
|
| | 36 | | internal readonly IClientQuestsService clientQuestsService; |
| | 37 | | internal readonly IQuestRewardsResolver questRewardsResolver; |
| | 38 | | private Service<IWebRequestController> webRequestController; |
| 0 | 39 | | internal readonly Dictionary<string, UniTaskCompletionSource<Quest>> definitionCache = new (); |
| 0 | 40 | | internal readonly Dictionary<string, UniTaskCompletionSource<IReadOnlyList<QuestReward>>> rewardsCache = new (); |
| | 41 | |
|
| 0 | 42 | | internal readonly CancellationTokenSource disposeCts = new (); |
| 0 | 43 | | internal readonly UniTaskCompletionSource gettingInitialState = new (); |
| | 44 | |
|
| 0 | 45 | | public QuestsService(IClientQuestsService clientQuestsService, IQuestRewardsResolver questRewardsResolver) |
| | 46 | | { |
| 0 | 47 | | this.clientQuestsService = clientQuestsService; |
| 0 | 48 | | this.questRewardsResolver = questRewardsResolver; |
| 0 | 49 | | Subscribe().Forget(); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | private async UniTaskVoid Subscribe() |
| | 53 | | { |
| | 54 | | //Obtain initial state |
| 0 | 55 | | var allquests = await clientQuestsService.GetAllQuests(new Empty()); |
| | 56 | |
|
| | 57 | | if(VERBOSE) |
| | 58 | | Debug.Log("[QuestsService] Getting all quests"); |
| | 59 | |
|
| 0 | 60 | | foreach (QuestInstance questInstance in allquests.Quests.Instances) |
| | 61 | | { |
| | 62 | | if(VERBOSE) |
| | 63 | | Debug.Log($"[QuestsService]\n{questInstance}"); |
| 0 | 64 | | questInstances[questInstance.Id] = questInstance; |
| 0 | 65 | | string questId = questInstance.Quest.Id; |
| | 66 | |
|
| 0 | 67 | | if (!definitionCache.TryGetValue(questId, out var completionSource)) |
| 0 | 68 | | definitionCache[questId] = completionSource = new UniTaskCompletionSource<Quest>(); |
| | 69 | |
|
| 0 | 70 | | completionSource.TrySetResult(questInstance.Quest); |
| | 71 | |
|
| 0 | 72 | | questUpdated.Write(questInstance); |
| | 73 | | } |
| | 74 | |
|
| | 75 | | //Listen to updates |
| 0 | 76 | | var enumerable = clientQuestsService.Subscribe(new Empty()); |
| | 77 | |
|
| | 78 | | if(VERBOSE) |
| | 79 | | Debug.Log($"[QuestsService] Subscribing"); |
| | 80 | |
|
| 0 | 81 | | await foreach (UserUpdate userUpdate in enumerable.WithCancellation(disposeCts.Token)) |
| | 82 | | { |
| | 83 | | if(VERBOSE) |
| | 84 | | Debug.Log($"[QuestsService] Update:\n{userUpdate}"); |
| 0 | 85 | | switch (userUpdate.MessageCase) |
| | 86 | | { |
| | 87 | | case UserUpdate.MessageOneofCase.Subscribed: |
| 0 | 88 | | gettingInitialState.TrySetResult(); |
| 0 | 89 | | break; |
| | 90 | | case UserUpdate.MessageOneofCase.QuestStateUpdate: |
| 0 | 91 | | if (!questInstances.TryGetValue(userUpdate.QuestStateUpdate.InstanceId, out var questUpdatedInst |
| | 92 | | { |
| | 93 | | if(VERBOSE) |
| | 94 | | Debug.Log($"Received quest update which instance was not received before: {userUpdate.To |
| | 95 | |
|
| | 96 | | continue; |
| | 97 | | } |
| 0 | 98 | | questUpdatedInstance.State = userUpdate.QuestStateUpdate.QuestState; |
| 0 | 99 | | questUpdated.Write(questUpdatedInstance); |
| 0 | 100 | | break; |
| | 101 | |
|
| | 102 | | case UserUpdate.MessageOneofCase.NewQuestStarted: |
| 0 | 103 | | var questInstance = userUpdate.NewQuestStarted; |
| 0 | 104 | | questInstances[questInstance.Id] = questInstance; |
| | 105 | |
|
| 0 | 106 | | string questId = questInstance.Quest.Id; |
| 0 | 107 | | if (!definitionCache.TryGetValue(questId, out var completionSource)) |
| 0 | 108 | | definitionCache[questId] = completionSource = new UniTaskCompletionSource<Quest>(); |
| | 109 | |
|
| 0 | 110 | | questStarted.Write(questInstance); |
| 0 | 111 | | completionSource.TrySetResult(questInstance.Quest); |
| | 112 | | break; |
| | 113 | | } |
| | 114 | | } |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | public async UniTask<StartQuestResponse> StartQuest(string questId) |
| | 118 | | { |
| 0 | 119 | | await gettingInitialState.Task; |
| 0 | 120 | | return await clientQuestsService.StartQuest(new StartQuestRequest { QuestId = questId }); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | public async UniTask<AbortQuestResponse> AbortQuest(string questInstanceId) |
| | 124 | | { |
| 0 | 125 | | await gettingInitialState.Task; |
| 0 | 126 | | AbortQuestResponse abortQuestResponse = await clientQuestsService.AbortQuest(new AbortQuestRequest { QuestIn |
| | 127 | |
|
| 0 | 128 | | if (abortQuestResponse.ResponseCase == AbortQuestResponse.ResponseOneofCase.Accepted) |
| 0 | 129 | | questInstances.Remove(questInstanceId); |
| 0 | 130 | | return abortQuestResponse; |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public UniTask<Quest> GetDefinition(string questId, CancellationToken cancellationToken = default) |
| | 134 | | { |
| | 135 | | UniTaskCompletionSource<Quest> definitionCompletionSource; |
| | 136 | |
|
| | 137 | | async UniTask<Quest> RetrieveTask() |
| | 138 | | { |
| 0 | 139 | | GetQuestDefinitionResponse definition = await clientQuestsService.GetQuestDefinition(new GetQuestDefinit |
| | 140 | |
|
| 0 | 141 | | if (definitionCache.TryGetValue(definition.Quest.Id, out definitionCompletionSource)) |
| 0 | 142 | | definitionCache[definition.Quest.Id].TrySetResult(definition.Quest); |
| | 143 | |
|
| 0 | 144 | | return definition.Quest; |
| 0 | 145 | | } |
| | 146 | |
|
| 0 | 147 | | if (!definitionCache.TryGetValue(questId, out definitionCompletionSource)) |
| | 148 | | { |
| 0 | 149 | | definitionCompletionSource = new UniTaskCompletionSource<Quest>(); |
| 0 | 150 | | definitionCache[questId] = definitionCompletionSource; |
| 0 | 151 | | RetrieveTask().Forget(); |
| | 152 | | } |
| | 153 | |
|
| 0 | 154 | | return definitionCompletionSource.Task.AttachExternalCancellation(cancellationToken); |
| | 155 | | } |
| | 156 | |
|
| | 157 | | public UniTask<IReadOnlyList<QuestReward>> GetQuestRewards(string questId, CancellationToken cancellationToken = |
| | 158 | | { |
| | 159 | | UniTaskCompletionSource<IReadOnlyList<QuestReward>> rewardsCompletionSource; |
| | 160 | |
|
| | 161 | | async UniTask<IReadOnlyList<QuestReward>> RetrieveTask() |
| | 162 | | { |
| 0 | 163 | | IReadOnlyList<QuestReward> response = await questRewardsResolver.ResolveRewards(questId, cancellationTok |
| | 164 | |
|
| 0 | 165 | | if (rewardsCache.TryGetValue(questId, out rewardsCompletionSource)) |
| 0 | 166 | | rewardsCache[questId].TrySetResult(response); |
| | 167 | |
|
| 0 | 168 | | return response; |
| 0 | 169 | | } |
| | 170 | |
|
| 0 | 171 | | if (!rewardsCache.TryGetValue(questId, out rewardsCompletionSource)) |
| | 172 | | { |
| 0 | 173 | | rewardsCompletionSource = new UniTaskCompletionSource<IReadOnlyList<QuestReward>>(); |
| 0 | 174 | | rewardsCache[questId] = rewardsCompletionSource; |
| 0 | 175 | | RetrieveTask().Forget(); |
| | 176 | | } |
| | 177 | |
|
| 0 | 178 | | return rewardsCompletionSource.Task.AttachExternalCancellation(cancellationToken); |
| | 179 | | } |
| | 180 | |
|
| | 181 | | public void Dispose() |
| | 182 | | { |
| 0 | 183 | | disposeCts.SafeCancelAndDispose(); |
| 0 | 184 | | questStarted.Dispose(); |
| 0 | 185 | | questUpdated.Dispose(); |
| 0 | 186 | | } |
| | 187 | | } |
| | 188 | | } |