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 | import { relative } from 'node:path';
import readline from 'node:readline';
import type { BeMusicPlayLevel } from '@be-music/chart-source';
import type { PlayerSummary } from '@be-music/player-engine';
import { resolveResultScreenActionFromKey, type ResultScreenAction } from './music-select-navigation.ts';
import { formatPlayLevelLabel, formatPlayerLabel, formatRankLabel, truncateForDisplay } from './selection-format.ts';
import { beginRawInputCapture } from './input-capture.ts';
export interface PlayedChartResult {
chartPath: string;
summary: PlayerSummary;
title?: string;
artist?: string;
player?: number;
rank?: number;
rankLabel?: string;
playLevel?: BeMusicPlayLevel;
}
interface ResultScreenOptions {
allowReplay?: boolean;
nextActionLabel?: string;
}
export async function showResultScreen(
rootDir: string,
played: PlayedChartResult,
options: ResultScreenOptions = {},
): Promise<ResultScreenAction> {
const allowReplay = options.allowReplay ?? true;
const nextActionLabel = options.nextActionLabel ?? 'return to music selection';
if (!process.stdin.isTTY || !process.stdout.isTTY) {
return 'enter';
}
const relativePath = relative(rootDir, played.chartPath).replaceAll('\\', '/');
const titleLine = played.title ?? relativePath;
const artistLine = played.artist ? `ARTIST ${played.artist}` : undefined;
const totalNotes = Math.max(0, Math.floor(played.summary.total));
const maxExScore = Math.max(0, totalNotes * 2);
const judgedNotes = Math.max(
0,
Math.floor(
played.summary.perfect + played.summary.great + played.summary.good + played.summary.bad + played.summary.poor,
),
);
const notesProgress = `${Math.min(totalNotes, judgedNotes)}/${totalNotes}`;
const gaugeLine = formatGrooveGaugeResultLine(played.summary);
const inputCapture = beginRawInputCapture();
process.stdout.write('\u001b[?25l');
const render = (): void => {
const columns = process.stdout.columns ?? 80;
const lineWidth = Math.max(16, columns - 2);
const lines: string[] = [];
lines.push('RESULT');
lines.push(truncateForDisplay(titleLine, lineWidth));
if (artistLine) {
lines.push(truncateForDisplay(artistLine, lineWidth));
}
lines.push(truncateForDisplay(`FILE ${relativePath}`, lineWidth));
lines.push('');
lines.push(
`PLAYER ${formatPlayerLabel(played.player)} RANK ${played.rankLabel ?? formatRankLabel(played.rank)} PLAYLEVEL ${formatPlayLevelLabel(played.playLevel)}`,
);
if (gaugeLine) {
lines.push(truncateForDisplay(gaugeLine, lineWidth));
}
lines.push(`NOTES ${notesProgress}`);
lines.push(`EX-SCORE ${played.summary.exScore}/${maxExScore} SCORE ${played.summary.score}/200000`);
lines.push(
`PGREAT ${played.summary.perfect} GREAT ${played.summary.great} GOOD ${played.summary.good} BAD ${played.summary.bad} POOR ${played.summary.poor}`,
);
lines.push(`FAST ${played.summary.fast} SLOW ${played.summary.slow}`);
lines.push('');
if (allowReplay) {
lines.push('Press r to replay this chart.');
}
lines.push('Press Enter or Esc to close result screen.');
lines.push(`Next: ${nextActionLabel}.`);
lines.push('Press Ctrl+C to quit.');
process.stdout.write(`\u001b[2J\u001b[H${lines.join('\n')}\u001b[J`);
};
return new Promise<ResultScreenAction>((resolvePromise) => {
let finished = false;
const cleanup = (action: ResultScreenAction): void => {
if (finished) {
return;
}
finished = true;
inputCapture.stdin.removeListener('keypress', onKeyPress);
inputCapture.restore();
process.stdout.write('\u001b[?25h\u001b[2J\u001b[H');
resolvePromise(action);
};
const onKeyPress = (chunk: string | undefined, key: readline.Key): void => {
const action = resolveResultScreenActionFromKey(chunk, key);
if (!action) {
return;
}
if (action === 'replay' && !allowReplay) {
return;
}
cleanup(action);
};
inputCapture.stdin.on('keypress', onKeyPress);
render();
});
}
function formatGrooveGaugeResultLine(summary: PlayerSummary): string | undefined {
const gauge = summary.gauge;
if (!gauge) {
return undefined;
}
const status = gauge.cleared ? 'CLEAR' : 'FAILED';
return `GAUGE ${renderPlainGrooveGaugeBar(gauge.current, gauge.max, 24)} ${gauge.current.toFixed(2)}% ${status} TOTAL ${formatGrooveGaugeNumber(gauge.effectiveTotal)}`;
}
function renderPlainGrooveGaugeBar(current: number, max: number, width: number): string {
const safeWidth = Math.max(1, Math.floor(width));
const safeMax = Number.isFinite(max) && max > 0 ? max : 100;
const filled = Math.round(safeWidth * Math.max(0, Math.min(1, current / safeMax)));
return `[${'#'.repeat(filled)}${'-'.repeat(safeWidth - filled)}]`;
}
function formatGrooveGaugeNumber(value: number): string {
if (!Number.isFinite(value)) {
return '-';
}
const rounded = Math.round(value);
return Math.abs(value - rounded) <= 1e-9 ? String(rounded) : value.toFixed(2);
}
|