diff --git a/src/plugins/api/api.ts b/src/plugins/api/api.ts index 20848054b21..796f7d1b830 100644 --- a/src/plugins/api/api.ts +++ b/src/plugins/api/api.ts @@ -1,5 +1,7 @@ +import type { PokerogueApiClearSessionData } from "#app/@types/pokerogue-api"; import { loggedInUser } from "#app/account"; import { SESSION_ID_COOKIE_NAME } from "#app/constants"; +import type { SessionSaveData } from "#app/system/game-data"; import type { RankingEntry, ScoreboardCategory } from "#app/ui/daily-run-scoreboard"; import { getCookie, removeCookie, setCookie } from "#app/utils"; import type { AccountInfoResponse } from "./models/AccountInfo"; @@ -251,6 +253,42 @@ export class Api { } } + /** + * Clears the session savedata of the given slot. + * @param slotId The slot ID + * @param trainerId The trainer ID + * @param sessionId The session ID + * @param sessionData The {@linkcode SessionSaveData} object + */ + public async clearSessionSavedata( + slotId: number, + trainerId: number, + sessionId: string, + sessionData: SessionSaveData + ) { + try { + const params = new URLSearchParams(); + params.append("slot", String(slotId)); + params.append("trainerId", String(trainerId)); + params.append("clientSessionId", sessionId); + + const response = await this.doPost(`/savedata/session/clear?${params}`, sessionData); + + if (response.ok) { + if (loggedInUser) { + loggedInUser!.lastSessionSlot = -1; + } + localStorage.removeItem(`sessionData${slotId > 0 ? slotId : ""}_${loggedInUser?.username}`); + } + + return (await response.json()) as PokerogueApiClearSessionData; + } catch (err) { + console.warn("Could not clear session savedata!", err); + } + + return null; + } + /** * Get the daily rankings for a {@linkcode ScoreboardCategory}. * @param category The {@linkcode ScoreboardCategory} to fetch. diff --git a/src/system/game-data.ts b/src/system/game-data.ts index f3f32b7805b..4123b9af1c3 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -49,7 +49,6 @@ import { RUN_HISTORY_LIMIT } from "#app/ui/run-history-ui-handler"; import { applySessionDataPatches, applySettingsDataPatches, applySystemDataPatches } from "#app/system/version-converter"; import { MysteryEncounterSaveData } from "#app/data/mystery-encounters/mystery-encounter-save-data"; import { MysteryEncounterType } from "#enums/mystery-encounter-type"; -import { PokerogueApiClearSessionData } from "#app/@types/pokerogue-api"; import { api } from "#app/plugins/api/api"; export const defaultStarterSpecies: Species[] = [ @@ -1190,17 +1189,10 @@ export class GameData { result = [true, true]; } else { const sessionData = this.getSessionSaveData(scene); - const response = await Utils.apiPost(`savedata/session/clear?slot=${slotId}&trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`, JSON.stringify(sessionData), undefined, true); + const jsonResponse = await api.clearSessionSavedata(slotId, this.trainerId, clientSessionId, sessionData); - if (response.ok) { - loggedInUser!.lastSessionSlot = -1; // TODO: is the bang correct? - localStorage.removeItem(`sessionData${this.scene.sessionSlotId ? this.scene.sessionSlotId : ""}_${loggedInUser?.username}`); - } - - const jsonResponse: PokerogueApiClearSessionData = await response.json(); - - if (!jsonResponse.error) { - result = [true, jsonResponse.success ?? false]; + if (!jsonResponse?.error) { + result = [true, jsonResponse?.success ?? false]; } else { if (jsonResponse && jsonResponse.error?.startsWith("session out of date")) { this.scene.clearPhaseQueue();