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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | import { createId } from '@paralleldrive/cuid2' import { eq } from 'drizzle-orm' import { db, schema } from '../../db' import { computeBktFromHistory, type SkillBktResult } from '../curriculum/bkt' import { BKT_THRESHOLDS } from '../curriculum/config/bkt-integration' import { getRecentSessionResults } from '../curriculum/session-planner' import type { SessionPart, SessionSummary, SlotResult } from '../../db/schema/session-plans' import type { GameResultsReport } from '../arcade/game-sdk/types' import { directEnrollStudent } from '../classroom/enrollment-manager' import type { SkillConfig, TestStudentProfile, TuningRound } from './types' import { generateSlotResults, checkSuccessCriteria, applyTuningAdjustments } from './helpers' import { formatActualOutcomes } from './formatting' /** * Create a test student from a profile definition. * Returns the player ID, BKT classifications, and raw BKT result. */ export async function createTestStudent( profile: TestStudentProfile, userId: string, skillHistoryOverride?: SkillConfig[] ): Promise<{ playerId: string classifications: Record<string, number> bktResult: { skills: SkillBktResult[] } }> { let effectiveSkillHistory = skillHistoryOverride ?? profile.skillHistory // If ensureAllPracticingHaveHistory is set, add missing practicing skills with default strong history if (profile.ensureAllPracticingHaveHistory) { const historySkillIds = new Set(effectiveSkillHistory.map((c) => c.skillId)) const missingSkills: SkillConfig[] = [] for (const skillId of profile.practicingSkills) { if (!historySkillIds.has(skillId)) { missingSkills.push({ skillId, targetClassification: 'strong', problems: 15, }) } } if (missingSkills.length > 0) { effectiveSkillHistory = [...effectiveSkillHistory, ...missingSkills] } } // Delete existing player with this name (and their parent_child relationship) const existing = await db.query.players.findFirst({ where: eq(schema.players.name, profile.name), }) if (existing) { await db.delete(schema.parentChild).where(eq(schema.parentChild.childPlayerId, existing.id)) await db.delete(schema.players).where(eq(schema.players.id, existing.id)) } // Create player with intention notes only (will update with actual outcomes later) const playerId = createId() await db.insert(schema.players).values({ id: playerId, userId, name: profile.name, emoji: profile.emoji, color: profile.color, isActive: true, isExpungeable: true, notes: profile.intentionNotes, }) // Create parent-child relationship so access control works await db.insert(schema.parentChild).values({ parentUserId: userId, childPlayerId: playerId, }) // Build a map of skill -> age from skill history const skillAgeMap = new Map<string, number>() for (const config of effectiveSkillHistory) { skillAgeMap.set(config.skillId, config.ageDays ?? 1) } // Create skill mastery records for practicing skills for (const skillId of profile.practicingSkills) { const ageDays = skillAgeMap.get(skillId) ?? 1 const lastPracticedAt = new Date(Date.now() - ageDays * 24 * 60 * 60 * 1000) await db.insert(schema.playerSkillMastery).values({ id: createId(), playerId, skillId, isPracticing: true, practiceLevel: 'visual', lastPracticedAt, }) } // Create tutorial progress records for completed tutorials if (profile.tutorialCompletedSkills) { for (const skillId of profile.tutorialCompletedSkills) { await db.insert(schema.skillTutorialProgress).values({ id: createId(), playerId, skillId, tutorialCompleted: true, completedAt: new Date(Date.now() - 48 * 60 * 60 * 1000), // 2 days ago teacherOverride: false, skipCount: 0, }) } } // ========================================================================== // MULTI-SESSION DISTRIBUTION // ========================================================================== const minSessions = profile.minSessions ?? 5 const sessionSpreadDays = profile.sessionSpreadDays ?? 30 // Group problems by their skill's ageDays (for staleness preservation) interface ProblemWithMeta { result: SlotResult skillId: string skillAgeDays: number } const problemsByAge = new Map<number, ProblemWithMeta[]>() for (const config of effectiveSkillHistory) { const ageDays = config.ageDays ?? 1 const sessionStartTime = new Date() // Placeholder, will be updated per-session const results = generateSlotResults(config, 0, sessionStartTime) const existing = problemsByAge.get(ageDays) ?? [] for (const result of results) { existing.push({ result, skillId: config.skillId, skillAgeDays: ageDays, }) } problemsByAge.set(ageDays, existing) } // Count total problems let totalProblems = 0 for (const problems of problemsByAge.values()) { totalProblems += problems.length } // If no problems, skip session creation if (totalProblems > 0) { const maxAgeDays = Math.max(...Array.from(problemsByAge.keys())) const actualSpreadDays = Math.max(sessionSpreadDays, maxAgeDays) const ageGroups = Array.from(problemsByAge.keys()).sort((a, b) => b - a) // oldest first const totalAgeGroups = ageGroups.length const baseSessionsPerGroup = Math.max(1, Math.floor(minSessions / totalAgeGroups)) let sessionNumber = 0 for (const ageDays of ageGroups) { const groupProblems = problemsByAge.get(ageDays)! const problemsPerSession = Math.max(3, Math.ceil(groupProblems.length / baseSessionsPerGroup)) const sessionsForGroup = Math.max( baseSessionsPerGroup, Math.ceil(groupProblems.length / problemsPerSession) ) let problemIndex = 0 for (let i = 0; i < sessionsForGroup; i++) { const sessionAgeDays = ageDays + (sessionsForGroup - 1 - i) const sessionStartTime = new Date(Date.now() - sessionAgeDays * 24 * 60 * 60 * 1000) const remainingProblems = groupProblems.length - problemIndex const remainingSessions = sessionsForGroup - i const isLastSession = i === sessionsForGroup - 1 const problemsThisSession = isLastSession ? remainingProblems : Math.ceil(remainingProblems / remainingSessions) if (problemsThisSession === 0) continue const sessionProblems = groupProblems.slice( problemIndex, problemIndex + problemsThisSession ) problemIndex += problemsThisSession sessionNumber++ // Update timestamps const orderedResults: SlotResult[] = sessionProblems.map((p, idx) => ({ ...p.result, slotIndex: idx, timestamp: new Date(sessionStartTime.getTime() + idx * 10000), })) // Create session const sessionId = createId() const sessionEndTime = new Date(sessionStartTime.getTime() + orderedResults.length * 10000) const slots = orderedResults.map((r, idx) => ({ slotId: crypto.randomUUID(), index: idx, purpose: 'focus' as const, constraints: {}, problem: r.problem, })) const parts: SessionPart[] = [ { partNumber: 1, type: 'linear', format: 'linear', useAbacus: false, slots, estimatedMinutes: 30, }, ] const summary: SessionSummary = { focusDescription: `Test session ${sessionNumber} for ${profile.name} (${sessionAgeDays} days ago)`, totalProblemCount: orderedResults.length, estimatedMinutes: 30, parts: [ { partNumber: 1, type: 'linear', description: 'Mental Math (Linear)', problemCount: orderedResults.length, estimatedMinutes: 30, }, ], } await db.insert(schema.sessionPlans).values({ id: sessionId, playerId, targetDurationMinutes: 30, estimatedProblemCount: orderedResults.length, avgTimePerProblemSeconds: 30, parts, summary, masteredSkillIds: profile.practicingSkills, status: 'completed', currentPartIndex: 1, currentSlotIndex: 0, sessionHealth: { overall: 'good', accuracy: 0.6, pacePercent: 100, currentStreak: 0, avgResponseTimeMs: 5000, }, adjustments: [], results: orderedResults, createdAt: sessionStartTime, approvedAt: sessionStartTime, startedAt: sessionStartTime, completedAt: sessionEndTime, }) } } } // Compute BKT classifications from the generated data const problemHistory = await getRecentSessionResults(playerId, 5000) const bktResult = computeBktFromHistory(problemHistory, { confidenceThreshold: BKT_THRESHOLDS.confidence, }) const classifications: Record<string, number> = { weak: 0, developing: 0, strong: 0, } for (const skill of bktResult.skills) { if (skill.masteryClassification) { classifications[skill.masteryClassification]++ } } return { playerId, classifications, bktResult } } /** * Generate game results for scoreboard testing. * Creates realistic game history based on the profile's gameHistory configs. */ export async function generateGameResults( playerId: string, profile: TestStudentProfile ): Promise<number> { if (!profile.gameHistory || profile.gameHistory.length === 0) { return 0 } let totalGames = 0 const now = Date.now() for (const gameConfig of profile.gameHistory) { const spreadMs = (gameConfig.spreadDays ?? 30) * 24 * 60 * 60 * 1000 for (let i = 0; i < gameConfig.gameCount; i++) { const gameAgeMs = (spreadMs * i) / Math.max(1, gameConfig.gameCount - 1) const playedAt = new Date(now - spreadMs + gameAgeMs) const scoreVariation = (Math.random() - 0.5) * 10 const normalizedScore = Math.max(0, Math.min(100, gameConfig.targetScore + scoreVariation)) const accuracy = normalizedScore * (0.8 + Math.random() * 0.2) let difficulty: 'easy' | 'medium' | 'hard' | 'expert' if (normalizedScore >= 85) difficulty = 'hard' else if (normalizedScore >= 70) difficulty = 'medium' else difficulty = 'easy' const durationMs = (120 + Math.random() * 480) * 1000 const fullReport: GameResultsReport = { gameName: gameConfig.gameName, gameDisplayName: gameConfig.displayName, gameIcon: gameConfig.icon, durationMs, completedNormally: true, startedAt: playedAt.getTime() - durationMs, endedAt: playedAt.getTime(), gameMode: 'single-player', playerCount: 1, playerResults: [ { playerId, playerName: profile.name.replace(/^[^\s]+\s*/, ''), // Remove emoji prefix playerEmoji: profile.emoji, userId: '', score: Math.round(normalizedScore), rank: 1, }, ], leaderboardEntry: { normalizedScore, category: gameConfig.category, difficulty, }, headline: normalizedScore >= 90 ? 'Excellent!' : normalizedScore >= 70 ? 'Great Job!' : 'Good Try!', resultTheme: normalizedScore >= 90 ? 'success' : normalizedScore >= 70 ? 'good' : 'neutral', } await db.insert(schema.gameResults).values({ playerId, gameName: gameConfig.gameName, gameDisplayName: gameConfig.displayName, gameIcon: gameConfig.icon, sessionType: 'practice-break', normalizedScore, rawScore: Math.round(normalizedScore), accuracy, category: gameConfig.category, difficulty, durationMs: Math.round(durationMs), playedAt, fullReport, }) totalGames++ } } return totalGames } /** * Create a test student with iterative tuning (up to maxRounds) */ export async function createTestStudentWithTuning( profile: TestStudentProfile, userId: string, classroomId: string, maxRounds: number = 3, onProgress?: (message: string) => void ): Promise<{ playerId: string classifications: Record<string, number> tuningHistory: TuningRound[] }> { const tuningHistory: TuningRound[] = [] let currentSkillHistory = profile.skillHistory let result: { playerId: string classifications: Record<string, number> bktResult: { skills: SkillBktResult[] } } for (let round = 1; round <= maxRounds; round++) { result = await createTestStudent(profile, userId, currentSkillHistory) const { success, reasons } = checkSuccessCriteria( result.classifications, profile.successCriteria ) const roundEntry: TuningRound = { round, classifications: { ...result.classifications }, success, failureReasons: reasons, adjustmentsApplied: [], } if (success || round === maxRounds) { tuningHistory.push(roundEntry) break } // Need to tune - apply adjustments if (profile.tuningAdjustments) { currentSkillHistory = applyTuningAdjustments(currentSkillHistory, profile.tuningAdjustments) roundEntry.adjustmentsApplied = profile.tuningAdjustments.map((adj) => { const parts: string[] = [] if (adj.accuracyMultiplier) parts.push(`accuracy × ${adj.accuracyMultiplier}`) if (adj.problemsAdd) parts.push(`problems + ${adj.problemsAdd}`) if (adj.problemsMultiplier) parts.push(`problems × ${adj.problemsMultiplier}`) return `${adj.skillId}: ${parts.join(', ')}` }) } tuningHistory.push(roundEntry) if (onProgress) { onProgress(`Tuning round ${round}: ${reasons.join(', ')}`) } // Delete the student so we can recreate with adjusted params await db.delete(schema.players).where(eq(schema.players.id, result.playerId)) } // Update the final student's notes with tuning history const actualOutcomes = formatActualOutcomes(result!.bktResult, profile, tuningHistory) const fullNotes = profile.intentionNotes + actualOutcomes await db .update(schema.players) .set({ notes: fullNotes }) .where(eq(schema.players.id, result!.playerId)) // Generate game results for this student const gameCount = await generateGameResults(result!.playerId, profile) if (gameCount > 0 && onProgress) { onProgress(`Generated ${gameCount} game results`) } // Enroll the student in the teacher's classroom await directEnrollStudent(classroomId, result!.playerId) // Record the seed profile → player mapping await db.insert(schema.seedProfilePlayers).values({ profileId: profile.name, playerId: result!.playerId, }) return { playerId: result!.playerId, classifications: result!.classifications, tuningHistory, } } |