mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
refactor: update QuestMap component to use new node structure and simplify DAG class
This commit is contained in:
1 parent
1616f62755
commit
a711e08328
3 files changed
+19
-15
No files matched your search
@@ -69,14 +69,14 @@ async function computeGraphData() {
|
|||||||
const nodesValues = dag.getNodes()
|
const nodesValues = dag.getNodes()
|
||||||
|
|
||||||
const nodes = nodesValues.map((node) => ({
|
const nodes = nodesValues.map((node) => ({
|
||||||
id: node._id,
|
id: node.id,
|
||||||
title: node.title,
|
title: node.title,
|
||||||
x: marginX * node.layer * 2,
|
x: marginX * node.layer * 2,
|
||||||
y:
|
y:
|
||||||
marginY * node.index -
|
marginY * node.index -
|
||||||
(marginX * dag.getLayer(node._id)) / 2 +
|
(marginX * dag.getLayer(node.id)) / 2 +
|
||||||
svgHeight.value / 2,
|
svgHeight.value / 2,
|
||||||
completed: store.progress[node._id]?.completed || false,
|
completed: store.progress[node.id]?.completed || false,
|
||||||
unlocked: node.requirements.every(
|
unlocked: node.requirements.every(
|
||||||
(req: string) => store.progress[req]?.completed
|
(req: string) => store.progress[req]?.completed
|
||||||
),
|
),
|
||||||
|
|||||||
+1
-4
@@ -15,10 +15,7 @@
|
|||||||
"jsx": "preserve",
|
"jsx": "preserve",
|
||||||
|
|
||||||
/* Linting */
|
/* Linting */
|
||||||
"strict": true,
|
"strict": true
|
||||||
"noUnusedLocals": true,
|
|
||||||
"noUnusedParameters": true,
|
|
||||||
"noFallthroughCasesInSwitch": true
|
|
||||||
},
|
},
|
||||||
"include": ["src/**/*.ts", "src/**/*.vue"],
|
"include": ["src/**/*.ts", "src/**/*.vue"],
|
||||||
"references": [
|
"references": [
|
||||||
|
|||||||
@@ -1,15 +1,22 @@
|
|||||||
interface Node {
|
export interface NodeInput {
|
||||||
id: string
|
id: string
|
||||||
requirements: string[]
|
requirements: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class DAG {
|
type Node<T extends NodeInput> = T & {
|
||||||
private nodes = new Map()
|
layer: number
|
||||||
|
index: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class DAG<T extends NodeInput> {
|
||||||
|
private nodes = new Map<string, Node<T>>()
|
||||||
private edges = new Set<[string, string]>()
|
private edges = new Set<[string, string]>()
|
||||||
private layers: number[] = []
|
private layers: number[] = []
|
||||||
|
|
||||||
constructor(nodes: Node[]) {
|
constructor(nodes: T[]) {
|
||||||
this.nodes = new Map(nodes.map((node) => [node.id, node]))
|
this.nodes = new Map(
|
||||||
|
nodes.map((node) => [node.id, { ...node, layer: 0, index: 0 }])
|
||||||
|
)
|
||||||
this.edges = new Set<[string, string]>()
|
this.edges = new Set<[string, string]>()
|
||||||
this.nodes.forEach((_, id) => {
|
this.nodes.forEach((_, id) => {
|
||||||
this.setLayer(id)
|
this.setLayer(id)
|
||||||
@@ -38,7 +45,7 @@ export default class DAG {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getLayer(id: string) {
|
getLayer(id: string) {
|
||||||
const node = this.getNode(id)
|
const node = this.nodes.get(id)
|
||||||
if (!node) {
|
if (!node) {
|
||||||
throw new Error(`Node ${id} not found`)
|
throw new Error(`Node ${id} not found`)
|
||||||
}
|
}
|
||||||
@@ -46,7 +53,7 @@ export default class DAG {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private setLayer(id: string) {
|
private setLayer(id: string) {
|
||||||
const node = this.getNode(id)
|
const node = this.nodes.get(id)
|
||||||
if (!node) {
|
if (!node) {
|
||||||
throw new Error(`Node ${id} not found`)
|
throw new Error(`Node ${id} not found`)
|
||||||
}
|
}
|
||||||
@@ -67,7 +74,7 @@ export default class DAG {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addEdges(id: string) {
|
addEdges(id: string) {
|
||||||
const node = this.getNode(id)
|
const node = this.nodes.get(id)
|
||||||
if (!node) {
|
if (!node) {
|
||||||
throw new Error(`Node ${id} not found`)
|
throw new Error(`Node ${id} not found`)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user