ChessUp/src/shared/timeControl.ts

96 lines
4.1 KiB
TypeScript

import type { BotLevelConfig, RatedCategory, TimeCategory, TimeControl } from './types';
export const TIME_PRESETS: ReadonlyArray<{ label: string; initialMs: number; incrementMs: number }> = [
{ label: '1+0', initialMs: 60_000, incrementMs: 0 },
{ label: '2+1', initialMs: 120_000, incrementMs: 1_000 },
{ label: '3+0', initialMs: 180_000, incrementMs: 0 },
{ label: '3+2', initialMs: 180_000, incrementMs: 2_000 },
{ label: '5+0', initialMs: 300_000, incrementMs: 0 },
{ label: '5+3', initialMs: 300_000, incrementMs: 3_000 },
{ label: '10+0', initialMs: 600_000, incrementMs: 0 },
{ label: '10+5', initialMs: 600_000, incrementMs: 5_000 },
{ label: '15+10', initialMs: 900_000, incrementMs: 10_000 },
{ label: '30+0', initialMs: 1_800_000, incrementMs: 0 },
{ label: 'Unlimited', initialMs: 0, incrementMs: 0 }
];
export const RATED_CATEGORIES: ReadonlyArray<RatedCategory> = ['bullet', 'blitz', 'rapid', 'classical'];
export const BOT_LEVELS: ReadonlyArray<BotLevelConfig> = [
{ level: 1, name: 'Stockfish Lv 1', approxElo: 800, description: 'Beginner — misses tactics and drops material' },
{ level: 2, name: 'Stockfish Lv 2', approxElo: 1100, description: 'Casual — spots simple captures, plays loosely' },
{ level: 3, name: 'Stockfish Lv 3', approxElo: 1350, description: 'Club novice — basic tactics and development' },
{ level: 4, name: 'Stockfish Lv 4', approxElo: 1600, description: 'Intermediate — solid positional and tactical play' },
{ level: 5, name: 'Stockfish Lv 5', approxElo: 1850, description: 'Strong club — punishes inaccuracies consistently' },
{ level: 6, name: 'Stockfish Lv 6', approxElo: 2100, description: 'Expert — deep tactical awareness and clean endgames' },
{ level: 7, name: 'Stockfish Lv 7', approxElo: 2400, description: 'Master — relentless pressure and calculation' },
{ level: 8, name: 'Stockfish Lv 8', approxElo: 2800, description: 'Grandmaster — full-strength Stockfish engine' }
];
export function classifyTimeControl(initialMs: number, incrementMs: number): TimeCategory {
if (initialMs <= 0 && incrementMs <= 0) {
return 'unlimited';
}
const estimatedSec = Math.max(0, initialMs) / 1000 + 40 * (Math.max(0, incrementMs) / 1000);
if (estimatedSec < 180) return 'bullet';
if (estimatedSec < 480) return 'blitz';
if (estimatedSec < 1500) return 'rapid';
return 'classical';
}
export function buildTimeControl(initialMs: number, incrementMs: number): TimeControl {
const safeInitial = Math.max(0, Math.floor(initialMs));
const safeIncrement = Math.max(0, Math.floor(incrementMs));
return {
initialMs: safeInitial,
incrementMs: safeIncrement,
category: classifyTimeControl(safeInitial, safeIncrement)
};
}
export function formatTimeControl(initialMs: number, incrementMs: number): string {
if (initialMs <= 0 && incrementMs <= 0) {
return 'Unlimited';
}
const initSec = Math.round(initialMs / 1000);
const incSec = Math.round(incrementMs / 1000);
if (initSec < 60 && initSec % 60 !== 0) {
return `${initSec}s+${incSec}`;
}
const mins = initSec / 60;
const minLabel = Number.isInteger(mins) ? String(mins) : mins.toFixed(1);
return `${minLabel}+${incSec}`;
}
export function formatClock(ms: number): string {
const clamped = Math.max(0, ms);
if (clamped < 10_000) {
const totalTenths = Math.ceil(clamped / 100);
const secs = Math.floor(totalTenths / 10);
const tenths = totalTenths % 10;
return `0:${secs < 10 ? '0' : ''}${secs}.${tenths}`;
}
const totalSecs = Math.ceil(clamped / 1000);
const hours = Math.floor(totalSecs / 3600);
const mins = Math.floor((totalSecs % 3600) / 60);
const secs = totalSecs % 60;
if (hours > 0) {
return `${hours}:${mins < 10 ? '0' : ''}${mins}:${secs < 10 ? '0' : ''}${secs}`;
}
return `${mins < 10 ? '0' : ''}${mins}:${secs < 10 ? '0' : ''}${secs}`;
}
export function categoryLabel(category: TimeCategory): string {
switch (category) {
case 'bullet':
return 'Bullet';
case 'blitz':
return 'Blitz';
case 'rapid':
return 'Rapid';
case 'classical':
return 'Classical';
case 'unlimited':
return 'Unlimited';
}
}