jumpstorm/server/src/server.ts

202 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-08-25 18:48:17 -04:00
import { Game } from '@engine/Game';
import { EntityNames, Player } from '@engine/entities';
import { WallBounds, Physics, Collision, NetworkUpdate } from '@engine/systems';
import {
2023-08-23 21:44:59 -04:00
type MessageQueueProvider,
type MessagePublisher,
MessageType,
type MessageProcessor,
2023-08-25 18:48:17 -04:00
type Message
} from '@engine/network';
import { stringify, parse } from '@engine/utils';
import { Grid } from '@engine/structures';
import { Miscellaneous } from '@engine/config';
import { Server } from 'bun';
2023-07-21 01:22:26 -04:00
2023-08-23 21:44:59 -04:00
const SERVER_PORT = 8080;
2023-08-25 18:48:17 -04:00
const SERVER_TICK_RATE = (1 / 60) * 1000;
const GAME_TOPIC = 'game';
const MAX_PLAYERS = 8;
2023-08-23 21:44:59 -04:00
type SessionData = { sessionId: string };
interface ServerMessage extends Message {
sessionData: SessionData;
}
class ServerSocketMessageReceiver implements MessageQueueProvider {
2023-08-23 21:44:59 -04:00
private messages: ServerMessage[];
2023-07-21 01:22:26 -04:00
constructor() {
this.messages = [];
}
2023-08-23 21:44:59 -04:00
public addMessage(message: ServerMessage) {
this.messages.push(message);
}
2023-08-23 21:44:59 -04:00
public getNewMessages() {
return this.messages;
}
2023-08-23 21:44:59 -04:00
public clearMessages() {
this.messages = [];
}
}
2023-08-23 21:44:59 -04:00
class ServerMessageProcessor implements MessageProcessor {
constructor() {}
public process(_message: ServerMessage) {}
}
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;
}
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-23 21:44:59 -04:00
public publish() {
2023-08-25 18:48:17 -04:00
if (this.messages.length) {
this.server?.publish(GAME_TOPIC, stringify(this.messages));
2023-08-23 21:44:59 -04:00
2023-08-25 18:48:17 -04:00
this.messages = [];
}
2023-08-23 21:44:59 -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-25 18:48:17 -04:00
const sessions = new Set<string>();
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();
2023-08-25 18:48:17 -04:00
headers.set('Access-Control-Allow-Origin', '*');
if (url.pathname == '/assign') {
if (sessions.size > MAX_PLAYERS)
return new Response('too many players', { headers, status: 400 });
2023-08-23 21:44:59 -04:00
const sessionId = crypto.randomUUID();
2023-08-25 18:48:17 -04:00
headers.set('Set-Cookie', `SessionId=${sessionId};`);
sessions.add(sessionId);
2023-08-23 21:44:59 -04:00
return new Response(sessionId, { headers });
}
2023-08-25 18:48:17 -04:00
const cookie = req.headers.get('cookie');
2023-08-23 21:44:59 -04:00
if (!cookie) {
2023-08-25 18:48:17 -04:00
return new Response('No session', { headers, status: 401 });
2023-08-23 21:44:59 -04:00
}
2023-08-25 18:48:17 -04:00
const sessionId = cookie.split(';').at(0)!.split('SessionId=').at(1);
2023-08-23 21:44:59 -04:00
2023-08-25 18:48:17 -04:00
if (url.pathname == '/game') {
2023-08-23 21:44:59 -04:00
headers.set(
2023-08-25 18:48:17 -04:00
'Set-Cookie',
`SessionId=${sessionId}; HttpOnly; SameSite=Strict;`
2023-08-23 21:44:59 -04:00
);
server.upgrade(req, {
headers,
data: {
2023-08-25 18:48:17 -04:00
sessionId
}
2023-08-23 21:44:59 -04:00
});
2023-08-25 18:48:17 -04:00
return new Response('upgraded', { headers });
2023-08-23 21:44:59 -04:00
}
2023-08-25 18:48:17 -04:00
if (url.pathname == '/me') {
2023-08-23 21:44:59 -04:00
return new Response(sessionId, { headers });
}
2023-08-25 18:48:17 -04:00
return new Response('Not found', { headers, status: 404 });
},
websocket: {
open(ws) {
const { sessionId } = ws.data;
if (sessionControllableEntities.has(sessionId)) {
return;
}
2023-08-23 21:44:59 -04:00
const player = new Player(sessionId);
game.addEntity(player);
2023-08-25 18:48:17 -04:00
sessionControllableEntities.set(sessionId, new Set([player.id]));
2023-08-23 21:44:59 -04:00
messagePublisher.addMessage({
2023-08-25 18:48:17 -04:00
type: MessageType.NEW_ENTITIES,
body: [
{
entityName: EntityNames.Player,
args: { playerId: sessionId, id: player.id }
}
]
2023-08-23 21:44:59 -04:00
});
ws.subscribe(GAME_TOPIC);
},
message(ws, message) {
2023-08-25 18:48:17 -04:00
if (typeof message == 'string') {
2023-08-23 21:44:59 -04:00
const receivedMessage = parse<ServerMessage>(message);
receivedMessage.sessionData = ws.data;
messageReceiver.addMessage(receivedMessage);
}
},
2023-08-25 18:48:17 -04:00
close(ws) {
const { sessionId } = ws.data;
sessions.delete(sessionId);
const sessionEntities = sessionControllableEntities.get(sessionId);
if (!sessionEntities) return;
messagePublisher.addMessage({
type: MessageType.REMOVE_ENTITIES,
body: Array.from(sessionEntities)
});
}
}
});
2023-08-23 21:44:59 -04:00
messagePublisher.setServer(server);
[
new Physics(),
new Collision(new Grid()),
2023-08-25 18:48:17 -04:00
new WallBounds(),
new NetworkUpdate(messageReceiver, messagePublisher, messageProcessor)
].forEach((system) => game.addSystem(system));
game.start();
setInterval(() => {
game.doGameLoop(performance.now());
2023-08-23 21:44:59 -04:00
}, SERVER_TICK_RATE);
console.log(`Listening on ${server.hostname}:${server.port}`);