Download src/lib/difficulty.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 7.32 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/difficulty.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/difficulty.ts
-
curl -L -o difficulty.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/difficulty.ts
7.32 kB
| import type { PerformanceSummary, CategoryPerformance } from "./performance"; | |
| import type { Category } from "@/config/xp"; | |
| import { CATEGORIES } from "@/config/xp"; | |
| export type DifficultyLevel = "easy" | "medium" | "hard"; | |
| // --------------------------------------------------------------------------- | |
| // Thresholds | |
| // --------------------------------------------------------------------------- | |
| /** Success rate (0β100) below which we recommend easy difficulty */ | |
| const EASY_THRESHOLD = 40; | |
| /** Success rate (0β100) at or above which we recommend hard difficulty */ | |
| const HARD_THRESHOLD = 70; | |
| /** Streak days at or above which we boost difficulty one level */ | |
| const HOT_STREAK_DAYS = 5; | |
| // --------------------------------------------------------------------------- | |
| // Core selectors | |
| // --------------------------------------------------------------------------- | |
| /** | |
| * Select a base difficulty level from a success rate (0β100). | |
| * | |
| * < 40% β easy (user is struggling) | |
| * 40β69% β medium (building consistency) | |
| * β₯ 70% β hard (ready for a challenge) | |
| */ | |
| export function selectDifficulty(successRate: number): DifficultyLevel { | |
| if (successRate < EASY_THRESHOLD) return "easy"; | |
| if (successRate < HARD_THRESHOLD) return "medium"; | |
| return "hard"; | |
| } | |
| /** | |
| * Shift a difficulty one step up or down, clamped to valid values. | |
| */ | |
| function bumpDifficulty( | |
| level: DifficultyLevel, | |
| direction: "up" | "down", | |
| ): DifficultyLevel { | |
| const order: DifficultyLevel[] = ["easy", "medium", "hard"]; | |
| const idx = order.indexOf(level); | |
| const next = direction === "up" ? idx + 1 : idx - 1; | |
| return order[Math.max(0, Math.min(2, next))]; | |
| } | |
| // --------------------------------------------------------------------------- | |
| // Next-skill labels per category and level band | |
| // --------------------------------------------------------------------------- | |
| const NEXT_SKILL_MAP: Record<Category, [string, string, string]> = { | |
| chess: [ | |
| "basic tactics", | |
| "pattern recognition", | |
| "complex combinations", | |
| ], | |
| reading: [ | |
| "active recall", | |
| "inference & analysis", | |
| "critical evaluation", | |
| ], | |
| feynman: [ | |
| "concept fundamentals", | |
| "simplified explanation", | |
| "teaching mastery", | |
| ], | |
| logic: [ | |
| "basic deduction", | |
| "multi-step reasoning", | |
| "complex problem solving", | |
| ], | |
| "critical-thinking": [ | |
| "identifying fallacies", | |
| "argument reconstruction", | |
| "steelman & synthesis", | |
| ], | |
| quests: [ | |
| "foundational concepts", | |
| "applied knowledge", | |
| "expert application", | |
| ], | |
| coding: [ | |
| "core implementation", | |
| "debugging & review", | |
| "system architecture", | |
| ], | |
| }; | |
| function getNextSkill(category: Category, level: number): string { | |
| const [beginner, intermediate, advanced] = NEXT_SKILL_MAP[category]; | |
| if (level <= 2) return beginner; | |
| if (level <= 5) return intermediate; | |
| return advanced; | |
| } | |
| // --------------------------------------------------------------------------- | |
| // Types | |
| // --------------------------------------------------------------------------- | |
| export interface DifficultyRecommendation { | |
| difficulty: DifficultyLevel; | |
| nextSkill: string; | |
| adjustmentReason: string; | |
| /** False when the category has no activity history (brand-new user). */ | |
| hasHistory: boolean; | |
| } | |
| export interface DifficultyRecommendations { | |
| recommendations: Record<Category, DifficultyRecommendation>; | |
| updatedAt: string; | |
| } | |
| // --------------------------------------------------------------------------- | |
| // Main recommendation builder | |
| // --------------------------------------------------------------------------- | |
| /** | |
| * Compute per-category difficulty recommendations from a performance summary. | |
| * | |
| * Adjustment rules (applied in order): | |
| * 1. Base difficulty from success rate bands. | |
| * 2. Hot streak (β₯ 5 days): bump up one level. | |
| * 3. Cold streak (0 days, but user has some history): bump down one level. | |
| * 4. Improving trend: bump up one level (capped at hard). | |
| * 5. Declining trend: bump down one level (capped at easy). | |
| * | |
| * Edge cases: | |
| * - Category with 0 attempts: returns medium with hasHistory = false. | |
| * - Brand-new user (totalExercises = 0): all categories get hasHistory = false. | |
| */ | |
| export function getRecommendedSettings( | |
| summary: PerformanceSummary, | |
| ): DifficultyRecommendations { | |
| const recommendations = {} as Record<Category, DifficultyRecommendation>; | |
| for (const cat of summary.categories as CategoryPerformance[]) { | |
| const category = cat.category; | |
| // No history for this category yet | |
| if (cat.attempts === 0) { | |
| recommendations[category] = { | |
| difficulty: "medium", | |
| nextSkill: getNextSkill(category, cat.level), | |
| adjustmentReason: "No activity yet β starting at medium difficulty", | |
| hasHistory: false, | |
| }; | |
| continue; | |
| } | |
| // --- Step 1: base difficulty from success rate --- | |
| let adjusted = selectDifficulty(cat.successRate); | |
| const reasons: string[] = [buildBaseReason(adjusted, cat.successRate)]; | |
| // --- Step 2: hot streak boost --- | |
| if (summary.streakDays >= HOT_STREAK_DAYS) { | |
| const bumped = bumpDifficulty(adjusted, "up"); | |
| if (bumped !== adjusted) { | |
| adjusted = bumped; | |
| reasons.push(`hot streak (${summary.streakDays} days) boosted difficulty`); | |
| } | |
| } | |
| // --- Step 3: cold/broken streak reduction --- | |
| if (summary.streakDays === 0 && summary.totalExercises > 0) { | |
| const bumped = bumpDifficulty(adjusted, "down"); | |
| if (bumped !== adjusted) { | |
| adjusted = bumped; | |
| reasons.push("easing back in after a break"); | |
| } | |
| } | |
| // --- Step 4 & 5: trend adjustment --- | |
| if (cat.trend === "improving") { | |
| const bumped = bumpDifficulty(adjusted, "up"); | |
| if (bumped !== adjusted) { | |
| adjusted = bumped; | |
| reasons.push("upward trend detected"); | |
| } | |
| } else if (cat.trend === "declining") { | |
| const bumped = bumpDifficulty(adjusted, "down"); | |
| if (bumped !== adjusted) { | |
| adjusted = bumped; | |
| reasons.push("declining trend detected"); | |
| } | |
| } | |
| recommendations[category] = { | |
| difficulty: adjusted, | |
| nextSkill: getNextSkill(category, cat.level), | |
| adjustmentReason: reasons.join(" β "), | |
| hasHistory: true, | |
| }; | |
| } | |
| // Ensure all categories are present even if missing from summary.categories | |
| for (const cat of CATEGORIES) { | |
| if (!recommendations[cat]) { | |
| recommendations[cat] = { | |
| difficulty: "medium", | |
| nextSkill: getNextSkill(cat, 1), | |
| adjustmentReason: "No activity yet β starting at medium difficulty", | |
| hasHistory: false, | |
| }; | |
| } | |
| } | |
| return { | |
| recommendations, | |
| updatedAt: new Date().toISOString(), | |
| }; | |
| } | |
| // --------------------------------------------------------------------------- | |
| // Helpers | |
| // --------------------------------------------------------------------------- | |
| function buildBaseReason( | |
| difficulty: DifficultyLevel, | |
| successRate: number, | |
| ): string { | |
| const pct = successRate.toFixed(1); | |
| switch (difficulty) { | |
| case "easy": | |
| return `Low success rate (${pct}%) β focusing on fundamentals`; | |
| case "medium": | |
| return `Moderate success rate (${pct}%) β building consistency`; | |
| case "hard": | |
| return `High success rate (${pct}%) β ready for a challenge`; | |
| } | |
| } | |