mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: add reset function
This commit is contained in:
1 parent
e122800c81
commit
313d30edda
4 files changed
+94
-52
No files matched your search
+2
-25
@@ -6,6 +6,7 @@ import HintPart from './components/HintPart.vue'
|
|||||||
import QuestPart from './components/QuestPart.vue'
|
import QuestPart from './components/QuestPart.vue'
|
||||||
import TerminalPart from './components/TerminalPart.vue'
|
import TerminalPart from './components/TerminalPart.vue'
|
||||||
import VisualizationPart from './components/VisualizationPart.vue'
|
import VisualizationPart from './components/VisualizationPart.vue'
|
||||||
|
import ControlPalette from './components/ControlPalette.vue'
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -44,31 +45,7 @@ import VisualizationPart from './components/VisualizationPart.vue'
|
|||||||
</section>
|
</section>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<section
|
<ControlPalette />
|
||||||
class="h-[10%] p-3 grid grid-cols-3 gap-4 place-content-between"
|
|
||||||
>
|
|
||||||
<button id="solution" class="bg-background-secondary rounded-lg p-2">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['far', 'circle-question']"
|
|
||||||
class="text-text-primary"
|
|
||||||
/>
|
|
||||||
<p class="text-text-primary inline">Solution</p>
|
|
||||||
</button>
|
|
||||||
<button id="reset" class="bg-background-secondary rounded-lg p-2">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'arrow-rotate-left']"
|
|
||||||
class="text-text-primary"
|
|
||||||
/>
|
|
||||||
<p class="text-text-primary inline">Reset</p>
|
|
||||||
</button>
|
|
||||||
<button id="continue" class="bg-disabled-background rounded-lg p-2">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['far', 'circle-right']"
|
|
||||||
class="text-disabled-text"
|
|
||||||
/>
|
|
||||||
<p class="text-disabled-text inline">Continue</p>
|
|
||||||
</button>
|
|
||||||
</section>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
<template>
|
||||||
|
<section class="h-[10%] p-3 grid grid-cols-3 gap-4 place-content-between">
|
||||||
|
<button id="solution" class="bg-background-secondary rounded-lg p-2">
|
||||||
|
<font-awesome-icon
|
||||||
|
:icon="['far', 'circle-question']"
|
||||||
|
class="text-text-primary"
|
||||||
|
/>
|
||||||
|
<p class="text-text-primary inline">Solution</p>
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
id="reset"
|
||||||
|
class="bg-background-secondary rounded-lg p-2"
|
||||||
|
@click="reset"
|
||||||
|
>
|
||||||
|
<font-awesome-icon
|
||||||
|
:icon="['fas', 'arrow-rotate-left']"
|
||||||
|
class="text-text-primary"
|
||||||
|
/>
|
||||||
|
<p class="text-text-primary inline">Reset</p>
|
||||||
|
</button>
|
||||||
|
<button id="continue" class="bg-disabled-background rounded-lg p-2">
|
||||||
|
<font-awesome-icon
|
||||||
|
:icon="['far', 'circle-right']"
|
||||||
|
class="text-disabled-text"
|
||||||
|
/>
|
||||||
|
<p class="text-disabled-text inline">Continue</p>
|
||||||
|
</button>
|
||||||
|
</section>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import sessionManager from '../utils/session'
|
||||||
|
|
||||||
|
const reset = async () => {
|
||||||
|
console.log('resetting...')
|
||||||
|
await sessionManager.createSession()
|
||||||
|
}
|
||||||
|
</script>
|
||||||
@@ -12,15 +12,14 @@ import { Terminal } from 'xterm'
|
|||||||
import { FitAddon } from 'xterm-addon-fit'
|
import { FitAddon } from 'xterm-addon-fit'
|
||||||
import 'xterm/css/xterm.css'
|
import 'xterm/css/xterm.css'
|
||||||
import { io } from 'socket.io-client'
|
import { io } from 'socket.io-client'
|
||||||
import { onMounted, ref } from 'vue'
|
import { onMounted, ref, watch } from 'vue'
|
||||||
import sessionManager from '../utils/session'
|
import sessionManager from '../utils/session'
|
||||||
|
|
||||||
const rows = ref(40)
|
const rows = ref(40)
|
||||||
const cols = ref(100)
|
const cols = ref(100)
|
||||||
const terminal = ref(null)
|
const terminal = ref(null)
|
||||||
|
|
||||||
onMounted(async () => {
|
const term = new Terminal({
|
||||||
const term = new Terminal({
|
|
||||||
rendererType: 'canvas', // 渲染类型
|
rendererType: 'canvas', // 渲染类型
|
||||||
rows: rows.value, // 行数
|
rows: rows.value, // 行数
|
||||||
cols: cols.value, // 不指定行数,自动回车后光标从下一行开始
|
cols: cols.value, // 不指定行数,自动回车后光标从下一行开始
|
||||||
@@ -35,9 +34,40 @@ onMounted(async () => {
|
|||||||
cursor: 'help', // 设置光标
|
cursor: 'help', // 设置光标
|
||||||
lineHeight: 20,
|
lineHeight: 20,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const session = await sessionManager.latestOrCreate()
|
const fitAddon = new FitAddon()
|
||||||
|
term.loadAddon(fitAddon)
|
||||||
|
fitAddon.fit()
|
||||||
|
term.writeln('Welcome to \x1b[1;32mLinux Odyssey\x1b[0m!')
|
||||||
|
|
||||||
|
function resizeScreen() {
|
||||||
|
try {
|
||||||
|
// 窗口大小改变时,触发xterm的resize方法使自适应
|
||||||
|
fitAddon.fit()
|
||||||
|
} catch (e) {
|
||||||
|
console.log('e', e.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.addEventListener('resize', resizeScreen)
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
term.open(terminal.value)
|
||||||
|
term.focus()
|
||||||
|
resizeScreen()
|
||||||
|
})
|
||||||
|
|
||||||
|
let currentSocket = null
|
||||||
|
|
||||||
|
watch(sessionManager.session, async (session) => {
|
||||||
|
if (!session) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (currentSocket) {
|
||||||
|
term.clear()
|
||||||
|
currentSocket.disconnect()
|
||||||
|
currentSocket.off()
|
||||||
|
}
|
||||||
const host = 'wss://odyssey.wancat.cc'
|
const host = 'wss://odyssey.wancat.cc'
|
||||||
|
|
||||||
console.log(session)
|
console.log(session)
|
||||||
@@ -47,6 +77,7 @@ onMounted(async () => {
|
|||||||
session_id: session._id,
|
session_id: session._id,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
currentSocket = socket
|
||||||
// console.log(sessionId)
|
// console.log(sessionId)
|
||||||
socket.on('connect', function open() {
|
socket.on('connect', function open() {
|
||||||
console.log('Connected to the server.')
|
console.log('Connected to the server.')
|
||||||
@@ -55,28 +86,12 @@ onMounted(async () => {
|
|||||||
console.log('Disconnected from the server.')
|
console.log('Disconnected from the server.')
|
||||||
})
|
})
|
||||||
socket.on('message', (message) => {
|
socket.on('message', (message) => {
|
||||||
console.log(message)
|
|
||||||
term.write(message)
|
term.write(message)
|
||||||
// (program.opts().create ? (await createdSession())._id : null) ||
|
// (program.opts().create ? (await createdSession())._id : null) ||
|
||||||
// (await lastSession())._id
|
// (await lastSession())._id
|
||||||
})
|
})
|
||||||
term.onData((key) => {
|
term.onData((key) => {
|
||||||
console.log(key)
|
|
||||||
socket.send(key)
|
socket.send(key)
|
||||||
})
|
})
|
||||||
term.open(terminal.value)
|
|
||||||
const fitAddon = new FitAddon()
|
|
||||||
term.loadAddon(fitAddon)
|
|
||||||
fitAddon.fit()
|
|
||||||
term.writeln('Welcome to \x1b[1;32mLinux Odyssey\x1b[0m!')
|
|
||||||
function resizeScreen() {
|
|
||||||
try {
|
|
||||||
// 窗口大小改变时,触发xterm的resize方法使自适应
|
|
||||||
fitAddon.fit()
|
|
||||||
} catch (e) {
|
|
||||||
console.log('e', e.message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
window.addEventListener('resize', resizeScreen)
|
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
@@ -1,16 +1,23 @@
|
|||||||
|
import { ref } from 'vue'
|
||||||
import api from './api'
|
import api from './api'
|
||||||
|
|
||||||
class SessionManager {
|
class SessionManager {
|
||||||
constructor(questId) {
|
constructor(questId) {
|
||||||
this.questId = questId
|
this.questId = questId
|
||||||
|
this.session = ref(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
async createdSession() {
|
setSession(session) {
|
||||||
|
console.log('Update session:', session)
|
||||||
|
this.session.value = session
|
||||||
|
}
|
||||||
|
|
||||||
|
async createSession() {
|
||||||
console.log('Creating a new session...')
|
console.log('Creating a new session...')
|
||||||
const res = await api.post('/sessions', { quest_id: this.questId })
|
const res = await api.post('/sessions', { quest_id: this.questId })
|
||||||
const { data } = res
|
const { data } = res
|
||||||
console.log('Created Session ID:', data._id)
|
console.log('Created Session ID:', data._id)
|
||||||
return data
|
this.setSession(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSessionList() {
|
async getSessionList() {
|
||||||
@@ -24,20 +31,25 @@ class SessionManager {
|
|||||||
async lastSession() {
|
async lastSession() {
|
||||||
const sessions = await this.getSessionList()
|
const sessions = await this.getSessionList()
|
||||||
if (sessions.length === 0) {
|
if (sessions.length === 0) {
|
||||||
return null
|
return
|
||||||
}
|
}
|
||||||
const session = sessions[sessions.length - 1]
|
const session = sessions[sessions.length - 1]
|
||||||
console.log(
|
console.log(
|
||||||
`Last Session ID: ${session._id}, Quest: ${session.quest}, Created At: ${session.createdAt}`
|
`Last Session ID: ${session._id}, Quest: ${session.quest}, Created At: ${session.createdAt}`
|
||||||
)
|
)
|
||||||
return session
|
this.setSession(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
async latestOrCreate() {
|
async lastOrCreate() {
|
||||||
return (await this.lastSession()) || this.createdSession()
|
await this.lastSession()
|
||||||
|
if (!this.session.value) {
|
||||||
|
await this.createSession()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const sessionManager = new SessionManager('helloworld')
|
const sessionManager = new SessionManager('helloworld')
|
||||||
|
|
||||||
|
sessionManager.lastOrCreate().catch(console.error)
|
||||||
|
|
||||||
export default sessionManager
|
export default sessionManager
|
||||||
Reference in new issue
Block a user