import { useMemo, useState } from 'react';
import type {
DragPuzzlePieceRequest,
SwapPuzzlePiecesRequest,
} from '../packages/shared/src/contracts/puzzleRuntimeSession';
import type { PuzzleWorkSummary } from '../packages/shared/src/contracts/puzzleWorkSummary';
import { PuzzleRuntimeShell } from './components/puzzle-runtime/PuzzleRuntimeShell';
import {
applyLocalPuzzleFreezeTime,
advanceLocalPuzzleLevel,
dragLocalPuzzlePiece,
setLocalPuzzlePaused,
startLocalPuzzleRun,
swapLocalPuzzlePieces,
} from './services/puzzle-runtime/puzzleLocalRuntime';
const PLACEHOLDER_PUZZLE_IMAGE =
'data:image/svg+xml;utf8,' +
encodeURIComponent(`
`);
function buildPlaceholderPuzzleWork(): PuzzleWorkSummary {
return {
workId: 'placeholder-puzzle-work',
profileId: 'placeholder-puzzle-profile',
ownerUserId: 'placeholder-user',
sourceSessionId: null,
authorDisplayName: '占位作者',
levelName: '暮色群山',
summary: '用于直达玩法调试的本地占位拼图。',
themeTags: ['占位', '风景', '调试'],
coverImageSrc: PLACEHOLDER_PUZZLE_IMAGE,
coverAssetId: null,
publicationStatus: 'published',
updatedAt: new Date(0).toISOString(),
publishedAt: new Date(0).toISOString(),
playCount: 0,
likeCount: 0,
publishReady: true,
};
}
export default function PuzzlePlaygroundApp() {
const placeholderWork = useMemo(() => buildPlaceholderPuzzleWork(), []);
const [run, setRun] = useState(() => startLocalPuzzleRun(placeholderWork));
const handleSwapPieces = (payload: SwapPuzzlePiecesRequest) => {
setRun((currentRun) => swapLocalPuzzlePieces(currentRun, payload));
};
const handleDragPiece = (payload: DragPuzzlePieceRequest) => {
setRun((currentRun) => dragLocalPuzzlePiece(currentRun, payload));
};
const handleRestart = () => {
setRun(startLocalPuzzleRun(placeholderWork));
};
const handleAdvanceNextLevel = () => {
setRun((currentRun) => advanceLocalPuzzleLevel(currentRun));
};
const handlePauseChange = async (paused: boolean) => {
setRun((currentRun) => setLocalPuzzlePaused(currentRun, paused));
};
const handleUseProp = async (
propKind: 'hint' | 'reference' | 'freezeTime',
) => {
setRun((currentRun) =>
propKind === 'freezeTime'
? applyLocalPuzzleFreezeTime(currentRun)
: setLocalPuzzlePaused(currentRun, propKind === 'reference'),
);
};
return (
);
}