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

80% Statements 52/65
55% Branches 11/20
75% Functions 36/48
77.08% Lines 37/48

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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122                                                      2886x               2852x 64x 64x 1088x 1088x   64x   332x 332x     332x 768x                     32x 34x 32x 34x 34x 32x                 32x               16x 32x 16x 16x 4812x 2406x   4852x 16x 4742x 4742x 32x                                   26x 156x   4742x 14x 2489x 32x 32x 32x      
import type { PreparedPlaybackChartData } from './bootstrap.ts';
import type { JudgeCoreScore } from './judge-core-types.ts';
import type { PlayerSummary } from './engine-types.ts';
import type { PlaySession } from './play-session.ts';
import type { JudgeStrategy, PlayJudge } from './play-strategy.ts';
import type { RulesetConfig } from '../ruleset/definitions.ts';
import type { PlayerHCNLaneState } from './ui-signal-bus.ts';
 
/**
 * Two players on one chart: LR2 BATTLE (`battle` 1) and That-Which-Must-Not-Be-Named two-player play. Each side of
 * the duplicated chart is its own player, with its own judgments, combo, score and gauge; the chart, its clock and its
 * audio are shared. Each side is judged by the play's own strategy on a view of the session that sees only that side.
 */
 
/** The second player of a two-player play: its ruleset (its gauge and its notes' TOTAL), score and summary. */
export interface SecondPlayer {
  readonly ruleset: RulesetConfig;
  readonly score: JudgeCoreScore;
  readonly summary: PlayerSummary;
  readonly endOnFailure: boolean;
  readonly resetScore: () => void;
  /** The gauge the 2P player runs on: a chart's GAMELEVEL / EXTRA (#845), else its own pick, else 1P's. */
  readonly gauge?: string;
}
 
/** A side owns its lane channels (`1x` / `2x`), including its mines and invisible objects. */
export function isSideChannel(channel: string, side: 1 | 2): boolean {
  return channel.startsWith(String(side));
}
 
/**
 * The view of the prepared chart a side's judge reads: that side's notes, lanes and key bindings. The note objects are
 * the shared ones, so the judged marks reach the renderer.
 */
export function selectPlaybackChartSide(chart: PreparedPlaybackChartData, side: 1 | 2): PreparedPlaybackChartData {
  const own = (channel: string): boolean => isSideChannel(channel, side);
  const inputTokenToChannels = new Map<string, string[]>();
  for (const [token, channels] of chart.inputTokenToChannels) {
    const owned = channels.filter(own);
    if (owned.length > 0) inputTokenToChannels.set(token, owned);
  }
  return {
    ...chart,
    notes: chart.notes.filter((note) => own(note.channel)),
    scorableNotes: chart.scorableNotes.filter((note) => own(note.channel)),
    landmineNotes: chart.landmineNotes.filter((note) => own(note.channel)),
    invisibleNotes: chart.invisibleNotes.filter((note) => own(note.channel)),
    laneSoundNotes: chart.laneSoundNotes.filter((note) => own(note.channel)),
    laneBindings: chart.laneBindings.filter((binding) => own(binding.channel)),
    inputTokenToChannels,
  };
}
 
/** The session a side's judge sees: the shared session (its clock and buses read live) with the side's own parts. */
function sideSession(
  session: PlaySession,
  side: 1 | 2,
  parts: Pick<PlaySession, 'ruleset' | 'score' | 'summary' | 'endOnFailure'>,
): PlaySession {
  const view = Object.create(session) as PlaySession;
  const own = (channel: string): boolean => isSideChannel(channel, side);
  const replayInputs = session.options.replayInputs
    ?.map((event) => ({ ...event, channels: event.channels.filter(own) }))
    .filter((event) => event.channels.length > 0);
  Object.defineProperties(view, {
    playbackChart: { value: selectPlaybackChartSide(session.playbackChart, side) },
    ruleset: { value: parts.ruleset },
    score: { value: parts.score },
    summary: { value: parts.summary },
    endOnFailure: { value: parts.endOnFailure },
    options: { value: replayInputs === undefined ? session.options : { ...session.options, replayInputs } },
    readHCNLanes: { value: undefined, writable: true },
  });
  return view;
}
 
/**
 * One judge for both players: each side's judge from `strategy`, ticked, sought and finished together. The play ends
 * when both sides are settled, or when judging closed for both (LR2 stops once both players have failed).
 */
export function createTwoPlayerJudge(strategy: JudgeStrategy, session: PlaySession, second: SecondPlayer): PlayJudge {
  const views = [sideSession(session, 1, session), sideSession(session, 2, second)] as const;
  const judges = views.map((view) => strategy.createJudge(view));
  const empty: readonly PlayerHCNLaneState[] = [];
  session.readHCNLanes = (seconds) => {
    const lanes = views.flatMap((view) => view.readHCNLanes?.(seconds) ?? empty);
    return lanes.length === 0 ? empty : lanes;
  };
  const each = (run: (judge: PlayJudge) => void): void => judges.forEach(run);
  return {
    beforeAudio: (tick) => each((judge) => judge.beforeAudio?.(tick)),
    judge: (tick) => each((judge) => judge.judge(tick)),
    ...(judges.every((judge) => judge.seek !== undefined)
      ? {
          seek: (seconds: number) => {
            const rebuilt = judges.map((judge) => judge.seek!(seconds));
            return {
              bmsonBGAInputs: rebuilt
                .flatMap((part) => part.bmsonBGAInputs ?? [])
                .sort((a, b) => a.seconds - b.seconds),
              chartTextInputs: rebuilt
                .flatMap((part) => part.chartTextInputs ?? [])
                .sort((a, b) => a.seconds - b.seconds),
              judgments: rebuilt.flatMap((part) => part.judgments).sort((a, b) => a.seconds - b.seconds),
              heldLongNotes: rebuilt.flatMap((part) => part.heldLongNotes),
              hellChargeNotes: rebuilt.flatMap((part) => part.hellChargeNotes ?? []),
            };
          },
        }
      : {}),
    ...(judges.some((judge) => judge.handleLaneCommand !== undefined)
      ? { handleLaneCommand: (command) => each((judge) => judge.handleLaneCommand?.(command)) }
      : {}),
    judgedThroughSeconds: (tick) => Math.min(...judges.map((judge) => judge.judgedThroughSeconds(tick))),
    isSettled: () => judges.every((judge) => judge.isSettled() || judge.isClosed()),
    isClosed: () => judges.every((judge) => judge.isClosed()),
    beforeFinish: () => each((judge) => judge.beforeFinish?.()),
    finish: () => each((judge) => judge.finish()),
    settledThroughSeconds: () => Math.max(...judges.map((judge) => judge.settledThroughSeconds())),
  };
}