2023-12-30 15:41:25 -08:00
|
|
|
import { bypassLogin } from "./battle-scene";
|
|
|
|
import * as Utils from "./utils";
|
|
|
|
|
2023-12-31 15:30:37 -08:00
|
|
|
export interface UserInfo {
|
|
|
|
username: string;
|
2024-03-14 18:49:49 -07:00
|
|
|
lastSessionSlot: integer;
|
2023-12-31 15:30:37 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export let loggedInUser: UserInfo = null;
|
2023-12-30 15:41:25 -08:00
|
|
|
|
2024-04-09 22:32:49 -07:00
|
|
|
export function updateUserInfo(): Promise<[boolean, integer]> {
|
|
|
|
return new Promise<[boolean, integer]>(resolve => {
|
2023-12-30 15:41:25 -08:00
|
|
|
if (bypassLogin) {
|
2024-03-14 18:49:49 -07:00
|
|
|
let lastSessionSlot = -1;
|
|
|
|
for (let s = 0; s < 2; s++) {
|
|
|
|
if (localStorage.getItem(`sessionData${s ? s : ''}`)) {
|
|
|
|
lastSessionSlot = s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loggedInUser = { username: 'Guest', lastSessionSlot: lastSessionSlot };
|
2024-04-09 22:32:49 -07:00
|
|
|
return resolve([ true, 200 ]);
|
2023-12-30 15:41:25 -08:00
|
|
|
}
|
|
|
|
Utils.apiFetch('account/info').then(response => {
|
|
|
|
if (!response.ok) {
|
2024-04-09 22:32:49 -07:00
|
|
|
resolve([ false, response.status ]);
|
2023-12-30 15:41:25 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
}).then(jsonResponse => {
|
|
|
|
loggedInUser = jsonResponse;
|
2024-04-09 22:32:49 -07:00
|
|
|
resolve([ true, 200 ]);
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
resolve([ false, 500 ]);
|
2023-12-30 15:41:25 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|