forked from pagefaultgames/pokerogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.ts
38 lines (35 loc) · 1.01 KB
/
account.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { bypassLogin } from "./battle-scene";
import * as Utils from "./utils";
export interface UserInfo {
username: string;
lastSessionSlot: integer;
}
export let loggedInUser: UserInfo = null;
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 : ''}`)) {
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 ]);
});
});
}