|
| 1 | +import type { Interface } from "readline/promises"; |
| 2 | +import type { UserClassScore } from "./types"; |
| 3 | +import { List } from "@foxkit/list"; |
| 4 | +import { printItemList } from "~/utils/printItemList"; |
| 5 | +import { log } from "~/utils/logger"; |
| 6 | + |
| 7 | +export async function unlockScoreNodes( |
| 8 | + scoreData: ClassScore, |
| 9 | + userScore: UserClassScore, |
| 10 | + rl: Interface |
| 11 | +) { |
| 12 | + const list = new List( |
| 13 | + scoreData.startNodes.map(id => scoreData.nodes[id]).reverse() |
| 14 | + ); |
| 15 | + let current: ClassScoreNode | undefined; |
| 16 | + |
| 17 | + while ((current = list.pop())) { |
| 18 | + // if already unlocked push next nodes to list |
| 19 | + if (userScore.unlockedNodes.includes(current.id)) { |
| 20 | + log.debug(`Node ${current.id} already unlocked`); |
| 21 | + for (const id of current.next) { |
| 22 | + list.push(scoreData.nodes[id]); |
| 23 | + } |
| 24 | + continue; |
| 25 | + } |
| 26 | + |
| 27 | + // Present node |
| 28 | + console.log(`${scoreData.name} [${current.id}]`); |
| 29 | + if (current.detail) console.log(`${current.detail}`); |
| 30 | + await printItemList(Object.fromEntries(current.items), current.qp); |
| 31 | + |
| 32 | + // Ask whether to unlock, continue if no |
| 33 | + const choice = (await rl.question("Unlock node? [Yn]: ")).toLowerCase(); |
| 34 | + if (choice == "n") { |
| 35 | + log.debug(`Node ${current.id} stayed locked`); |
| 36 | + continue; |
| 37 | + } |
| 38 | + userScore.unlockedNodes.push(current.id); |
| 39 | + log.debug(`Node ${current.id} now unlocked`); |
| 40 | + |
| 41 | + // push next nodes after newly unlocked node |
| 42 | + for (const id of current.next) { |
| 43 | + list.push(scoreData.nodes[id]); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments