File size: 3,402 Bytes
9580dce | 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 | /**
* 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,
};
}
|