Improve networking

This commit is contained in:
2026-09-25 00:08:01 -03:00
parent 6458bc0440
commit 2a2ccae9ce
18 changed files with 600 additions and 162 deletions
+16 -3
View File
@@ -4,6 +4,7 @@ import { load_and_check, load_order } from "$/tools/check_mods.ts";
import type { ServerModSource } from "$/server/game/load_mods.ts";
import { GameServer } from "$/server/game/game_server.ts";
import { start_game } from "$/server/game/load_mods.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
// mods straight from their source folders, skipping the build: scripts are imported as typescript
export function mod_sources(mods_dir: string): ServerModSource[] {
@@ -20,6 +21,7 @@ export function mod_sources(mods_dir: string): ServerModSource[] {
version: String(mod.manifest?.version),
hash: "source",
data: `mods/${mod.id}/source/data.json`,
sha256: { data: "" },
},
data: {
blocks: mod.blocks.map((b) => b.json),
@@ -37,14 +39,18 @@ export function mod_sources(mods_dir: string): ServerModSource[] {
export async function test_game(mods_dir: string, save?: string, seed = "test-seed") {
EverythingRegistry.clear();
const outbox = new Map<number, unknown[]>();
const closed = new Set<number>();
const host = {
send: (conn: number, data: string) => {
if (!outbox.has(conn)) outbox.set(conn, []);
outbox.get(conn)!.push(JSON.parse(data));
},
close() {},
close(conn: number) {
closed.add(conn);
},
};
const game: GameServer = await start_game(host, save, seed, mod_sources(mods_dir));
const atlas = { png: "atlas.png", json: "atlas.json", sha256: { png: "", json: "" } };
const game: GameServer = await start_game(host, save, seed, atlas, mod_sources(mods_dir));
// deno-lint-ignore no-explicit-any
const take = (conn: number): any[] => {
const messages = outbox.get(conn) ?? [];
@@ -52,7 +58,14 @@ export async function test_game(mods_dir: string, save?: string, seed = "test-se
return messages;
};
const send = (conn: number, message: unknown) => game.on_message(conn, JSON.stringify(message));
return { game, take, send };
// the whole handshake: hello, welcome, ready, join. returns join
const join = (conn: number, name: string) => {
game.on_connect(conn);
send(conn, { type: "hello", name, protocol: PROTOCOL_VERSION });
send(conn, { type: "ready" });
return take(conn).find((m) => m.type === "join");
};
return { game, take, send, join, closed };
}
export function copy_dir(from: string, to: string) {