diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-08-15 18:30:19 -0600 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-08-15 18:30:19 -0600 |
commit | 732fe6f4811cc082bf938fed2d28c1f9c8bbd1f6 (patch) | |
tree | bf9d2aa160fb3e5564f4ae788ecc86927b716e0b /server | |
parent | 2dc3120831fbcd03b635bbad59213ff0bf1f8879 (diff) | |
download | jumpstorm-732fe6f4811cc082bf938fed2d28c1f9c8bbd1f6.tar.gz jumpstorm-732fe6f4811cc082bf938fed2d28c1f9c8bbd1f6.zip |
generate uuids for entities; scaffolding for a server
Diffstat (limited to 'server')
-rw-r--r-- | server/src/server.ts | 75 | ||||
-rw-r--r-- | server/tsconfig.json | 29 |
2 files changed, 67 insertions, 37 deletions
diff --git a/server/src/server.ts b/server/src/server.ts index 74d901b..9a73f11 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -1,37 +1,60 @@ import { Game } from "../../engine/Game"; import { Floor, Player } from "../../engine/entities"; -import { WallBounds, Physics, Collision } from "../../engine/systems"; +import { + WallBounds, + Physics, + Collision, + MessageQueueProvider, + MessagePublisher, +} from "../../engine/systems"; import { Miscellaneous } from "../../engine/config"; const TICK_RATE = 60 / 1000; -const game = new Game(); +class Server { + private server: any; + private game: Game; -[ - new Physics(), - new Collision({ width: Miscellaneous.WIDTH, height: Miscellaneous.HEIGHT }), - new WallBounds(Miscellaneous.WIDTH), -].forEach((system) => game.addSystem(system)); + constructor() { + this.game = new Game(); -[new Floor(160), new Player()].forEach((entity) => game.addEntity(entity)); + [ + new Physics(), + new Collision({ + width: Miscellaneous.WIDTH, + height: Miscellaneous.HEIGHT, + }), + new WallBounds(Miscellaneous.WIDTH), + ].forEach((system) => this.game.addSystem(system)); -game.start(); -setInterval(() => { - game.doGameLoop(performance.now()); -}, TICK_RATE); + [new Floor(160), new Player()].forEach((entity) => + this.game.addEntity(entity), + ); -const server = Bun.serve<>({ - port: 8080, - fetch(req, server) { - server.upgrade(req, { - data: {}, + this.game.start(); + setInterval(() => { + this.game.doGameLoop(performance.now()); + }, TICK_RATE); + + this.server = Bun.serve<any>({ + websocket: { + open(ws) { + ws.subscribe("the-group-chat"); + ws.publish("the-group-chat", msg); + }, + message(ws, message) { + // this is a group chat + // so the server re-broadcasts incoming message to everyone + ws.publish("the-group-chat", `${ws.data.username}: ${message}`); + }, + close(ws) { + const msg = `${ws.data.username} has left the chat`; + ws.unsubscribe("the-group-chat"); + ws.publish("the-group-chat", msg); + }, + }, }); - }, - websocket: { - // handler called when a message is received - async message(ws, message) { - console.log(`Received ${message}`); - }, - }, -}); -console.log(`Listening on localhost:${server.port}`); + } +} + +new Server(); diff --git a/server/tsconfig.json b/server/tsconfig.json index 29f8aa0..2567512 100644 --- a/server/tsconfig.json +++ b/server/tsconfig.json @@ -1,21 +1,28 @@ { "compilerOptions": { - "lib": ["ESNext"], + // add Bun type definitions + "types": ["bun-types"], + + // enable latest features + "lib": ["esnext"], "module": "esnext", "target": "esnext", + + // if TS 5.x+ "moduleResolution": "bundler", - "moduleDetection": "force", + "noEmit": true, "allowImportingTsExtensions": true, + "moduleDetection": "force", + // if TS 4.x or earlier + "moduleResolution": "nodenext", + + "jsx": "react-jsx", // support JSX + "allowJs": true, // allow importing `.js` from `.ts` + "esModuleInterop": true, // allow default imports for CommonJS modules + + // best practices "strict": true, - "downlevelIteration": true, - "skipLibCheck": true, - "jsx": "preserve", - "allowSyntheticDefaultImports": true, "forceConsistentCasingInFileNames": true, - "allowJs": true, - "noEmit": true, - "types": [ - "bun-types" // add Bun global - ] + "skipLibCheck": true } } |