migrate updateSessionSavedata to api

This commit is contained in:
flx-sta 2024-10-04 09:53:54 -07:00
parent 3e3315d223
commit 9df8a616d4
4 changed files with 46 additions and 15 deletions

View File

@ -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<AccountLoginRequest>(
"/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<string>(`/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<string>(`/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

View File

@ -0,0 +1,6 @@
export class UpdateSessionSavedataRequest {
slot: number;
trainerId: number;
secretId: number;
clientSessionId: string;
}

View File

@ -0,0 +1,5 @@
export class UpdateSystemSavedataRequest {
clientSessionId: string;
trainerId?: number;
secretId?: number;
}

View File

@ -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<string | null>;
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);