From 523011f9ed90ec92535decdaf3f5dd61f8dd2ebb Mon Sep 17 00:00:00 2001 From: Pancakes Date: Sun, 9 Jun 2024 00:42:59 -0400 Subject: [PATCH] Use new save endpoints --- src/system/game-data.ts | 8 ++++---- src/utils.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/system/game-data.ts b/src/system/game-data.ts index a6070c6330f..0d1cc9d7667 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -301,7 +301,7 @@ export class GameData { localStorage.setItem(`data_${loggedInUser.username}`, encrypt(systemData, bypassLogin)); if (!bypassLogin) { - Utils.apiPost(`savedata/update?datatype=${GameDataType.SYSTEM}&clientSessionId=${clientSessionId}`, systemData, undefined, true) + Utils.apiPut(`savedata/system?clientSessionId=${clientSessionId}`, systemData, undefined) .then(response => response.text()) .then(error => { this.scene.ui.savingIcon.hide(); @@ -951,7 +951,7 @@ export class GameData { if (success !== null && !success) { return resolve(false); } - Utils.apiFetch(`savedata/delete?datatype=${GameDataType.SESSION}&slot=${slotId}&clientSessionId=${clientSessionId}`, true).then(response => { + Utils.apiDelete(`savedata/session?slot=${slotId}&clientSessionId=${clientSessionId}`).then(response => { if (response.ok) { loggedInUser.lastSessionSlot = -1; localStorage.removeItem(`sessionData${this.scene.sessionSlotId ? this.scene.sessionSlotId : ""}_${loggedInUser.username}`); @@ -1124,7 +1124,7 @@ export class GameData { console.debug("Session data saved"); if (!bypassLogin && sync) { - Utils.apiPost("savedata/updateall", JSON.stringify(request, (k: any, v: any) => typeof v === "bigint" ? v <= maxIntAttrValue ? Number(v) : v.toString() : v), undefined, true) + Utils.apiPut("savedata/updateall", JSON.stringify(request, (k: any, v: any) => typeof v === "bigint" ? v <= maxIntAttrValue ? Number(v) : v.toString() : v), undefined) .then(response => response.text()) .then(error => { if (sync) { @@ -1266,7 +1266,7 @@ export class GameData { if (!success) { return displayError(`Could not contact the server. Your ${dataName} data could not be imported.`); } - Utils.apiPost(`savedata/update?datatype=${dataType}${dataType === GameDataType.SESSION ? `&slot=${slotId}` : ""}&trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`, dataStr, undefined, true) + Utils.apiPut(`savedata/${dataType === GameDataType.SESSION ? `session?slot=${slotId}&` : "system?"}trainerId=${this.trainerId}&secretId=${this.secretId}&clientSessionId=${clientSessionId}`, dataStr, undefined) .then(response => response.text()) .then(error => { if (error) { diff --git a/src/utils.ts b/src/utils.ts index 6f2c9cd0c65..75703378d23 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -361,6 +361,34 @@ export function apiPost(path: string, data?: any, contentType: string = "applica }) : new Promise(() => {}); } +export function apiPut(path: string, data?: any, contentType: string = "application/json"): Promise { + return (isLocal && isLocalServerConnected) || !isLocal ? new Promise((resolve, reject) => { + const headers = { + "Content-Type": contentType, + }; + const sId = getCookie(sessionIdKey); + if (sId) { + headers["Authorization"] = sId; + } + fetch(`${apiUrl}/${path}`, { method: "PUT", headers: headers, body: data }) + .then(response => resolve(response)) + .catch(err => reject(err)); + }) : new Promise(() => {}); +} + +export function apiDelete(path: string): Promise { + return (isLocal && isLocalServerConnected) || !isLocal ? new Promise((resolve, reject) => { + const headers = {}; + const sId = getCookie(sessionIdKey); + if (sId) { + headers["Authorization"] = sId; + } + fetch(`${apiUrl}/${path}`, { method: "DELETE", headers: headers}) + .then(response => resolve(response)) + .catch(err => reject(err)); + }) : new Promise(() => {}); +} + export class BooleanHolder { public value: boolean;