| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Interface; |
| | 5 | | using DCL.NotificationModel; |
| | 6 | | using DCL.Tasks; |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Threading; |
| | 10 | | using UnityEngine; |
| | 11 | | using Type = System.Type; |
| | 12 | |
|
| | 13 | | namespace DCL.LoadingScreen |
| | 14 | | { |
| | 15 | | public class LoadingScreenTimeoutController : IDisposable |
| | 16 | | { |
| | 17 | | private const int LOAD_SCENE_TIMEOUT = 120000; |
| | 18 | | private const int WEBSOCKET_TIMEOUT = 15000; |
| | 19 | |
|
| | 20 | | internal int currentEvaluatedTimeout; |
| | 21 | |
|
| | 22 | | private LoadingScreenController loadingScreenController; |
| | 23 | | private CancellationTokenSource timeoutCTS; |
| | 24 | | internal ILoadingScreenTimeoutView view; |
| | 25 | | private IWorldState worldState; |
| | 26 | | private Vector2Int currentDestination; |
| | 27 | |
|
| | 28 | | internal bool goHomeRequested; |
| | 29 | |
|
| 0 | 30 | | public LoadingScreenTimeoutController(ILoadingScreenTimeoutView loadingScreenTimeoutView, IWorldState worldState |
| | 31 | | { |
| 0 | 32 | | this.view = loadingScreenTimeoutView; |
| 0 | 33 | | this.loadingScreenController = loadingScreenController; |
| | 34 | |
|
| 0 | 35 | | view.OnExitButtonClicked += ExitClicked; |
| 0 | 36 | | view.OnJumpHomeButtonClicked += GoBackHomeClicked; |
| | 37 | |
|
| 0 | 38 | | this.worldState = worldState; |
| | 39 | |
|
| 0 | 40 | | currentEvaluatedTimeout = Application.platform == RuntimePlatform.WebGLPlayer ? LOAD_SCENE_TIMEOUT : WEBSOCK |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | private async UniTaskVoid StartTimeoutCounter(CancellationToken ct) |
| | 44 | | { |
| 0 | 45 | | if (!await UniTask.Delay(TimeSpan.FromMilliseconds(currentEvaluatedTimeout), cancellationToken: ct).Suppress |
| 0 | 46 | | DoTimeout(); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | private void DoTimeout() |
| | 50 | | { |
| 0 | 51 | | IParcelScene destinationScene = worldState.GetScene(worldState.GetSceneNumberByCoords(currentDestination)); |
| | 52 | |
|
| 0 | 53 | | if (destinationScene != null) |
| | 54 | | { |
| 0 | 55 | | Dictionary<string, string> variables = new Dictionary<string, string> |
| | 56 | | { |
| | 57 | | { "sceneID", destinationScene.sceneData?.id }, |
| | 58 | | { "contentServer", destinationScene.contentProvider?.baseUrl }, |
| | 59 | | { "contentServerBundlesUrl", destinationScene.contentProvider?.assetBundlesBaseUrl }, |
| | 60 | | }; |
| | 61 | |
|
| 0 | 62 | | GenericAnalytics.SendAnalytic("scene_loading_failed", variables); |
| | 63 | | } |
| | 64 | |
|
| 0 | 65 | | if (goHomeRequested) |
| 0 | 66 | | loadingScreenController.RandomPositionRequested(); |
| | 67 | | else |
| 0 | 68 | | view.ShowSceneTimeout(); |
| | 69 | |
|
| 0 | 70 | | goHomeRequested = false; |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public void StartTimeout(Vector2Int newDestination) |
| | 74 | | { |
| 0 | 75 | | timeoutCTS = timeoutCTS.SafeRestart(); |
| 0 | 76 | | currentDestination = newDestination; |
| 0 | 77 | | StartTimeoutCounter(timeoutCTS.Token).Forget(); |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void StopTimeout() |
| | 81 | | { |
| | 82 | | //Once the websocket has connected and the first fadeout has been done, its always LOAD_SCENE_TIMEOUT |
| 0 | 83 | | currentEvaluatedTimeout = LOAD_SCENE_TIMEOUT; |
| | 84 | |
|
| 0 | 85 | | view.HideSceneTimeout(); |
| 0 | 86 | | timeoutCTS.SafeCancelAndDispose(); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private void ExitClicked() |
| | 90 | | { |
| 0 | 91 | | Utils.QuitApplication(); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | internal void GoBackHomeClicked() |
| | 95 | | { |
| 0 | 96 | | WebInterface.SendChatMessage(new ChatMessage |
| | 97 | | { |
| | 98 | | messageType = ChatMessage.Type.NONE, |
| | 99 | | recipient = string.Empty, |
| | 100 | | body = "/goto home", |
| | 101 | | }); |
| 0 | 102 | | goHomeRequested = true; |
| 0 | 103 | | view.HideSceneTimeout(); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public void Dispose() |
| | 107 | | { |
| 0 | 108 | | timeoutCTS?.SafeCancelAndDispose(); |
| 0 | 109 | | view.Dispose(); |
| 0 | 110 | | } |
| | 111 | | } |
| | 112 | | } |