2023-08-21 16:50:09 -04:00
|
|
|
import { Game } from "@engine/Game";
|
|
|
|
import { Floor, Player } from "@engine/entities";
|
2023-08-15 20:30:19 -04:00
|
|
|
import {
|
|
|
|
WallBounds,
|
|
|
|
Physics,
|
|
|
|
Collision,
|
2023-08-21 19:22:23 -04:00
|
|
|
NetworkUpdate,
|
2023-08-15 20:30:19 -04:00
|
|
|
MessageQueueProvider,
|
|
|
|
MessagePublisher,
|
2023-08-21 16:50:09 -04:00
|
|
|
} from "@engine/systems";
|
|
|
|
import { Grid } from "@engine/structures";
|
|
|
|
import { Miscellaneous } from "@engine/config";
|
2023-08-21 19:22:23 -04:00
|
|
|
import { Server } from "bun";
|
2023-07-21 01:22:26 -04:00
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
class ServerSocketMessageReceiver implements MessageQueueProvider {
|
|
|
|
private messages: any[];
|
2023-07-21 01:22:26 -04:00
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
constructor() {
|
|
|
|
this.messages = [];
|
|
|
|
}
|
2023-08-16 17:41:35 -04:00
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
addMessage(message: any) {
|
|
|
|
this.messages.push(message);
|
|
|
|
}
|
2023-08-16 17:41:35 -04:00
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
getNewMessages() {
|
|
|
|
return this.messages;
|
|
|
|
}
|
2023-08-16 17:41:35 -04:00
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
clearMessages() {
|
|
|
|
this.messages = [];
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 17:41:35 -04:00
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
class ServerSocketMessagePublisher implements MessagePublisher {
|
|
|
|
private server: Server;
|
|
|
|
private messages: any[];
|
|
|
|
|
|
|
|
constructor(server: Server) {
|
|
|
|
this.server = server;
|
|
|
|
this.messages = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
addMessage(_message: any) {}
|
2023-08-16 17:41:35 -04:00
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
publish() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const game = new Game();
|
|
|
|
|
|
|
|
const messageReceiver = new ServerSocketMessageReceiver();
|
|
|
|
|
|
|
|
const server = Bun.serve<{ sessionId: string }>({
|
2023-08-16 17:41:35 -04:00
|
|
|
port: 8080,
|
2023-08-21 16:50:09 -04:00
|
|
|
fetch: async (req, server): Promise<string> => {
|
2023-08-21 19:22:23 -04:00
|
|
|
const sessionId = crypto.randomUUID();
|
2023-08-16 17:41:35 -04:00
|
|
|
|
|
|
|
server.upgrade(req, {
|
|
|
|
headers: {
|
|
|
|
"Set-Cookie": `SessionId=${sessionId}`,
|
2023-08-15 20:30:19 -04:00
|
|
|
},
|
2023-08-21 19:22:23 -04:00
|
|
|
data: {
|
|
|
|
sessionId,
|
|
|
|
},
|
2023-07-21 01:22:26 -04:00
|
|
|
});
|
2023-08-21 16:50:09 -04:00
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
return sessionId;
|
2023-08-16 17:41:35 -04:00
|
|
|
},
|
|
|
|
websocket: {
|
2023-08-21 19:22:23 -04:00
|
|
|
open(ws) {
|
|
|
|
const { sessionId } = ws.data;
|
|
|
|
|
|
|
|
if (sessionControllableEntities.has(sessionId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const player = new Player();
|
|
|
|
game.addEntity(player);
|
|
|
|
|
|
|
|
sessionControllableEntities.set(sessionId, new Set(player.id));
|
|
|
|
},
|
2023-08-16 17:41:35 -04:00
|
|
|
message(ws, message) {
|
2023-08-21 19:22:23 -04:00
|
|
|
console.log(JSON.parse(message));
|
|
|
|
messageReceiver.addMessage(message);
|
2023-08-16 17:41:35 -04:00
|
|
|
},
|
|
|
|
close(ws) {},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
const messagePublisher = new ServerSocketMessagePublisher(server);
|
|
|
|
|
|
|
|
[
|
|
|
|
new Physics(),
|
|
|
|
new Collision(new Grid()),
|
|
|
|
new WallBounds(Miscellaneous.WIDTH),
|
|
|
|
new NetworkUpdate(messageReceiver, messagePublisher),
|
|
|
|
].forEach((system) => game.addSystem(system));
|
|
|
|
|
|
|
|
[new Floor(160), new Player()].forEach((entity) => game.addEntity(entity));
|
|
|
|
|
|
|
|
game.start();
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
game.doGameLoop(performance.now());
|
|
|
|
}, Miscellaneous.SERVER_TICK_RATE);
|
|
|
|
|
|
|
|
const sessionControllableEntities: Map<string, Set<string>> = new Map();
|
|
|
|
|
2023-08-16 17:41:35 -04:00
|
|
|
console.log(`Listening on ${server.hostname}:${server.port}`);
|