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

100% Statements 22/22
100% Branches 45/45
100% Functions 1/1
100% Lines 21/21

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                                          3019x 3019x   3019x 3019x 4x   3015x 121x   3015x 3015x 3015x   3015x       3019x 3015x 3019x 3019x 2096x       2096x     3015x 2707x                                                                 3015x     3019x    
import { PLUGOUT_SCORE_MAX } from '../ruleset/plugout.ts';
import { formatSeconds } from '../utils.ts';
import { renderSummary, writeRuntimeEventLog } from './engine-trace.ts';
import { PlayerInterruptedError, type PlayerSummary } from './engine-types.ts';
import type { GaugeType } from '../ruleset/gauge-type.ts';
import type { PlayControls } from './play-controls.ts';
import type { PlayOutcome } from './play-loop.ts';
import type { PlaySession } from './play-session.ts';
import type { PlayJudge } from './play-strategy.ts';
 
/**
 * Ends a play the same way whatever judged it: an ESC returns the summary of an aborted play, a restart or Ctrl+C
 * throws {@link PlayerInterruptedError}, anything else completes (or fails). The replay is finalized and handed to
 * the host (unless the play sought), the no-TUI trace records the end, and the summary is written out.
 */
export function finishPlay(
  session: PlaySession,
  judge: PlayJudge,
  controls: PlayControls,
  outcome: PlayOutcome,
): PlayerSummary {
  const { options, summary, totalSeconds, replayRecorder, writeOutput } = session;
  const { interruptedReason, endedOnFailure } = outcome;
  // View changes the host sent as the play ended, after the loop's last drain, still belong to it.
  controls.recordPendingViewChanges();
  if (interruptedReason === 'ctrl-c' || interruptedReason === 'restart') {
    throw new PlayerInterruptedError(interruptedReason);
  }
  if (session.ruleset.comboBreaksOnEmptyPoor && summary.emptyPoor > 0) {
    summary.fullCombo = summary.total > 0 && session.score.scoreTracker.combo === summary.total;
  }
  const aborted = interruptedReason === 'escape';
  const reversal = session.reversalAnchor;
  const stoppedAt = judge.settledThroughSeconds();
  const exitedReversal =
    aborted &&
    reversal !== undefined &&
    Math.round(stoppedAt * 1e6) >= Math.round(reversal.seconds * 1e6) &&
    (!session.replay || options.replayExitedReversal === true);
  if (exitedReversal) judge.finish();
  const reason = aborted ? 'escape' : endedOnFailure ? 'failed' : 'complete';
  const endSeconds = exitedReversal || outcome.completedAtReversal ? stoppedAt : totalSeconds;
  if (!session.uiEnabled) {
    writeRuntimeEventLog(writeOutput, 'playback-end', [
      ['time', formatSeconds(endSeconds)],
      ['reason', reason],
    ]);
    session.stateLogger.logResult(endSeconds, { reason, summary });
  }
  // A play that sought is practice: its replay was discarded at the first seek.
  if (replayRecorder && session.seekGeneration === 0) {
    options.onReplayRecorded?.(
      replayRecorder.finalize({
        summary,
        maxCombo: session.score.scoreTracker.maxCombo,
        completedAtReversal: outcome.completedAtReversal,
        reversalActivationSeconds:
          session.replay && options.replayReversalActivationUs === undefined
            ? undefined
            : session.reversalAnchor?.seconds,
        ...(session.secondPlayer !== undefined
          ? {
              secondPlayer: {
                summary: session.secondPlayer.summary,
                maxCombo: session.secondPlayer.score.scoreTracker.maxCombo,
                gauge: (session.secondPlayer.gauge ??
                  session.options.gauge2P ??
                  session.options.gauge ??
                  'GROOVE') as GaugeType,
              },
            }
          : {}),
        ...(aborted
          ? {
              aborted: true,
              // Where the judge had settled through, so a re-simulation stops there and a course counts the stage's
              // time up to it.
              abortedAtSeconds: stoppedAt,
              exitedReversal,
            }
          : {}),
      }),
    );
  }
  writeOutput(
    renderSummary(summary, session.ruleset.plugoutScore === true ? PLUGOUT_SCORE_MAX : session.ruleset.moneyScore),
  );
  return summary;
}