All files / chart-model/src fingerprint-compose.ts

93.61% Statements 44/47
80% Branches 16/20
100% Functions 7/7
93.47% Lines 43/46

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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                                                                                  659x 659x 28663x     28663x 28663x 28663x 26519x     26519x 26519x 71165x 71165x 117273x   26519x   28663x                     658x 152x   506x       12x                         658x 658x 568x     568x 568x 568x 674x 6439x           568x   506x 6271x 6271x 7220x   6271x         4518x 4518x 4518x 9853x 9853x 10119x 10119x   9853x     9853x 21615x           4518x    
/**
 * Composition of play documents from a chart model or a chart document: the play document of one outcome. Not part of
 * the package API; the differential tests use it to check the chart model against the parser.
 */
import type { BeMusicJSON } from '@be-music/chart-source';
import type { RandomDraws } from './control-flow.ts';
import type { Rational } from './fingerprint-rational.ts';
import { classOfDraws } from './fingerprint-control-flow.ts';
import {
  parseJudge,
  parseMine,
  parseNote,
  parseRational,
  parseTimeline,
  parseTotal,
  resolveLaneModeExtension,
  type FlatItems,
} from './fingerprint-items.ts';
import {
  type BeMusicFingerprintDocument,
  type BeMusicFingerprintOptions,
  type BeMusicPlayFingerprintDocument,
  type FingerprintRandomNode,
  assemblePlayDocument,
} from './fingerprint-document.ts';
import { type ItemDelta, applyDelta, parseDelta } from './fingerprint-delta.ts';
import { buildChartModel } from './fingerprint-chart-model.ts';
 
// ---------------------------------------------------------------------------------------------------------------------
// Composition (used by tests and by verifiers): the play document of one outcome
// ---------------------------------------------------------------------------------------------------------------------
 
/**
 * A function from an outcome (the chart's `RandomDraws`, as a replay records them) to its play document, composed
 * from the chart model rather than by resolving the chart. The function throws for an outcome of an opaque factor. Not
 * part of the package API; used to check the model against the parser.
 */
export function createOutcomeComposer(
  json: BeMusicJSON,
  options: BeMusicFingerprintOptions,
): (draws: RandomDraws) => BeMusicPlayFingerprintDocument {
  const model = buildChartModel(json, resolveLaneModeExtension(options.chartPath));
  return (draws) => {
    Iif (!model.topFlat || !model.analysis) {
      return model.document as BeMusicPlayFingerprintDocument;
    }
    const analysis = model.analysis;
    const items: FlatItems = { ...model.topFlat };
    for (const factor of model.factors) {
      Iif (!factor.tree) {
        throw new Error('fingerprint: an opaque factor has no per-outcome content');
      }
      let tree = factor.tree;
      while (tree.kind === 'node') {
        const variable = analysis.variables[tree.variable]!;
        const valueClass = classOfDraws(variable, draws);
        tree = tree.branches.find((branch) => branch.classes.includes(valueClass))!.tree;
      }
      applyDelta(items, tree.delta);
    }
    return assemblePlayDocument(items, model.extension);
  };
}
 
/**
 * Every outcome a chart document stands for, with its probability, composed from the document alone: one entry per
 * combination of branch paths of its `random` nodes. Not part of the package API; used to check documents in tests.
 */
export function expandChartFingerprintDocument(
  document: BeMusicFingerprintDocument,
): Array<{ probability: Rational; document: BeMusicPlayFingerprintDocument }> {
  if (!('random' in document)) {
    return [{ probability: { n: 1n, d: 1n }, document }];
  }
  const top: FlatItems = {
    notes: document.notes.map(parseNote),
    mines: document.mines.map(parseMine),
    timeline: document.timeline.map(parseTimeline),
    lengths: new Map(document.measures.lengths.map(([measure, length]) => [Number(measure), parseRational(length)])),
    header: {
      bpm: parseRational(document.bpm),
      baseBPM: document.baseBPM === null ? null : parseRational(document.baseBPM),
      judge: parseJudge(document.judge),
      total: parseTotal(document.total),
      player3: document.lanes.player3,
      modeHint: document.lanes.modeHint ?? null,
      lrxLaneType: document.lanes.lrxLaneType ?? null,
      barLines: true,
      playOptions: { ...document.playOptions },
    },
  };
  let combos: Array<{ probability: Rational; deltas: ItemDelta[] }> = [{ probability: { n: 1n, d: 1n }, deltas: [] }];
  for (const node of document.random) {
    Iif ('opaque' in node) {
      throw new Error('fingerprint: an opaque node has no per-outcome content');
    }
    const leaves = expandNode(node);
    const next: typeof combos = [];
    for (const combo of combos) {
      for (const leaf of leaves) {
        next.push({
          probability: { n: combo.probability.n * leaf.probability.n, d: combo.probability.d * leaf.probability.d },
          deltas: [...combo.deltas, leaf.delta],
        });
      }
    }
    combos = next;
  }
  return combos.map((combo) => {
    const items: FlatItems = { ...top };
    for (const delta of combo.deltas) {
      applyDelta(items, delta);
    }
    return { probability: combo.probability, document: assemblePlayDocument(items, document.lanes.extension) };
  });
}
 
function expandNode(node: FingerprintRandomNode): Array<{ probability: Rational; delta: ItemDelta }> {
  const max = BigInt(node.random);
  const result: Array<{ probability: Rational; delta: ItemDelta }> = [];
  for (const branch of node.branches) {
    let count = 0n;
    for (const range of branch.values) {
      const dash = range.indexOf('-');
      count += dash < 0 ? 1n : BigInt(range.slice(dash + 1)) - BigInt(range.slice(0, dash)) + 1n;
    }
    const children = branch.next
      ? expandNode(branch.next)
      : [{ probability: { n: 1n, d: 1n }, delta: parseDelta(branch) }];
    for (const child of children) {
      result.push({
        probability: { n: count * child.probability.n, d: max * child.probability.d },
        delta: child.delta,
      });
    }
  }
  return result;
}