| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Providers; |
| | 5 | | using DCL.Quests; |
| | 6 | | using DCLServices.QuestsService; |
| | 7 | | using Decentraland.Quests; |
| | 8 | | using rpc_csharp; |
| | 9 | | using RPC.Transports; |
| | 10 | | using System.Threading; |
| | 11 | | using UnityEngine; |
| | 12 | | using WebSocketSharp; |
| | 13 | |
|
| | 14 | | public class QuestsPlugin : IPlugin |
| | 15 | | { |
| | 16 | | private const string QUEST_TRACKER_HUD = "QuestTrackerHUD"; |
| | 17 | | private const string QUEST_COMPLETED_HUD = "QuestCompletedHUD"; |
| | 18 | | private const string QUEST_STARTED_POPUP = "QuestStartedPopupHUD"; |
| | 19 | | private const string QUEST_LOG = "QuestLogHUD"; |
| | 20 | | private readonly IAddressableResourceProvider resourceProvider; |
| 0 | 21 | | private IUserProfileBridge userProfileBridge = new UserProfileWebInterfaceBridge(); |
| | 22 | |
|
| | 23 | | private QuestTrackerComponentView questTrackerComponentView; |
| | 24 | | private QuestCompletedComponentView questCompletedComponentView; |
| | 25 | | private QuestStartedPopupComponentView questStartedPopupComponentView; |
| | 26 | | private QuestLogComponentView questLogComponentView; |
| | 27 | | private QuestsService questService; |
| | 28 | |
|
| | 29 | | private CancellationTokenSource cts; |
| | 30 | |
|
| 0 | 31 | | public QuestsPlugin(IAddressableResourceProvider resourceProvider) |
| | 32 | | { |
| 0 | 33 | | this.resourceProvider = resourceProvider; |
| 0 | 34 | | cts = new CancellationTokenSource(); |
| | 35 | |
|
| 0 | 36 | | InstantiateUIs(cts).Forget(); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | private async UniTaskVoid InstantiateUIs(CancellationTokenSource cts) |
| | 40 | | { |
| 0 | 41 | | questService = new QuestsService(await GetClientQuestsService(), new QuestRewardsResolver()); |
| 0 | 42 | | await UniTask.SwitchToMainThread(cts.Token); |
| | 43 | |
|
| 0 | 44 | | questTrackerComponentView = await resourceProvider.Instantiate<QuestTrackerComponentView>(QUEST_TRACKER_HUD, $"_ |
| 0 | 45 | | questCompletedComponentView = await resourceProvider.Instantiate<QuestCompletedComponentView>(QUEST_COMPLETED_HU |
| 0 | 46 | | questStartedPopupComponentView = await resourceProvider.Instantiate<QuestStartedPopupComponentView>(QUEST_STARTE |
| 0 | 47 | | questLogComponentView = await resourceProvider.Instantiate<QuestLogComponentView>(QUEST_LOG, $"_{QUEST_LOG}", ca |
| | 48 | |
|
| 0 | 49 | | questLogComponentView.gameObject.SetActive(false); |
| 0 | 50 | | DataStore.i.Quests.isInitialized.Set(true); |
| | 51 | |
|
| 0 | 52 | | QuestsController controller = new QuestsController( |
| | 53 | | questService, |
| | 54 | | questTrackerComponentView, |
| | 55 | | questCompletedComponentView, |
| | 56 | | questStartedPopupComponentView, |
| | 57 | | userProfileBridge, |
| | 58 | | new DefaultPlayerPrefs(), |
| | 59 | | DataStore.i, |
| | 60 | | Environment.i.world.teleportController, |
| | 61 | | new QuestLogController(questLogComponentView, userProfileBridge, questService), |
| | 62 | | new QuestAnalyticsService(Environment.i.platform.serviceProviders.analytics)); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public async UniTask<ClientQuestsService> GetClientQuestsService() |
| | 66 | | { |
| 0 | 67 | | var rpc = Environment.i.serviceLocator.Get<IRPC>(); |
| 0 | 68 | | await rpc.EnsureRpc(); |
| 0 | 69 | | var rpcSignRequest = new RPCSignRequest(rpc); |
| | 70 | |
|
| 0 | 71 | | AuthedWebSocketClientTransport webSocketClientTransport = new AuthedWebSocketClientTransport(rpcSignRequest, "ws |
| | 72 | |
|
| | 73 | | //TODO Quest Server is not accepting the correct url and by now it needs "/". Change it as soon as QuestServer i |
| 0 | 74 | | await webSocketClientTransport.Connect("/"); |
| | 75 | |
|
| 0 | 76 | | RpcClient rpcClient = new RpcClient(webSocketClientTransport); |
| 0 | 77 | | var clientPort = await rpcClient.CreatePort("Unity_QuestsService"); |
| 0 | 78 | | var module = await clientPort.LoadModule(QuestsServiceCodeGen.ServiceName); |
| 0 | 79 | | return new ClientQuestsService(module); |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | public void Dispose() |
| | 83 | | { |
| 0 | 84 | | cts?.Cancel(); |
| 0 | 85 | | cts?.Dispose(); |
| 0 | 86 | | cts = null; |
| 0 | 87 | | } |
| | 88 | | } |