105 lines
3.2 KiB
TypeScript
105 lines
3.2 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// the desktop app's singleplayer: what the main menu asks it (see client/gui/main_menu.ts), and the game server of
|
|
// the world being played, with the mods the world picked from the data folder. see saves.ts
|
|
import { join } from "@std/path";
|
|
import { type Host, start_host } from "../server/host.ts";
|
|
import { create_world, list_mods, list_worlds, mods_dir, set_world_mods, world_to_play } from "./saves.ts";
|
|
|
|
export interface SingleplayerOptions {
|
|
// what deno task build made: the client and the engine's assets
|
|
build_dir: string;
|
|
// the app's data folder, see saves.ts
|
|
data_dir: string;
|
|
// the world being played can't go on
|
|
on_fatal(message: string): void;
|
|
// how many workers generate chunks, see HostOptions
|
|
worldgen_workers?: number;
|
|
}
|
|
|
|
export function singleplayer_server(options: SingleplayerOptions) {
|
|
const data = options.data_dir;
|
|
// playing another world, or the same one again after changing its mods, stops it first
|
|
let host: Promise<Host> | undefined;
|
|
|
|
async function play(world: unknown) {
|
|
await stop();
|
|
const { dir, mod_files } = world_to_play(data, world);
|
|
host = start_host({
|
|
build_dir: options.build_dir,
|
|
server_mods_dir: mods_dir(data),
|
|
mod_files,
|
|
config_dir: join(data, "config"),
|
|
world_dir: dir,
|
|
worldgen_workers: options.worldgen_workers,
|
|
on_fatal: options.on_fatal,
|
|
});
|
|
try {
|
|
await host;
|
|
} catch (e) {
|
|
host = undefined;
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
// saves the world being played
|
|
async function stop() {
|
|
const playing = host;
|
|
host = undefined;
|
|
try {
|
|
await (await playing)?.shutdown();
|
|
} catch (e) {
|
|
console.error((e as Error).message);
|
|
}
|
|
}
|
|
|
|
// errors are told to the player
|
|
async function menu_request(req: Request, url: URL): Promise<Response> {
|
|
if (url.pathname === "/desktop/singleplayer") {
|
|
return Response.json({ worlds: list_worlds(data), mods: list_mods(data) });
|
|
}
|
|
let body: Record<string, unknown>;
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return new Response("Expected json", { status: 400 });
|
|
}
|
|
try {
|
|
switch (url.pathname) {
|
|
case "/desktop/create_world":
|
|
create_world(data, body.world, body.mods);
|
|
break;
|
|
case "/desktop/world_mods":
|
|
set_world_mods(data, body.world, body.mods);
|
|
break;
|
|
case "/desktop/play":
|
|
await play(body.world);
|
|
break;
|
|
default:
|
|
return new Response("Not found", { status: 404 });
|
|
}
|
|
} catch (e) {
|
|
console.error((e as Error).message);
|
|
return new Response((e as Error).message, { status: 400 });
|
|
}
|
|
return new Response(null, { status: 204 });
|
|
}
|
|
|
|
return {
|
|
// undefined for requests that aren't singleplayer's
|
|
async handle(req: Request, url: URL): Promise<Response | undefined> {
|
|
if (url.pathname.startsWith("/desktop/") && req.method === "POST") {
|
|
return await menu_request(req, url);
|
|
}
|
|
// the world's socket and its .cubi downloads
|
|
if (url.pathname === "/ws" || url.pathname.startsWith("/mods/")) {
|
|
const playing = await host?.catch(() => undefined);
|
|
return playing ? playing.handle(req) : new Response("No world is being played", { status: 503 });
|
|
}
|
|
return undefined;
|
|
},
|
|
stop,
|
|
};
|
|
}
|