pokerogue/src/account.ts

40 lines
1.1 KiB
TypeScript
Raw Normal View History

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