Active player list
This commit is contained in:
31
routes/api/manage.ts
Normal file
31
routes/api/manage.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { SERVER_STATE } from "../../state/serverState.ts";
|
||||
|
||||
export enum ManageAction {
|
||||
start = "start",
|
||||
stop = "stop",
|
||||
restart = "restart",
|
||||
}
|
||||
|
||||
export const handler: Handlers = {
|
||||
async POST(req) {
|
||||
const body: ManageAction & string = await req.text() as ManageAction;
|
||||
switch (body) {
|
||||
case ManageAction.start:
|
||||
SERVER_STATE.startMCServer();
|
||||
return new Response("started");
|
||||
case ManageAction.stop:
|
||||
SERVER_STATE.gracefullyStopMCServer();
|
||||
return new Response("stopped");
|
||||
case ManageAction.restart:
|
||||
SERVER_STATE.restartMCServer();
|
||||
return new Response("restarted");
|
||||
default:
|
||||
SERVER_STATE.sendStdIn(body);
|
||||
return new Response("action done");
|
||||
}
|
||||
},
|
||||
GET() {
|
||||
return new Response(SERVER_STATE.status);
|
||||
}
|
||||
};
|
44
routes/api/players.ts
Normal file
44
routes/api/players.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { SERVER_STATE } from "../../state/serverState.ts";
|
||||
import { getActivePlayers } from "../../util/players.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
GET(_req, _ctx) {
|
||||
let listener: (e: CustomEvent) => void;
|
||||
const body = new ReadableStream({
|
||||
async start(controller){
|
||||
console.log('did the thing')
|
||||
if (SERVER_STATE.status !== 'running') return;
|
||||
const players = await getActivePlayers();
|
||||
|
||||
const event = `event:players\ndata:${JSON.stringify(players)}\n\n`
|
||||
|
||||
controller.enqueue(event);
|
||||
console.log('sent the thing')
|
||||
|
||||
listener = async (e: CustomEvent<string>) => {
|
||||
console.log('message received')
|
||||
if (e.detail.includes('joined the game') || e.detail.includes('lost connection')) {
|
||||
console.log('connection change')
|
||||
const players = await getActivePlayers();
|
||||
const event = `event: players\ndata: ${JSON.stringify(players)}\n\n`
|
||||
|
||||
controller.enqueue(event);
|
||||
}
|
||||
}
|
||||
|
||||
globalThis.addEventListener('stdoutmsg' as any, listener);
|
||||
console.log('listened the thing')
|
||||
},
|
||||
cancel() {
|
||||
console.log('cancelled')
|
||||
globalThis.removeEventListener('stdoutmsg' as any, listener);
|
||||
}
|
||||
})
|
||||
|
||||
return new Response(body.pipeThrough(new TextEncoderStream()), { headers: {
|
||||
"Content-Type": "text/event-stream",
|
||||
"cache-control": "no-cache"
|
||||
}});
|
||||
}
|
||||
}
|
@@ -1 +0,0 @@
|
||||
// const
|
Reference in New Issue
Block a user