All files / player-engine/src/core play.ts

100% Statements 7/7
100% Branches 6/6
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                                  3027x   3019x     3027x 3027x 3019x             2703x         324x    
import type { BeMusicJSON } from '@be-music/chart-source';
import type { PlayerOptions, PlayerSummary } from './engine-types.ts';
import { autoStrategy } from './play-auto.ts';
import { createPlayControls } from './play-controls.ts';
import { finishPlay } from './play-finish.ts';
import { runPlayLoop } from './play-loop.ts';
import { manualStrategy } from './play-manual.ts';
import { replayStrategy } from './play-replay.ts';
import { createPlaySession } from './play-session.ts';
import type { JudgeStrategy } from './play-strategy.ts';
import { createTwoPlayerJudge } from './two-player.ts';
 
/**
 * Plays `json` with the judge `strategy` through the one play loop: sets the session up, runs the loop and finishes
 * the play. Internal: hosts call {@link manualPlay} or {@link autoPlay}.
 */
export async function play(json: BeMusicJSON, options: PlayerOptions, strategy: JudgeStrategy): Promise<PlayerSummary> {
  const session = await createPlaySession(json, options, strategy);
  const judge =
    session.secondPlayer !== undefined
      ? createTwoPlayerJudge(strategy, session, session.secondPlayer)
      : strategy.createJudge(session);
  const controls = createPlayControls(session, judge);
  const outcome = await runPlayLoop(session, judge, controls);
  return finishPlay(session, judge, controls, outcome);
}
 
/**
 * Manual play: judges the player's keys, or re-drives a recorded replay when `options.replayInputs` is set.
 */
export async function manualPlay(json: BeMusicJSON, options: PlayerOptions = {}): Promise<PlayerSummary> {
  return play(json, options, options.replayInputs !== undefined ? replayStrategy : manualStrategy);
}
 
/** AUTO play: plays and judges every note itself. */
export async function autoPlay(json: BeMusicJSON, options: PlayerOptions = {}): Promise<PlayerSummary> {
  return play(json, options, autoStrategy);
}