64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
import type { BasicOkResult } from '../../../packages/shared/src/contracts/runtime';
|
|
import type { RuntimeSaveCheckpointInput } from '../../persistence/gameSaveStorage';
|
|
import { rehydrateSavedSnapshot } from '../../persistence/runtimeSnapshot';
|
|
import type { HydratedSavedGameSnapshot } from '../../persistence/runtimeSnapshotTypes';
|
|
import {
|
|
requestRpgRuntimeJson,
|
|
type RuntimeRequestOptions,
|
|
} from './rpgRuntimeRequest';
|
|
|
|
export type { RuntimeRequestOptions };
|
|
|
|
/**
|
|
* RPG 运行时快照 client。
|
|
* 工作包 C 起由新域目录承载真实实现,旧 `storageService` 仅保留兼容转发。
|
|
*/
|
|
export async function getRpgSaveSnapshot(options: RuntimeRequestOptions = {}) {
|
|
// 中文注释:远端返回的是可序列化快照;
|
|
// 客户端每次读取后都先做一次 rehydrate,恢复 Date / 枚举 / 运行时默认字段。
|
|
const snapshot =
|
|
await requestRpgRuntimeJson<HydratedSavedGameSnapshot | null>(
|
|
'/save/snapshot',
|
|
{ method: 'GET' },
|
|
'读取存档失败',
|
|
options,
|
|
);
|
|
|
|
return snapshot ? rehydrateSavedSnapshot(snapshot) : null;
|
|
}
|
|
|
|
export async function putRpgSaveSnapshot(
|
|
checkpoint: RuntimeSaveCheckpointInput,
|
|
options: RuntimeRequestOptions = {},
|
|
) {
|
|
// 中文注释:自动/手动存档只提交 checkpoint 元数据;
|
|
// 运行时真相由服务端读取已持久化快照后刷新,避免浏览器上传整份 GameState。
|
|
const savedSnapshot = await requestRpgRuntimeJson<HydratedSavedGameSnapshot>(
|
|
'/save/snapshot',
|
|
{
|
|
method: 'PUT',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(checkpoint),
|
|
},
|
|
'保存存档失败',
|
|
options,
|
|
);
|
|
|
|
return rehydrateSavedSnapshot(savedSnapshot);
|
|
}
|
|
|
|
export function deleteRpgSaveSnapshot(options: RuntimeRequestOptions = {}) {
|
|
return requestRpgRuntimeJson<BasicOkResult>(
|
|
'/save/snapshot',
|
|
{ method: 'DELETE' },
|
|
'删除存档失败',
|
|
options,
|
|
);
|
|
}
|
|
|
|
export const rpgSnapshotClient = {
|
|
getSnapshot: getRpgSaveSnapshot,
|
|
putSnapshot: putRpgSaveSnapshot,
|
|
deleteSnapshot: deleteRpgSaveSnapshot,
|
|
};
|