From 9df8a616d4a40d7e259580cfad806f45c9382440 Mon Sep 17 00:00:00 2001 From: flx-sta <50131232+flx-sta@users.noreply.github.com> Date: Fri, 4 Oct 2024 09:53:54 -0700 Subject: [PATCH] migrate `updateSessionSavedata` to api --- src/plugins/api/api.ts | 38 ++++++++++++++----- .../api/models/UpdateSessionSavedata.ts | 6 +++ .../api/models/UpdateSystemSavedata.ts | 5 +++ src/system/game-data.ts | 12 +++--- 4 files changed, 46 insertions(+), 15 deletions(-) create mode 100644 src/plugins/api/models/UpdateSessionSavedata.ts create mode 100644 src/plugins/api/models/UpdateSystemSavedata.ts diff --git a/src/plugins/api/api.ts b/src/plugins/api/api.ts index 1f4e8a4bd40..af883cec162 100644 --- a/src/plugins/api/api.ts +++ b/src/plugins/api/api.ts @@ -8,6 +8,8 @@ import type { AccountInfoResponse } from "./models/AccountInfo"; import type { AccountLoginRequest, AccountLoginResponse } from "./models/AccountLogin"; import type { TitleStatsResponse } from "./models/TitleStats"; import type { UpdateAllSavedataRequest } from "./models/UpdateAllSavedata"; +import type { UpdateSessionSavedataRequest } from "./models/UpdateSessionSavedata"; +import type { UpdateSystemSavedataRequest } from "./models/UpdateSystemSavedata"; import type { VerifySavedataResponse } from "./models/VerifySavedata"; type DataType = "json" | "form-urlencoded"; @@ -66,10 +68,7 @@ export class Api { try { const response = await this.doPost( "/account/login", - { - username, - password, - }, + { username, password }, "form-urlencoded" ); @@ -186,17 +185,17 @@ export class Api { /** * Update a system savedata. - * @param clientSessionId The savedata session ID + * @param updateData The {@linkcode UpdateSystemSavedataRequest} to send * @param rawSystemData The raw {@linkcode SystemSaveData} * @returns an error message if something went wrong */ - public async updateSystemSavedata(clientSessionId: string, rawSystemData: string) { + public async updateSystemSavedata(updateData: UpdateSystemSavedataRequest, rawSystemData: string) { try { - const params = new URLSearchParams(); - params.append("clientSessionId", clientSessionId); + const updateArr = Object.entries(updateData).map(([key, value]) => [key, String(value)]); + const params = new URLSearchParams(updateArr); const response = await this.doPost(`/savedata/system/update?${params}`, rawSystemData); - return (await response.json()) as string; + return await response.text(); } catch (err) { console.warn("Could not update system savedata!", err); } @@ -225,6 +224,27 @@ export class Api { } } + /** + * Update a session savedata. + * @param updateData The {@linkcode UpdateSessionSavedataRequest} to send + * @param rawSavedata The raw savedata (as `string`) + * @returns an error message if something went wrong + */ + public async updateSessionSavedata(updateData: UpdateSessionSavedataRequest, rawSavedata: string) { + try { + const updateArr = Object.entries(updateData).map(([key, value]) => [key, String(value)]); + const params = new URLSearchParams(updateArr); + + const response = await this.doPost(`/savedata/session/update?${params}`, rawSavedata); + + return await response.text(); + } catch (err) { + console.warn("Could not update session savedata!", err); + } + + return null; + } + /** * Delete a session savedata slot. * @param slotId The slot ID to load diff --git a/src/plugins/api/models/UpdateSessionSavedata.ts b/src/plugins/api/models/UpdateSessionSavedata.ts new file mode 100644 index 00000000000..8e450e40f6c --- /dev/null +++ b/src/plugins/api/models/UpdateSessionSavedata.ts @@ -0,0 +1,6 @@ +export class UpdateSessionSavedataRequest { + slot: number; + trainerId: number; + secretId: number; + clientSessionId: string; +} diff --git a/src/plugins/api/models/UpdateSystemSavedata.ts b/src/plugins/api/models/UpdateSystemSavedata.ts new file mode 100644 index 00000000000..ebf74acfc7e --- /dev/null +++ b/src/plugins/api/models/UpdateSystemSavedata.ts @@ -0,0 +1,5 @@ +export class UpdateSystemSavedataRequest { + clientSessionId: string; + trainerId?: number; + secretId?: number; +} diff --git a/src/system/game-data.ts b/src/system/game-data.ts index e29a509d0c7..73984d18682 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -397,7 +397,7 @@ export class GameData { localStorage.setItem(`data_${loggedInUser?.username}`, encrypt(systemData, bypassLogin)); if (!bypassLogin) { - api.updateSystemSavedata(clientSessionId, systemData) + api.updateSystemSavedata({clientSessionId}, systemData) .then(error => { this.scene.ui.savingIcon.hide(); if (error) { @@ -1454,14 +1454,14 @@ export class GameData { if (!success[0]) { return displayError(`Could not contact the server. Your ${dataName} data could not be imported.`); } - let url: string; + const { trainerId, secretId } = this; + let updatePromise: Promise; if (dataType === GameDataType.SESSION) { - url = `savedata/session/update?slot=${slotId}&trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`; + updatePromise = api.updateSessionSavedata({slot: slotId, trainerId, secretId, clientSessionId}, dataStr); } else { - url = `savedata/system/update?trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`; + updatePromise = api.updateSystemSavedata({trainerId, secretId, clientSessionId}, dataStr); } - Utils.apiPost(url, dataStr, undefined, true) - .then(response => response.text()) + updatePromise .then(error => { if (error) { console.error(error);