From c2bc94a5f3e8bf587e082ab591b70160c6c3afaa Mon Sep 17 00:00:00 2001 From: Benjamin Odom Date: Thu, 23 May 2024 16:19:36 -0500 Subject: [PATCH] Increases the Amount of Time Until a Reconnect up to a Maximum (#1295) * Update unavailable-modal-ui-handler.ts * Update unavailable-modal-ui-handler.ts * Update unavailable-modal-ui-handler.ts --- src/ui/unavailable-modal-ui-handler.ts | 38 +++++++++++++++++++------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/src/ui/unavailable-modal-ui-handler.ts b/src/ui/unavailable-modal-ui-handler.ts index da2393f3b0d..102d98429e8 100644 --- a/src/ui/unavailable-modal-ui-handler.ts +++ b/src/ui/unavailable-modal-ui-handler.ts @@ -6,10 +6,17 @@ import { updateUserInfo } from "#app/account"; export default class UnavailableModalUiHandler extends ModalUiHandler { private reconnectTimer: number; + private reconnectInterval: number; private reconnectCallback: () => void; + private readonly minTime = 1000 * 5; + private readonly maxTime = 1000 * 60 * 5; + + private readonly randVarianceTime = 1000 * 10; + constructor(scene: BattleScene, mode?: Mode) { super(scene, mode); + this.reconnectInterval = this.minTime; } getModalTitle(): string { @@ -41,6 +48,26 @@ export default class UnavailableModalUiHandler extends ModalUiHandler { this.modalContainer.add(label); } + tryReconnect(): void { + updateUserInfo().then(response => { + if (response[0] || [200, 400].includes(response[1])) { + clearInterval(this.reconnectTimer); + this.reconnectTimer = null; + this.reconnectInterval = this.minTime; + this.scene.playSound("pb_bounce_1"); + this.reconnectCallback(); + } else { + clearInterval(this.reconnectTimer); + this.reconnectInterval = Math.min(this.reconnectInterval * 2, this.maxTime); // Set a max delay so it isn't infinite + this.reconnectTimer = + setTimeout( + () => this.tryReconnect(), + // Adds a random factor to avoid pendulum effect during long total breakdown + this.reconnectInterval + (Math.random() * this.randVarianceTime)); + } + }); + } + show(args: any[]): boolean { if (args.length >= 1 && args[0] instanceof Function) { const config: ModalConfig = { @@ -49,16 +76,7 @@ export default class UnavailableModalUiHandler extends ModalUiHandler { this.reconnectCallback = args[0]; - this.reconnectTimer = setInterval(() => { - updateUserInfo().then(response => { - if (response[0] || [200, 400].includes(response[1])) { - clearInterval(this.reconnectTimer); - this.reconnectTimer = null; - this.scene.playSound("pb_bounce_1"); - this.reconnectCallback(); - } - }); - }, 5000); + this.reconnectTimer = setInterval(() => this.tryReconnect(), this.reconnectInterval); return super.show([ config ]); }