diff --git a/deno.json b/deno.json index f9722d8..92f2cd6 100644 --- a/deno.json +++ b/deno.json @@ -8,36 +8,19 @@ "update": "deno run -A -r https://fresh.deno.dev/update .", "clean": "rm -rf ./server && rm ./mcgrizz.json && rm ./players.cache.json" }, - "lint": { - "rules": { - "tags": [ - "fresh", - "recommended" - ] - }, - "exclude": [ - "_fresh" - ] - }, - "fmt": { - "exclude": [ - "_fresh" - ] - }, + "lint": { "rules": { "tags": ["fresh", "recommended"] } }, "imports": { - "$fresh/": "https://deno.land/x/fresh@1.4.3/", - "preact": "https://esm.sh/preact@10.15.1", - "preact/": "https://esm.sh/preact@10.15.1/", - "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.1", - "@preact/signals": "https://esm.sh/*@preact/signals@1.1.3", - "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.2.3", + "$fresh/": "https://deno.land/x/fresh@1.5.2/", + "preact": "https://esm.sh/preact@10.18.1", + "preact/": "https://esm.sh/preact@10.18.1/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.2.2", + "@preact/signals": "https://esm.sh/*@preact/signals@1.2.1", + "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.5.0", "$std/": "https://deno.land/std@0.193.0/", "puppet": "https://deno.land/x/sockpuppet@0.6.2/mod.ts", "puppet/client": "https://deno.land/x/sockpuppet@0.6.2/client/mod.ts", "jsdom": "https://esm.sh/jsdom" }, - "compilerOptions": { - "jsx": "react-jsx", - "jsxImportSource": "preact" - } + "compilerOptions": { "jsx": "react-jsx", "jsxImportSource": "preact" }, + "exclude": ["**/_fresh/*"] } diff --git a/state/serverState.ts b/state/serverState.ts index 6450706..4ad4f7e 100644 --- a/state/serverState.ts +++ b/state/serverState.ts @@ -12,6 +12,7 @@ type status = "running" | "stopped"; class ServerState { private _status: status = "stopped"; + private pid?: number; public get status() { return this._status; } @@ -65,26 +66,31 @@ class ServerState { this._serverVersion = conf.version; this._eulaAccepted = checkEULA(); + } - this.sockpuppet = new Sockpuppet( - "ws://sockpuppet.cyborggrizzly.com", - () => { - this.sockpuppet.createChannel(this._channelId); - this.sockpuppet.joinChannel(this.channelId, async (msg) => { - if (msg === "log" && !IS_BROWSER) { - try { - const log = await Deno.readTextFile("./server/logs/latest.log"); - this.channel?.send(log); - } catch { - 1; + private configureSockpuppet(): Promise { + return new Promise((res) => { + this.sockpuppet = new Sockpuppet( + "ws://sockpuppet.cyborggrizzly.com", + async () => { + await this.sockpuppet.createChannel(this._channelId); + res(true); + this.sockpuppet.joinChannel(this.channelId, async (msg) => { + if (msg === "log" && !IS_BROWSER) { + try { + const log = await Deno.readTextFile("./server/logs/latest.log"); + this.sendMessageToChannel(log); + } catch { + 1; + } + } else { + console.log(msg); + this.sendStdIn(msg); } - } else { - console.log(msg); - this.sendStdIn(msg); - } - }); - }, - ); + }); + }, + ); + }); } public get channel() { @@ -103,9 +109,12 @@ class ServerState { public async startMCServer(instance = "server") { this.command = getServerStartCommands(this._serverType, instance); - this.channel?.send("clear"); + await this.configureSockpuppet(); + + this.sendMessageToChannel("clear"); this.process = this.command.spawn(); + this.pid = this.process.pid; const { readable, writable } = new TransformStream(); readable.pipeTo(this.process.stdin); @@ -119,26 +128,10 @@ class ServerState { } private async startStream() { - // const stream = this.process.stdout.getReader(); - // const decoder = new TextDecoder(); - - // while (true) { - // const { done, value } = await stream.read(); - // if (value) { - // const line = decoder.decode(value); - // this.channel?.send(line); - // const stdoutMsg = new CustomEvent("stdoutmsg", { - // detail: line, - // }); - // globalThis.dispatchEvent(stdoutMsg); - // } - // if (done) break; - // } - await this.process.stdout.pipeThrough(new TextDecoderStream()).pipeTo( new WritableStream({ write: (chunk) => { - this.channel?.send(chunk); + this.sendMessageToChannel(chunk); const stdoutMsg = new CustomEvent("stdoutmsg", { detail: chunk, }); @@ -153,9 +146,10 @@ class ServerState { this.sendStdIn("stop"); } - public forceStopMCServer() { + public async forceStopMCServer() { this.status = "stopped"; this.process.kill(); + await Deno.remove('./server/session.lock'); } public async restartMCServer() { @@ -166,10 +160,7 @@ class ServerState { }); globalThis.dispatchEvent(statusEvent); - - // while (true) { await this.process.status; - // } } this.startMCServer(); @@ -179,6 +170,21 @@ class ServerState { this._eulaAccepted = true; acceptEULA(); } + + private async sendMessageToChannel(msg: string, retries = 0) { + if (retries > 4) return console.log('Unable to reconnect to socket after 5 tries. Is the puppet server ok?') + try { + this.channel?.send(msg); + } catch (e) { + console.log(e); + console.log( + "Error sending message via socket, likely closed. Reconnecting", + ); + + await this.configureSockpuppet(); + await this.sendMessageToChannel(msg, retries + 1); + } + } } export const SERVER_STATE = new ServerState();