Download src/lib/skill-unlock.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 3.4 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/skill-unlock.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/skill-unlock.ts
-
curl -L -o skill-unlock.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/skill-unlock.ts
3.4 kB
| /** | |
| * Automatic skill tree node unlocking. | |
| * | |
| * After a category level-up, checks for newly eligible nodes and creates | |
| * Unlock records. Also handles streak-shield nodes by granting streak freezes. | |
| */ | |
| import type { Prisma } from "@prisma/client"; | |
| export interface UnlockResult { | |
| /** Newly unlocked node names */ | |
| unlocked: string[]; | |
| /** Number of streak freezes granted (from streak-shield nodes) */ | |
| streakFreezesGranted: number; | |
| } | |
| /** | |
| * Process skill tree unlocks for a user in a specific category. | |
| * | |
| * Call this inside the same transaction that persists XP/level changes. | |
| * Only unlocks nodes where: | |
| * 1. The node's category matches | |
| * 2. The user's NEW category level >= node.levelRequired | |
| * 3. The node is not already unlocked | |
| * 4. The parent node (if any) is already unlocked (or is being unlocked in this batch) | |
| * | |
| * @param tx - Prisma transaction client | |
| * @param userId - The user's ID | |
| * @param category - The exercise category (e.g. "chess", "reading") | |
| * @param newCategoryLevel - The user's new level in this category (after level-up) | |
| * @returns UnlockResult with names of unlocked nodes | |
| */ | |
| export async function processSkillUnlocks( | |
| tx: Prisma.TransactionClient, | |
| userId: number, | |
| category: string, | |
| newCategoryLevel: number, | |
| ): Promise<UnlockResult> { | |
| // Fetch all nodes in this category | |
| const allNodes = await tx.skillTreeNode.findMany({ | |
| where: { category }, | |
| orderBy: { levelRequired: "asc" }, | |
| }); | |
| // Fetch existing unlocks for this user in this category | |
| const existingUnlocks = await tx.unlock.findMany({ | |
| where: { | |
| userId, | |
| node: { category }, | |
| }, | |
| select: { nodeId: true }, | |
| }); | |
| const alreadyUnlocked = new Set(existingUnlocks.map((u) => u.nodeId)); | |
| // Process in level order (BFS-like) to handle parent chains correctly. | |
| // A node is eligible if: | |
| // - levelRequired <= newCategoryLevel | |
| // - Not already unlocked | |
| // - Parent is either null (root) or already unlocked (including this batch) | |
| const newlyUnlocked: Array<{ id: number; name: string; nodeType: string }> = []; | |
| const unlockSet = new Set(alreadyUnlocked); | |
| // Multiple passes to handle chains (child unlocked because parent just unlocked) | |
| let changed = true; | |
| while (changed) { | |
| changed = false; | |
| for (const node of allNodes) { | |
| if (unlockSet.has(node.id)) continue; | |
| if (node.levelRequired > newCategoryLevel) continue; | |
| // Check parent requirement | |
| if (node.parentNodeId !== null) { | |
| if (!unlockSet.has(node.parentNodeId)) continue; | |
| } | |
| unlockSet.add(node.id); | |
| newlyUnlocked.push({ id: node.id, name: node.name, nodeType: node.nodeType }); | |
| changed = true; | |
| } | |
| } | |
| // Nothing new to unlock | |
| if (newlyUnlocked.length === 0) { | |
| return { unlocked: [], streakFreezesGranted: 0 }; | |
| } | |
| // Create Unlock records | |
| for (const node of newlyUnlocked) { | |
| await tx.unlock.create({ | |
| data: { userId, nodeId: node.id }, | |
| }); | |
| } | |
| // Handle streak-shield nodes: grant streak freezes | |
| const streakShields = newlyUnlocked.filter((n) => n.nodeType === "streak-shield"); | |
| if (streakShields.length > 0) { | |
| await tx.user.update({ | |
| where: { id: userId }, | |
| data: { streakFreezes: { increment: streakShields.length } }, | |
| }); | |
| } | |
| return { | |
| unlocked: newlyUnlocked.map((n) => n.name), | |
| streakFreezesGranted: streakShields.length, | |
| }; | |
| } | |