mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
Load and update quests on startup
This commit is contained in:
1 parent
b2173d180d
commit
3637d41529
3 files changed
+33
-7
No files matched your search
@@ -7,6 +7,7 @@ import swaggerUI from 'swagger-ui-express'
|
|||||||
import socketServer from './api/socket.js'
|
import socketServer from './api/socket.js'
|
||||||
import connectDB from './db.js'
|
import connectDB from './db.js'
|
||||||
import apiRouter from './api/routes/index.js'
|
import apiRouter from './api/routes/index.js'
|
||||||
|
import loadAndUpdateQuests from './utils/quest.js'
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const port = 3000
|
const port = 3000
|
||||||
@@ -14,6 +15,7 @@ async function main() {
|
|||||||
const server = http.createServer(app)
|
const server = http.createServer(app)
|
||||||
socketServer(server)
|
socketServer(server)
|
||||||
await connectDB()
|
await connectDB()
|
||||||
|
await loadAndUpdateQuests()
|
||||||
|
|
||||||
const file = await fs.readFile('./swagger.yaml', 'utf8')
|
const file = await fs.readFile('./swagger.yaml', 'utf8')
|
||||||
const swaggerDocument = YAML.parse(file)
|
const swaggerDocument = YAML.parse(file)
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import { model } from 'mongoose'
|
import { model, Schema } from 'mongoose'
|
||||||
import { Schema } from 'yaml'
|
|
||||||
|
|
||||||
const Quest = model(
|
const Quest = model(
|
||||||
'Quest',
|
'Quest',
|
||||||
new Schema(
|
new Schema(
|
||||||
{
|
{
|
||||||
name: String,
|
name: String,
|
||||||
description: String,
|
title: String,
|
||||||
|
order: Number,
|
||||||
|
content: String,
|
||||||
},
|
},
|
||||||
{ timestamp: true }
|
{ timestamp: true }
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
import fs from 'fs/promises'
|
import fs from 'fs/promises'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { marked } from 'marked'
|
import { Marked } from 'marked'
|
||||||
import yaml from 'yaml'
|
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')
|
const questDirectory = path.join(process.cwd(), 'quests')
|
||||||
|
|
||||||
async function parseQuests() {
|
async function parseQuests() {
|
||||||
@@ -22,14 +29,17 @@ async function parseQuests() {
|
|||||||
const content = markdown.slice(match[0].length)
|
const content = markdown.slice(match[0].length)
|
||||||
const htmlContent = marked.parse(content)
|
const htmlContent = marked.parse(content)
|
||||||
|
|
||||||
|
const { title, order } = metadata
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
metadata,
|
title,
|
||||||
|
order,
|
||||||
content: htmlContent,
|
content: htmlContent,
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error parsing quest ${name}:`, error)
|
console.error(`Error parsing quest ${name}:`, error)
|
||||||
return null
|
throw error
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@@ -37,4 +47,17 @@ async function parseQuests() {
|
|||||||
return quests
|
return quests
|
||||||
}
|
}
|
||||||
|
|
||||||
parseQuests().then((quests) => console.log(quests))
|
async function updateQuests(quests) {
|
||||||
|
Promise.all(
|
||||||
|
quests.map((quest) => {
|
||||||
|
return Quest.findOneAndUpdate({ name: quest.name }, quest, {
|
||||||
|
upsert: true,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function loadAndUpdateQuests() {
|
||||||
|
const quests = await parseQuests()
|
||||||
|
await updateQuests(quests)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user