diff --git a/.gitignore b/.gitignore index a389d25..8534ca9 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/packages/client/index.js b/packages/client/index.js index f7988ed..99ec410 100644 --- a/packages/client/index.js +++ b/packages/client/index.js @@ -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) diff --git a/packages/client/package.json b/packages/client/package.json index 968cf60..983b984 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -8,6 +8,7 @@ "start": "node index.js" }, "dependencies": { + "axios": "^1.5.0", "commander": "^11.0.0", "socket.io-client": "^4.7.2" } diff --git a/server/src/api/controllers/sessionController.js b/server/src/api/controllers/sessionController.js index aa37963..3130288 100644 --- a/server/src/api/controllers/sessionController.js +++ b/server/src/api/controllers/sessionController.js @@ -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 }) } } diff --git a/server/src/game/commandHandler.js b/server/src/game/commandHandler.js index 01ea533..f18900c 100644 --- a/server/src/game/commandHandler.js +++ b/server/src/game/commandHandler.js @@ -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, diff --git a/server/src/game/sessionHandler.js b/server/src/game/sessionHandler.js index de9208e..124c305 100644 --- a/server/src/game/sessionHandler.js +++ b/server/src/game/sessionHandler.js @@ -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, } } } diff --git a/yarn.lock b/yarn.lock index 874a6cd..9f189e4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"