mirror of
https://github.com/pagefaultgames/pokerogue.git
synced 2025-05-04 07:23:57 +01:00
* Loading data now checks statusCode not error string * Bump version to 1.8.5 --------- Co-authored-by: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com>
81 lines
2.6 KiB
TypeScript
81 lines
2.6 KiB
TypeScript
import type {
|
|
GetSystemSavedataRequest,
|
|
UpdateSystemSavedataRequest,
|
|
VerifySystemSavedataRequest,
|
|
VerifySystemSavedataResponse,
|
|
} from "#app/@types/PokerogueSystemSavedataApi";
|
|
import { ApiBase } from "#app/plugins/api/api-base";
|
|
|
|
/**
|
|
* A wrapper for PokéRogue system savedata API requests.
|
|
*/
|
|
export class PokerogueSystemSavedataApi extends ApiBase {
|
|
//#region Public
|
|
|
|
/**
|
|
* Get a system savedata.
|
|
* @param params The {@linkcode GetSystemSavedataRequest} to send
|
|
* @returns The system savedata as `string` or either the status code or `null` on error
|
|
*/
|
|
public async get(params: GetSystemSavedataRequest): Promise<string | number | null> {
|
|
try {
|
|
const urlSearchParams = this.toUrlSearchParams(params);
|
|
const response = await this.doGet(`/savedata/system/get?${urlSearchParams}`);
|
|
const rawSavedata = await response.text();
|
|
if (!response.ok) {
|
|
console.warn("Could not get system savedata!", response.status, rawSavedata);
|
|
return response.status;
|
|
}
|
|
return rawSavedata;
|
|
} catch (err) {
|
|
console.warn("Could not get system savedata!", err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Verify if the session is valid.
|
|
* If not the {@linkcode SystemSaveData} is returned.
|
|
* @param params The {@linkcode VerifySystemSavedataRequest} to send
|
|
* @returns A {@linkcode SystemSaveData} if **NOT** valid, otherwise `null`.
|
|
*
|
|
* TODO: add handling for errors
|
|
*/
|
|
public async verify(params: VerifySystemSavedataRequest) {
|
|
const urlSearchParams = this.toUrlSearchParams(params);
|
|
const response = await this.doGet(`/savedata/system/verify?${urlSearchParams}`);
|
|
|
|
if (response.ok) {
|
|
const verifySavedata = (await response.json()) as VerifySystemSavedataResponse;
|
|
|
|
if (!verifySavedata.valid) {
|
|
console.warn("Invalid system savedata!");
|
|
return verifySavedata.systemData;
|
|
}
|
|
} else {
|
|
console.warn("System savedata verification failed!", response.status, response.statusText);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Update a system savedata.
|
|
* @param params The {@linkcode UpdateSystemSavedataRequest} to send
|
|
* @param rawSystemData The raw {@linkcode SystemSaveData}
|
|
* @returns An error message if something went wrong
|
|
*/
|
|
public async update(params: UpdateSystemSavedataRequest, rawSystemData: string) {
|
|
try {
|
|
const urSearchParams = this.toUrlSearchParams(params);
|
|
const response = await this.doPost(`/savedata/system/update?${urSearchParams}`, rawSystemData);
|
|
|
|
return await response.text();
|
|
} catch (err) {
|
|
console.warn("Could not update system savedata!", err);
|
|
}
|
|
|
|
return "Unknown Error";
|
|
}
|
|
}
|