diff --git a/server/MaaJSLoader b/server/MaaJSLoader index ba3909d..3f60ed2 160000 --- a/server/MaaJSLoader +++ b/server/MaaJSLoader @@ -1 +1 @@ -Subproject commit ba3909d03946345c9b420431d960d39e62bd8faf +Subproject commit 3f60ed24a82349d3e95a1fef6d4ed012b3558cb7 diff --git a/server/src/index.ts b/server/src/index.ts index b1ac028..1f2f5a4 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -9,7 +9,12 @@ import os from 'os' import path from 'path' import sms from 'source-map-support' -import { MaaController, MaaFrameworkLoader, MaaResource } from '../MaaJSLoader' +import { + MaaController, + MaaFrameworkLoader, + MaaInstance, + MaaResource +} from '../MaaJSLoader' import { MaaAdbControllerTypeEnum } from '../MaaJSLoader/src/framework/types' interface Config { @@ -105,17 +110,84 @@ async function main() { }) }) + app.ws('/api/instance', async (ws, req) => { + if (!controller) { + ws.close() + return + } + + const resource = await prepareResource() + if (!resource) { + 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() + }) + + ws.send( + JSON.stringify({ + type: 'inited' + }) + ) + + ws.on('message', async data => { + const action = JSON.parse(data.toString('utf-8')) as { + action: 'start' + task: string[] + } + if (controller) { + switch (action.action) { + case 'start': { + for (const task of action.task) { + instance.post(task, {}, status => { + ws.send( + JSON.stringify({ + type: 'status', + task, + status + }) + ) + }) + } + } + } + } + }) + }) + app.listen(config.port, () => { console.log(`server started: http://localhost:${config.port}/`) }) - loader = new MaaFrameworkLoader() - loader.load(path.join(config.maaframework.root, 'bin')) + if (config.maaframework.emulator) { + loader = new MaaFrameworkLoader() + loader.load(path.join(config.maaframework.root, 'bin')) - loader.setLogging(config.maaframework.log) - loader.setDebugMode(config.maaframework.debug) + loader.setLogging(config.maaframework.log) + loader.setDebugMode(config.maaframework.debug) - if (config.maaframework.emulator) { const ctrl = await prepareController() if (ctrl) { controller = ctrl diff --git a/src/App.vue b/src/App.vue index a740571..3ecd6fc 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,5 +1,28 @@