All files / app-play/src random-mix-draw.ts

100% Statements 37/37
95.65% Branches 22/23
100% Functions 6/6
100% Lines 33/33

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                                                                                        17x 17x           17x 17x         64x 67x 67x 61x 61x 3x 3x   58x 58x 58x 58x 58x                             17x 17x 61x 61x 55x   17x       67x 86x       4x               67x 67x     17x 17x 17x 17x 2x 2x      
// A RANDOM MIX drawn one stage at a time from the charts that fit the RANDOM MIX dialog's conditions. Only the charts a
// draw walks have their BPMs read, and only the songs it picks have their chart file hashed (a course keys its stages
// by MD5), so a draw costs the same however large the library is. The draw rules are `drawRandomMixStage`'s
// (`@be-music/player-engine/course`).
 
import {
  drawRandomMixStage,
  type LR2RandomMixSettings,
  type RandomMixBPMRange,
  type RandomMixCandidate,
  type RandomMixStage,
} from '@be-music/player-engine/course';
import type { LibraryChart } from '@be-music/player-scene-model';
 
/** A entry a RANDOM MIX can draw: its candidate facts and the entry itself. */
export type RandomMixPoolChart = RandomMixCandidate & { readonly chart: LibraryChart };
 
/** A drawn stage: the entry and the stage as the course keys it. */
export interface RandomMixDrawnStage {
  chart: LibraryChart;
  stage: RandomMixStage;
}
 
export interface RandomMixDrawOptions {
  /** LR2's RANDOM MIX settings: the BPM limits, the BPM range and the favorite level. */
  settings: LR2RandomMixSettings;
  /** A entry's BPMs, read only for the songs a draw walks. */
  bpmOf: (chart: LibraryChart) => RandomMixBPMRange;
  /** A entry's chart MD5, read only for the songs drawn; `undefined` for one that cannot be hashed (never drawn again). */
  md5Of: (chart: LibraryChart) => Promise<string | undefined>;
  random: () => number;
  /** Draw a chart whose BPM changes too (`RandomMixConditions.allowBPMChanges`). */
  allowBPMChanges?: boolean;
  /**
   * An endless draw: once every entry folder has been drawn, the folders are free again (all but the last one drawn,
   * unless it is the only one), so the draw goes on as long as the pool holds a entry.
   */
  endless?: boolean;
}
 
/** The stages of one RANDOM MIX, drawn as they are asked for. */
export class RandomMixDraw {
  private readonly pool: readonly RandomMixPoolChart[];
  private readonly options: RandomMixDrawOptions;
  private readonly usedFolders = new Set<string>();
  private readonly excluded = new Set<RandomMixPoolChart>();
  private first: RandomMixPoolChart | undefined;
  private last: RandomMixPoolChart | undefined;
 
  /** `pool`: the charts that fit the conditions (`filterRandomMixCandidates`). */
  constructor(pool: readonly RandomMixPoolChart[], options: RandomMixDrawOptions) {
    this.pool = pool;
    this.options = options;
  }
 
  /** Draws the next stage, or `undefined` when no entry is left that fits. */
  async next(): Promise<RandomMixDrawnStage | undefined> {
    for (;;) {
      const entry = this.pick();
      if (entry === undefined) return undefined;
      const md5 = await this.options.md5Of(entry.chart);
      if (md5 === undefined || !/^[0-9a-f]{32}$/iu.test(md5)) {
        this.excluded.add(entry);
        continue;
      }
      this.first ??= entry;
      this.last = entry;
      this.usedFolders.add(entry.folder);
      const { chart } = entry;
      return {
        chart,
        stage: {
          md5: md5.toLowerCase(),
          keyMode: entry.keyMode,
          title: chart.title,
          ...(chart.subtitle !== undefined ? { subtitle: chart.subtitle } : {}),
          ...(chart.artist !== undefined ? { artist: chart.artist } : {}),
        },
      };
    }
  }
 
  /** Draws `count` stages up front (fewer when the pool runs out). */
  async take(count: number): Promise<RandomMixDrawnStage[]> {
    const stages: RandomMixDrawnStage[] = [];
    while (stages.length < count) {
      const stage = await this.next();
      if (stage === undefined) break;
      stages.push(stage);
    }
    return stages;
  }
 
  private pick(): RandomMixPoolChart | undefined {
    const draw = () =>
      drawRandomMixStage(
        this.pool,
        {
          settings: this.options.settings,
          bpmOf: (entry) => this.options.bpmOf(entry.chart),
          ...(this.options.allowBPMChanges === true ? { allowBPMChanges: true } : {}),
          ...(this.first !== undefined ? { first: this.first } : {}),
          usedFolders: this.usedFolders,
          excluded: this.excluded,
        },
        this.options.random,
      );
    const entry = draw();
    if (entry !== undefined || this.options.endless !== true || this.last === undefined) return entry;
    // Every folder has been drawn: free them again, all but the last one, so a entry does not follow itself unless the
    // pool has only its folder.
    this.usedFolders.clear();
    this.usedFolders.add(this.last.folder);
    const again = draw();
    if (again !== undefined) return again;
    this.usedFolders.clear();
    return draw();
  }
}