refactor: 迁移拼图与跳跃 runtime 请求骨架

This commit is contained in:
2026-06-03 16:42:18 +08:00
parent ab49c32e33
commit 3783f0d2af
7 changed files with 284 additions and 152 deletions

View File

@@ -1,6 +1,6 @@
import type {
DragPuzzlePieceRequest,
AdvancePuzzleNextLevelRequest,
DragPuzzlePieceRequest,
PuzzleRunResponse,
StartPuzzleRunRequest,
SubmitPuzzleLeaderboardRequest,
@@ -12,11 +12,8 @@ import {
type ApiRetryOptions,
requestJson,
} from '../apiClient';
import {
buildRuntimeGuestAuthOptions,
buildRuntimeGuestHeaders,
type RuntimeGuestRequestOptions,
} from '../runtimeGuestAuth';
import { type RuntimeGuestRequestOptions } from '../runtimeGuestAuth';
import { buildRuntimeApiPath, requestRuntimeJson } from '../runtimeRequest';
const PUZZLE_RUNTIME_API_BASE = '/api/runtime/puzzle/runs';
const PUZZLE_RUNTIME_READ_RETRY: ApiRetryOptions = {
@@ -42,38 +39,25 @@ export async function startPuzzleRun(
payload: StartPuzzleRunRequest,
options: PuzzleRuntimeRequestOptions = {},
) {
const requestOptions = buildRuntimeGuestAuthOptions(options);
return requestJson<PuzzleRunResponse>(
PUZZLE_RUNTIME_API_BASE,
{
method: 'POST',
headers: buildRuntimeGuestHeaders(options, {
'Content-Type': 'application/json',
}),
body: JSON.stringify(payload),
},
'启动拼图玩法失败',
{
retry: PUZZLE_RUNTIME_WRITE_RETRY,
...requestOptions,
},
);
return requestRuntimeJson<PuzzleRunResponse>({
url: PUZZLE_RUNTIME_API_BASE,
method: 'POST',
jsonBody: payload,
fallbackMessage: '启动拼图玩法失败',
retry: PUZZLE_RUNTIME_WRITE_RETRY,
requestOptions: options,
});
}
/**
* 读取拼图运行态快照。
*/
export async function getPuzzleRun(runId: string) {
return requestJson<PuzzleRunResponse>(
`${PUZZLE_RUNTIME_API_BASE}/${encodeURIComponent(runId)}`,
{
method: 'GET',
},
'读取拼图运行快照失败',
{
retry: PUZZLE_RUNTIME_READ_RETRY,
},
);
return requestRuntimeJson<PuzzleRunResponse>({
url: buildRuntimeApiPath(PUZZLE_RUNTIME_API_BASE, runId),
fallbackMessage: '读取拼图运行快照失败',
retry: PUZZLE_RUNTIME_READ_RETRY,
});
}
/**
@@ -83,18 +67,13 @@ export async function swapPuzzlePieces(
runId: string,
payload: SwapPuzzlePiecesRequest,
) {
return requestJson<PuzzleRunResponse>(
`${PUZZLE_RUNTIME_API_BASE}/${encodeURIComponent(runId)}/swap`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
},
'交换拼图块失败',
{
retry: PUZZLE_RUNTIME_WRITE_RETRY,
},
);
return requestRuntimeJson<PuzzleRunResponse>({
url: buildRuntimeApiPath(PUZZLE_RUNTIME_API_BASE, runId, 'swap'),
method: 'POST',
jsonBody: payload,
fallbackMessage: '交换拼图块失败',
retry: PUZZLE_RUNTIME_WRITE_RETRY,
});
}
/**
@@ -104,18 +83,13 @@ export async function dragPuzzlePieceOrGroup(
runId: string,
payload: DragPuzzlePieceRequest,
) {
return requestJson<PuzzleRunResponse>(
`${PUZZLE_RUNTIME_API_BASE}/${encodeURIComponent(runId)}/drag`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
},
'拖动拼图块失败',
{
retry: PUZZLE_RUNTIME_WRITE_RETRY,
},
);
return requestRuntimeJson<PuzzleRunResponse>({
url: buildRuntimeApiPath(PUZZLE_RUNTIME_API_BASE, runId, 'drag'),
method: 'POST',
jsonBody: payload,
fallbackMessage: '拖动拼图块失败',
retry: PUZZLE_RUNTIME_WRITE_RETRY,
});
}
/**
@@ -126,7 +100,6 @@ export async function advancePuzzleNextLevel(
payload: AdvancePuzzleNextLevelRequest = {},
options: PuzzleRuntimeRequestOptions = {},
) {
const requestOptions = buildRuntimeGuestAuthOptions(options);
const targetProfileId = payload.targetProfileId?.trim() ?? '';
const preferSimilarWork = payload.preferSimilarWork === true;
const requestPayload = {
@@ -134,27 +107,14 @@ export async function advancePuzzleNextLevel(
...(preferSimilarWork ? { preferSimilarWork: true } : {}),
};
const hasRequestPayload = Object.keys(requestPayload).length > 0;
return requestJson<PuzzleRunResponse>(
`${PUZZLE_RUNTIME_API_BASE}/${encodeURIComponent(runId)}/next-level`,
{
method: 'POST',
...(hasRequestPayload
? {
headers: buildRuntimeGuestHeaders(options, {
'Content-Type': 'application/json',
}),
body: JSON.stringify(requestPayload),
}
: {
headers: buildRuntimeGuestHeaders(options),
}),
},
'进入下一关失败',
{
retry: PUZZLE_RUNTIME_WRITE_RETRY,
...requestOptions,
},
);
return requestRuntimeJson<PuzzleRunResponse>({
url: buildRuntimeApiPath(PUZZLE_RUNTIME_API_BASE, runId, 'next-level'),
method: 'POST',
...(hasRequestPayload ? { jsonBody: requestPayload } : {}),
fallbackMessage: '进入下一关失败',
retry: PUZZLE_RUNTIME_WRITE_RETRY,
requestOptions: options,
});
}
/**
@@ -165,22 +125,14 @@ export async function submitPuzzleLeaderboard(
payload: SubmitPuzzleLeaderboardRequest,
options: PuzzleRuntimeRequestOptions = {},
) {
const requestOptions = buildRuntimeGuestAuthOptions(options);
return requestJson<PuzzleRunResponse>(
`${PUZZLE_RUNTIME_API_BASE}/${encodeURIComponent(runId)}/leaderboard`,
{
method: 'POST',
headers: buildRuntimeGuestHeaders(options, {
'Content-Type': 'application/json',
}),
body: JSON.stringify(payload),
},
'提交拼图排行榜失败',
{
retry: PUZZLE_RUNTIME_LEADERBOARD_RETRY,
...requestOptions,
},
);
return requestRuntimeJson<PuzzleRunResponse>({
url: buildRuntimeApiPath(PUZZLE_RUNTIME_API_BASE, runId, 'leaderboard'),
method: 'POST',
jsonBody: payload,
fallbackMessage: '提交拼图排行榜失败',
retry: PUZZLE_RUNTIME_LEADERBOARD_RETRY,
requestOptions: options,
});
}
/**
@@ -192,7 +144,7 @@ export async function updatePuzzleRunPause(
options: PuzzleRuntimeRequestOptions = {},
) {
return requestJson<PuzzleRunResponse>(
`${PUZZLE_RUNTIME_API_BASE}/${encodeURIComponent(runId)}/pause`,
buildRuntimeApiPath(PUZZLE_RUNTIME_API_BASE, runId, 'pause'),
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
@@ -218,7 +170,7 @@ export async function usePuzzleRuntimeProp(
options: PuzzleRuntimeRequestOptions = {},
) {
return requestJson<PuzzleRunResponse>(
`${PUZZLE_RUNTIME_API_BASE}/${encodeURIComponent(runId)}/props`,
buildRuntimeApiPath(PUZZLE_RUNTIME_API_BASE, runId, 'props'),
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },