feat: send response to frontend

This commit is contained in:
wancat committed 2024-02-29 10:54:53 +08:00
1 parent fb69cb4e10
commit 88ee8f2cc3
8 files changed
+46 -35

No files matched your search

+16 -11
View File
@@ -20,6 +20,7 @@ function newSession() {
pwd: '', pwd: '',
hints: [], hints: [],
tasks: [], tasks: [],
responses: [],
} }
} }
@@ -41,6 +42,7 @@ function setQuest(questId) {
async function setSession(session) { async function setSession(session) {
try { try {
console.log('Setting session...', session)
store.session = session store.session = session
store.session.graph = new FileGraph(session.graph) store.session.graph = new FileGraph(session.graph)
term.reset() term.reset()
@@ -85,18 +87,18 @@ function updateGraph(event) {
} }
} }
function updateHints(hints) { function newResponse(response) {
store.session.hints.push(hints) console.log('newResponse', response, 'session', store.session)
store.session.responses.push(response.responses)
store.session.hints.push(response.hints)
store.session.tasks = response.tasks
store.session.status = response.status
} }
function updateStatus(status) { function updateStatus(status) {
store.session.status = status store.session.status = status
} }
function setTasks(tasks) {
store.session.tasks = tasks
}
export function useTerminal() { export function useTerminal() {
return term return term
} }
@@ -135,11 +137,14 @@ function setup() {
socket.on('graph', (event) => { socket.on('graph', (event) => {
updateGraph(event) updateGraph(event)
}) })
socket.on('hints', (event) => { // socket.on('hints', (event) => {
updateHints(event) // updateHints(event)
}) // })
socket.on('tasks', (tasks) => { // socket.on('tasks', (tasks) => {
setTasks(tasks) // setTasks(tasks)
// })
socket.on('response', (response) => {
newResponse(response)
}) })
socket.on('status', (event) => { socket.on('status', (event) => {
updateStatus(event) updateStatus(event)
+4 -4
View File
@@ -72,13 +72,13 @@ onMounted(async () => {
</div> </div>
<!-- Terminal and Hint --> <!-- Terminal and Hint -->
<div class="bg-bg h-full w-1/3 rounded-lg"> <div class="bg-bg h-full w-1/3 rounded-lg">
<section id="terminal" class="h-3/5 overflow-hidden">
<TerminalPart />
</section>
<hr class="border-border border" />
<section id="hint" class="h-2/5"> <section id="hint" class="h-2/5">
<HintPart /> <HintPart />
</section> </section>
<hr class="border-border border" />
<section id="terminal" class="h-3/5 overflow-hidden">
<TerminalPart />
</section>
</div> </div>
<!-- Visualization --> <!-- Visualization -->
<div class="h-full w-1/3"> <div class="h-full w-1/3">
+8
View File
@@ -1,5 +1,7 @@
import { model, Schema, Types } from 'mongoose' import { model, Schema, Types } from 'mongoose'
import { IResponse, responseSchema } from './response.js'
export interface INode { export interface INode {
path: string path: string
type: string type: string
@@ -53,6 +55,7 @@ export interface ISession {
lastActivityAt: Date lastActivityAt: Date
tasks: ITask[] tasks: ITask[]
hints: string[][] hints: string[][]
responses: IResponse[][]
graph: INode graph: INode
} }
@@ -85,6 +88,11 @@ export const Session = model<ISession>(
}, },
tasks: [taskSchema], tasks: [taskSchema],
hints: [[String]], hints: [[String]],
responses: {
type: [[responseSchema]],
required: true,
default: [],
},
graph: { graph: {
type: nodeSchema, type: nodeSchema,
default: { default: {
@@ -51,16 +51,16 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
c.stage = response.stage c.stage = response.stage
await c.save() await c.save()
session.responses.push(response.responses)
if (response.stage === 'END') { if (response.stage === 'END') {
await finishSession(session) await finishSession(session)
} }
commandCompleteCallbacks.set(session.id, response.callback) // commandCompleteCallbacks.set(session.id, response.callback)
pushToSession(session.id, 'status', 'pending') pushToSession(session.id, 'response', response)
res.status(201).json(response)
} else {
res.status(200).end()
} }
res.status(200).end()
await session.save() await session.save()
}) })
@@ -25,6 +25,7 @@ function sessionDetail(session: ISession) {
...sessionSummary(session), ...sessionSummary(session),
hints: session.hints, hints: session.hints,
tasks: session.tasks, tasks: session.tasks,
responses: session.responses,
graph: session.graph, graph: session.graph,
} }
} }
@@ -55,6 +56,7 @@ export const createSession = asyncHandler(
export const getSessionById = asyncHandler( export const getSessionById = asyncHandler(
async (req: Request, res: Response) => { async (req: Request, res: Response) => {
const { sessionId } = matchedData(req) const { sessionId } = matchedData(req)
console.log(sessionId)
const session = await Session.findOne({ const session = await Session.findOne({
_id: sessionId, _id: sessionId,
user: (req.user as Express.ExistingUser).id, user: (req.user as Express.ExistingUser).id,
+5 -1
View File
@@ -22,6 +22,10 @@ router.post(
activeSessionValidator, activeSessionValidator,
sessionController.getOrCreateSession sessionController.getOrCreateSession
) )
router.get('/:id', sessionDetailValidator, sessionController.getSessionById) router.get(
'/:sessionId',
sessionDetailValidator,
sessionController.getSessionById
)
export default router export default router
@@ -9,6 +9,6 @@ export const checkSessionId = () =>
check('sessionId').notEmpty().isString().isMongoId() check('sessionId').notEmpty().isString().isMongoId()
export const sessionListValidator = [checkQuestId(), checkSessionStatus()] export const sessionListValidator = [checkQuestId(), checkSessionStatus()]
export const sessionDetailValidator = [checkSessionId(), noError] export const sessionDetailValidator = [checkSessionId()]
export const createSessionValidator = [checkQuestId(), noError] export const createSessionValidator = [checkQuestId(), noError]
export const activeSessionValidator = [checkQuestId(), noError] export const activeSessionValidator = [checkQuestId(), noError]
+5 -13
View File
@@ -8,12 +8,12 @@ import type {
IStage, IStage,
ITask, ITask,
} from '@linux-odyssey/models' } from '@linux-odyssey/models'
import { pushToSession } from '../api/socket.js'
export interface ExecuteResult { export interface ExecuteResult {
responses: IResponse[] responses: IResponse[]
hints: string[] hints: string[]
callback: () => void tasks?: ITask[]
status: string
stage?: string stage?: string
end?: boolean end?: boolean
} }
@@ -82,12 +82,8 @@ export default class SessionHandler {
return { return {
responses: stage.responses, responses: stage.responses,
hints: stage.hints, hints: stage.hints,
callback: () => { tasks: this.session.tasks,
pushToSession(this.session.id, 'tasks', this.session.tasks) status: this.session.status,
if (stage.hints && stage.hints.length > 0)
pushToSession(this.session.id, 'hints', stage.hints)
pushToSession(this.session.id, 'status', this.session.status)
},
} }
} }
@@ -98,11 +94,7 @@ export default class SessionHandler {
return { return {
responses, responses,
hints, hints,
callback: () => { status: this.session.status,
if (hints && hints.length > 0)
pushToSession(this.session.id, 'hints', hints)
pushToSession(this.session.id, 'status', this.session.status)
},
} }
} }
} }