77 lines
2.1 KiB
TypeScript
77 lines
2.1 KiB
TypeScript
import { type GameDefinition, type GameControl, type PunchPolicyType } from '../core/gameDefinition'
|
|
import { type OrienteeringCourseData } from '../../utils/orienteeringCourse'
|
|
|
|
function sortBySequence<T extends { sequence: number | null }>(items: T[]): T[] {
|
|
return [...items].sort((a, b) => (a.sequence || 0) - (b.sequence || 0))
|
|
}
|
|
|
|
function buildDisplayBody(label: string, sequence: number | null): string {
|
|
if (typeof sequence === 'number') {
|
|
return `检查点 ${sequence} · ${label || String(sequence)}`
|
|
}
|
|
|
|
return label
|
|
}
|
|
|
|
export function buildGameDefinitionFromCourse(
|
|
course: OrienteeringCourseData,
|
|
controlRadiusMeters: number,
|
|
mode: GameDefinition['mode'] = 'classic-sequential',
|
|
autoFinishOnLastControl = true,
|
|
punchPolicy: PunchPolicyType = 'enter-confirm',
|
|
punchRadiusMeters = 5,
|
|
): GameDefinition {
|
|
const controls: GameControl[] = []
|
|
|
|
for (const start of course.layers.starts) {
|
|
controls.push({
|
|
id: `start-${controls.length + 1}`,
|
|
code: start.label || 'S',
|
|
label: start.label || 'Start',
|
|
kind: 'start',
|
|
point: start.point,
|
|
sequence: null,
|
|
displayContent: null,
|
|
})
|
|
}
|
|
|
|
for (const control of sortBySequence(course.layers.controls)) {
|
|
const label = control.label || String(control.sequence)
|
|
controls.push({
|
|
id: `control-${control.sequence}`,
|
|
code: label,
|
|
label,
|
|
kind: 'control',
|
|
point: control.point,
|
|
sequence: control.sequence,
|
|
displayContent: {
|
|
title: `收集 ${label}`,
|
|
body: buildDisplayBody(label, control.sequence),
|
|
},
|
|
})
|
|
}
|
|
|
|
for (const finish of course.layers.finishes) {
|
|
controls.push({
|
|
id: `finish-${controls.length + 1}`,
|
|
code: finish.label || 'F',
|
|
label: finish.label || 'Finish',
|
|
kind: 'finish',
|
|
point: finish.point,
|
|
sequence: null,
|
|
displayContent: null,
|
|
})
|
|
}
|
|
|
|
return {
|
|
id: `course-${course.title || 'default'}`,
|
|
mode,
|
|
title: course.title || 'Classic Sequential',
|
|
controlRadiusMeters,
|
|
punchRadiusMeters,
|
|
punchPolicy,
|
|
controls,
|
|
autoFinishOnLastControl,
|
|
}
|
|
}
|