refactor: update session management to handle tasks and responses in a single update

This commit is contained in:
Justin Lin committed 2025-04-23 16:03:39 +08:00
1 parent 66434ff897
commit 2fcca0c4b9
4 files changed
+32 -17

No files matched your search

+9 -3
View File
@@ -6,7 +6,7 @@ import MarkdownText from '../MarkdownText.vue'
const sessionStore = useSession() const sessionStore = useSession()
const current = ref(-1) const current = ref(-1)
const length = computed(() => { const length = computed(() => {
return sessionStore.session?.responses.length ?? 0 return sessionStore.session?.responses?.length ?? 0
}) })
const disabled = computed(() => { const disabled = computed(() => {
@@ -14,6 +14,7 @@ const disabled = computed(() => {
}) })
watch(length, () => { watch(length, () => {
console.log('length update', length.value)
current.value = length.value - 1 current.value = length.value - 1
}) })
@@ -30,7 +31,10 @@ const right = () => {
} }
const response = computed(() => { const response = computed(() => {
return sessionStore.session?.responses[current.value] if (!sessionStore.session) return null
if (current.value < 0) return null
if (current.value >= sessionStore.session.responses.length) return null
return sessionStore.session.responses[current.value]
}) })
</script> </script>
@@ -41,7 +45,9 @@ const response = computed(() => {
:icon="['far', 'lightbulb']" :icon="['far', 'lightbulb']"
class="text-yellow-200 p-2 content-center" class="text-yellow-200 p-2 content-center"
/> />
<h1 class="inline text-text w-1/2 font-xl p-2 m-1">說明</h1> <h1 class="inline text-text w-1/2 font-xl p-2 m-1">
說明 {{ current }}/{{ length }}
</h1>
<div v-if="current !== -1" class="flex w-full justify-end items-end"> <div v-if="current !== -1" class="flex w-full justify-end items-end">
<button <button
class="p-2 m-1 w-1/8" class="p-2 m-1 w-1/8"
+1
View File
@@ -5,6 +5,7 @@ import useSession from '../../store/session'
const sessionStore = useSession() const sessionStore = useSession()
const tasks = computed(() => { const tasks = computed(() => {
console.log('session', sessionStore.session)
return sessionStore.session?.tasks.map((task) => { return sessionStore.session?.tasks.map((task) => {
const prefix = task.completed ? '✓' : '➤' const prefix = task.completed ? '✓' : '➤'
const color = task.completed ? 'text-text-primary' : 'text-text' const color = task.completed ? 'text-text-primary' : 'text-text'
+14 -12
View File
@@ -18,6 +18,12 @@ interface QuestDetailResponse {
requirements: string[] requirements: string[]
} }
interface SessionUpdate {
status: string
responses: IResponse[]
tasks: ITask[]
}
interface Store { interface Store {
session: Session | null session: Session | null
questId: string questId: string
@@ -36,7 +42,8 @@ const useSession = defineStore('session', {
this.questId = questId this.questId = questId
}, },
async setSession(session: Session) { async setSession(session: Session) {
this.session = session console.log('set session', session)
this.session = { ...session, tasks: [] }
// this.session.graph = new FileGraph(session.graph) // this.session.graph = new FileGraph(session.graph)
term.reset() term.reset()
await socket.connect(session) await socket.connect(session)
@@ -62,9 +69,11 @@ const useSession = defineStore('session', {
this.session.pwd = event.pwd this.session.pwd = event.pwd
} }
}, },
newResponse(responses: IResponse[]) { newUpdate(update: SessionUpdate) {
if (!this.session) return if (!this.session) return
this.session.responses = responses this.session.responses = update.responses
this.session.tasks = update.tasks
this.session.status = update.status
// this.session.hints.push(response.hints) // this.session.hints.push(response.hints)
// if (response.tasks) { // if (response.tasks) {
// this.session.tasks = response.tasks // this.session.tasks = response.tasks
@@ -77,10 +86,6 @@ const useSession = defineStore('session', {
// } // }
// this.session.status = response.status // this.session.status = response.status
}, },
newTask(tasks: ITask[]) {
if (!this.session) return
this.session.tasks = tasks
},
finish() { finish() {
if (!this.session) return if (!this.session) return
this.session.status = 'finished' this.session.status = 'finished'
@@ -101,11 +106,8 @@ const useSession = defineStore('session', {
socket.on('graph', (event: { discover: FileObject[]; pwd: string }) => { socket.on('graph', (event: { discover: FileObject[]; pwd: string }) => {
this.updateGraph(event) this.updateGraph(event)
}) })
socket.on('response', (responses: IResponse[]) => { socket.on('update', (update: SessionUpdate) => {
this.newResponse(responses) this.newUpdate(update)
})
socket.on('task', (tasks: ITask[]) => {
this.newTask(tasks)
}) })
hasSetup = true hasSetup = true
}, },
@@ -11,6 +11,8 @@ import { CLIFileExistenceChecker } from '../../containers/cli.js'
export const newCommand = asyncHandler(async (req: Request, res: Response) => { export const newCommand = asyncHandler(async (req: Request, res: Response) => {
const { command, pwd, output, error, params } = matchedData(req) const { command, pwd, output, error, params } = matchedData(req)
console.log('command', command)
const { sessionId } = req.user as any const { sessionId } = req.user as any
const session = await Session.findById(sessionId) const session = await Session.findById(sessionId)
if (!session) { if (!session) {
@@ -52,6 +54,7 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
) )
const event = await gameSession.runCommand(c) const event = await gameSession.runCommand(c)
console.log('event', event)
if (event) { if (event) {
c.stage = event c.stage = event
await c.save() await c.save()
@@ -62,8 +65,11 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
if ((session.status as string) === 'finished') { if ((session.status as string) === 'finished') {
await finishSession(session) await finishSession(session)
} }
pushToSession(session.id, 'response', gameSession.getResponses()) pushToSession(session.id, 'update', {
pushToSession(session.id, 'task', gameSession.getTasks()) responses: gameSession.getResponses(),
tasks: gameSession.getTasks(),
status: session.status,
})
} }
res.status(200).end() res.status(200).end()