Skip to content

Commit faa4fdb

Browse files
committed
add: node unlocking
1 parent 495b66f commit faa4fdb

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

src/manager/scores/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createBlankUserScore } from "./createBlankUserScore";
77
import { log } from "~/utils/logger";
88
import { getScoreMats } from "./getScoreMats";
99
import { printItemList } from "~/utils/printItemList";
10+
import { unlockScoreNodes } from "./unlockScoreNodes";
1011

1112
export async function scoresManager(rl: Interface) {
1213
const scoresList = await scoresCache.read();
@@ -32,8 +33,9 @@ export async function scoresManager(rl: Interface) {
3233
);
3334
switch (opt) {
3435
case "1":
35-
console.error("UNIMPLEMENTED");
36-
continue; // TEMP
36+
await unlockScoreNodes(scoreData, userScore, rl);
37+
await userScoreFile.write(userScore);
38+
continue;
3739
case "2": {
3840
const scoreMats = getScoreMats(scoreData, userScore);
3941
console.log(
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

src/manager/script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ async function main() {
1616
if (args.verbose) logger.setLogLevel("Debug");
1717
log.debug(args);
1818

19-
// CODE GOES HERE
19+
// TEMP: immediatly load scoresManager, while nothing else is implemented
2020
const { scoresManager } = await import("./scores");
2121
const res = await scoresManager(rl);
2222
log.debug(res);

0 commit comments

Comments
 (0)