diff --git a/.eslintrc.js b/.eslintrc.js index 7d9e2c6..f438449 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -21,5 +21,6 @@ module.exports = { 'import/extensions': 'off', camelcase: ['error', { properties: 'never', ignoreDestructuring: true }], 'no-param-reassign': ['error', { props: false }], + 'no-underscore-dangle': ['error', { allow: ['_id'] }], }, } diff --git a/app/src/components/TerminalPart.vue b/app/src/components/TerminalPart.vue index 9187d7a..9b92dc6 100644 --- a/app/src/components/TerminalPart.vue +++ b/app/src/components/TerminalPart.vue @@ -13,13 +13,13 @@ import { FitAddon } from 'xterm-addon-fit' import 'xterm/css/xterm.css' import { io } from 'socket.io-client' import { onMounted, ref } from 'vue' -// import api from '../utils/api' +import sessionManager from '../utils/session' const rows = ref(40) const cols = ref(100) const terminal = ref(null) -onMounted(() => { +onMounted(async () => { const term = new Terminal({ rendererType: 'canvas', // 渲染类型 rows: rows.value, // 行数 @@ -36,35 +36,15 @@ onMounted(() => { lineHeight: 20, }, }) + + const session = await sessionManager.latestOrCreate() const host = 'wss://odyssey.wancat.cc' - const sessionhost = 'http://odyssey.wancat.cc' - // const host = 'ws://localhost:3000' - // const sessions = async () => { - // await api.get(`/sessions`).json() - // } - // console.log(sessions) - const res = async () => { - await fetch(`${sessionhost}/api/v1/sessions`, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - quest_id: 'helloworld', - }), - }) - } - const data = async () => { - await res.json() - } - const sessionId = data._id - // async () => {} - // console.log(api.get('/quests/helloworld').session) - // console.log(await sessions[sessions.length - 1].id) + + console.log(session) + const socket = io(host, { query: { - session_id: sessionId, - // session_id: '64c1164a5b34af580963da6b', + session_id: session._id, }, }) // console.log(sessionId) diff --git a/app/src/utils/session.js b/app/src/utils/session.js new file mode 100644 index 0000000..73200e2 --- /dev/null +++ b/app/src/utils/session.js @@ -0,0 +1,43 @@ +import api from './api' + +class SessionManager { + constructor(questId) { + this.questId = questId + } + + async createdSession() { + console.log('Creating a new session...') + const res = await api.post('/sessions', { quest_id: this.questId }) + const { data } = res + console.log('Created Session ID:', data._id) + return data + } + + async getSessionList() { + const res = await api.get('/sessions', { + params: { quest_id: this.questId }, + }) + const { data } = res + return data + } + + async lastSession() { + const sessions = await this.getSessionList() + if (sessions.length === 0) { + return null + } + const session = sessions[sessions.length - 1] + console.log( + `Last Session ID: ${session._id}, Quest: ${session.quest}, Created At: ${session.createdAt}` + ) + return session + } + + async latestOrCreate() { + return (await this.lastSession()) || this.createdSession() + } +} + +const sessionManager = new SessionManager('helloworld') + +export default sessionManager diff --git a/packages/cli/index.js b/packages/cli/index.js deleted file mode 100644 index 1e87ff1..0000000 --- a/packages/cli/index.js +++ /dev/null @@ -1,50 +0,0 @@ -import { stdin, stdout, exit } from 'process' -import os from 'os' -import pty from 'node-pty' - -stdin.setRawMode(true) -stdin.resume() -stdin.setEncoding('utf8') - -// Create a new pseudoterminal -const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash' -const ptyProcess = pty.spawn(shell, [], { - name: 'xterm-color', - cols: 80, - rows: 30, - cwd: process.env.HOME, - env: process.env, -}) - -// Listen for output data -ptyProcess.on('data', function (data) { - // Intercept and process the output here - stdout.write(data) -}) - -// Write a command to the pseudoterminal -ptyProcess.write('ls\r') - -let commandBuffer = '' - -stdin.on('data', (key) => { - if (key === '\u0004') { - exit() - } else if (key === '\r') { - // User pressed enter, log and execute the command - console.log('User command: ', commandBuffer) - commandBuffer = '' - } else if (key === '\u007F') { - // User pressed backspace, remove the last character from the buffer - commandBuffer = commandBuffer.slice(0, -1) - } else { - // Add the typed character to the command buffer - commandBuffer += key - } - ptyProcess.write(key) -}) - -// Listen for exit event -ptyProcess.on('exit', function (exitCode) { - console.log('Process exited with code: ', exitCode) -}) diff --git a/yarn.lock b/yarn.lock index 9f1b4b7..e7d8450 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3612,13 +3612,6 @@ node-int64@^0.4.0: resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b" integrity sha512-O5lz91xSOeoXP6DulyHfllpq+Eg00MWitZIbtPfoSEvqIHdl5gfcY6hYzDWnj0qD5tz52PI08u9qUvSVeUBeHw== -node-pty@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/node-pty/-/node-pty-1.0.0.tgz#7daafc0aca1c4ca3de15c61330373af4af5861fd" - integrity sha512-wtBMWWS7dFZm/VgqElrTvtfMq4GzJ6+edFI0Y0zyzygUSZMgZdraDUMUhCIvkjhJjme15qWmbyJbtAx4ot4uZA== - dependencies: - nan "^2.17.0" - node-releases@^2.0.12: version "2.0.13" resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-2.0.13.tgz#d5ed1627c23e3461e819b02e57b75e4899b1c81d"