scoreORule.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. import { type LonLatPoint } from '../../utils/projection'
  2. import { DEFAULT_GAME_AUDIO_CONFIG } from '../audio/audioConfig'
  3. import { type GameControl, type GameDefinition } from '../core/gameDefinition'
  4. import { type GameEvent } from '../core/gameEvent'
  5. import { type GameEffect, type GameResult } from '../core/gameResult'
  6. import { type GameSessionState } from '../core/gameSessionState'
  7. import { type GamePresentationState } from '../presentation/presentationState'
  8. import { type HudPresentationState } from '../presentation/hudPresentationState'
  9. import { type MapPresentationState } from '../presentation/mapPresentationState'
  10. import { type RulePlugin } from './rulePlugin'
  11. type ScoreOModeState = {
  12. phase: 'start' | 'controls' | 'finish' | 'done'
  13. focusedControlId: string | null
  14. }
  15. function getApproxDistanceMeters(a: LonLatPoint, b: LonLatPoint): number {
  16. const avgLatRad = ((a.lat + b.lat) / 2) * Math.PI / 180
  17. const dx = (b.lon - a.lon) * 111320 * Math.cos(avgLatRad)
  18. const dy = (b.lat - a.lat) * 110540
  19. return Math.sqrt(dx * dx + dy * dy)
  20. }
  21. function getStartControl(definition: GameDefinition): GameControl | null {
  22. return definition.controls.find((control) => control.kind === 'start') || null
  23. }
  24. function getFinishControl(definition: GameDefinition): GameControl | null {
  25. return definition.controls.find((control) => control.kind === 'finish') || null
  26. }
  27. function getScoreControls(definition: GameDefinition): GameControl[] {
  28. return definition.controls.filter((control) => control.kind === 'control')
  29. }
  30. function getControlScore(control: GameControl): number {
  31. return control.kind === 'control' && typeof control.score === 'number' ? control.score : 0
  32. }
  33. function getRemainingScoreControls(definition: GameDefinition, state: GameSessionState): GameControl[] {
  34. return getScoreControls(definition).filter((control) => !state.completedControlIds.includes(control.id))
  35. }
  36. function getModeState(state: GameSessionState): ScoreOModeState {
  37. const rawModeState = state.modeState as Partial<ScoreOModeState> | null
  38. return {
  39. phase: rawModeState && rawModeState.phase ? rawModeState.phase : 'start',
  40. focusedControlId: rawModeState && typeof rawModeState.focusedControlId === 'string' ? rawModeState.focusedControlId : null,
  41. }
  42. }
  43. function withModeState(state: GameSessionState, modeState: ScoreOModeState): GameSessionState {
  44. return {
  45. ...state,
  46. modeState,
  47. }
  48. }
  49. function canFocusFinish(definition: GameDefinition, state: GameSessionState): boolean {
  50. const startControl = getStartControl(definition)
  51. const finishControl = getFinishControl(definition)
  52. const completedStart = !!startControl && state.completedControlIds.includes(startControl.id)
  53. const completedFinish = !!finishControl && state.completedControlIds.includes(finishControl.id)
  54. return completedStart && !completedFinish
  55. }
  56. function getNearestRemainingControl(
  57. definition: GameDefinition,
  58. state: GameSessionState,
  59. referencePoint?: LonLatPoint | null,
  60. ): GameControl | null {
  61. const remainingControls = getRemainingScoreControls(definition, state)
  62. if (!remainingControls.length) {
  63. return getFinishControl(definition)
  64. }
  65. if (!referencePoint) {
  66. return remainingControls[0]
  67. }
  68. let nearestControl = remainingControls[0]
  69. let nearestDistance = getApproxDistanceMeters(referencePoint, nearestControl.point)
  70. for (let index = 1; index < remainingControls.length; index += 1) {
  71. const control = remainingControls[index]
  72. const distance = getApproxDistanceMeters(referencePoint, control.point)
  73. if (distance < nearestDistance) {
  74. nearestControl = control
  75. nearestDistance = distance
  76. }
  77. }
  78. return nearestControl
  79. }
  80. function getFocusedTarget(
  81. definition: GameDefinition,
  82. state: GameSessionState,
  83. remainingControls?: GameControl[],
  84. ): GameControl | null {
  85. const modeState = getModeState(state)
  86. if (!modeState.focusedControlId) {
  87. return null
  88. }
  89. const controls = remainingControls || getRemainingScoreControls(definition, state)
  90. for (const control of controls) {
  91. if (control.id === modeState.focusedControlId) {
  92. return control
  93. }
  94. }
  95. const finishControl = getFinishControl(definition)
  96. if (finishControl && canFocusFinish(definition, state) && finishControl.id === modeState.focusedControlId) {
  97. return finishControl
  98. }
  99. return null
  100. }
  101. function resolveInteractiveTarget(
  102. definition: GameDefinition,
  103. modeState: ScoreOModeState,
  104. primaryTarget: GameControl | null,
  105. focusedTarget: GameControl | null,
  106. ): GameControl | null {
  107. if (modeState.phase === 'start') {
  108. return primaryTarget
  109. }
  110. if (definition.requiresFocusSelection) {
  111. return focusedTarget
  112. }
  113. return focusedTarget || primaryTarget
  114. }
  115. function getNearestInRangeControl(
  116. controls: GameControl[],
  117. referencePoint: LonLatPoint,
  118. radiusMeters: number,
  119. ): GameControl | null {
  120. let nearest: GameControl | null = null
  121. let nearestDistance = Number.POSITIVE_INFINITY
  122. for (const control of controls) {
  123. const distance = getApproxDistanceMeters(control.point, referencePoint)
  124. if (distance > radiusMeters) {
  125. continue
  126. }
  127. if (!nearest || distance < nearestDistance) {
  128. nearest = control
  129. nearestDistance = distance
  130. }
  131. }
  132. return nearest
  133. }
  134. function getGuidanceState(definition: GameDefinition, distanceMeters: number): GameSessionState['guidanceState'] {
  135. if (distanceMeters <= definition.punchRadiusMeters) {
  136. return 'ready'
  137. }
  138. const approachDistanceMeters = definition.audioConfig ? definition.audioConfig.approachDistanceMeters : DEFAULT_GAME_AUDIO_CONFIG.approachDistanceMeters
  139. if (distanceMeters <= approachDistanceMeters) {
  140. return 'approaching'
  141. }
  142. return 'searching'
  143. }
  144. function getGuidanceEffects(
  145. previousState: GameSessionState['guidanceState'],
  146. nextState: GameSessionState['guidanceState'],
  147. controlId: string | null,
  148. ): GameEffect[] {
  149. if (previousState === nextState) {
  150. return []
  151. }
  152. return [{ type: 'guidance_state_changed', guidanceState: nextState, controlId }]
  153. }
  154. function getDisplayTargetLabel(control: GameControl | null): string {
  155. if (!control) {
  156. return '目标点'
  157. }
  158. if (control.kind === 'start') {
  159. return '开始点'
  160. }
  161. if (control.kind === 'finish') {
  162. return '终点'
  163. }
  164. return '目标点'
  165. }
  166. function buildPunchHintText(
  167. definition: GameDefinition,
  168. state: GameSessionState,
  169. primaryTarget: GameControl | null,
  170. focusedTarget: GameControl | null,
  171. ): string {
  172. if (state.status === 'idle') {
  173. return '点击开始后先打开始点'
  174. }
  175. if (state.status === 'finished') {
  176. return '本局已完成'
  177. }
  178. const modeState = getModeState(state)
  179. if (modeState.phase === 'controls' || modeState.phase === 'finish') {
  180. if (definition.requiresFocusSelection && !focusedTarget) {
  181. return modeState.phase === 'finish'
  182. ? '点击地图选中终点后结束比赛'
  183. : '点击地图选中一个目标点'
  184. }
  185. const displayTarget = resolveInteractiveTarget(definition, modeState, primaryTarget, focusedTarget)
  186. const targetLabel = getDisplayTargetLabel(displayTarget)
  187. if (displayTarget && state.inRangeControlId === displayTarget.id) {
  188. return definition.punchPolicy === 'enter'
  189. ? `${targetLabel}内,自动打点中`
  190. : `${targetLabel}内,可点击打点`
  191. }
  192. return definition.punchPolicy === 'enter'
  193. ? `进入${targetLabel}自动打点`
  194. : `进入${targetLabel}后点击打点`
  195. }
  196. const targetLabel = getDisplayTargetLabel(primaryTarget)
  197. if (state.inRangeControlId && primaryTarget && state.inRangeControlId === primaryTarget.id) {
  198. return definition.punchPolicy === 'enter'
  199. ? `${targetLabel}内,自动打点中`
  200. : `${targetLabel}内,可点击打点`
  201. }
  202. return definition.punchPolicy === 'enter'
  203. ? `进入${targetLabel}自动打点`
  204. : `进入${targetLabel}后点击打点`
  205. }
  206. function buildCompletedEffect(control: GameControl): GameEffect {
  207. if (control.kind === 'start') {
  208. return {
  209. type: 'control_completed',
  210. controlId: control.id,
  211. controlKind: 'start',
  212. sequence: null,
  213. label: control.label,
  214. displayTitle: '比赛开始',
  215. displayBody: '已完成开始点打卡,开始自由打点。',
  216. }
  217. }
  218. if (control.kind === 'finish') {
  219. return {
  220. type: 'control_completed',
  221. controlId: control.id,
  222. controlKind: 'finish',
  223. sequence: null,
  224. label: control.label,
  225. displayTitle: '比赛结束',
  226. displayBody: '已完成终点打卡,本局结束。',
  227. }
  228. }
  229. const sequenceText = typeof control.sequence === 'number' ? String(control.sequence) : control.label
  230. return {
  231. type: 'control_completed',
  232. controlId: control.id,
  233. controlKind: 'control',
  234. sequence: control.sequence,
  235. label: control.label,
  236. displayTitle: control.displayContent ? control.displayContent.title : `收集 ${sequenceText}`,
  237. displayBody: control.displayContent ? control.displayContent.body : control.label,
  238. }
  239. }
  240. function buildPresentation(definition: GameDefinition, state: GameSessionState): GamePresentationState {
  241. const modeState = getModeState(state)
  242. const running = state.status === 'running'
  243. const startControl = getStartControl(definition)
  244. const finishControl = getFinishControl(definition)
  245. const completedStart = !!startControl && state.completedControlIds.includes(startControl.id)
  246. const completedFinish = !!finishControl && state.completedControlIds.includes(finishControl.id)
  247. const remainingControls = getRemainingScoreControls(definition, state)
  248. const scoreControls = getScoreControls(definition)
  249. const primaryTarget = definition.controls.find((control) => control.id === state.currentTargetControlId) || null
  250. const focusedTarget = getFocusedTarget(definition, state, remainingControls)
  251. const canSelectFinish = running && completedStart && !completedFinish && !!finishControl
  252. const activeControlIds = running && modeState.phase === 'controls'
  253. ? remainingControls.map((control) => control.id)
  254. : []
  255. const activeControlSequences = running && modeState.phase === 'controls'
  256. ? remainingControls
  257. .filter((control) => typeof control.sequence === 'number')
  258. .map((control) => control.sequence as number)
  259. : []
  260. const completedControls = scoreControls.filter((control) => state.completedControlIds.includes(control.id))
  261. const completedControlSequences = completedControls
  262. .filter((control) => typeof control.sequence === 'number')
  263. .map((control) => control.sequence as number)
  264. const revealFullCourse = completedStart
  265. const interactiveTarget = resolveInteractiveTarget(definition, modeState, primaryTarget, focusedTarget)
  266. const punchButtonEnabled = running
  267. && definition.punchPolicy === 'enter-confirm'
  268. && !!interactiveTarget
  269. && state.inRangeControlId === interactiveTarget.id
  270. const mapPresentation: MapPresentationState = {
  271. controlVisualMode: modeState.phase === 'controls' ? 'multi-target' : 'single-target',
  272. showCourseLegs: false,
  273. guidanceLegAnimationEnabled: false,
  274. focusableControlIds: canSelectFinish
  275. ? [...activeControlIds, finishControl!.id]
  276. : activeControlIds.slice(),
  277. focusedControlId: focusedTarget ? focusedTarget.id : null,
  278. focusedControlSequences: focusedTarget && focusedTarget.kind === 'control' && typeof focusedTarget.sequence === 'number'
  279. ? [focusedTarget.sequence]
  280. : [],
  281. activeControlIds,
  282. activeControlSequences,
  283. activeStart: running && modeState.phase === 'start',
  284. completedStart,
  285. activeFinish: running && modeState.phase === 'finish',
  286. focusedFinish: !!focusedTarget && focusedTarget.kind === 'finish',
  287. completedFinish,
  288. revealFullCourse,
  289. activeLegIndices: [],
  290. completedLegIndices: [],
  291. completedControlIds: completedControls.map((control) => control.id),
  292. completedControlSequences,
  293. skippedControlIds: [],
  294. skippedControlSequences: [],
  295. }
  296. const hudPresentation: HudPresentationState = {
  297. actionTagText: modeState.phase === 'start'
  298. ? '目标'
  299. : focusedTarget && focusedTarget.kind === 'finish'
  300. ? '终点'
  301. : modeState.phase === 'finish'
  302. ? '终点'
  303. : '自由',
  304. distanceTagText: modeState.phase === 'start'
  305. ? '点距'
  306. : focusedTarget && focusedTarget.kind === 'finish'
  307. ? '终点距'
  308. : focusedTarget
  309. ? '选中点距'
  310. : modeState.phase === 'finish'
  311. ? '终点距'
  312. : '最近点距',
  313. hudTargetControlId: focusedTarget
  314. ? focusedTarget.id
  315. : primaryTarget
  316. ? primaryTarget.id
  317. : null,
  318. progressText: `已收集 ${completedControls.length}/${scoreControls.length}`,
  319. punchableControlId: punchButtonEnabled && interactiveTarget ? interactiveTarget.id : null,
  320. punchButtonEnabled,
  321. punchButtonText: modeState.phase === 'start'
  322. ? '开始打卡'
  323. : focusedTarget && focusedTarget.kind === 'finish'
  324. ? '结束打卡'
  325. : modeState.phase === 'finish'
  326. ? '结束打卡'
  327. : '打点',
  328. punchHintText: buildPunchHintText(definition, state, primaryTarget, focusedTarget),
  329. }
  330. return {
  331. map: mapPresentation,
  332. hud: hudPresentation,
  333. }
  334. }
  335. function applyCompletion(
  336. definition: GameDefinition,
  337. state: GameSessionState,
  338. control: GameControl,
  339. at: number,
  340. referencePoint: LonLatPoint | null,
  341. ): GameResult {
  342. const completedControlIds = state.completedControlIds.includes(control.id)
  343. ? state.completedControlIds
  344. : [...state.completedControlIds, control.id]
  345. const previousModeState = getModeState(state)
  346. const nextStateDraft: GameSessionState = {
  347. ...state,
  348. startedAt: control.kind === 'start' && state.startedAt === null ? at : state.startedAt,
  349. endedAt: control.kind === 'finish' ? at : state.endedAt,
  350. completedControlIds,
  351. currentTargetControlId: null,
  352. inRangeControlId: null,
  353. score: getScoreControls(definition)
  354. .filter((item) => completedControlIds.includes(item.id))
  355. .reduce((sum, item) => sum + getControlScore(item), 0),
  356. status: control.kind === 'finish' ? 'finished' : state.status,
  357. guidanceState: 'searching',
  358. }
  359. const remainingControls = getRemainingScoreControls(definition, nextStateDraft)
  360. let phase: ScoreOModeState['phase']
  361. if (control.kind === 'finish') {
  362. phase = 'done'
  363. } else if (control.kind === 'start') {
  364. phase = remainingControls.length ? 'controls' : 'finish'
  365. } else {
  366. phase = remainingControls.length ? 'controls' : 'finish'
  367. }
  368. const nextModeState: ScoreOModeState = {
  369. phase,
  370. focusedControlId: control.id === previousModeState.focusedControlId ? null : previousModeState.focusedControlId,
  371. }
  372. const nextPrimaryTarget = phase === 'controls'
  373. ? getNearestRemainingControl(definition, nextStateDraft, referencePoint)
  374. : phase === 'finish'
  375. ? getFinishControl(definition)
  376. : null
  377. const nextState = withModeState({
  378. ...nextStateDraft,
  379. currentTargetControlId: nextPrimaryTarget ? nextPrimaryTarget.id : null,
  380. }, nextModeState)
  381. const effects: GameEffect[] = [buildCompletedEffect(control)]
  382. if (control.kind === 'finish') {
  383. effects.push({ type: 'session_finished' })
  384. }
  385. return {
  386. nextState,
  387. presentation: buildPresentation(definition, nextState),
  388. effects,
  389. }
  390. }
  391. export class ScoreORule implements RulePlugin {
  392. get mode(): 'score-o' {
  393. return 'score-o'
  394. }
  395. initialize(definition: GameDefinition): GameSessionState {
  396. const startControl = getStartControl(definition)
  397. return {
  398. status: 'idle',
  399. startedAt: null,
  400. endedAt: null,
  401. completedControlIds: [],
  402. skippedControlIds: [],
  403. currentTargetControlId: startControl ? startControl.id : null,
  404. inRangeControlId: null,
  405. score: 0,
  406. guidanceState: 'searching',
  407. modeState: {
  408. phase: 'start',
  409. focusedControlId: null,
  410. },
  411. }
  412. }
  413. buildPresentation(definition: GameDefinition, state: GameSessionState): GamePresentationState {
  414. return buildPresentation(definition, state)
  415. }
  416. reduce(definition: GameDefinition, state: GameSessionState, event: GameEvent): GameResult {
  417. if (event.type === 'session_started') {
  418. const startControl = getStartControl(definition)
  419. const nextState = withModeState({
  420. ...state,
  421. status: 'running',
  422. startedAt: null,
  423. endedAt: null,
  424. currentTargetControlId: startControl ? startControl.id : null,
  425. inRangeControlId: null,
  426. guidanceState: 'searching',
  427. }, {
  428. phase: 'start',
  429. focusedControlId: null,
  430. })
  431. return {
  432. nextState,
  433. presentation: buildPresentation(definition, nextState),
  434. effects: [{ type: 'session_started' }],
  435. }
  436. }
  437. if (event.type === 'session_ended') {
  438. const nextState = withModeState({
  439. ...state,
  440. status: 'finished',
  441. endedAt: event.at,
  442. guidanceState: 'searching',
  443. }, {
  444. phase: 'done',
  445. focusedControlId: null,
  446. })
  447. return {
  448. nextState,
  449. presentation: buildPresentation(definition, nextState),
  450. effects: [{ type: 'session_finished' }],
  451. }
  452. }
  453. if (state.status !== 'running') {
  454. return {
  455. nextState: state,
  456. presentation: buildPresentation(definition, state),
  457. effects: [],
  458. }
  459. }
  460. const modeState = getModeState(state)
  461. const targetControl = state.currentTargetControlId
  462. ? definition.controls.find((control) => control.id === state.currentTargetControlId) || null
  463. : null
  464. if (event.type === 'gps_updated') {
  465. const referencePoint = { lon: event.lon, lat: event.lat }
  466. const remainingControls = getRemainingScoreControls(definition, state)
  467. const focusedTarget = getFocusedTarget(definition, state, remainingControls)
  468. let nextPrimaryTarget = targetControl
  469. let guidanceTarget = targetControl
  470. let punchTarget: GameControl | null = null
  471. if (modeState.phase === 'controls') {
  472. nextPrimaryTarget = getNearestRemainingControl(definition, state, referencePoint)
  473. guidanceTarget = focusedTarget || nextPrimaryTarget
  474. if (focusedTarget && getApproxDistanceMeters(focusedTarget.point, referencePoint) <= definition.punchRadiusMeters) {
  475. punchTarget = focusedTarget
  476. } else if (!definition.requiresFocusSelection) {
  477. punchTarget = getNearestInRangeControl(remainingControls, referencePoint, definition.punchRadiusMeters)
  478. }
  479. } else if (modeState.phase === 'finish') {
  480. nextPrimaryTarget = getFinishControl(definition)
  481. guidanceTarget = focusedTarget || nextPrimaryTarget
  482. if (focusedTarget && getApproxDistanceMeters(focusedTarget.point, referencePoint) <= definition.punchRadiusMeters) {
  483. punchTarget = focusedTarget
  484. } else if (!definition.requiresFocusSelection && nextPrimaryTarget && getApproxDistanceMeters(nextPrimaryTarget.point, referencePoint) <= definition.punchRadiusMeters) {
  485. punchTarget = nextPrimaryTarget
  486. }
  487. } else if (targetControl) {
  488. guidanceTarget = targetControl
  489. if (getApproxDistanceMeters(targetControl.point, referencePoint) <= definition.punchRadiusMeters) {
  490. punchTarget = targetControl
  491. }
  492. }
  493. const guidanceState = guidanceTarget
  494. ? getGuidanceState(definition, getApproxDistanceMeters(guidanceTarget.point, referencePoint))
  495. : 'searching'
  496. const nextState: GameSessionState = {
  497. ...state,
  498. currentTargetControlId: nextPrimaryTarget ? nextPrimaryTarget.id : null,
  499. inRangeControlId: punchTarget ? punchTarget.id : null,
  500. guidanceState,
  501. }
  502. const guidanceEffects = getGuidanceEffects(state.guidanceState, guidanceState, guidanceTarget ? guidanceTarget.id : null)
  503. if (definition.punchPolicy === 'enter' && punchTarget) {
  504. const completionResult = applyCompletion(definition, nextState, punchTarget, event.at, referencePoint)
  505. return {
  506. ...completionResult,
  507. effects: [...guidanceEffects, ...completionResult.effects],
  508. }
  509. }
  510. return {
  511. nextState,
  512. presentation: buildPresentation(definition, nextState),
  513. effects: guidanceEffects,
  514. }
  515. }
  516. if (event.type === 'control_focused') {
  517. if (modeState.phase !== 'controls' && modeState.phase !== 'finish') {
  518. return {
  519. nextState: state,
  520. presentation: buildPresentation(definition, state),
  521. effects: [],
  522. }
  523. }
  524. const focusableControlIds = getRemainingScoreControls(definition, state).map((control) => control.id)
  525. const finishControl = getFinishControl(definition)
  526. if (finishControl && canFocusFinish(definition, state)) {
  527. focusableControlIds.push(finishControl.id)
  528. }
  529. const nextFocusedControlId = event.controlId && focusableControlIds.includes(event.controlId)
  530. ? modeState.focusedControlId === event.controlId
  531. ? null
  532. : event.controlId
  533. : null
  534. const nextState = withModeState({
  535. ...state,
  536. }, {
  537. ...modeState,
  538. focusedControlId: nextFocusedControlId,
  539. })
  540. return {
  541. nextState,
  542. presentation: buildPresentation(definition, nextState),
  543. effects: [],
  544. }
  545. }
  546. if (event.type === 'punch_requested') {
  547. const focusedTarget = getFocusedTarget(definition, state)
  548. if (definition.requiresFocusSelection && (modeState.phase === 'controls' || modeState.phase === 'finish') && !focusedTarget) {
  549. return {
  550. nextState: state,
  551. presentation: buildPresentation(definition, state),
  552. effects: [{ type: 'punch_feedback', text: modeState.phase === 'finish' ? '请先选中终点' : '请先选中目标点', tone: 'warning' }],
  553. }
  554. }
  555. let controlToPunch: GameControl | null = null
  556. if (state.inRangeControlId) {
  557. controlToPunch = definition.controls.find((control) => control.id === state.inRangeControlId) || null
  558. }
  559. if (!controlToPunch || (definition.requiresFocusSelection && focusedTarget && controlToPunch.id !== focusedTarget.id)) {
  560. return {
  561. nextState: state,
  562. presentation: buildPresentation(definition, state),
  563. effects: [{
  564. type: 'punch_feedback',
  565. text: focusedTarget
  566. ? `未进入${getDisplayTargetLabel(focusedTarget)}打卡范围`
  567. : modeState.phase === 'start'
  568. ? '未进入开始点打卡范围'
  569. : '未进入目标打点范围',
  570. tone: 'warning',
  571. }],
  572. }
  573. }
  574. return applyCompletion(definition, state, controlToPunch, event.at, this.getReferencePoint(definition, state, controlToPunch))
  575. }
  576. return {
  577. nextState: state,
  578. presentation: buildPresentation(definition, state),
  579. effects: [],
  580. }
  581. }
  582. private getReferencePoint(definition: GameDefinition, state: GameSessionState, completedControl: GameControl): LonLatPoint | null {
  583. if (completedControl.kind === 'control') {
  584. const remaining = getRemainingScoreControls(definition, {
  585. ...state,
  586. completedControlIds: [...state.completedControlIds, completedControl.id],
  587. })
  588. return remaining.length ? completedControl.point : (getFinishControl(definition) ? getFinishControl(definition)!.point : completedControl.point)
  589. }
  590. return completedControl.point
  591. }
  592. }