scoreORule.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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: control.displayContent ? control.displayContent.title : '比赛开始',
  215. displayBody: control.displayContent ? control.displayContent.body : '已完成开始点打卡,开始自由打点。',
  216. displayAutoPopup: control.displayContent ? control.displayContent.autoPopup : true,
  217. displayOnce: control.displayContent ? control.displayContent.once : false,
  218. displayPriority: control.displayContent ? control.displayContent.priority : 1,
  219. }
  220. }
  221. if (control.kind === 'finish') {
  222. return {
  223. type: 'control_completed',
  224. controlId: control.id,
  225. controlKind: 'finish',
  226. sequence: null,
  227. label: control.label,
  228. displayTitle: control.displayContent ? control.displayContent.title : '比赛结束',
  229. displayBody: control.displayContent ? control.displayContent.body : '已完成终点打卡,本局结束。',
  230. displayAutoPopup: control.displayContent ? control.displayContent.autoPopup : true,
  231. displayOnce: control.displayContent ? control.displayContent.once : false,
  232. displayPriority: control.displayContent ? control.displayContent.priority : 2,
  233. }
  234. }
  235. const sequenceText = typeof control.sequence === 'number' ? String(control.sequence) : control.label
  236. return {
  237. type: 'control_completed',
  238. controlId: control.id,
  239. controlKind: 'control',
  240. sequence: control.sequence,
  241. label: control.label,
  242. displayTitle: control.displayContent ? control.displayContent.title : `收集 ${sequenceText}`,
  243. displayBody: control.displayContent ? control.displayContent.body : control.label,
  244. displayAutoPopup: control.displayContent ? control.displayContent.autoPopup : true,
  245. displayOnce: control.displayContent ? control.displayContent.once : false,
  246. displayPriority: control.displayContent ? control.displayContent.priority : 1,
  247. }
  248. }
  249. function buildPresentation(definition: GameDefinition, state: GameSessionState): GamePresentationState {
  250. const modeState = getModeState(state)
  251. const running = state.status === 'running'
  252. const startControl = getStartControl(definition)
  253. const finishControl = getFinishControl(definition)
  254. const completedStart = !!startControl && state.completedControlIds.includes(startControl.id)
  255. const completedFinish = !!finishControl && state.completedControlIds.includes(finishControl.id)
  256. const remainingControls = getRemainingScoreControls(definition, state)
  257. const scoreControls = getScoreControls(definition)
  258. const primaryTarget = definition.controls.find((control) => control.id === state.currentTargetControlId) || null
  259. const focusedTarget = getFocusedTarget(definition, state, remainingControls)
  260. const canSelectFinish = running && completedStart && !completedFinish && !!finishControl
  261. const activeControlIds = running && modeState.phase === 'controls'
  262. ? remainingControls.map((control) => control.id)
  263. : []
  264. const activeControlSequences = running && modeState.phase === 'controls'
  265. ? remainingControls
  266. .filter((control) => typeof control.sequence === 'number')
  267. .map((control) => control.sequence as number)
  268. : []
  269. const completedControls = scoreControls.filter((control) => state.completedControlIds.includes(control.id))
  270. const completedControlSequences = completedControls
  271. .filter((control) => typeof control.sequence === 'number')
  272. .map((control) => control.sequence as number)
  273. const revealFullCourse = completedStart
  274. const interactiveTarget = resolveInteractiveTarget(definition, modeState, primaryTarget, focusedTarget)
  275. const punchButtonEnabled = running
  276. && definition.punchPolicy === 'enter-confirm'
  277. && !!interactiveTarget
  278. && state.inRangeControlId === interactiveTarget.id
  279. const mapPresentation: MapPresentationState = {
  280. controlVisualMode: modeState.phase === 'controls' ? 'multi-target' : 'single-target',
  281. showCourseLegs: false,
  282. guidanceLegAnimationEnabled: false,
  283. focusableControlIds: canSelectFinish
  284. ? [...activeControlIds, finishControl!.id]
  285. : activeControlIds.slice(),
  286. focusedControlId: focusedTarget ? focusedTarget.id : null,
  287. focusedControlSequences: focusedTarget && focusedTarget.kind === 'control' && typeof focusedTarget.sequence === 'number'
  288. ? [focusedTarget.sequence]
  289. : [],
  290. activeControlIds,
  291. activeControlSequences,
  292. activeStart: running && modeState.phase === 'start',
  293. completedStart,
  294. activeFinish: running && modeState.phase === 'finish',
  295. focusedFinish: !!focusedTarget && focusedTarget.kind === 'finish',
  296. completedFinish,
  297. revealFullCourse,
  298. activeLegIndices: [],
  299. completedLegIndices: [],
  300. completedControlIds: completedControls.map((control) => control.id),
  301. completedControlSequences,
  302. skippedControlIds: [],
  303. skippedControlSequences: [],
  304. }
  305. const hudPresentation: HudPresentationState = {
  306. actionTagText: modeState.phase === 'start'
  307. ? '目标'
  308. : focusedTarget && focusedTarget.kind === 'finish'
  309. ? '终点'
  310. : modeState.phase === 'finish'
  311. ? '终点'
  312. : '自由',
  313. distanceTagText: modeState.phase === 'start'
  314. ? '点距'
  315. : focusedTarget && focusedTarget.kind === 'finish'
  316. ? '终点距'
  317. : focusedTarget
  318. ? '选中点距'
  319. : modeState.phase === 'finish'
  320. ? '终点距'
  321. : '最近点距',
  322. hudTargetControlId: focusedTarget
  323. ? focusedTarget.id
  324. : primaryTarget
  325. ? primaryTarget.id
  326. : null,
  327. progressText: `已收集 ${completedControls.length}/${scoreControls.length}`,
  328. punchableControlId: punchButtonEnabled && interactiveTarget ? interactiveTarget.id : null,
  329. punchButtonEnabled,
  330. punchButtonText: modeState.phase === 'start'
  331. ? '开始打卡'
  332. : focusedTarget && focusedTarget.kind === 'finish'
  333. ? '结束打卡'
  334. : modeState.phase === 'finish'
  335. ? '结束打卡'
  336. : '打点',
  337. punchHintText: buildPunchHintText(definition, state, primaryTarget, focusedTarget),
  338. }
  339. return {
  340. map: mapPresentation,
  341. hud: hudPresentation,
  342. }
  343. }
  344. function applyCompletion(
  345. definition: GameDefinition,
  346. state: GameSessionState,
  347. control: GameControl,
  348. at: number,
  349. referencePoint: LonLatPoint | null,
  350. ): GameResult {
  351. const completedControlIds = state.completedControlIds.includes(control.id)
  352. ? state.completedControlIds
  353. : [...state.completedControlIds, control.id]
  354. const previousModeState = getModeState(state)
  355. const nextStateDraft: GameSessionState = {
  356. ...state,
  357. startedAt: control.kind === 'start' && state.startedAt === null ? at : state.startedAt,
  358. endedAt: control.kind === 'finish' ? at : state.endedAt,
  359. completedControlIds,
  360. currentTargetControlId: null,
  361. inRangeControlId: null,
  362. score: getScoreControls(definition)
  363. .filter((item) => completedControlIds.includes(item.id))
  364. .reduce((sum, item) => sum + getControlScore(item), 0),
  365. status: control.kind === 'finish' ? 'finished' : state.status,
  366. guidanceState: 'searching',
  367. }
  368. const remainingControls = getRemainingScoreControls(definition, nextStateDraft)
  369. let phase: ScoreOModeState['phase']
  370. if (control.kind === 'finish') {
  371. phase = 'done'
  372. } else if (control.kind === 'start') {
  373. phase = remainingControls.length ? 'controls' : 'finish'
  374. } else {
  375. phase = remainingControls.length ? 'controls' : 'finish'
  376. }
  377. const nextModeState: ScoreOModeState = {
  378. phase,
  379. focusedControlId: control.id === previousModeState.focusedControlId ? null : previousModeState.focusedControlId,
  380. }
  381. const nextPrimaryTarget = phase === 'controls'
  382. ? getNearestRemainingControl(definition, nextStateDraft, referencePoint)
  383. : phase === 'finish'
  384. ? getFinishControl(definition)
  385. : null
  386. const nextState = withModeState({
  387. ...nextStateDraft,
  388. currentTargetControlId: nextPrimaryTarget ? nextPrimaryTarget.id : null,
  389. }, nextModeState)
  390. const effects: GameEffect[] = [buildCompletedEffect(control)]
  391. if (control.kind === 'finish') {
  392. effects.push({ type: 'session_finished' })
  393. }
  394. return {
  395. nextState,
  396. presentation: buildPresentation(definition, nextState),
  397. effects,
  398. }
  399. }
  400. export class ScoreORule implements RulePlugin {
  401. get mode(): 'score-o' {
  402. return 'score-o'
  403. }
  404. initialize(definition: GameDefinition): GameSessionState {
  405. const startControl = getStartControl(definition)
  406. return {
  407. status: 'idle',
  408. startedAt: null,
  409. endedAt: null,
  410. completedControlIds: [],
  411. skippedControlIds: [],
  412. currentTargetControlId: startControl ? startControl.id : null,
  413. inRangeControlId: null,
  414. score: 0,
  415. guidanceState: 'searching',
  416. modeState: {
  417. phase: 'start',
  418. focusedControlId: null,
  419. },
  420. }
  421. }
  422. buildPresentation(definition: GameDefinition, state: GameSessionState): GamePresentationState {
  423. return buildPresentation(definition, state)
  424. }
  425. reduce(definition: GameDefinition, state: GameSessionState, event: GameEvent): GameResult {
  426. if (event.type === 'session_started') {
  427. const startControl = getStartControl(definition)
  428. const nextState = withModeState({
  429. ...state,
  430. status: 'running',
  431. startedAt: null,
  432. endedAt: null,
  433. currentTargetControlId: startControl ? startControl.id : null,
  434. inRangeControlId: null,
  435. guidanceState: 'searching',
  436. }, {
  437. phase: 'start',
  438. focusedControlId: null,
  439. })
  440. return {
  441. nextState,
  442. presentation: buildPresentation(definition, nextState),
  443. effects: [{ type: 'session_started' }],
  444. }
  445. }
  446. if (event.type === 'session_ended') {
  447. const nextState = withModeState({
  448. ...state,
  449. status: 'finished',
  450. endedAt: event.at,
  451. guidanceState: 'searching',
  452. }, {
  453. phase: 'done',
  454. focusedControlId: null,
  455. })
  456. return {
  457. nextState,
  458. presentation: buildPresentation(definition, nextState),
  459. effects: [{ type: 'session_finished' }],
  460. }
  461. }
  462. if (state.status !== 'running') {
  463. return {
  464. nextState: state,
  465. presentation: buildPresentation(definition, state),
  466. effects: [],
  467. }
  468. }
  469. const modeState = getModeState(state)
  470. const targetControl = state.currentTargetControlId
  471. ? definition.controls.find((control) => control.id === state.currentTargetControlId) || null
  472. : null
  473. if (event.type === 'gps_updated') {
  474. const referencePoint = { lon: event.lon, lat: event.lat }
  475. const remainingControls = getRemainingScoreControls(definition, state)
  476. const focusedTarget = getFocusedTarget(definition, state, remainingControls)
  477. let nextPrimaryTarget = targetControl
  478. let guidanceTarget = targetControl
  479. let punchTarget: GameControl | null = null
  480. if (modeState.phase === 'controls') {
  481. nextPrimaryTarget = getNearestRemainingControl(definition, state, referencePoint)
  482. guidanceTarget = focusedTarget || nextPrimaryTarget
  483. if (focusedTarget && getApproxDistanceMeters(focusedTarget.point, referencePoint) <= definition.punchRadiusMeters) {
  484. punchTarget = focusedTarget
  485. } else if (!definition.requiresFocusSelection) {
  486. punchTarget = getNearestInRangeControl(remainingControls, referencePoint, definition.punchRadiusMeters)
  487. }
  488. } else if (modeState.phase === 'finish') {
  489. nextPrimaryTarget = getFinishControl(definition)
  490. guidanceTarget = focusedTarget || nextPrimaryTarget
  491. if (focusedTarget && getApproxDistanceMeters(focusedTarget.point, referencePoint) <= definition.punchRadiusMeters) {
  492. punchTarget = focusedTarget
  493. } else if (!definition.requiresFocusSelection && nextPrimaryTarget && getApproxDistanceMeters(nextPrimaryTarget.point, referencePoint) <= definition.punchRadiusMeters) {
  494. punchTarget = nextPrimaryTarget
  495. }
  496. } else if (targetControl) {
  497. guidanceTarget = targetControl
  498. if (getApproxDistanceMeters(targetControl.point, referencePoint) <= definition.punchRadiusMeters) {
  499. punchTarget = targetControl
  500. }
  501. }
  502. const guidanceState = guidanceTarget
  503. ? getGuidanceState(definition, getApproxDistanceMeters(guidanceTarget.point, referencePoint))
  504. : 'searching'
  505. const nextState: GameSessionState = {
  506. ...state,
  507. currentTargetControlId: nextPrimaryTarget ? nextPrimaryTarget.id : null,
  508. inRangeControlId: punchTarget ? punchTarget.id : null,
  509. guidanceState,
  510. }
  511. const guidanceEffects = getGuidanceEffects(state.guidanceState, guidanceState, guidanceTarget ? guidanceTarget.id : null)
  512. if (definition.punchPolicy === 'enter' && punchTarget) {
  513. const completionResult = applyCompletion(definition, nextState, punchTarget, event.at, referencePoint)
  514. return {
  515. ...completionResult,
  516. effects: [...guidanceEffects, ...completionResult.effects],
  517. }
  518. }
  519. return {
  520. nextState,
  521. presentation: buildPresentation(definition, nextState),
  522. effects: guidanceEffects,
  523. }
  524. }
  525. if (event.type === 'control_focused') {
  526. if (modeState.phase !== 'controls' && modeState.phase !== 'finish') {
  527. return {
  528. nextState: state,
  529. presentation: buildPresentation(definition, state),
  530. effects: [],
  531. }
  532. }
  533. const focusableControlIds = getRemainingScoreControls(definition, state).map((control) => control.id)
  534. const finishControl = getFinishControl(definition)
  535. if (finishControl && canFocusFinish(definition, state)) {
  536. focusableControlIds.push(finishControl.id)
  537. }
  538. const nextFocusedControlId = event.controlId && focusableControlIds.includes(event.controlId)
  539. ? modeState.focusedControlId === event.controlId
  540. ? null
  541. : event.controlId
  542. : null
  543. const nextState = withModeState({
  544. ...state,
  545. }, {
  546. ...modeState,
  547. focusedControlId: nextFocusedControlId,
  548. })
  549. return {
  550. nextState,
  551. presentation: buildPresentation(definition, nextState),
  552. effects: [],
  553. }
  554. }
  555. if (event.type === 'punch_requested') {
  556. const focusedTarget = getFocusedTarget(definition, state)
  557. if (definition.requiresFocusSelection && (modeState.phase === 'controls' || modeState.phase === 'finish') && !focusedTarget) {
  558. return {
  559. nextState: state,
  560. presentation: buildPresentation(definition, state),
  561. effects: [{ type: 'punch_feedback', text: modeState.phase === 'finish' ? '请先选中终点' : '请先选中目标点', tone: 'warning' }],
  562. }
  563. }
  564. let controlToPunch: GameControl | null = null
  565. if (state.inRangeControlId) {
  566. controlToPunch = definition.controls.find((control) => control.id === state.inRangeControlId) || null
  567. }
  568. if (!controlToPunch || (definition.requiresFocusSelection && focusedTarget && controlToPunch.id !== focusedTarget.id)) {
  569. return {
  570. nextState: state,
  571. presentation: buildPresentation(definition, state),
  572. effects: [{
  573. type: 'punch_feedback',
  574. text: focusedTarget
  575. ? `未进入${getDisplayTargetLabel(focusedTarget)}打卡范围`
  576. : modeState.phase === 'start'
  577. ? '未进入开始点打卡范围'
  578. : '未进入目标打点范围',
  579. tone: 'warning',
  580. }],
  581. }
  582. }
  583. return applyCompletion(definition, state, controlToPunch, event.at, this.getReferencePoint(definition, state, controlToPunch))
  584. }
  585. return {
  586. nextState: state,
  587. presentation: buildPresentation(definition, state),
  588. effects: [],
  589. }
  590. }
  591. private getReferencePoint(definition: GameDefinition, state: GameSessionState, completedControl: GameControl): LonLatPoint | null {
  592. if (completedControl.kind === 'control') {
  593. const remaining = getRemainingScoreControls(definition, {
  594. ...state,
  595. completedControlIds: [...state.completedControlIds, completedControl.id],
  596. })
  597. return remaining.length ? completedControl.point : (getFinishControl(definition) ? getFinishControl(definition)!.point : completedControl.point)
  598. }
  599. return completedControl.point
  600. }
  601. }