pokerogue/src/account.ts

51 lines
2.0 KiB
TypeScript
Raw Normal View History

[Refactor] Pokerogue API client (#4583) * start migrating Utils.apiFetch to api class * move dailyranking to api * use api in title-ui-handler * remove: Utils.apiFetch * migrate `updateSystemSavedata` to api * migrate clear session savedata to api * migrate updateAllSavedata to api * migrate `updateSessionSavedata` to api * rename `api` to `pokerogue-api` * migrate unlink discord to pokerogue-api * migrate unlink google to pokerogue-api * update pokerogue-api login * migrate register account to pokerogue-api * remove Utils.apiPost * reset overrides.ts * chore: cleanup * fix env.development * fix circular dependencies with api * fix gamedata verify missing await * fix daily api calls in daily-run-scorebard * fix discord-link request body being empty there was a double `toUrlSearchParams()` call involved * add pokerogue-api test coverge * add test-utils `getApiBaseUrl()` method * add pokerogue-admin-api test coverage * add pokerogue-account-api test coverage * add pokerogue-daily-api test coverage * add pokerogue-savedata-api test coverage * fix some test describes * add pokerogue-session-savedata-api test coverage * add pokerogue-system-savedata-api test coverage * fix tests * fix tryExportData thanks @MokaStitcher * chore: fix menu-ui-handlers.ts * fix admin-ui-handler (types) * extend test-coverage for admin-api * remove outdated code * skip some clowning-around-encounter tests if events are active this is not a permanent solution * Update src/system/game-data.ts Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com> * Revert "skip some clowning-around-encounter tests if events are active" This reverts commit a97dafe8b2479e9b2ddd49d4dc9710814d7c7b67. * mark `localServerUrl` and `apiUrl` as deprecated in `utils.ts` --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>
2024-11-04 12:57:21 -08:00
import { pokerogueApi } from "#app/plugins/api/pokerogue-api";
import type { UserInfo } from "#app/@types/UserInfo";
2023-12-30 18:41:25 -05:00
import { bypassLogin } from "./battle-scene";
import * as Utils from "./utils";
[Refactor] use typescript `strict-null` (#3259) * TS: enable strict-null * fix battle-scene.ts * fix voucher.ts * adapt more files to strict-null * adapt more files to strict-null ( 2) * adapt ability.ts to strict-null * adapt `arena.ts` to strict-null * adapt TagAddedEvent constructor to strict-null * adapt phases.ts.to strict-null * adapt status-effect.ts to strict-null * adapt `account.ts` to strict-null * adapt `configHandler.ts` to strict-null * adapt `ability.ts` to strict-null * adapt `biomes.ts` to strict-null * adapt `challenge.ts` to strict-null * adapt `daily-run.ts` to strict-null * adapt `nature.ts` to strict-null * adapt `pokemon-forms.ts` to strict-null * adapt `tainer-names.ts` to strict-null * adapt `types.ts` to strict-null * adapt `weather.ts` to strict-null * adapt `egg-hatch-phase.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `pokemon-sprite-sparkle-handler.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `game-mode.ts` to strict-null * adapt `utils.ts` to strict-null * adapt `voucher-ui-handler.ts` to strict-null * adapt `src/ui/unavailable-modal-ui-handler.ts` to strict-null * adapt `src/ui/ui.ts` to strict-null * adapt `src/ui/ui-theme.ts` to strict-null * adapt `src/ui/title-ui-handler.ts` to strict-null * adapt `src/ui/time-of-day-widget.ts` to strict-null * adapt `src/ui/text.ts` to strict-null * adapt `src/ui/target-select-ui-handler.ts` to strict-null * adapt `src/ui/settings/settings-keyboard-ui-handler.ts` to strict-null * adapt more files to strict-null (3) * adapt more files to strict-null (4) * adapt more files (mostly tests) to strict-null (5) * adapt more files to strict-null (6) * adapt more files to strict-null (7) * Update `src/data/pokemon-evolutions.ts` for strict-null Partial update `src/data/pokemon-species.ts` for strict-null * adapt more files to strict-null (8) * adapt more files to strict-null (9) * Strict some more nulls (still a few errors remaining) * adapt rest of the files to strict-null (9) * fix tests (check for null instead of undefined) * repalce a lot of `??` with bangs And added TODO notice as usual * fix more tests * all tests pass now * fix broken game-loop after trainer battle add some console.warn for missing cases and falling back to default * remove guessed fallback from utils.rgbHexToRgba * add TODO for this.currentBattle = null * adjust getPokemonById() return to include `null` * fix compilation errors * add test for pokemon.trySetStatus * `chanceMultiplier` shouldn't be optional * allow `null` for currentPhase * adjust hasExpSprite logic for no keymatch found * reduce bang usage in account.updateUserInfo() * fix new strict-null issues after merge * fix `strict-null` issues in dropdown.ts and sand_spit.test.ts * fix egg-gacha * adapt gul_missile.test.ts to strict-null * fix move.ts strict-null * fix i18n.ts strict-null * fix strict-null issues * fix baton_pass test after accidentially breaking it * chore: fix compiler errors * revert accidential changes in baton_pass.test.ts --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2024-08-07 09:23:12 -07:00
export let loggedInUser: UserInfo | null = null;
// 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
export const clientSessionId = Utils.randomString(32);
2023-12-30 18:41:25 -05:00
export function initLoggedInUser(): void {
loggedInUser = { username: "Guest", lastSessionSlot: -1, discordId: "", googleId: "", hasAdminRole: false };
}
2024-04-10 01:32:49 -04:00
export function updateUserInfo(): Promise<[boolean, integer]> {
return new Promise<[boolean, integer]>(resolve => {
2023-12-30 18:41:25 -05:00
if (bypassLogin) {
2024-10-04 13:08:31 +08:00
loggedInUser = { username: "Guest", lastSessionSlot: -1, discordId: "", googleId: "", hasAdminRole: false };
let lastSessionSlot = -1;
2024-05-15 10:55:17 -04:00
for (let s = 0; s < 5; s++) {
if (localStorage.getItem(`sessionData${s ? s : ""}_${loggedInUser.username}`)) {
lastSessionSlot = s;
break;
}
}
2024-05-15 01:42:36 -04:00
loggedInUser.lastSessionSlot = lastSessionSlot;
2024-05-15 10:55:17 -04:00
// Migrate old data from before the username was appended
[ "data", "sessionData", "sessionData1", "sessionData2", "sessionData3", "sessionData4" ].map(d => {
[Refactor] use typescript `strict-null` (#3259) * TS: enable strict-null * fix battle-scene.ts * fix voucher.ts * adapt more files to strict-null * adapt more files to strict-null ( 2) * adapt ability.ts to strict-null * adapt `arena.ts` to strict-null * adapt TagAddedEvent constructor to strict-null * adapt phases.ts.to strict-null * adapt status-effect.ts to strict-null * adapt `account.ts` to strict-null * adapt `configHandler.ts` to strict-null * adapt `ability.ts` to strict-null * adapt `biomes.ts` to strict-null * adapt `challenge.ts` to strict-null * adapt `daily-run.ts` to strict-null * adapt `nature.ts` to strict-null * adapt `pokemon-forms.ts` to strict-null * adapt `tainer-names.ts` to strict-null * adapt `types.ts` to strict-null * adapt `weather.ts` to strict-null * adapt `egg-hatch-phase.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `pokemon-sprite-sparkle-handler.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `game-mode.ts` to strict-null * adapt `utils.ts` to strict-null * adapt `voucher-ui-handler.ts` to strict-null * adapt `src/ui/unavailable-modal-ui-handler.ts` to strict-null * adapt `src/ui/ui.ts` to strict-null * adapt `src/ui/ui-theme.ts` to strict-null * adapt `src/ui/title-ui-handler.ts` to strict-null * adapt `src/ui/time-of-day-widget.ts` to strict-null * adapt `src/ui/text.ts` to strict-null * adapt `src/ui/target-select-ui-handler.ts` to strict-null * adapt `src/ui/settings/settings-keyboard-ui-handler.ts` to strict-null * adapt more files to strict-null (3) * adapt more files to strict-null (4) * adapt more files (mostly tests) to strict-null (5) * adapt more files to strict-null (6) * adapt more files to strict-null (7) * Update `src/data/pokemon-evolutions.ts` for strict-null Partial update `src/data/pokemon-species.ts` for strict-null * adapt more files to strict-null (8) * adapt more files to strict-null (9) * Strict some more nulls (still a few errors remaining) * adapt rest of the files to strict-null (9) * fix tests (check for null instead of undefined) * repalce a lot of `??` with bangs And added TODO notice as usual * fix more tests * all tests pass now * fix broken game-loop after trainer battle add some console.warn for missing cases and falling back to default * remove guessed fallback from utils.rgbHexToRgba * add TODO for this.currentBattle = null * adjust getPokemonById() return to include `null` * fix compilation errors * add test for pokemon.trySetStatus * `chanceMultiplier` shouldn't be optional * allow `null` for currentPhase * adjust hasExpSprite logic for no keymatch found * reduce bang usage in account.updateUserInfo() * fix new strict-null issues after merge * fix `strict-null` issues in dropdown.ts and sand_spit.test.ts * fix egg-gacha * adapt gul_missile.test.ts to strict-null * fix move.ts strict-null * fix i18n.ts strict-null * fix strict-null issues * fix baton_pass test after accidentially breaking it * chore: fix compiler errors * revert accidential changes in baton_pass.test.ts --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2024-08-07 09:23:12 -07: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);
}
[Refactor] use typescript `strict-null` (#3259) * TS: enable strict-null * fix battle-scene.ts * fix voucher.ts * adapt more files to strict-null * adapt more files to strict-null ( 2) * adapt ability.ts to strict-null * adapt `arena.ts` to strict-null * adapt TagAddedEvent constructor to strict-null * adapt phases.ts.to strict-null * adapt status-effect.ts to strict-null * adapt `account.ts` to strict-null * adapt `configHandler.ts` to strict-null * adapt `ability.ts` to strict-null * adapt `biomes.ts` to strict-null * adapt `challenge.ts` to strict-null * adapt `daily-run.ts` to strict-null * adapt `nature.ts` to strict-null * adapt `pokemon-forms.ts` to strict-null * adapt `tainer-names.ts` to strict-null * adapt `types.ts` to strict-null * adapt `weather.ts` to strict-null * adapt `egg-hatch-phase.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `pokemon-sprite-sparkle-handler.ts` to strict-null * adapt `evolution-phase.ts` to strict-null * adapt `game-mode.ts` to strict-null * adapt `utils.ts` to strict-null * adapt `voucher-ui-handler.ts` to strict-null * adapt `src/ui/unavailable-modal-ui-handler.ts` to strict-null * adapt `src/ui/ui.ts` to strict-null * adapt `src/ui/ui-theme.ts` to strict-null * adapt `src/ui/title-ui-handler.ts` to strict-null * adapt `src/ui/time-of-day-widget.ts` to strict-null * adapt `src/ui/text.ts` to strict-null * adapt `src/ui/target-select-ui-handler.ts` to strict-null * adapt `src/ui/settings/settings-keyboard-ui-handler.ts` to strict-null * adapt more files to strict-null (3) * adapt more files to strict-null (4) * adapt more files (mostly tests) to strict-null (5) * adapt more files to strict-null (6) * adapt more files to strict-null (7) * Update `src/data/pokemon-evolutions.ts` for strict-null Partial update `src/data/pokemon-species.ts` for strict-null * adapt more files to strict-null (8) * adapt more files to strict-null (9) * Strict some more nulls (still a few errors remaining) * adapt rest of the files to strict-null (9) * fix tests (check for null instead of undefined) * repalce a lot of `??` with bangs And added TODO notice as usual * fix more tests * all tests pass now * fix broken game-loop after trainer battle add some console.warn for missing cases and falling back to default * remove guessed fallback from utils.rgbHexToRgba * add TODO for this.currentBattle = null * adjust getPokemonById() return to include `null` * fix compilation errors * add test for pokemon.trySetStatus * `chanceMultiplier` shouldn't be optional * allow `null` for currentPhase * adjust hasExpSprite logic for no keymatch found * reduce bang usage in account.updateUserInfo() * fix new strict-null issues after merge * fix `strict-null` issues in dropdown.ts and sand_spit.test.ts * fix egg-gacha * adapt gul_missile.test.ts to strict-null * fix move.ts strict-null * fix i18n.ts strict-null * fix strict-null issues * fix baton_pass test after accidentially breaking it * chore: fix compiler errors * revert accidential changes in baton_pass.test.ts --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com>
2024-08-07 09:23:12 -07:00
localStorage.setItem(`${d}_${loggedInUser.username}`, lsItem);
2024-05-15 10:55:17 -04:00
localStorage.removeItem(d);
}
});
2024-04-10 01:32:49 -04:00
return resolve([ true, 200 ]);
2023-12-30 18:41:25 -05:00
}
[Refactor] Pokerogue API client (#4583) * start migrating Utils.apiFetch to api class * move dailyranking to api * use api in title-ui-handler * remove: Utils.apiFetch * migrate `updateSystemSavedata` to api * migrate clear session savedata to api * migrate updateAllSavedata to api * migrate `updateSessionSavedata` to api * rename `api` to `pokerogue-api` * migrate unlink discord to pokerogue-api * migrate unlink google to pokerogue-api * update pokerogue-api login * migrate register account to pokerogue-api * remove Utils.apiPost * reset overrides.ts * chore: cleanup * fix env.development * fix circular dependencies with api * fix gamedata verify missing await * fix daily api calls in daily-run-scorebard * fix discord-link request body being empty there was a double `toUrlSearchParams()` call involved * add pokerogue-api test coverge * add test-utils `getApiBaseUrl()` method * add pokerogue-admin-api test coverage * add pokerogue-account-api test coverage * add pokerogue-daily-api test coverage * add pokerogue-savedata-api test coverage * fix some test describes * add pokerogue-session-savedata-api test coverage * add pokerogue-system-savedata-api test coverage * fix tests * fix tryExportData thanks @MokaStitcher * chore: fix menu-ui-handlers.ts * fix admin-ui-handler (types) * extend test-coverage for admin-api * remove outdated code * skip some clowning-around-encounter tests if events are active this is not a permanent solution * Update src/system/game-data.ts Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com> * Revert "skip some clowning-around-encounter tests if events are active" This reverts commit a97dafe8b2479e9b2ddd49d4dc9710814d7c7b67. * mark `localServerUrl` and `apiUrl` as deprecated in `utils.ts` --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>
2024-11-04 12:57:21 -08:00
pokerogueApi.account.getInfo().then(([ accountInfo, status ]) => {
if (!accountInfo) {
resolve([ false, status ]);
2023-12-30 18:41:25 -05:00
return;
[Refactor] Pokerogue API client (#4583) * start migrating Utils.apiFetch to api class * move dailyranking to api * use api in title-ui-handler * remove: Utils.apiFetch * migrate `updateSystemSavedata` to api * migrate clear session savedata to api * migrate updateAllSavedata to api * migrate `updateSessionSavedata` to api * rename `api` to `pokerogue-api` * migrate unlink discord to pokerogue-api * migrate unlink google to pokerogue-api * update pokerogue-api login * migrate register account to pokerogue-api * remove Utils.apiPost * reset overrides.ts * chore: cleanup * fix env.development * fix circular dependencies with api * fix gamedata verify missing await * fix daily api calls in daily-run-scorebard * fix discord-link request body being empty there was a double `toUrlSearchParams()` call involved * add pokerogue-api test coverge * add test-utils `getApiBaseUrl()` method * add pokerogue-admin-api test coverage * add pokerogue-account-api test coverage * add pokerogue-daily-api test coverage * add pokerogue-savedata-api test coverage * fix some test describes * add pokerogue-session-savedata-api test coverage * add pokerogue-system-savedata-api test coverage * fix tests * fix tryExportData thanks @MokaStitcher * chore: fix menu-ui-handlers.ts * fix admin-ui-handler (types) * extend test-coverage for admin-api * remove outdated code * skip some clowning-around-encounter tests if events are active this is not a permanent solution * Update src/system/game-data.ts Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com> * Revert "skip some clowning-around-encounter tests if events are active" This reverts commit a97dafe8b2479e9b2ddd49d4dc9710814d7c7b67. * mark `localServerUrl` and `apiUrl` as deprecated in `utils.ts` --------- Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com>
2024-11-04 12:57:21 -08:00
} else {
loggedInUser = accountInfo;
resolve([ true, 200 ]);
2023-12-30 18:41:25 -05:00
}
});
});
}