Files
Genarrative/src/services/match3d-runtime/match3dRuntimeAdapter.ts
T

107 lines
3.2 KiB
TypeScript

import type {
Match3DClickItemRequest,
Match3DClickItemResult,
Match3DRunResponse,
} from '../../../packages/shared/src/contracts/match3dRuntime';
import {
confirmLocalMatch3DClick,
resolveLocalMatch3DTimer,
startLocalMatch3DRun,
stopLocalMatch3DRun,
} from './match3dLocalRuntime';
import {
clickMatch3DItem,
finishMatch3DTimeUp,
getMatch3DRun,
type Match3DRuntimeRequestOptions,
restartMatch3DRun,
startMatch3DRun,
stopMatch3DRun,
} from './match3dRuntimeClient';
export type Match3DRuntimeAdapter = {
startRun: (
profileId: string,
options?: Match3DRuntimeRequestOptions,
) => Promise<Match3DRunResponse>;
getRun: (runId: string) => Promise<Match3DRunResponse>;
clickItem: (
runId: string,
payload: Match3DClickItemRequest,
) => Promise<Match3DClickItemResult>;
restartRun: (runId: string) => Promise<Match3DRunResponse>;
stopRun: (runId: string) => Promise<Match3DRunResponse>;
finishTimeUp: (runId: string) => Promise<Match3DRunResponse>;
};
export type LocalMatch3DRuntimeAdapterOptions = {
clearCount?: number;
initialRun?: Match3DRunResponse['run'];
};
type ServerMatch3DRuntimeAdapterDependencies = {
clickItem: typeof clickMatch3DItem;
finishTimeUp: typeof finishMatch3DTimeUp;
getRun: typeof getMatch3DRun;
restartRun: typeof restartMatch3DRun;
startRun: typeof startMatch3DRun;
stopRun: typeof stopMatch3DRun;
};
const defaultServerMatch3DRuntimeAdapterDependencies: ServerMatch3DRuntimeAdapterDependencies = {
clickItem: clickMatch3DItem,
finishTimeUp: finishMatch3DTimeUp,
getRun: getMatch3DRun,
restartRun: restartMatch3DRun,
startRun: startMatch3DRun,
stopRun: stopMatch3DRun,
};
export function createServerMatch3DRuntimeAdapter(
dependencies: ServerMatch3DRuntimeAdapterDependencies =
defaultServerMatch3DRuntimeAdapterDependencies,
): Match3DRuntimeAdapter {
return {
clickItem: (runId, payload) => dependencies.clickItem(runId, payload),
finishTimeUp: (runId) => dependencies.finishTimeUp(runId),
getRun: (runId) => dependencies.getRun(runId),
restartRun: (runId) => dependencies.restartRun(runId),
startRun: (profileId, options) => dependencies.startRun(profileId, options),
stopRun: (runId) => dependencies.stopRun(runId),
};
}
export function createLocalMatch3DRuntimeAdapter(
options: LocalMatch3DRuntimeAdapterOptions = {},
): Match3DRuntimeAdapter {
let authorityRun = options.initialRun ?? startLocalMatch3DRun(options.clearCount);
return {
async startRun() {
authorityRun = startLocalMatch3DRun(options.clearCount);
return { run: authorityRun };
},
async getRun() {
authorityRun = resolveLocalMatch3DTimer(authorityRun);
return { run: authorityRun };
},
async clickItem(_runId, payload) {
const result = await confirmLocalMatch3DClick(authorityRun, payload);
authorityRun = result.run;
return result;
},
async restartRun() {
authorityRun = startLocalMatch3DRun(options.clearCount);
return { run: authorityRun };
},
async stopRun() {
authorityRun = stopLocalMatch3DRun(authorityRun);
return { run: authorityRun };
},
async finishTimeUp() {
authorityRun = resolveLocalMatch3DTimer(authorityRun);
return { run: authorityRun };
},
};
}