All files / player-ui/src/components gameplay-cabinet.ts

2.77% Statements 1/36
0% Branches 0/118
0% Functions 0/12
3.12% Lines 1/32

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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314                                      48x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
import { html, nothing, type TemplateResult } from 'lit';
import {
  formatScoreDiff,
  resolveCabinetLayout,
  resolveScratchAngle,
  resolveDifficulty,
  resolveDifficultyName,
  formatPacemakerLabel,
  resolveScorePace,
  resolveStageLabel,
  type BeMusicDOMGameplayFrame,
  type BeMusicRect,
  type ScorePace,
} from '@be-music/player-skin-sdk';
import type { BeMusicGameplayPanels } from '../gameplay-data.ts';
 
type BeMusicSecondPlayerPanels = Pick<BeMusicGameplayPanels, 'score' | 'gauge' | 'judgments'>;
 
/** DJ level lines on the score graph, as fractions of the maximum EX score. */
const RANK_LINES = [
  ['AAA', 8 / 9],
  ['AA', 7 / 9],
  ['A', 6 / 9],
] as const;
 
/** What the cabinet arrangement draws from, besides the frame. */
export interface GameplayCabinetInput {
  readonly frame: BeMusicDOMGameplayFrame;
  readonly panels: BeMusicGameplayPanels;
  readonly second: BeMusicSecondPlayerPanels | undefined;
  /** The Pause / Retry / Back buttons and the play-information toggle, placed in the stage band. */
  readonly actions: TemplateResult;
  /** The play information, shown over the BGA while open. */
  readonly details: TemplateResult | typeof nothing;
}
 
/**
 * The landscape gameplay HUD in the cabinet layout of `@be-music/player-skin-sdk` (`resolveCabinetLayout`): each panel
 * placed on its rect of the stage, in percentages, so it follows the letterboxed stage like the playfield does.
 */
export function renderGameplayCabinet({
  frame: f,
  panels,
  second,
  actions,
  details,
}: GameplayCabinetInput): TemplateResult {
  const { stage } = f.layout;
  const cabinet = resolveCabinetLayout(f.layout);
  const place = (rect: BeMusicRect): string =>
    `left:${(rect.x / stage.width) * 100}%;top:${(rect.y / stage.height) * 100}%;width:${(rect.w / stage.width) * 100}%;height:${(rect.h / stage.height) * 100}%`;
  const r = f.runtime;
  const pace = resolveScorePace(r);
  const { keys, gauge } = cabinet;
  const gaugeBox = {
    x: gauge.x,
    y: keys.y + keys.h,
    w: gauge.w,
    h: gauge.y + gauge.h - keys.y - keys.h,
  };
  const gaugeStyle = (rect: BeMusicRect): string =>
    `${place(rect)};--gauge-caption-y:${((cabinet.gaugeLabelY - rect.y) / rect.h) * 100}%;--gauge-bar-y:${((gauge.y - rect.y) / rect.h) * 100}%;--gauge-bar-height:${(gauge.h / rect.h) * 100}%`;
  const split = second && !cabinet.secondSide ? gaugeBox.x + (gaugeBox.w + 12) / 2 : gaugeBox.x + gaugeBox.w;
  const secondGaugeBox = cabinet.secondSide
    ? { ...gaugeBox, x: cabinet.secondSide.gauge.x, w: cabinet.secondSide.gauge.w }
    : { ...gaugeBox, x: split, w: gaugeBox.x + gaugeBox.w - split };
  const hiSpeed = (r.hiSpeed ?? 0).toFixed(r.floatingHiSpeed ? 2 : 1);
  const difficulty = resolveDifficultyName(f.chart);
  const difficultyBasis = resolveDifficulty(f.chart)?.basis;
  const difficultyEstimate = difficultyBasis === 'chart' ? undefined : difficultyBasis;
  const level = f.chart?.playLevel;
  const counts = [
    ['PG', panels.judgments.perfect],
    ['GR', panels.judgments.great],
    ['GD', panels.judgments.good],
    ['BD', panels.judgments.bad],
    ['PR', panels.judgments.poor],
    ['CB', panels.judgments.bad + panels.judgments.poor],
  ] as const;
  const tempo = r.tempo ?? 1;
  const mode = html`<span class="eyebrow"
    >${f.replay ? 'Replay' : f.autoPlay ? 'Auto play' : f.stage === 'loading' ? 'Loading' : 'Live'}</span
  >`;
  return html`<section
    class="gameplay cabinet"
    data-orientation="landscape"
    ?data-double-play=${cabinet.doublePlay}
    ?data-two-player=${!!cabinet.secondSide}
    style=${`--stage-ratio:${stage.width / stage.height};--stage-height:${stage.height};--playfield-center:${(f.layout.playfield.centerX / stage.width) * 100}%`}
  >
    <div class="cab play-frame" part="play" style=${place(cabinet.play)}></div>
    ${
      cabinet.progress
        ? html`<div class="cab chart-progress" part="progress" style=${place(cabinet.progress)} aria-hidden="true">
            <div class="progress-fill" style=${`height:${Math.max(0, Math.min(1, r.progressRatio ?? 0)) * 100}%`}></div>
          </div>`
        : nothing
    }
    <div class="cab keys" part="keys" style=${place(keys)} aria-hidden="true">
      ${f.layout.lanes.map(
        (lane) =>
          html`<span
            class="key"
            data-kind=${lane.kind}
            ?data-active=${(r.keyBeams?.[lane.channel ?? ''] ?? 0) > 0}
            style=${`--scratch-angle:${resolveScratchAngle(r.chartBeat ?? 0, r.scratchInputs?.[lane.channel ?? ''], r.nowMs)}rad;left:${((lane.x - keys.x) / keys.w) * 100}%;width:${(lane.w / keys.w) * 100}%`}
          ></span>`,
      )}
    </div>
    ${
      !r.hasBGA && cabinet.bga.w > 0
        ? html`<div class="cab panel sound-only" style=${place(cabinet.bga)}>
            <span class="eyebrow">Sound only</span>
          </div>`
        : nothing
    }
    <be-music-gauge-panel
      class="cab"
      compact
      data-cabinet
      exportparts="meter:gauge-meter"
      style=${gaugeStyle(second && !cabinet.secondSide ? { ...gaugeBox, w: split - 6 - gaugeBox.x } : gaugeBox)}
      .data=${panels.gauge}
    ></be-music-gauge-panel>
    ${
      second
        ? html`<be-music-gauge-panel
            class="cab"
            compact
            data-cabinet
            exportparts="meter:gauge-meter"
            style=${gaugeStyle(secondGaugeBox)}
            .data=${second.gauge}
          ></be-music-gauge-panel>`
        : nothing
    }
    ${
      cabinet.center
        ? html`<div class="cab panel center" part="center" style=${place(cabinet.center)} aria-label="Gauge">
            <strong>${Math.floor(Math.max(0, Math.min(100, panels.gauge.value)))}</strong>
          </div>`
        : nothing
    }
    <div class="cab panel figure" part="score" style=${place(cabinet.score)}>
      <span class="eyebrow">${cabinet.secondSide ? '1P Score' : 'Score'}</span><strong>${r.score ?? 0}</strong>
    </div>
    <div class="cab panel figure" part="hi-speed" style=${place(cabinet.hiSpeed)}>
      <span class="eyebrow">${r.floatingHiSpeed ? 'FHS' : 'Hi-speed'}</span><strong>${hiSpeed}</strong>
    </div>
    ${
      cabinet.secondSide && r.secondPlayer
        ? html`
            <div class="cab panel figure" part="second-score" style=${place(cabinet.secondSide.score)}>
              <span class="eyebrow">2P Score</span><strong>${r.secondPlayer.score ?? 0}</strong>
            </div>
            <div class="cab panel figure" part="second-hi-speed" style=${place(cabinet.secondSide.hiSpeed)}>
              <span class="eyebrow">${r.floatingHiSpeed ? 'FHS' : 'Hi-speed'}</span><strong>${hiSpeed}</strong>
            </div>
          `
        : nothing
    }
    ${cabinet.graph ? renderGraph(cabinet.graph, pace, r.random1P, place, !!cabinet.secondSide) : nothing}
    <div class="cab panel stage-band" part="stage" style=${place(cabinet.stage)}>
      ${mode}
      <strong>${resolveStageLabel(f.course, r.practice)}</strong>
      ${cabinet.controls ? nothing : actions}
    </div>
    ${
      cabinet.controls
        ? html`<div class="cab panel stage-band controls" part="controls" style=${place(cabinet.controls)}>
            ${actions}
          </div>`
        : nothing
    }
    <div class="cab panel players" part="players" style=${place(cabinet.players)}>
      <div class="plate">
        <span class="eyebrow">${r.playerName ?? 'Player 1P'}</span>
        ${
          difficulty
            ? html`<span class="difficulty" part="difficulty" data-estimated=${difficultyEstimate ?? nothing}
                >${difficulty}</span
              >`
            : nothing
        }
        <strong>Lv.${level ?? '-'}</strong>
      </div>
      <div class="chart-title">
        <h2 title=${panels.title}>${panels.title}</h2>
        <p class="muted">${f.chart?.artist ?? ''}</p>
      </div>
      ${
        second
          ? html`<div class="plate" part="second-player" aria-label="2P">
              <span class="eyebrow">Player 2P</span><strong>EX ${second.score.exScore}</strong>
            </div>`
          : nothing
      }
    </div>
    <div class="cab panel judge" part="judge" style=${place(cabinet.judge)}>
      ${cabinet.secondSide ? html`<span class="judge-owner eyebrow">1P JUDGE</span>` : nothing}
      ${
        // The player turned the judgment counts off: the panel keeps its frame.
        r.hidesJudgeCounts
          ? nothing
          : html`<dl class="counts">
                ${counts.map(
                  ([label, value]) =>
                    html`<dt>${label}</dt>
                      <dd>${value}</dd>`,
                )}
              </dl>
              <dl class="fast-slow">
                <dt class="fast">FAST</dt>
                <dd>${panels.judgments.fast}</dd>
                <dt class="slow">SLOW</dt>
                <dd>${panels.judgments.slow}</dd>
              </dl>
              ${renderTiming(r.lastJudgeDeltaMs, r.judgeWindows?.note.great.lateMs ?? 40)}`
      }
    </div>
 
    <div class="cab panel bpm" part="bpm" style=${place(cabinet.bpm)}>
      <strong>${Math.round(f.bpm ?? r.bpm ?? 0)}</strong>
      <div class="bpm-range">
        <span><span class="eyebrow">MIN</span>${Math.round(r.minBPM ?? f.bpm ?? 0)}</span>
        <span class="eyebrow">BPM</span>
        <span><span class="eyebrow">MAX</span>${Math.round(r.maxBPM ?? f.bpm ?? 0)}</span>
      </div>
    </div>
    <div class="cab panel tempo" part="tempo" style=${place(cabinet.tempo)}>
      <span class="eyebrow">Tempo</span><strong>${tempo === 1 ? 'OFF' : `x${tempo.toFixed(2)}`}</strong>
    </div>
    ${
      details === nothing
        ? nothing
        : html`<div
            class="cab cabinet-details"
            style=${place({ x: cabinet.bga.x, y: cabinet.bga.y, w: cabinet.bga.w, h: cabinet.bga.h })}
          >
            ${details}
          </div>`
    }
    ${f.stage === 'paused' ? html`<p class="paused" part="paused" role="status">Paused</p>` : nothing}
  </section>`;
}
 
/** YOU and the pacemaker over three bars (the player, the best record, the pacemaker), with the gaps under them. */
function renderGraph(
  graph: NonNullable<ReturnType<typeof resolveCabinetLayout>['graph']>,
  pace: ScorePace,
  random: string | undefined,
  place: (rect: BeMusicRect) => string,
  twoPlayer = false,
): TemplateResult {
  const height = (score: number) => `${pace.max > 0 ? Math.min(1, score / pace.max) * 100 : 0}%`;
  const bar = (kind: string, full: number | undefined, now: number) =>
    html`<div class="bar" data-kind=${kind}>
      ${full === undefined ? nothing : html`<span class="bar-full" style=${`height:${height(full)}`}></span>`}
      <span class="bar-now" style=${`height:${height(now)}`}></span>
    </div>`;
  const diff = (value: number | undefined) => (value === undefined ? '----' : formatScoreDiff(value));
  return html`<div class="cab panel graph-header" part="graph-header" style=${place(graph.header)}>
      <dl>
        <dt>${twoPlayer ? '1P' : 'YOU'}</dt>
        <dd>${pace.exScore}</dd>
        <dt>PACEMAKER</dt>
        <dd>${pace.target.exScore}</dd>
      </dl>
    </div>
    <div class="cab panel graph" part="graph" style=${place(graph.bars)} role="img" aria-label="Score graph">
      <span class="eyebrow">Max score</span>
      <div class="plot">
        ${RANK_LINES.map(
          ([label, share]) => html`<span class="rank-line" style=${`bottom:${share * 100}%`}>${label}</span>`,
        )}
        <div class="bars">
          ${bar('you', undefined, pace.exScore)}
          ${pace.best ? bar('best', pace.best.exScore, pace.best.pace) : html`<div class="bar"></div>`}
          ${bar('target', pace.target.exScore, pace.target.pace)}
        </div>
      </div>
      <dl class="diffs">
        <dt>TARGET</dt>
        <dd>${diff(pace.target.diff)}</dd>
        <dt>MY BEST</dt>
        <dd>${diff(pace.best?.diff)}</dd>
      </dl>
    </div>
    <div class="cab panel graph-footer" part="graph-footer" style=${place(graph.footer)}>
      <div class="option-row"><span class="eyebrow">Option</span><strong>${random ?? 'OFF'}</strong></div>
      <span class="pacemaker">${formatPacemakerLabel(pace.target.label)}</span>
    </div>`;
}
 
/** Where the last timed press landed between EARLY (bottom) and LATE (top), on the GREAT window's scale. */
function renderTiming(delta: number | undefined, scaleMs: number): TemplateResult {
  const t = delta === undefined ? undefined : Math.max(-1, Math.min(1, delta / Math.max(1, scaleMs)));
  return html`<div class="timing" aria-label="Last timing">
    <span class="eyebrow">LATE</span>
    <div class="timing-track">
      ${
        t === undefined
          ? nothing
          : html`<span
              class="timing-mark"
              data-side=${delta! > 0 ? 'late' : delta! < 0 ? 'early' : 'just'}
              style=${`bottom:${50 + t * 50}%`}
            ></span>`
      }
    </div>
    <span class="eyebrow">EARLY</span>
  </div>`;
}