From 88ee8f2cc3c4ce8fed746d6e1f024ff6afc9d57c Mon Sep 17 00:00:00 2001 From: wancat Date: Sun, 25 Feb 2024 13:11:24 +0800 Subject: [PATCH 01/20] feat: send response to frontend --- app/src/store/session.js | 27 +++++++++++-------- app/src/views/GamePage.vue | 8 +++--- packages/models/src/session.ts | 8 ++++++ .../src/api/controllers/commandController.ts | 10 +++---- .../src/api/controllers/sessionController.ts | 2 ++ server/src/api/routes/sessionRouter.ts | 6 ++++- server/src/api/validators/sessionValidator.ts | 2 +- server/src/game/sessionHandler.ts | 18 ++++--------- 8 files changed, 46 insertions(+), 35 deletions(-) diff --git a/app/src/store/session.js b/app/src/store/session.js index 5464bd0..f3ec7e9 100644 --- a/app/src/store/session.js +++ b/app/src/store/session.js @@ -20,6 +20,7 @@ function newSession() { pwd: '', hints: [], tasks: [], + responses: [], } } @@ -41,6 +42,7 @@ function setQuest(questId) { async function setSession(session) { try { + console.log('Setting session...', session) store.session = session store.session.graph = new FileGraph(session.graph) term.reset() @@ -85,18 +87,18 @@ function updateGraph(event) { } } -function updateHints(hints) { - store.session.hints.push(hints) +function newResponse(response) { + 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) { store.session.status = status } -function setTasks(tasks) { - store.session.tasks = tasks -} - export function useTerminal() { return term } @@ -135,11 +137,14 @@ function setup() { socket.on('graph', (event) => { updateGraph(event) }) - socket.on('hints', (event) => { - updateHints(event) - }) - socket.on('tasks', (tasks) => { - setTasks(tasks) + // socket.on('hints', (event) => { + // updateHints(event) + // }) + // socket.on('tasks', (tasks) => { + // setTasks(tasks) + // }) + socket.on('response', (response) => { + newResponse(response) }) socket.on('status', (event) => { updateStatus(event) diff --git a/app/src/views/GamePage.vue b/app/src/views/GamePage.vue index a11e1ad..c722da2 100644 --- a/app/src/views/GamePage.vue +++ b/app/src/views/GamePage.vue @@ -72,13 +72,13 @@ onMounted(async () => {
-
- -
-
+
+
+ +
diff --git a/packages/models/src/session.ts b/packages/models/src/session.ts index fdc749f..8f2ff85 100644 --- a/packages/models/src/session.ts +++ b/packages/models/src/session.ts @@ -1,5 +1,7 @@ import { model, Schema, Types } from 'mongoose' +import { IResponse, responseSchema } from './response.js' + export interface INode { path: string type: string @@ -53,6 +55,7 @@ export interface ISession { lastActivityAt: Date tasks: ITask[] hints: string[][] + responses: IResponse[][] graph: INode } @@ -85,6 +88,11 @@ export const Session = model( }, tasks: [taskSchema], hints: [[String]], + responses: { + type: [[responseSchema]], + required: true, + default: [], + }, graph: { type: nodeSchema, default: { diff --git a/server/src/api/controllers/commandController.ts b/server/src/api/controllers/commandController.ts index 5e4f5f2..085d2c5 100644 --- a/server/src/api/controllers/commandController.ts +++ b/server/src/api/controllers/commandController.ts @@ -51,16 +51,16 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => { c.stage = response.stage await c.save() + session.responses.push(response.responses) + if (response.stage === 'END') { await finishSession(session) } - commandCompleteCallbacks.set(session.id, response.callback) - pushToSession(session.id, 'status', 'pending') - res.status(201).json(response) - } else { - res.status(200).end() + // commandCompleteCallbacks.set(session.id, response.callback) + pushToSession(session.id, 'response', response) } + res.status(200).end() await session.save() }) diff --git a/server/src/api/controllers/sessionController.ts b/server/src/api/controllers/sessionController.ts index 2fb6f00..6c4f348 100644 --- a/server/src/api/controllers/sessionController.ts +++ b/server/src/api/controllers/sessionController.ts @@ -25,6 +25,7 @@ function sessionDetail(session: ISession) { ...sessionSummary(session), hints: session.hints, tasks: session.tasks, + responses: session.responses, graph: session.graph, } } @@ -55,6 +56,7 @@ export const createSession = asyncHandler( export const getSessionById = asyncHandler( async (req: Request, res: Response) => { const { sessionId } = matchedData(req) + console.log(sessionId) const session = await Session.findOne({ _id: sessionId, user: (req.user as Express.ExistingUser).id, diff --git a/server/src/api/routes/sessionRouter.ts b/server/src/api/routes/sessionRouter.ts index ddc7cdb..52e4e0c 100644 --- a/server/src/api/routes/sessionRouter.ts +++ b/server/src/api/routes/sessionRouter.ts @@ -22,6 +22,10 @@ router.post( activeSessionValidator, sessionController.getOrCreateSession ) -router.get('/:id', sessionDetailValidator, sessionController.getSessionById) +router.get( + '/:sessionId', + sessionDetailValidator, + sessionController.getSessionById +) export default router diff --git a/server/src/api/validators/sessionValidator.ts b/server/src/api/validators/sessionValidator.ts index ddb3c84..558b5b6 100644 --- a/server/src/api/validators/sessionValidator.ts +++ b/server/src/api/validators/sessionValidator.ts @@ -9,6 +9,6 @@ export const checkSessionId = () => check('sessionId').notEmpty().isString().isMongoId() export const sessionListValidator = [checkQuestId(), checkSessionStatus()] -export const sessionDetailValidator = [checkSessionId(), noError] +export const sessionDetailValidator = [checkSessionId()] export const createSessionValidator = [checkQuestId(), noError] export const activeSessionValidator = [checkQuestId(), noError] diff --git a/server/src/game/sessionHandler.ts b/server/src/game/sessionHandler.ts index 6808f5c..a4a126f 100644 --- a/server/src/game/sessionHandler.ts +++ b/server/src/game/sessionHandler.ts @@ -8,12 +8,12 @@ import type { IStage, ITask, } from '@linux-odyssey/models' -import { pushToSession } from '../api/socket.js' export interface ExecuteResult { responses: IResponse[] hints: string[] - callback: () => void + tasks?: ITask[] + status: string stage?: string end?: boolean } @@ -82,12 +82,8 @@ export default class SessionHandler { return { responses: stage.responses, hints: stage.hints, - callback: () => { - pushToSession(this.session.id, 'tasks', this.session.tasks) - if (stage.hints && stage.hints.length > 0) - pushToSession(this.session.id, 'hints', stage.hints) - pushToSession(this.session.id, 'status', this.session.status) - }, + tasks: this.session.tasks, + status: this.session.status, } } @@ -98,11 +94,7 @@ export default class SessionHandler { return { responses, hints, - callback: () => { - if (hints && hints.length > 0) - pushToSession(this.session.id, 'hints', hints) - pushToSession(this.session.id, 'status', this.session.status) - }, + status: this.session.status, } } } From 90e661f77bb497fd0f06d814584c3e3a33760012 Mon Sep 17 00:00:00 2001 From: wancat Date: Sun, 25 Feb 2024 13:21:10 +0800 Subject: [PATCH 02/20] Display dialogue in hints. Adjust Game page layout --- app/src/components/HintPart.vue | 13 +++++++++++-- app/src/views/GamePage.vue | 10 +++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/app/src/components/HintPart.vue b/app/src/components/HintPart.vue index f22d998..3d6b4bd 100644 --- a/app/src/components/HintPart.vue +++ b/app/src/components/HintPart.vue @@ -26,7 +26,7 @@ const right = () => {