From 51fc0bbb380ead9a1f094b2a170f47dfaadc2602 Mon Sep 17 00:00:00 2001 From: nekosu Date: Fri, 11 Aug 2023 13:25:42 +0800 Subject: [PATCH] feat: fix task running --- server/src/index.ts | 67 +++++++++++++++++-------------- src/App.vue | 19 ++++++++- src/api.ts | 19 +++++++++ src/components/ConfigEdit.vue | 42 ++++++++++++++++++++ src/data/config.ts | 5 +++ src/data/index.ts | 1 + src/loader.ts | 11 ++++++ src/misc/utils.ts | 12 ++++-- src/types.ts | 16 ++++++++ src/views/EvalView.vue | 74 ++++++++++++++++++++--------------- 10 files changed, 200 insertions(+), 66 deletions(-) create mode 100644 src/components/ConfigEdit.vue create mode 100644 src/data/config.ts diff --git a/server/src/index.ts b/server/src/index.ts index 1f2f5a4..271a285 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -36,6 +36,9 @@ interface Config { let config: Config let loader: MaaFrameworkLoader let controller: MaaController +let resource: MaaResource +let instance: MaaInstance +const instanceListener: ((msg: string, detail: unknown) => void)[] = [] sms.install() @@ -79,6 +82,21 @@ async function main() { } }) + app.post('/api/config/load', (req, res) => { + res.send({ + success: true, + config + }) + }) + + app.post('/api/config/save', (req, res) => { + config = req.body + fs.writeFile('config.json', JSON.stringify(config, null, 2)) + res.send({ + success: true + }) + }) + app.ws('/api/controller', async (ws, req) => { ws.on('message', async data => { const action = JSON.parse(data.toString('utf-8')) as { @@ -116,34 +134,19 @@ async function main() { return } - const resource = await prepareResource() - if (!resource) { + const loaded = await prepareResource() + if (!loaded) { ws.close() return } - const instance = new MaaInstance(loader, (msg, detail) => { - ws.send( - JSON.stringify({ - type: 'callback', - msg, - detail - }) - ) - }) - - instance.bindController(controller) - instance.bindResource(resource) - if (!instance.inited()) { ws.close() - resource.destroy() return } ws.on('close', () => { - // instance.destroy() - resource.destroy() + instance.stop() }) ws.send( @@ -189,9 +192,21 @@ async function main() { loader.setDebugMode(config.maaframework.debug) const ctrl = await prepareController() - if (ctrl) { - controller = ctrl + if (!ctrl) { + return } + + controller = ctrl + resource = new MaaResource(loader) + instance = new MaaInstance(loader, (msg, detail) => { + detail = JSON.parse(detail) + for (const cb of instanceListener) { + cb(msg, detail) + } + }) + + instance.bindController(controller) + instance.bindResource(resource) } } @@ -220,16 +235,10 @@ async function prepareResource() { ) console.log('resource prepared at', tempResourceDir) - const res = new MaaResource(loader) - const loaded = await res.load(tempResourceDir) + const loaded = await resource.load(tempResourceDir) console.log('resource load:', loaded) - if (!loaded) { - res.destroy() - return null - } - - return res + return loaded } async function prepareController() { @@ -237,7 +246,7 @@ async function prepareController() { loader, config.maaframework.adb, config.maaframework.address, - MaaAdbControllerTypeEnum.Input_Preset_Minitouch | + MaaAdbControllerTypeEnum.Input_Preset_Adb | MaaAdbControllerTypeEnum.Screencap_Encode, await fs.readFile( path.join(config.maaframework.root, 'controller_config.json'), diff --git a/src/App.vue b/src/App.vue index 3ecd6fc..70c0ca2 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,12 +1,27 @@ + + diff --git a/src/data/config.ts b/src/data/config.ts new file mode 100644 index 0000000..3563309 --- /dev/null +++ b/src/data/config.ts @@ -0,0 +1,5 @@ +import { ref } from 'vue' + +import type { Config } from '@/types' + +export const config = ref(null) diff --git a/src/data/index.ts b/src/data/index.ts index 094ebf6..a64e4c5 100644 --- a/src/data/index.ts +++ b/src/data/index.ts @@ -3,6 +3,7 @@ import { computed } from 'vue' import { history } from '@/data' import type { PathKey } from '@/filesystem' +export * from './config' export * from './filesystem' export * from './history' export * from './image' diff --git a/src/loader.ts b/src/loader.ts index 3de9df1..3a9c76e 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -1,4 +1,5 @@ import * as api from '@/api' +import { config } from '@/data' import { fs } from '@/filesystem' export async function loadFS() { @@ -10,3 +11,13 @@ export async function saveFS() { const zip = await fs.saveZip() await api.save(zip) } + +export async function loadCfg() { + config.value = await api.loadConfig() +} + +export async function saveCfg() { + if (config.value) { + await api.saveConfig(config.value) + } +} diff --git a/src/misc/utils.ts b/src/misc/utils.ts index a787773..da29c0a 100644 --- a/src/misc/utils.ts +++ b/src/misc/utils.ts @@ -1,8 +1,14 @@ -import { type Ref, computed } from 'vue' +import { type Ref, type WritableComputedRef, computed } from 'vue' -export function wrapProp(obj: Ref, key: K) { +export function wrapProp( + obj: Ref | WritableComputedRef, + key: K +) { return computed | null>({ set(v) { + if (obj.value === null) { + return + } if (v === null) { if (key in obj.value) { delete obj.value[key] @@ -12,7 +18,7 @@ export function wrapProp(obj: Ref, key: K) { } }, get() { - return obj.value[key] ?? null + return obj.value?.[key] ?? null } }) } diff --git a/src/types.ts b/src/types.ts index 964c1d7..9053256 100644 --- a/src/types.ts +++ b/src/types.ts @@ -171,3 +171,19 @@ export const enum DispatcherStatus { Failed, Stopped } + +export interface Config { + port: number + web: string + date: string + saves: string + active: string + maaframework: { + emulator: boolean + adb: string + address: string + root: string + log: string + debug: boolean + } +} diff --git a/src/views/EvalView.vue b/src/views/EvalView.vue index 97b5d67..e047326 100644 --- a/src/views/EvalView.vue +++ b/src/views/EvalView.vue @@ -8,13 +8,14 @@ import { PlayArrowOutlined } from '@vicons/material' import { NButton, NCard, NCheckbox, NIcon } from 'naive-ui' -import { ref } from 'vue' +import { onMounted, onUnmounted, ref } from 'vue' import { useRouter } from 'vue-router' import * as api from '@/api' -import { taskIndex } from '@/data' +import { config, taskIndex } from '@/data' import { DispatcherStatus, type TaskRunInfo } from '@/types' +import ConfigEdit from '@/components/ConfigEdit.vue' import ArrayEdit from '@/components/array/ArrayEdit.vue' import ClearButton from '@/components/atomic/ClearButton.vue' import SingleNavigateEdit from '@/components/task/SingleNavigateEdit.vue' @@ -25,7 +26,36 @@ const router = useRouter() const runInfo = ref([]) let instance: WebSocket -let inited = false +const inited = ref(false) + +onMounted(async () => { + inited.value = false + instance = await api.instance() + instance.onmessage = ev => { + const obj = JSON.parse(ev.data) as { + type: 'inited' | 'callback' | 'status' + msg: string + detail: string + task: string + status: DispatcherStatus + } + switch (obj.type) { + case 'inited': + inited.value = true + break + case 'callback': + console.log(obj.msg, JSON.parse(obj.detail)) + break + case 'status': { + const idx = runInfo.value.findIndex(x => x.task === obj.task) + if (idx !== -1) { + runInfo.value[idx].status = translateStatus(obj.status) + } + break + } + } + } +}) function translateStatus(status: DispatcherStatus) { switch (status) { @@ -44,34 +74,13 @@ function translateStatus(status: DispatcherStatus) { } } -async function tryStart() { - if (!instance) { - instance = await api.instance() - instance.onmessage = ev => { - const obj = JSON.parse(ev.data) as { - type: 'inited' | 'callback' | 'status' - msg: string - detail: string - task: string - status: DispatcherStatus - } - switch (obj.type) { - case 'inited': - inited = true - break - case 'callback': - console.log(obj.msg, JSON.parse(obj.detail)) - break - case 'status': { - const idx = runInfo.value.findIndex(x => x.task === obj.task) - if (idx !== -1) { - runInfo.value[idx].status = translateStatus(obj.status) - } - break - } - } - } +onUnmounted(() => { + if (instance) { + instance.close() } +}) + +async function tryStart() { if (inited) { instance.send( JSON.stringify({ @@ -95,7 +104,7 @@ async function tryStart() { - + -
+
+ 任务列表