Files
game_voice_recorder/packages/shared/src/live-game.ts

58 lines
3.4 KiB
TypeScript

import type { HistoryProgress, LiveGame } from './protocol.js';
import { fmtClock } from './time.js';
export const GAME_NAME: Record<LiveGame['game'], string> = { valorant: '발로란트', lol: '리그 오브 레전드' };
/** 지금 게임 상태를 한 줄로 (헤더 · 알림용): "경쟁전 · 어센트 · R15 8:6" */
export function liveGameLine(g: LiveGame): string {
if (!g.inMatch) return g.phase + (g.mode && g.connected ? ` · ${g.mode}` : '');
const parts = [g.mode, g.map].filter(Boolean) as string[];
if (g.round != null && g.score) parts.push(`R${g.round} ${g.score.ally}:${g.score.enemy}`);
else if (g.score) parts.push(`${g.score.ally}:${g.score.enemy}`);
if (g.gameTimeSec != null) parts.push(fmtClock(g.gameTimeSec * 1000));
return parts.join(' · ') || '경기 중';
}
/** 지금 게임 상태 자세히 (앱 게임 연동 탭 · 사이트 패널): [항목, 값] */
export function liveGameRows(g: LiveGame, now = Date.now()): [string, string][] {
const rows: [string, string][] = [['상태', g.phase]];
if (g.self?.name) rows.push(['내 계정', g.self.name]);
if (g.mode) rows.push(['모드', g.mode]);
if (g.map) rows.push(['맵', g.map]);
if (g.inMatch) {
if (g.round != null) rows.push(['라운드', `${g.round}라운드`]);
if (g.score) rows.push([g.round != null ? '점수 (우리 : 상대)' : '점수', `${g.score.ally} : ${g.score.enemy}`]);
if (g.gameTimeSec != null) rows.push(['게임 시간', fmtClock(g.gameTimeSec * 1000)]);
else if (g.startedAt) rows.push(['경과', fmtClock(Math.max(0, now - g.startedAt))]);
if (g.teamKills) rows.push(['팀 킬 (우리 : 상대)', `${g.teamKills.ally} : ${g.teamKills.enemy}`]);
if (g.self?.character) rows.push([g.game === 'lol' ? '내 챔피언' : '내 요원', g.self.character]);
if (g.self?.kda) rows.push(['KDA', g.self.kda]);
if (g.self?.extra) rows.push(['내 성적', g.self.extra]);
if (g.players) rows.push(['인원', `${g.players}명`]);
}
if (g.error) rows.push(['오류', g.error]);
return rows;
}
/** 남은 시간 글자: "약 3분 남음" · "40초 남음" · "남은 시간 계산 중" */
export function fmtEta(ms: number | null): string {
if (ms == null) return '남은 시간 계산 중';
if (ms < 60000) return `${Math.max(1, Math.ceil(ms / 10000) * 10)}초 남음`;
const min = Math.ceil(ms / 60000);
return min < 60 ? `약 ${min}분 남음` : `약 ${Math.floor(min / 60)}시간 ${min % 60}분 남음`;
}
/** 경기 기록 가져오기 진행 바 글자 (v1.9.1, 사이트 경기 탭 · 앱 게임 연동 탭). pct = null 이면 얼마나 걸릴지 아직 모름 */
export function historyStatus(p: HistoryProgress): { title: string; detail: string; pct: number | null } {
if (p.phase === 'scan')
return { title: '경기 기록 목록 확인 중', detail: `${p.scanned}경기 확인${p.total ? ` · 새 경기 ${p.total}개` : ''}`, pct: null };
const pct = p.total ? Math.min(100, Math.round((p.done / p.total) * 100)) : 100;
return { title: '경기 기록 가져오는 중', detail: `${p.done} / ${p.total}경기 (${pct}%) · ${fmtEta(p.etaMs)}`, pct };
}
/** 취소한 가져오기를 다시 하는 때: "약 30분 뒤" (모르면 "다음 확인 때") */
export function resumeText(resumeAt: number | null, now = Date.now()): string {
if (!resumeAt) return '다음 확인 때';
return `약 ${Math.max(1, Math.ceil((resumeAt - now) / 60000))}분 뒤`;
}