import { join } from "node:path"; import { WorldDatabase } from "./persistence/worldDb.js"; import { StateDatabase } from "./persistence/stateDb.js"; import { EditorService } from "./editor/service.js"; import { CharacterService } from "./characters/service.js"; import { StubAuth } from "./auth/stub.js"; import { Simulation } from "./simulation/simulation.js"; import { createHttpServer, repoRootFrom } from "./gateway/http.js"; import { attachGateway } from "./gateway/websocket.js"; const rootDir = repoRootFrom(import.meta.url); const dataDir = join(rootDir, "server", "data"); const port = Number(process.env.PORT ?? 8080); const worldDb = new WorldDatabase(join(dataDir, "world.sqlite")); const stateDb = new StateDatabase(join(dataDir, "state.sqlite")); const editor = new EditorService(worldDb); const characters = new CharacterService(stateDb, console); const simulation = new Simulation(worldDb, stateDb); const auth = new StubAuth(); const httpServer = createHttpServer(rootDir); const gateway = attachGateway({ httpServer, auth, stateDb, characters, editor, simulation }); const tickMs = Math.round(1000 / simulation.settings.simulationTickHz); const tickTimer = setInterval(() => { characters.processDueDeletions(); const result = simulation.step(); gateway.onTickResult(result); }, tickMs); httpServer.listen(port, () => { console.log(`Tile MMO listening on http://localhost:${port}`); console.log(`World editor: http://localhost:${port}/editor`); }); function shutdown () { clearInterval(tickTimer); for (const session of gateway.sessions.values()) { if (session.characterId) { simulation.despawnCharacter(session.characterId); } } worldDb.close(); stateDb.close(); httpServer.close(); process.exit(0); } process.on("SIGINT", shutdown); process.on("SIGTERM", shutdown);