migrate clear session savedata to api

This commit is contained in:
flx-sta 2024-10-03 17:28:02 -07:00
parent 9f63360484
commit ebf897822e
2 changed files with 41 additions and 11 deletions

View File

@ -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<SessionSaveData>(`/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.

View File

@ -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();