migrate updateSystemSavedata to api

This commit is contained in:
flx-sta 2024-10-03 17:21:02 -07:00
parent cc7ce0cb5d
commit 9f63360484
2 changed files with 32 additions and 5 deletions

View File

@ -136,6 +136,10 @@ export class Api {
} }
} }
/**
* Get a system savedata.
* @param sessionId The savedata session ID
*/
public async getSystemSavedata(sessionId: string) { public async getSystemSavedata(sessionId: string) {
try { try {
const params = new URLSearchParams(); const params = new URLSearchParams();
@ -177,6 +181,26 @@ export class Api {
return null; return null;
} }
/**
* Update a system savedata.
* @param clientSessionId The savedata session ID
* @param rawSystemData The raw {@linkcode SystemSaveData}
* @returns an error message if something went wrong
*/
public async updateSystemSavedata(clientSessionId: string, rawSystemData: string) {
try {
const params = new URLSearchParams();
params.append("clientSessionId", clientSessionId);
const response = await this.doPost<string>(`/savedata/system/update?${params}`, rawSystemData);
return (await response.json()) as string;
} catch (err) {
console.warn("Could not update system savedata!", err);
}
return null;
}
/** /**
* Get a session savedata. * Get a session savedata.
* @param slotId The slot ID to load * @param slotId The slot ID to load
@ -285,15 +309,19 @@ export class Api {
* @param bodyData The body-data to send. * @param bodyData The body-data to send.
* @param dataType The data-type of the {@linkcode bodyData}. * @param dataType The data-type of the {@linkcode bodyData}.
*/ */
private async doPost<D extends Record<string, any>>(path: string, bodyData: D, dataType: DataType = "json") { private async doPost<D>(path: string, bodyData: D, dataType: DataType = "json") {
let body: string = ""; let body: string = "";
const headers: HeadersInit = {}; const headers: HeadersInit = {};
if (dataType === "json") { if (dataType === "json") {
body = JSON.stringify(bodyData); body = typeof bodyData === "string" ? bodyData : JSON.stringify(bodyData);
headers["Content-Type"] = "application/json"; headers["Content-Type"] = "application/json";
} else if (dataType === "form-urlencoded") { } else if (dataType === "form-urlencoded") {
body = new URLSearchParams(Object.entries<any>(bodyData).map(([k, v]) => [k, v.toString()])).toString(); if (bodyData instanceof Object) {
body = new URLSearchParams(Object.entries<any>(bodyData).map(([k, v]) => [k, v.toString()])).toString();
} else {
console.warn("Could not add body data to form-urlencoded!", bodyData);
}
headers["Content-Type"] = "application/x-www-form-urlencoded"; headers["Content-Type"] = "application/x-www-form-urlencoded";
} else { } else {
console.warn(`Unsupported data type: ${dataType}`); console.warn(`Unsupported data type: ${dataType}`);

View File

@ -398,8 +398,7 @@ export class GameData {
localStorage.setItem(`data_${loggedInUser?.username}`, encrypt(systemData, bypassLogin)); localStorage.setItem(`data_${loggedInUser?.username}`, encrypt(systemData, bypassLogin));
if (!bypassLogin) { if (!bypassLogin) {
Utils.apiPost(`savedata/system/update?clientSessionId=${clientSessionId}`, systemData, undefined, true) api.updateSystemSavedata(clientSessionId, systemData)
.then(response => response.text())
.then(error => { .then(error => {
this.scene.ui.savingIcon.hide(); this.scene.ui.savingIcon.hide();
if (error) { if (error) {