2024-11-04 20:57:21 +00:00
import { pokerogueApi } from "#app/plugins/api/pokerogue-api" ;
import type { UserInfo } from "#app/@types/UserInfo" ;
2023-12-30 23:41:25 +00:00
import { bypassLogin } from "./battle-scene" ;
import * as Utils from "./utils" ;
2024-08-07 16:23:12 +00:00
export let loggedInUser : UserInfo | null = null ;
2024-06-17 01:44:07 +00:00
// This is a random string that is used to identify the client session - unique per session (tab or window) so that the game will only save on the one that the server is expecting
2024-05-15 04:52:06 +00:00
export const clientSessionId = Utils . randomString ( 32 ) ;
2023-12-30 23:41:25 +00:00
2024-06-13 12:42:25 +00:00
export function initLoggedInUser ( ) : void {
2024-08-29 09:22:01 +00:00
loggedInUser = { username : "Guest" , lastSessionSlot : - 1 , discordId : "" , googleId : "" , hasAdminRole : false } ;
2024-06-13 12:42:25 +00:00
}
2024-04-10 05:32:49 +00:00
export function updateUserInfo ( ) : Promise < [ boolean , integer ] > {
return new Promise < [ boolean , integer ] > ( resolve = > {
2023-12-30 23:41:25 +00:00
if ( bypassLogin ) {
2024-10-04 05:08:31 +00:00
loggedInUser = { username : "Guest" , lastSessionSlot : - 1 , discordId : "" , googleId : "" , hasAdminRole : false } ;
2024-03-15 01:49:49 +00:00
let lastSessionSlot = - 1 ;
2024-05-15 14:55:17 +00:00
for ( let s = 0 ; s < 5 ; s ++ ) {
2024-05-23 15:03:10 +00:00
if ( localStorage . getItem ( ` sessionData ${ s ? s : "" } _ ${ loggedInUser . username } ` ) ) {
2024-03-15 01:49:49 +00:00
lastSessionSlot = s ;
break ;
}
}
2024-05-15 05:42:36 +00:00
loggedInUser . lastSessionSlot = lastSessionSlot ;
2024-05-15 14:55:17 +00:00
// Migrate old data from before the username was appended
2024-05-23 15:03:10 +00:00
[ "data" , "sessionData" , "sessionData1" , "sessionData2" , "sessionData3" , "sessionData4" ] . map ( d = > {
2024-08-07 16:23:12 +00:00
const lsItem = localStorage . getItem ( d ) ;
if ( lsItem && ! ! loggedInUser ? . username ) {
const lsUserItem = localStorage . getItem ( ` ${ d } _ ${ loggedInUser . username } ` ) ;
if ( lsUserItem ) {
localStorage . setItem ( ` ${ d } _ ${ loggedInUser . username } _bak ` , lsUserItem ) ;
2024-05-23 15:03:10 +00:00
}
2024-08-07 16:23:12 +00:00
localStorage . setItem ( ` ${ d } _ ${ loggedInUser . username } ` , lsItem ) ;
2024-05-15 14:55:17 +00:00
localStorage . removeItem ( d ) ;
}
} ) ;
2024-04-10 05:32:49 +00:00
return resolve ( [ true , 200 ] ) ;
2023-12-30 23:41:25 +00:00
}
2024-11-04 20:57:21 +00:00
pokerogueApi . account . getInfo ( ) . then ( ( [ accountInfo , status ] ) = > {
if ( ! accountInfo ) {
resolve ( [ false , status ] ) ;
2023-12-30 23:41:25 +00:00
return ;
2024-11-04 20:57:21 +00:00
} else {
loggedInUser = accountInfo ;
resolve ( [ true , 200 ] ) ;
2023-12-30 23:41:25 +00:00
}
} ) ;
} ) ;
2024-05-23 15:03:10 +00:00
}