BlueGit

timfa questward

Files

questward / server / src / index.js

index.js

index.js 2.0 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 { FriendService } from "./friends/service.js";
7import { StubAuth } from "./auth/stub.js";
8import { Simulation } from "./simulation/simulation.js";
9import { createHttpServer, repoRootFrom } from "./gateway/http.js";
10import { attachGateway } from "./gateway/websocket.js";
11
12const rootDir = repoRootFrom(import.meta.url);
13const dataDir = join(rootDir, "server", "data");
14const port = Number(process.env.PORT ?? 8080);
15
16const worldDb = new WorldDatabase(join(dataDir, "world.sqlite"));
17const stateDb = new StateDatabase(join(dataDir, "state.sqlite"));
18const editor = new EditorService(worldDb);
19const characters = new CharacterService(stateDb, console);
20const friends = new FriendService(stateDb);
21const simulation = new Simulation(worldDb, stateDb);
22const auth = new StubAuth();
23const httpServer = createHttpServer(rootDir);
24const gateway = attachGateway({ httpServer, auth, stateDb, characters, friends, editor, simulation });
25
26let tickTimer = null;
27function scheduleTick ()
28{
29 const tickMs = Math.max(16, Math.round(1000 / (simulation.settings.simulationTickHz || 20)));
30 tickTimer = setTimeout(() =>
31 {
32 characters.processDueDeletions();
33 const result = simulation.step();
34 gateway.onTickResult(result);
35 scheduleTick();
36 }, tickMs);
37}
38scheduleTick();
39
40httpServer.listen(port, () =>
41{
42 console.log(`Tile MMO listening on http://localhost:${port}`);
43 console.log(`World editor: http://localhost:${port}/editor`);
44});
45
46function shutdown ()
47{
48 clearTimeout(tickTimer);
49 for (const session of gateway.sessions.values())
50 {
51 if (session.characterId)
52 { simulation.despawnCharacter(session.characterId); }
53 }
54 worldDb.close();
55 stateDb.close();
56 httpServer.close();
57 process.exit(0);
58}
59
60process.on("SIGINT", shutdown);
61process.on("SIGTERM", shutdown);