pokerogue/src/account.ts
Samuel H 67c18a15e2
Implement client session and re-implement hybrid saving (#888)
* Implement client session and re-implement hybrid saving (WiP)

* Fixes for hybrid saving

* Add local data clears where applicable

* Include client session ID in system update

* Change save threshold from 5 waves to 10 waves or 5 minutes
2024-05-15 00:52:06 -04:00

39 lines
1.1 KiB
TypeScript

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