fix circular dependencies with api

This commit is contained in:
flx-sta 2024-10-04 16:07:08 -07:00
parent d927ed8e6c
commit e66bceded3
6 changed files with 29 additions and 26 deletions

View File

@ -1,4 +1,4 @@
import type { UserInfo } from "#app/account"; import type { UserInfo } from "#app/@types/UserInfo";
export interface AccountInfoResponse extends UserInfo {} export interface AccountInfoResponse extends UserInfo {}

7
src/@types/UserInfo.ts Normal file
View File

@ -0,0 +1,7 @@
export interface UserInfo {
username: string;
lastSessionSlot: number;
discordId: string;
googleId: string;
hasAdminRole: boolean;
}

View File

@ -1,15 +1,8 @@
import { bypassLogin } from "./battle-scene";
import { pokerogueApi } from "#app/plugins/api/pokerogue-api"; import { pokerogueApi } from "#app/plugins/api/pokerogue-api";
import type { UserInfo } from "#app/@types/UserInfo";
import { bypassLogin } from "./battle-scene";
import * as Utils from "./utils"; import * as Utils from "./utils";
export interface UserInfo {
username: string;
lastSessionSlot: number;
discordId: string;
googleId: string;
hasAdminRole: boolean;
}
export let loggedInUser: UserInfo | null = null; 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 // 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); export const clientSessionId = Utils.randomString(32);

View File

@ -6,7 +6,6 @@ import type {
NewClearSessionSavedataRequest, NewClearSessionSavedataRequest,
UpdateSessionSavedataRequest, UpdateSessionSavedataRequest,
} from "#app/@types/PokerogueSessionSavedataApi"; } from "#app/@types/PokerogueSessionSavedataApi";
import { loggedInUser } from "#app/account";
import { ApiBase } from "#app/plugins/api/api-base"; import { ApiBase } from "#app/plugins/api/api-base";
import type { SessionSaveData } from "#app/system/game-data"; import type { SessionSaveData } from "#app/system/game-data";
@ -74,7 +73,7 @@ export class PokerogueSessionSavedataApi extends ApiBase {
/** /**
* Delete a session savedata slot. * Delete a session savedata slot.
* @param params The {@linkcode DeleteSessionSavedataRequest} to send * @param params The {@linkcode DeleteSessionSavedataRequest} to send
* @returns The session as `string` * @returns An error message if something went wrong
*/ */
public async delete(params: DeleteSessionSavedataRequest) { public async delete(params: DeleteSessionSavedataRequest) {
try { try {
@ -82,11 +81,7 @@ export class PokerogueSessionSavedataApi extends ApiBase {
const response = await this.doGet(`/savedata/session/delete?${urlSearchParams}`); const response = await this.doGet(`/savedata/session/delete?${urlSearchParams}`);
if (response.ok) { if (response.ok) {
if (loggedInUser) { return null;
loggedInUser.lastSessionSlot = -1;
}
localStorage.removeItem(`sessionData${params.slot > 0 ? params.slot : ""}_${loggedInUser?.username}`);
} else { } else {
return await response.text(); return await response.text();
} }
@ -107,18 +102,14 @@ export class PokerogueSessionSavedataApi extends ApiBase {
const urlSearchParams = this.toUrlSearchParams(params); const urlSearchParams = this.toUrlSearchParams(params);
const response = await this.doPost(`/savedata/session/clear?${urlSearchParams}`, sessionData); const response = await this.doPost(`/savedata/session/clear?${urlSearchParams}`, sessionData);
if (response.ok) {
if (loggedInUser) {
loggedInUser!.lastSessionSlot = -1;
}
localStorage.removeItem(`sessionData${params.slot > 0 ? params.slot : ""}_${loggedInUser?.username}`);
}
return (await response.json()) as ClearSessionSavedataResponse; return (await response.json()) as ClearSessionSavedataResponse;
} catch (err) { } catch (err) {
console.warn("Could not clear session savedata!", err); console.warn("Could not clear session savedata!", err);
} }
return null; return {
error: "Unknown error",
success: false,
} as ClearSessionSavedataResponse;
} }
} }

View File

@ -1142,8 +1142,15 @@ export class GameData {
} }
console.error(error); console.error(error);
resolve(false); resolve(false);
} else {
if (loggedInUser) {
loggedInUser.lastSessionSlot = -1;
}
localStorage.removeItem(`sessionData${slotId > 0 ? slotId : ""}_${loggedInUser?.username}`);
resolve(true);
} }
resolve(true);
}); });
}); });
}); });
@ -1196,6 +1203,10 @@ export class GameData {
if (!jsonResponse?.error) { if (!jsonResponse?.error) {
result = [true, jsonResponse?.success ?? false]; result = [true, jsonResponse?.success ?? false];
if (loggedInUser) {
loggedInUser!.lastSessionSlot = -1;
}
localStorage.removeItem(`sessionData${slotId > 0 ? slotId : ""}_${loggedInUser?.username}`);
} else { } else {
if (jsonResponse && jsonResponse.error?.startsWith("session out of date")) { if (jsonResponse && jsonResponse.error?.startsWith("session out of date")) {
this.scene.clearPhaseQueue(); this.scene.clearPhaseQueue();

View File

@ -328,6 +328,7 @@ export async function localPing() {
if (isLocal) { if (isLocal) {
const titleStats = await pokerogueApi.getGameTitleStats(); const titleStats = await pokerogueApi.getGameTitleStats();
isLocalServerConnected = !!titleStats; isLocalServerConnected = !!titleStats;
console.log("isLocalServerConnected:", isLocalServerConnected);
} }
} }