2023-08-21 16:50:09 -04:00
|
|
|
import { Game } from "@engine/Game";
|
2023-08-23 21:44:59 -04:00
|
|
|
import { EntityNames, Player } from "@engine/entities";
|
|
|
|
import { WallBounds, Physics, Collision, NetworkUpdate } from "@engine/systems";
|
2023-08-15 20:30:19 -04:00
|
|
|
import {
|
2023-08-23 21:44:59 -04:00
|
|
|
type MessageQueueProvider,
|
|
|
|
type MessagePublisher,
|
|
|
|
MessageType,
|
|
|
|
type MessageProcessor,
|
|
|
|
type Message,
|
|
|
|
} from "@engine/network";
|
|
|
|
import { stringify, parse } from "@engine/utils";
|
2023-08-21 16:50:09 -04:00
|
|
|
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-23 21:44:59 -04:00
|
|
|
const SERVER_PORT = 8080;
|
|
|
|
const SERVER_TICK_RATE = (1 / 100) * 1000;
|
|
|
|
const GAME_TOPIC = "game";
|
|
|
|
|
|
|
|
type SessionData = { sessionId: string };
|
|
|
|
|
|
|
|
interface ServerMessage extends Message {
|
|
|
|
sessionData: SessionData;
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
class ServerSocketMessageReceiver implements MessageQueueProvider {
|
2023-08-23 21:44:59 -04:00
|
|
|
private messages: ServerMessage[];
|
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-23 21:44:59 -04:00
|
|
|
public addMessage(message: ServerMessage) {
|
2023-08-21 19:22:23 -04:00
|
|
|
this.messages.push(message);
|
|
|
|
}
|
2023-08-16 17:41:35 -04:00
|
|
|
|
2023-08-23 21:44:59 -04:00
|
|
|
public getNewMessages() {
|
2023-08-21 19:22:23 -04:00
|
|
|
return this.messages;
|
|
|
|
}
|
2023-08-16 17:41:35 -04:00
|
|
|
|
2023-08-23 21:44:59 -04:00
|
|
|
public clearMessages() {
|
2023-08-21 19:22:23 -04:00
|
|
|
this.messages = [];
|
|
|
|
}
|
|
|
|
}
|
2023-08-16 17:41:35 -04:00
|
|
|
|
2023-08-23 21:44:59 -04:00
|
|
|
class ServerMessageProcessor implements MessageProcessor {
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
public process(_message: ServerMessage) {}
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:22:23 -04:00
|
|
|
class ServerSocketMessagePublisher implements MessagePublisher {
|
2023-08-23 21:44:59 -04:00
|
|
|
private server?: Server;
|
|
|
|
private messages: Message[];
|
|
|
|
|
|
|
|
constructor(server?: Server) {
|
|
|
|
if (server) {
|
|
|
|
this.server = server;
|
|
|
|
}
|
2023-08-21 19:22:23 -04:00
|
|
|
|
|
|
|
this.messages = [];
|
|
|
|
}
|
|
|
|
|
2023-08-23 21:44:59 -04:00
|
|
|
public setServer(server: Server) {
|
|
|
|
this.server = server;
|
|
|
|
}
|
|
|
|
|
|
|
|
public addMessage(message: Message) {
|
|
|
|
this.messages.push(message);
|
|
|
|
}
|
2023-08-16 17:41:35 -04:00
|
|
|
|
2023-08-23 21:44:59 -04:00
|
|
|
public publish() {
|
|
|
|
this.messages.forEach(
|
|
|
|
(message) => this.server?.publish(GAME_TOPIC, stringify(message)),
|
|
|
|
);
|
|
|
|
|
|
|
|
this.messages = [];
|
|
|
|
}
|
2023-08-21 19:22:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const game = new Game();
|
|
|
|
|
|
|
|
const messageReceiver = new ServerSocketMessageReceiver();
|
2023-08-23 21:44:59 -04:00
|
|
|
const messagePublisher = new ServerSocketMessagePublisher();
|
|
|
|
const messageProcessor = new ServerMessageProcessor();
|
|
|
|
const sessionControllableEntities: Map<string, Set<string>> = new Map();
|
2023-08-21 19:22:23 -04:00
|
|
|
|
2023-08-23 21:44:59 -04:00
|
|
|
const server = Bun.serve<SessionData>({
|
|
|
|
port: SERVER_PORT,
|
|
|
|
fetch: async (req, server): Promise<Response> => {
|
|
|
|
const url = new URL(req.url);
|
|
|
|
|
|
|
|
const headers = new Headers();
|
|
|
|
headers.set("Access-Control-Allow-Origin", "*");
|
|
|
|
|
|
|
|
if (url.pathname == "/assign") {
|
|
|
|
const sessionId = crypto.randomUUID();
|
|
|
|
headers.set("Set-Cookie", `SessionId=${sessionId};`);
|
|
|
|
|
|
|
|
return new Response(sessionId, { headers });
|
|
|
|
}
|
|
|
|
|
|
|
|
const cookie = req.headers.get("cookie");
|
|
|
|
if (!cookie) {
|
|
|
|
return new Response("No session", { headers, status: 401 });
|
|
|
|
}
|
|
|
|
|
|
|
|
const sessionId = cookie.split(";").at(0)!.split("SessionId=").at(1);
|
|
|
|
|
|
|
|
if (url.pathname == "/game") {
|
|
|
|
headers.set(
|
|
|
|
"Set-Cookie",
|
|
|
|
`SessionId=${sessionId}; HttpOnly; SameSite=Strict;`,
|
|
|
|
);
|
|
|
|
server.upgrade(req, {
|
|
|
|
headers,
|
|
|
|
data: {
|
|
|
|
sessionId,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return new Response("upgraded", { headers });
|
|
|
|
}
|
|
|
|
if (url.pathname == "/me") {
|
|
|
|
return new Response(sessionId, { headers });
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Response("Not found", { headers, status: 404 });
|
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)) {
|
2023-08-23 21:44:59 -04:00
|
|
|
// no need to add player
|
2023-08-21 19:22:23 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-23 21:44:59 -04:00
|
|
|
const player = new Player(sessionId);
|
2023-08-21 19:22:23 -04:00
|
|
|
game.addEntity(player);
|
|
|
|
sessionControllableEntities.set(sessionId, new Set(player.id));
|
2023-08-23 21:44:59 -04:00
|
|
|
|
|
|
|
messagePublisher.addMessage({
|
|
|
|
type: MessageType.NEW_ENTITY,
|
|
|
|
body: {
|
|
|
|
entityName: EntityNames.Player,
|
|
|
|
args: { playerId: sessionId },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ws.subscribe(GAME_TOPIC);
|
2023-08-21 19:22:23 -04:00
|
|
|
},
|
2023-08-16 17:41:35 -04:00
|
|
|
message(ws, message) {
|
2023-08-23 21:44:59 -04:00
|
|
|
if (typeof message == "string") {
|
|
|
|
const receivedMessage = parse<ServerMessage>(message);
|
|
|
|
receivedMessage.sessionData = ws.data;
|
|
|
|
|
|
|
|
messageReceiver.addMessage(receivedMessage);
|
|
|
|
}
|
2023-08-16 17:41:35 -04:00
|
|
|
},
|
2023-08-23 21:44:59 -04:00
|
|
|
close(_ws) {},
|
2023-08-16 17:41:35 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-08-23 21:44:59 -04:00
|
|
|
messagePublisher.setServer(server);
|
2023-08-21 19:22:23 -04:00
|
|
|
|
|
|
|
[
|
|
|
|
new Physics(),
|
|
|
|
new Collision(new Grid()),
|
|
|
|
new WallBounds(Miscellaneous.WIDTH),
|
2023-08-23 21:44:59 -04:00
|
|
|
new NetworkUpdate(messageReceiver, messagePublisher, messageProcessor),
|
2023-08-21 19:22:23 -04:00
|
|
|
].forEach((system) => game.addSystem(system));
|
|
|
|
|
|
|
|
game.start();
|
|
|
|
setInterval(() => {
|
|
|
|
game.doGameLoop(performance.now());
|
2023-08-23 21:44:59 -04:00
|
|
|
}, SERVER_TICK_RATE);
|
2023-08-21 19:22:23 -04:00
|
|
|
|
2023-08-16 17:41:35 -04:00
|
|
|
console.log(`Listening on ${server.hostname}:${server.port}`);
|