Merge remote-tracking branch 'origin/main' into feat-landing-page

This commit is contained in:
wancat committed 2024-03-01 23:13:15 +08:00
commit 6aed5789a5
13 files changed
+85 -100

No files matched your search

+15 -3
View File
@@ -26,7 +26,9 @@ const right = () => {
</script>
<template>
<div class="w-full flex items-center p-2">
<div class="h-full flex flex-col">
<!-- Hint Header -->
<div class="flex items-center p-2 h-10">
<font-awesome-icon
:icon="['far', 'lightbulb']"
class="text-yellow-200 p-2 content-center"
@@ -63,16 +65,26 @@ const right = () => {
</button>
</div>
</div>
<div id="hint" class="bg-bg flex flex-wrap p-8">
<div id="hint" class="bg-bg p-8 overflow-y-auto">
<ul>
<li
v-for="response in sessionStore.session.responses[current]"
:key="response"
class="text-text font-xl whitespace-pre-wrap"
>
<div v-for="content in response.content" :key="content">
<MarkdownText :content="content" />
</div>
</li>
<li
v-for="hint in sessionStore.session.hints[current]"
:key="hint"
class="text-text font-xl whitespace-pre-wrap"
class="text-text-primary font-xl whitespace-pre-wrap"
>
<MarkdownText :content="hint" />
</li>
</ul>
<br />
</div>
</div>
</template>
+10 -18
View File
@@ -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,16 +87,12 @@ function updateGraph(event) {
}
}
function updateHints(hints) {
store.session.hints.push(hints)
}
function updateStatus(status) {
store.session.status = status
}
function setTasks(tasks) {
store.session.tasks = tasks
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
}
export function useTerminal() {
@@ -135,14 +133,8 @@ function setup() {
socket.on('graph', (event) => {
updateGraph(event)
})
socket.on('hints', (event) => {
updateHints(event)
})
socket.on('tasks', (tasks) => {
setTasks(tasks)
})
socket.on('status', (event) => {
updateStatus(event)
socket.on('response', (response) => {
newResponse(response)
})
}
+7 -7
View File
@@ -61,7 +61,7 @@ onMounted(async () => {
<!-- main -->
<div id="main" class="h-[94vh] w-full flex p-3 space-x-3">
<!-- Topic and Command List -->
<div class="bg-bg-secondary h-full w-1/3 rounded-lg">
<div class="bg-bg-secondary h-full w-1/4 rounded-lg">
<section id="quest" class="h-full p-3 overflow-auto">
<QuestPart />
</section>
@@ -71,17 +71,17 @@ onMounted(async () => {
</section> -->
</div>
<!-- Terminal and Hint -->
<div class="bg-bg h-full w-1/3 rounded-lg">
<section id="terminal" class="h-3/5 overflow-hidden">
<TerminalPart />
<div class="bg-bg h-full w-1/2 rounded-lg">
<section id="hint" class="h-3/5">
<HintPart />
</section>
<hr class="border-border border" />
<section id="hint" class="h-2/5">
<HintPart />
<section id="terminal" class="h-2/5 overflow-hidden">
<TerminalPart />
</section>
</div>
<!-- Visualization -->
<div class="h-full w-1/3">
<div class="h-full w-1/4">
<div class="bg-border rounded-xl h-[90%] p-2">
<div class="bg-bg rounded-lg h-full p-2">
<section id="visualization" class="bg-bg-primary h-full rounded-md">
+9 -1
View File
@@ -1,5 +1,7 @@
import { model, Schema, Types } from 'mongoose'
import { IResponse, responseSchema } from './response.js'
export interface INode {
path: string
type: string
@@ -46,13 +48,14 @@ export interface ISession {
user: Types.ObjectId
quest: string
containerId: string | null
status: string
status: 'active' | 'finished' | 'inactive'
createdAt: Date
updatedAt: Date
finishedAt?: Date
lastActivityAt: Date
tasks: ITask[]
hints: string[][]
responses: IResponse[][]
graph: INode
}
@@ -85,6 +88,11 @@ export const Session = model<ISession>(
},
tasks: [taskSchema],
hints: [[String]],
responses: {
type: [[responseSchema]],
required: true,
default: [],
},
graph: {
type: nodeSchema,
default: {
@@ -6,8 +6,6 @@ import { pushToSession } from '../socket.js'
import { finishSession } from '../../models/sessionManager.js'
import { asyncHandler } from '../../middleware/error.js'
const commandCompleteCallbacks = new Map()
export const newCommand = asyncHandler(async (req: Request, res: Response) => {
const { command, pwd, output, error, params } = matchedData(req)
@@ -51,42 +49,14 @@ export const newCommand = asyncHandler(async (req: Request, res: Response) => {
c.stage = response.stage
await c.save()
if (response.stage === 'END') {
session.responses.push(response.responses)
if ((session.status as string) === 'finished') {
await finishSession(session)
}
commandCompleteCallbacks.set(session.id, response.callback)
pushToSession(session.id, 'status', 'pending')
res.status(201).json(response)
} else {
res.status(200).end()
pushToSession(session.id, 'response', response)
}
res.status(200).end()
await session.save()
})
export const completedCommand = asyncHandler(
async (req: Request, res: Response) => {
const { sessionId } = req.user as any
const session = await Session.findById(sessionId)
if (!session) {
res.status(404).json({ message: 'session not found' })
return
}
if (session.status === 'inactive') {
res.status(400).json({ message: 'session is not active' })
return
}
const callback = commandCompleteCallbacks.get(session.id)
if (!callback) {
res.status(200).json({ message: 'no callback found' })
return
}
callback()
commandCompleteCallbacks.delete(session.id)
res.json({ message: 'ok' })
}
)
@@ -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,
-1
View File
@@ -5,6 +5,5 @@ import { newCommandValidator } from '../validators/commandValidator.js'
const router = Router()
router.post('/', newCommandValidator, commandController.newCommand)
router.post('/completed', commandController.completedCommand)
export default router
+5 -1
View File
@@ -22,6 +22,10 @@ router.post(
activeSessionValidator,
sessionController.getOrCreateSession
)
router.get('/:id', sessionDetailValidator, sessionController.getSessionById)
router.get(
'/:sessionId',
sessionDetailValidator,
sessionController.getSessionById
)
export default router
@@ -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]
+11 -13
View File
@@ -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
}
@@ -79,15 +79,17 @@ export default class SessionHandler {
this.addNewTasks()
this.session.responses.push(stage.responses)
if (stage.id === 'END') {
this.session.status = 'finished'
}
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 +100,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,
}
}
}
+4 -4
View File
@@ -75,7 +75,7 @@ describe('example helloworld app', () => {
describe('Game Start Page UI', () => {
const title = '終端機的基本知識'
beforeEach(() => {
cy.PrepareForGame()
cy.PrepareForGame('get-started')
})
it('Check Header', () => {
cy.get('#GameTitle').should('contain', 'Linux Odyssey').and('be.visible')
@@ -124,7 +124,7 @@ describe('example helloworld app', () => {
})
describe('Game Play', () => {
beforeEach(() => {
cy.PrepareForGame()
cy.PrepareForGame('get-started')
})
it('Typing in Terminal', () => {
cy.typeInCommand('12345{enter}')
@@ -136,7 +136,7 @@ describe('example helloworld app', () => {
cy.checkTaskInit()
cy.InitTerminal()
cy.typeInCommand('echo start{enter}')
cy.checkPending()
// cy.checkPending()
cy.waitUntilActive()
cy.get('#Lbutton').should('be.visible').and('be.disabled')
cy.get('#Rbutton').should('be.visible').and('be.disabled')
@@ -144,7 +144,7 @@ describe('example helloworld app', () => {
cy.getQuestInfo('✓ 輸入 echo start 來開始教程').should('be.visible')
cy.getQuestInfo('➤ 在終端機輸入 echo hello').should('be.visible')
cy.typeInCommand('echo hello{enter}')
cy.checkPending()
// cy.checkPending()
cy.waitUntilActive()
cy.get('#Lbutton').should('be.visible').and('be.enabled')
cy.get('#Rbutton').should('be.visible').and('be.disabled')
-3
View File
@@ -1,7 +1,4 @@
describe('Complete quests with only commands', () => {
beforeEach(() => {
cy.PrepareForGame()
})
it('Complete quest : get-started', () => {
cy.CompleteStageWithCommands('get-started')
})
+16 -13
View File
@@ -38,7 +38,7 @@ Cypress.Commands.add('LoginWithPassword', (username, password) => {
cy.get('#password').type(password)
cy.get('#LogInOrSignUp').click()
})
Cypress.Commands.add('PrepareForGame', () => {
Cypress.Commands.add('PrepareForGame', (questId) => {
cy.visit('/')
// make sure the login page is loaded
cy.url().should('satisfy', (elements: string) => {
@@ -51,8 +51,9 @@ Cypress.Commands.add('PrepareForGame', () => {
)
// make sure the map page is loaded
cy.url().should('include', '/map')
cy.visit('/game/get-started')
cy.url().should('include', '/game/get-started')
const url = `/game/${questId}`
cy.visit(url)
cy.url().should('include', url)
// check task first so the terminal has better possibility loaded
cy.checkTaskInit()
cy.get('.xterm-screen', { timeout: 10000 })
@@ -114,8 +115,7 @@ Cypress.Commands.add('checkPending', () => {
)
})
Cypress.Commands.add('CompleteStageWithCommands', (stagename) => {
cy.visit(`/game/${stagename}`)
cy.url().should('include', `/game/${stagename}`, { timeout: 50000 })
cy.PrepareForGame(stagename)
cy.log(`Completing stage :${stagename}`)
cy.checkTaskInit()
cy.CheckTextElement('#reset', '重來', 'Reset').click()
@@ -123,19 +123,22 @@ Cypress.Commands.add('CompleteStageWithCommands', (stagename) => {
cy.InitTerminal()
cy.readFile(`../quests/${stagename}/answer.sh`, 'utf-8').then((answers) => {
const answerarr = answers.split('\n')
for (const element of answerarr) {
answerarr.forEach((element, index) => {
cy.typeInCommand(`${element}{enter}`)
if (answerarr.indexOf(element) + 1 !== answerarr.length) {
cy.checkPending()
if (index < answerarr.length - 1) {
cy.waitUntilActive()
}
if (answerarr.indexOf(element) + 1 === answerarr.length) {
cy.waitUntilActive(true)
cy.CheckTextElement('#QuestCompleted', '關卡完成!', 'Quest Completed!')
} else {
cy.waitUntilActive(true).then(() => {
cy.CheckTextElement(
'#QuestCompleted',
'關卡完成!',
'Quest Completed!'
)
cy.get('div[class="modal"]').find('p').should('be.visible')
cy.get('#BacktoMap').should('be.visible').and('contain', '回到地圖')
})
}
}
})
})
})
Cypress.Commands.add('CheckTextElement', (id, chText, enText) => {