mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
update store
This commit is contained in:
1 parent
c324bfcfee
commit
d924ccaaa9
3 files changed
+30
-20
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import FileNode from './FileNode.vue'
|
import FileNode from './FileNode.vue'
|
||||||
import sessionStore from '../store/session.js'
|
import sessionStore from '../store/session'
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { reactive } from 'vue'
|
import { reactive } from 'vue'
|
||||||
import { useToast } from 'vue-toastification'
|
import { useToast } from 'vue-toastification'
|
||||||
import { FileGraph } from '@linux-odyssey/file-graph'
|
import { FileGraph, FileObject } from '@linux-odyssey/file-graph'
|
||||||
import api from '../utils/api'
|
import api from '../utils/api'
|
||||||
import Socket from '../utils/socket'
|
import Socket from '../utils/socket'
|
||||||
import SocketTerminal from '../utils/terminal'
|
import SocketTerminal from '../utils/terminal'
|
||||||
@@ -33,14 +33,14 @@ const store = reactive({
|
|||||||
const socket = new Socket()
|
const socket = new Socket()
|
||||||
const term = new SocketTerminal(40, 80)
|
const term = new SocketTerminal(40, 80)
|
||||||
|
|
||||||
function setQuest(questId) {
|
function setQuest(questId: string) {
|
||||||
return api.get(`/quests/${questId}`).then((res) => {
|
return api.get(`/quests/${questId}`).then((res) => {
|
||||||
store.questId = res.data._id
|
store.questId = res.data._id
|
||||||
store.quest = res.data
|
store.quest = res.data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setSession(session) {
|
async function setSession(session: any) {
|
||||||
try {
|
try {
|
||||||
console.log('Setting session...', session)
|
console.log('Setting session...', session)
|
||||||
store.session = session
|
store.session = session
|
||||||
@@ -48,7 +48,7 @@ async function setSession(session) {
|
|||||||
term.reset()
|
term.reset()
|
||||||
await socket.connect(session)
|
await socket.connect(session)
|
||||||
term.focus()
|
term.focus()
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
toast.error(err.message)
|
toast.error(err.message)
|
||||||
throw err
|
throw err
|
||||||
@@ -67,7 +67,7 @@ export async function createSession() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getActiveSession(questId) {
|
async function getActiveSession(questId: any) {
|
||||||
try {
|
try {
|
||||||
const res = await api.post('/sessions/active', { questId })
|
const res = await api.post('/sessions/active', { questId })
|
||||||
await setSession(res.data)
|
await setSession(res.data)
|
||||||
@@ -78,7 +78,7 @@ async function getActiveSession(questId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateGraph(event) {
|
function updateGraph(event: { discover: FileObject[]; pwd: string }) {
|
||||||
if (event.discover) {
|
if (event.discover) {
|
||||||
store.session.graph.discover(event.discover)
|
store.session.graph.discover(event.discover)
|
||||||
}
|
}
|
||||||
@@ -87,12 +87,16 @@ function updateGraph(event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function newResponse(response) {
|
function updateHints(hints: never) {
|
||||||
console.log('newResponse', response, 'session', store.session)
|
store.session.hints.push(hints)
|
||||||
store.session.responses.push(response.responses)
|
}
|
||||||
store.session.hints.push(response.hints)
|
|
||||||
store.session.tasks = response.tasks
|
function updateStatus(status: string) {
|
||||||
store.session.status = response.status
|
store.session.status = status
|
||||||
|
}
|
||||||
|
|
||||||
|
function setTasks(tasks: never[]) {
|
||||||
|
store.session.tasks = tasks
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useTerminal() {
|
export function useTerminal() {
|
||||||
@@ -106,7 +110,7 @@ export function reset() {
|
|||||||
store.quest = null
|
store.quest = null
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function init(questId) {
|
export async function init(questId: string) {
|
||||||
if (!questId) throw new Error('No quest ID provided')
|
if (!questId) throw new Error('No quest ID provided')
|
||||||
reset()
|
reset()
|
||||||
try {
|
try {
|
||||||
@@ -124,17 +128,23 @@ export async function init(questId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setup() {
|
function setup() {
|
||||||
socket.on('terminal', (data) => {
|
socket.on('terminal', (data: any) => {
|
||||||
term.write(data)
|
term.write(data)
|
||||||
})
|
})
|
||||||
term.onData((data) => {
|
term.onData((data: any) => {
|
||||||
socket.emit('terminal', data)
|
socket.emit('terminal', data)
|
||||||
})
|
})
|
||||||
socket.on('graph', (event) => {
|
socket.on('graph', (event: { discover: FileObject[]; pwd: string }) => {
|
||||||
updateGraph(event)
|
updateGraph(event)
|
||||||
})
|
})
|
||||||
socket.on('response', (response) => {
|
socket.on('hints', (event: never) => {
|
||||||
newResponse(response)
|
updateHints(event)
|
||||||
|
})
|
||||||
|
socket.on('tasks', (tasks: never[]) => {
|
||||||
|
setTasks(tasks)
|
||||||
|
})
|
||||||
|
socket.on('status', (event: string) => {
|
||||||
|
updateStatus(event)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ export async function loadUserProfile() {
|
|||||||
store.email = email
|
store.email = email
|
||||||
store.progress = progress
|
store.progress = progress
|
||||||
return store
|
return store
|
||||||
} catch (err) {
|
} catch (err: any) {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
throw new Error(`Failed to load user profile: ${err.message}`)
|
throw new Error(`Failed to load user profile: ${err.message}`)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user