BlueGit

timfa questward

Files

questward / server / src / index.js

index.js

index.js 1.8 KiB Download Raw
1import { join } from "node:path";
2import { WorldDatabase } from "./persistence/worldDb.js";
3import { StateDatabase } from "./persistence/stateDb.js";
4import { EditorService } from "./editor/service.js";
5import { CharacterService } from "./characters/service.js";
6import { StubAuth } from "./auth/stub.js";
7import { Simulation } from "./simulation/simulation.js";
8import { createHttpServer, repoRootFrom } from "./gateway/http.js";
9import { attachGateway } from "./gateway/websocket.js";
10
11const rootDir = repoRootFrom(import.meta.url);
12const dataDir = join(rootDir, "server", "data");
13const port = Number(process.env.PORT ?? 8080);
14
15const worldDb = new WorldDatabase(join(dataDir, "world.sqlite"));
16const stateDb = new StateDatabase(join(dataDir, "state.sqlite"));
17const editor = new EditorService(worldDb);
18const characters = new CharacterService(stateDb, console);
19const simulation = new Simulation(worldDb, stateDb);
20const auth = new StubAuth();
21const httpServer = createHttpServer(rootDir);
22const gateway = attachGateway({ httpServer, auth, stateDb, characters, editor, simulation });
23
24const tickMs = Math.round(1000 / simulation.settings.simulationTickHz);
25const tickTimer = setInterval(() =>
26{
27 characters.processDueDeletions();
28 const result = simulation.step();
29 gateway.onTickResult(result);
30}, tickMs);
31
32httpServer.listen(port, () =>
33{
34 console.log(`Tile MMO listening on http://localhost:${port}`);
35 console.log(`World editor: http://localhost:${port}/editor`);
36});
37
38function shutdown ()
39{
40 clearInterval(tickTimer);
41 for (const session of gateway.sessions.values())
42 {
43 if (session.characterId)
44 { simulation.despawnCharacter(session.characterId); }
45 }
46 worldDb.close();
47 stateDb.close();
48 httpServer.close();
49 process.exit(0);
50}
51
52process.on("SIGINT", shutdown);
53process.on("SIGTERM", shutdown);