All files / app-play/src/ui random-mix-save-dialog.ts

45.45% Statements 20/44
41.66% Branches 15/36
28.57% Functions 4/14
44.73% Lines 17/38

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162                                            3x 3x 3x 5x 3x 3x       3x 1x   2x 2x         3x 3x 12x   3x 1x   3x 3x                                                                                                                                                                                                                                  
import { html, nothing, render, type TemplateResult } from 'lit';
import type { CourseView } from '@be-music/player-scene-model';
import { CONNECTION_LABELS, RANDOM_MIX_FIELDS } from '../course-maker-panel.ts';
import { formatLR2RandomMixSetting } from '@be-music/skin-lr2/state';
import { playerText } from '../player-copy.ts';
import { dialogShortcutAria, dialogShortcutHint } from './dialog-shortcuts.ts';
import { RANDOM_MIX_DIFFICULTY_NAMES } from './random-mix-dialog.ts';
import { formatRandomMixSaveName, type RandomMixSaveRequest, type RandomMixSetup } from '../random-mix-save.ts';
 
// The RANDOM MIX save dialog: once a RANDOM MIX run is over (played through or given up) and its result left, the select
// screen asks whether to keep the run's course as one of the player's own. It is named by the date and time the run
// ended unless the player types another name, lists the stages drawn, and shows the settings the run was started with,
// which it cannot change: they are what drew these stages.
 
/** One line of the settings the dialog shows. */
export interface RandomMixSetupRow {
  label: string;
  value: string;
}
 
/** The settings a run was started with, as the dialog lists them, in the RANDOM MIX dialog's order. */
export function describeRandomMixSetup(setup: RandomMixSetup): RandomMixSetupRow[] {
  const t = playerText().course.randomMixDialog;
  const save = playerText().course.randomMixSave;
  const { conditions, settings } = setup;
  const onOff = (on: boolean): string => (on ? save.on : save.off);
  const level = (value: number): string => (value > 0 ? String(value) : t.anyLevel);
  const rows: RandomMixSetupRow[] = [
    { label: save.kind, value: setup.kind === 'nonstop' ? 'NONSTOP' : 'EXPERT' },
    { label: t.keys, value: t.keyMode(conditions.keyMode) },
  ];
  if (conditions.by === 'level') {
    rows.push({ label: t.byLevel, value: `${level(conditions.minLevel)} – ${level(conditions.maxLevel)}` });
  } else {
    const names = conditions.difficulties.map((difficulty) => RANDOM_MIX_DIFFICULTY_NAMES[difficulty] ?? difficulty);
    rows.push(
      { label: t.byDifficulty, value: names.length > 0 ? names.join(', ') : save.none },
      { label: t.estimateDifficulty, value: onOff(conditions.estimateDifficulty) },
    );
  }
  rows.push({ label: t.count, value: conditions.count === 'infinity' ? t.infinity : String(conditions.count) });
  for (const { setting, label } of RANDOM_MIX_FIELDS) {
    rows.push({ label: label(), value: formatLR2RandomMixSetting(setting, settings[setting]) });
  }
  if (setup.kind === 'nonstop') {
    rows.push({ label: t.defaultConnection, value: CONNECTION_LABELS[setup.defaultConnection] });
  }
  rows.push({ label: t.allowBPMChanges, value: onOff(conditions.allowBPMChanges) });
  return rows;
}
 
/**
 * Opens the save dialog. Resolves the name to save the course under when Save is pressed (the offered one when the field
 * is left blank), or `undefined` when the player does not save it (Don't save, Escape). A dialog already open is left
 * as it is and resolves `undefined`.
 */
export function promptRandomMixSave(request: RandomMixSaveRequest): Promise<string | undefined> {
  const dialog = document.querySelector<HTMLDialogElement>('#random-mix-save-dialog');
  if (dialog === null || dialog.open) return Promise.resolve(undefined);
  const offered = formatRandomMixSaveName(request.endedAt);
  let name = offered;
  render(
    template(request, offered, (value) => {
      name = value;
    }),
    dialog,
  );
  dialog.returnValue = '';
  // The dialog owns keyboard navigation; its keys must not reach the select screen behind it.
  dialog.onkeydown = (event) => event.stopPropagation();
  return new Promise((resolve) => {
    dialog.addEventListener(
      'close',
      () => resolve(dialog.returnValue === 'save' ? name.trim() || offered : undefined),
      { once: true },
    );
    dialog.showModal();
  });
}
 
function stageLine(stage: CourseView['stages'][number]): TemplateResult {
  return html`<li class="random-mix-save-stage">
    <span class="random-mix-save-title"
      >${stage.title}${stage.subtitle !== undefined && stage.subtitle !== '' ? html` <small>${stage.subtitle}</small>` : nothing}</span
    >
    ${stage.artist !== undefined && stage.artist !== '' ? html`<span class="random-mix-save-artist">${stage.artist}</span>` : nothing}
  </li>`;
}
 
function template(request: RandomMixSaveRequest, offered: string, setName: (name: string) => void): TemplateResult {
  const t = playerText().course.randomMixSave;
  return html`<form
    method="dialog"
    class="account-dialog-body random-mix-form"
    @keydown=${(event: KeyboardEvent) => {
      // Enter in the name field saves, not the form's first submit button (Don't save).
      if (event.key !== 'Enter' || !(event.target instanceof HTMLInputElement)) return;
      event.preventDefault();
      const form = event.currentTarget as HTMLFormElement;
      form.requestSubmit(form.querySelector<HTMLButtonElement>('button[value="save"]'));
    }}
  >
    <header>
      <h2 id="random-mix-save-dialog-title">${t.title}</h2>
      <p id="random-mix-save-dialog-hint">${t.hint}</p>
    </header>
    <div class="random-mix-row">
      <label class="label" for="random-mix-save-name">${t.name}</label>
      <input
        class="input random-mix-save-name"
        id="random-mix-save-name"
        type="text"
        autocomplete="off"
        .value=${offered}
        placeholder=${offered}
        @input=${(event: Event) => setName((event.currentTarget as HTMLInputElement).value)}
      />
    </div>
    <section class="random-mix-group" aria-labelledby="random-mix-save-stages">
      <h3 class="label" id="random-mix-save-stages">${t.stages(request.course.stages.length)}</h3>
      <ol class="random-mix-save-stages">
        ${request.course.stages.map(stageLine)}
      </ol>
    </section>
    <section class="random-mix-group" aria-labelledby="random-mix-save-settings">
      <h3 class="label" id="random-mix-save-settings">${t.settings}</h3>
      <dl class="random-mix-save-setup">
        ${describeRandomMixSetup(request.setup).map(
          (row) =>
            html`<div class="random-mix-save-row">
              <dt>${row.label}</dt>
              <dd>${row.value}</dd>
            </div>`,
        )}
      </dl>
      <p class="random-mix-note">${t.settingsFixed}</p>
    </section>
    <footer>
      <button
        type="submit"
        value=""
        class="btn"
        data-variant="outline"
        data-dialog-shortcut="cancel"
        aria-keyshortcuts=${dialogShortcutAria('cancel')}
      >
        ${t.discard}${dialogShortcutHint('cancel')}
      </button>
      <button
        type="submit"
        value="save"
        class="btn"
        autofocus
        data-dialog-shortcut="primary"
        aria-keyshortcuts=${dialogShortcutAria('primary')}
      >
        ${t.save}${dialogShortcutHint('primary')}
      </button>
    </footer>
  </form>`;
}