Files
igny8/frontend/src/utils/difficulty.ts
2025-11-09 10:27:02 +00:00

107 lines
3.1 KiB
TypeScript

/**
* Global Difficulty Mapping Utility
* Maps numeric difficulty values (0-100) to number scale (1-5)
* 1 = Very Easy (0-10)
* 2 = Easy (11-30)
* 3 = Medium (31-50)
* 4 = Hard (51-70)
* 5 = Very Hard (71-100)
*/
export const DIFFICULTY_RANGES = {
'Very Easy': { min: 0, max: 10, number: 1 },
'Easy': { min: 11, max: 30, number: 2 },
'Medium': { min: 31, max: 50, number: 3 },
'Hard': { min: 51, max: 70, number: 4 },
'Very Hard': { min: 71, max: 100, number: 5 },
} as const;
export type DifficultyLabel = keyof typeof DIFFICULTY_RANGES;
/**
* Convert numeric difficulty (0-100) to number scale (1-5)
*/
export function getDifficultyNumber(difficulty: number | null | undefined): number | string {
if (difficulty === null || difficulty === undefined) {
return '-';
}
if (difficulty <= 10) return 1;
if (difficulty <= 30) return 2;
if (difficulty <= 50) return 3;
if (difficulty <= 70) return 4;
return 5;
}
/**
* Convert numeric difficulty to label (legacy function - still used for filtering)
*/
export function getDifficultyLabel(difficulty: number | null | undefined): DifficultyLabel | string {
if (difficulty === null || difficulty === undefined) {
return '-';
}
if (difficulty <= 10) return 'Very Easy';
if (difficulty <= 30) return 'Easy';
if (difficulty <= 50) return 'Medium';
if (difficulty <= 70) return 'Hard';
return 'Very Hard';
}
/**
* Convert difficulty label to numeric range for filtering
*/
export function getDifficultyRange(label: DifficultyLabel): { min: number; max: number } {
return { min: DIFFICULTY_RANGES[label].min, max: DIFFICULTY_RANGES[label].max };
}
/**
* Get all difficulty labels as array
*/
export function getDifficultyLabels(): DifficultyLabel[] {
return Object.keys(DIFFICULTY_RANGES) as DifficultyLabel[];
}
/**
* Get difficulty number from label (1-5)
*/
export function getDifficultyNumberFromLabel(label: DifficultyLabel): number {
return DIFFICULTY_RANGES[label].number;
}
/**
* Get difficulty label from number (1-5)
*/
export function getDifficultyLabelFromNumber(num: number): DifficultyLabel | null {
for (const [label, range] of Object.entries(DIFFICULTY_RANGES)) {
if (range.number === num) {
return label as DifficultyLabel;
}
}
return null;
}
/**
* Get all difficulty numbers (1-5) with their labels
*/
export function getDifficultyOptions(): Array<{ value: string; label: string; number: number }> {
return [
{ value: '1', label: '1 - Very Easy', number: 1 },
{ value: '2', label: '2 - Easy', number: 2 },
{ value: '3', label: '3 - Medium', number: 3 },
{ value: '4', label: '4 - Hard', number: 4 },
{ value: '5', label: '5 - Very Hard', number: 5 },
];
}
/**
* Convert difficulty number (1-5) to 0-100 range value (using middle of range)
* Used when saving form data where user selected 1-5 dropdown
*/
export function getDifficultyValueFromNumber(num: number): number {
if (num <= 1) return 5; // Very Easy: middle of 0-10
if (num === 2) return 20; // Easy: middle of 11-30
if (num === 3) return 40; // Medium: middle of 31-50
if (num === 4) return 60; // Hard: middle of 51-70
return 85; // Very Hard: middle of 71-100
}