2024-04-10 01:32:49 -04:00
import BattleScene from "../battle-scene" ;
import { ModalConfig , ModalUiHandler } from "./modal-ui-handler" ;
import { addTextObject , TextStyle } from "./text" ;
import { Mode } from "./ui" ;
import { updateUserInfo } from "#app/account" ;
export default class UnavailableModalUiHandler extends ModalUiHandler {
private reconnectTimer : number ;
2024-05-24 17:57:49 -05:00
private reconnectDuration : number ;
2024-04-10 01:32:49 -04:00
private reconnectCallback : ( ) = > void ;
2024-05-23 16:19:36 -05:00
private readonly minTime = 1000 * 5 ;
private readonly maxTime = 1000 * 60 * 5 ;
private readonly randVarianceTime = 1000 * 10 ;
2024-04-10 01:32:49 -04:00
constructor ( scene : BattleScene , mode? : Mode ) {
super ( scene , mode ) ;
2024-05-24 17:57:49 -05:00
this . reconnectDuration = this . minTime ;
2024-04-10 01:32:49 -04:00
}
getModalTitle ( ) : string {
2024-05-23 17:03:10 +02:00
return "" ;
2024-04-10 01:32:49 -04:00
}
getWidth ( ) : number {
return 160 ;
}
getHeight ( ) : number {
return 64 ;
}
getMargin ( ) : [ number , number , number , number ] {
return [ 0 , 0 , 48 , 0 ] ;
}
getButtonLabels ( ) : string [ ] {
return [ ] ;
}
setup ( ) : void {
super . setup ( ) ;
2024-05-23 17:03:10 +02:00
const label = addTextObject ( this . scene , this . getWidth ( ) / 2 , this . getHeight ( ) / 2 , "Oops! There was an issue contacting the server.\n\nYou may leave this window open,\nthe game will automatically reconnect." , TextStyle . WINDOW , { fontSize : "48px" , align : "center" } ) ;
2024-04-10 01:32:49 -04:00
label . setOrigin ( 0.5 , 0.5 ) ;
this . modalContainer . add ( label ) ;
}
2024-05-23 16:19:36 -05:00
tryReconnect ( ) : void {
updateUserInfo ( ) . then ( response = > {
if ( response [ 0 ] || [ 200 , 400 ] . includes ( response [ 1 ] ) ) {
this . reconnectTimer = null ;
2024-05-24 17:57:49 -05:00
this . reconnectDuration = this . minTime ;
2024-05-23 16:19:36 -05:00
this . scene . playSound ( "pb_bounce_1" ) ;
this . reconnectCallback ( ) ;
} else {
2024-05-24 17:57:49 -05:00
this . reconnectDuration = Math . min ( this . reconnectDuration * 2 , this . maxTime ) ; // Set a max delay so it isn't infinite
2024-05-24 01:45:04 +02:00
this . reconnectTimer =
2024-05-23 16:19:36 -05:00
setTimeout (
2024-05-24 01:45:04 +02:00
( ) = > this . tryReconnect ( ) ,
2024-05-23 16:19:36 -05:00
// Adds a random factor to avoid pendulum effect during long total breakdown
2024-05-24 17:57:49 -05:00
this . reconnectDuration + ( Math . random ( ) * this . randVarianceTime ) ) ;
2024-05-23 16:19:36 -05:00
}
} ) ;
}
2024-04-10 01:32:49 -04:00
show ( args : any [ ] ) : boolean {
if ( args . length >= 1 && args [ 0 ] instanceof Function ) {
const config : ModalConfig = {
buttonActions : [ ]
} ;
this . reconnectCallback = args [ 0 ] ;
2024-05-24 17:57:49 -05:00
this . reconnectDuration = this . minTime ;
this . reconnectTimer = setTimeout ( ( ) = > this . tryReconnect ( ) , this . reconnectDuration ) ;
2024-04-10 01:32:49 -04:00
return super . show ( [ config ] ) ;
}
return false ;
}
2024-05-23 17:03:10 +02:00
}