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(Date.now(), (characterId) => simulation.despawnCharacter(characterId));
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 simulation.shutdown().finally(() =>
50 {
51 worldDb.close();
52 stateDb.close();
53 httpServer.close();
54 process.exit(0);
55 });
56}
57
58process.on("SIGINT", shutdown);
59process.on("SIGTERM", shutdown);