feat: design game file and parse it to mongo

* 取消用 README 來寫關卡說明
This commit is contained in:
wancat committed 2023-08-08 20:38:27 +08:00
1 parent 6204bd0b17
commit 0de4b6ac9d
4 files changed
+61 -20

No files matched your search

+31 -1
View File
@@ -1,12 +1,42 @@
import { model, Schema } from 'mongoose'
const stageSchema = new Schema({
name: {
type: String,
required: true,
},
condition: {
command: {
type: [String],
required: false,
},
output: {
type: [String],
required: false,
},
error: {
type: [String],
required: false,
},
},
responses: {
type: [String],
required: true,
},
hint: {
type: [String],
required: true,
},
})
const Quest = model(
'Quest',
new Schema({
_id: String,
title: String,
order: Number,
content: String,
introduction: String,
stages: [stageSchema],
})
)
+5 -19
View File
@@ -1,15 +1,9 @@
import fs from 'fs/promises'
import path from 'path'
import { Marked } from 'marked'
import yaml from 'yaml'
import Quest from '../models/quest.js'
const marked = new Marked({
mangle: false,
headerIds: false,
})
const questDirectory = path.join(process.cwd(), 'quests')
export default async function loadAndUpdateQuests() {
@@ -22,27 +16,19 @@ export default async function loadAndUpdateQuests() {
if (!stat.isDirectory()) {
return null
}
const fullPath = path.join(questDirectory, id, 'README.md')
const fullPath = path.join(questDirectory, id, 'game.yml')
try {
// Read and parse the README.md file.
const markdown = await fs.readFile(fullPath, 'utf-8')
const body = await fs.readFile(fullPath, 'utf-8')
// Extract and parse YAML frontmatter.
const match = /---\n([\s\S]+?)\n---/.exec(markdown)
const metadata = yaml.parse(match[1])
const content = markdown.slice(match[0].length)
const htmlContent = marked.parse(content)
const { title, order } = metadata
const quest = yaml.parse(body)
return Quest.findByIdAndUpdate(
id,
{
_id: id,
title,
order,
content: htmlContent,
id,
...quest,
},
{
upsert: true,