mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: fetch quests from api
This commit is contained in:
1 parent
b19d6bf6e2
commit
34e9b20f2e
1 file changed
+36
-47
@@ -20,56 +20,27 @@ import { GraphChart } from 'echarts/charts'
|
|||||||
import { SVGRenderer } from 'echarts/renderers'
|
import { SVGRenderer } from 'echarts/renderers'
|
||||||
import { TitleComponent, TooltipComponent } from 'echarts/components'
|
import { TitleComponent, TooltipComponent } from 'echarts/components'
|
||||||
import { DAG } from '@linux-odyssey/utils'
|
import { DAG } from '@linux-odyssey/utils'
|
||||||
|
import api from '../utils/api'
|
||||||
|
|
||||||
|
const marginX = 100
|
||||||
|
const marginY = 100
|
||||||
|
|
||||||
const chartContainer = ref(null)
|
const chartContainer = ref(null)
|
||||||
let chartInstance = null
|
let chartInstance = null
|
||||||
|
|
||||||
use([GraphChart, SVGRenderer, TitleComponent, TooltipComponent])
|
use([GraphChart, SVGRenderer, TitleComponent, TooltipComponent])
|
||||||
|
|
||||||
const data = [
|
async function getQuests() {
|
||||||
{
|
try {
|
||||||
_id: 'discover',
|
const res = await api.get('/quests')
|
||||||
title: 'Discover the World!',
|
return res.data
|
||||||
requirements: ['spell'],
|
} catch (err) {
|
||||||
},
|
console.error(err)
|
||||||
{
|
throw err
|
||||||
_id: 'read',
|
}
|
||||||
title: 'Read the File',
|
}
|
||||||
requirements: ['spell'],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
_id: 'helloworld',
|
|
||||||
title: 'Hello, Linux World!',
|
|
||||||
requirements: [],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
_id: 'spell',
|
|
||||||
title: 'Learn to spell',
|
|
||||||
requirements: ['helloworld'],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
_id: 'move',
|
|
||||||
requirements: ['read', 'discover'],
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
const marginX = 100
|
const genOption = (nodes, edges) => ({
|
||||||
const marginY = 100
|
|
||||||
|
|
||||||
const dag = new DAG(data)
|
|
||||||
const nodes = dag.getNodes().map((node) => ({
|
|
||||||
id: node._id,
|
|
||||||
name: node.title,
|
|
||||||
x: marginX * node.index - (marginX * dag.getLayer(node._id)) / 2,
|
|
||||||
y: marginY * node.layer,
|
|
||||||
}))
|
|
||||||
|
|
||||||
const edges = dag.getEdgesArray().map((edge) => ({
|
|
||||||
source: edge[0],
|
|
||||||
target: edge[1],
|
|
||||||
}))
|
|
||||||
|
|
||||||
const option = {
|
|
||||||
title: {
|
title: {
|
||||||
text: 'Quest Map',
|
text: 'Quest Map',
|
||||||
},
|
},
|
||||||
@@ -101,16 +72,32 @@ const option = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
})
|
||||||
|
|
||||||
|
function getOption(quests) {
|
||||||
|
const dag = new DAG(quests)
|
||||||
|
|
||||||
|
const nodes = dag.getNodes().map((node) => ({
|
||||||
|
id: node._id,
|
||||||
|
name: node.title,
|
||||||
|
x: marginX * node.index - (marginX * dag.getLayer(node._id)) / 2,
|
||||||
|
y: marginY * node.layer,
|
||||||
|
}))
|
||||||
|
|
||||||
|
const edges = dag.getEdgesArray().map((edge) => ({
|
||||||
|
source: edge[0],
|
||||||
|
target: edge[1],
|
||||||
|
}))
|
||||||
|
return genOption(nodes, edges)
|
||||||
}
|
}
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
function initChart() {
|
function initChart(option) {
|
||||||
console.log('initChart', chartContainer.value)
|
console.log('initChart', chartContainer.value)
|
||||||
chartInstance = init(chartContainer.value)
|
chartInstance = init(chartContainer.value)
|
||||||
chartInstance.setOption(option)
|
chartInstance.setOption(option)
|
||||||
chartInstance.on('click', (params) => {
|
chartInstance.on('click', (params) => {
|
||||||
console.log('click', params)
|
|
||||||
const {
|
const {
|
||||||
data: { id },
|
data: { id },
|
||||||
} = params
|
} = params
|
||||||
@@ -119,7 +106,9 @@ function initChart() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(async () => {
|
||||||
initChart()
|
const quests = await getQuests()
|
||||||
|
const option = getOption(quests)
|
||||||
|
initChart(option)
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
Reference in new issue
Block a user