feat: add initial task on session creation

This commit is contained in:
wancat committed 2023-09-01 17:16:32 +08:00
1 parent 97b0fb0f1d
commit 0ea44e1f71
7 files changed
+59 -28

No files matched your search

+1
View File
@@ -9,6 +9,7 @@ lerna-debug.log*
.bash_history
.viminfo
*.swp
home
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
+20 -14
View File
@@ -1,8 +1,11 @@
/* eslint-disable no-underscore-dangle */
import axios from 'axios'
import { stdin, stdout, exit } from 'process'
import { io } from 'socket.io-client'
import { program } from 'commander'
let api = null
function get(key, defaultValue) {
const value = process.env[key]
if (value != null) {
@@ -20,26 +23,23 @@ function debug(...args) {
}
}
async function createdSession() {
async function createSession() {
console.log('Creating a new session...')
const res = await fetch(`${program.opts().host}/api/v1/sessions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
quest_id: 'helloworld',
}),
const res = await api.post('/sessions', {
quest_id: 'helloworld',
})
const data = await res.json()
const { data } = await res
if (!data._id) {
console.error(data)
exit(1)
}
console.log('Created Session ID:', data._id)
return data
}
async function getSessionList() {
const res = await fetch(`${program.opts().host}/api/v1/sessions`)
const data = await res.json()
return data
const res = await api.get('/sessions')
return res.data
}
async function lastSession() {
@@ -102,12 +102,18 @@ async function connect(sessionId) {
}
async function main() {
api = axios.create({
baseURL: `${program.opts().host}/api/v1`,
headers: {
'Content-Type': 'application/json',
},
})
console.log(program.opts().session)
// main()
const sessionId =
program.opts().session ||
(program.opts().create ? (await createdSession())._id : null) ||
(program.opts().create ? (await createSession())._id : null) ||
(await lastSession())._id
await connect(sessionId)
+1
View File
@@ -8,6 +8,7 @@
"start": "node index.js"
},
"dependencies": {
"axios": "^1.5.0",
"commander": "^11.0.0",
"socket.io-client": "^4.7.2"
}
@@ -1,6 +1,7 @@
import Session from '../../models/session.js'
import Quest from '../../models/quest.js'
import { createContainer } from '../../containers/docker.js'
import SessionHandler from '../../game/sessionHandler.js'
export async function getSessionList(req, res) {
try {
@@ -45,20 +46,22 @@ export async function createSession(req, res) {
}
try {
const progress = quest.stages[0].id
const container = await createContainer(
`quest-${quest.id}-${req.user.username}-${Date.now()}`
)
const newSession = new Session({
user: req.user,
quest,
progress,
containerId: container.id,
})
const session = await newSession.save()
res.status(201).json(session)
const session = new SessionHandler(newSession)
session.addNewTasks()
await newSession.save()
res.status(201).json(newSession)
} catch (err) {
console.error(err)
res.status(400).json({ message: err.message })
}
}
-1
View File
@@ -75,7 +75,6 @@ export default class CommandHandler extends SessionHandler {
.filter((s) => this.isMatch(s))
.reduce((r, s) => ({ ...r, ...this.execute(s) }), {})
console.log(this.session)
return {
end: this.session.status === 'finished',
...response,
+21 -9
View File
@@ -1,8 +1,9 @@
import { pushToSession } from '../api/socket.js'
export default class SessionWrapper {
export default class SessionHandler {
constructor(session) {
this.session = session
this.quest = session.quest
}
getTask(stage) {
@@ -22,29 +23,40 @@ export default class SessionWrapper {
)
}
getNewTasks() {
const stages = this.getStages()
return stages
.filter((s) => s.task)
.filter((s) => !this.session.tasks.some((t) => t.id === s.id))
.map((s) => ({ id: s.id, name: s.task, completed: false }))
}
addNewTasks() {
const newTasks = this.getNewTasks()
this.session.tasks.push(...newTasks)
pushToSession(this.session.id, 'tasks', this.session.tasks)
}
execute(stage) {
this.session.tasks
.filter((t) => t.id === stage.id)
.forEach((t) => {
t.completed = true
})
this.session.hints.push(...stage.hints)
pushToSession(this.session.id, 'hints', stage.hints)
if (stage.id === 'END') {
this.session.status = 'finished'
this.session.finishedAt = new Date()
}
const stages = this.getStages()
console.debug(stages)
const newTasks = stages
.filter((s) => s.task)
.map((s) => ({ id: s.id, name: s.task, completed: false }))
this.session.tasks.push(...newTasks)
pushToSession(this.session.id, 'tasks', this.session.tasks)
this.addNewTasks()
return {
responses: stage.responses,
hints: stage.hints,
tasks: newTasks,
}
}
}
+9
View File
@@ -1207,6 +1207,15 @@ axios@^1.4.0:
form-data "^4.0.0"
proxy-from-env "^1.1.0"
axios@^1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.5.0.tgz#f02e4af823e2e46a9768cfc74691fdd0517ea267"
integrity sha512-D4DdjDo5CY50Qms0qGQTTw6Q44jl7zRwY7bthds06pUGfChBCTcQs+N743eFWGEd6pRTMd6A+I87aWyFV5wiZQ==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"
babel-jest@^29.6.1:
version "29.6.1"
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-29.6.1.tgz#a7141ad1ed5ec50238f3cd36127636823111233a"