jumpstorm/server/src/server.ts

154 lines
4.0 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';
2023-08-25 20:10:09 -04:00
import { MessageType } from '@engine/network';
import { Constants } from './constants';
import {
2023-08-25 20:10:09 -04:00
ServerSocketMessageReceiver,
ServerSocketMessagePublisher,
SessionData,
ServerMessage,
Session
} from './network';
import { parse } from '@engine/utils';
import { Server, ServerWebSocket } from 'bun';
export class GameServer {
private sessions: Map<string, Session>;
2023-07-21 01:22:26 -04:00
2023-08-25 20:10:09 -04:00
private server?: Server;
private game: Game;
private messageReceiver: ServerSocketMessageReceiver;
private messagePublisher: ServerSocketMessagePublisher;
constructor(
game: Game,
messageReceiver: ServerSocketMessageReceiver,
messagePublisher: ServerSocketMessagePublisher
) {
this.sessions = new Map();
this.game = game;
this.messageReceiver = messageReceiver;
this.messagePublisher = messagePublisher;
}
2023-08-25 20:10:09 -04:00
public serve() {
if (!this.server)
this.server = Bun.serve<SessionData>({
port: Constants.SERVER_PORT,
fetch: (req, srv) => this.fetchHandler(req, srv),
websocket: {
open: (ws) => this.openWebsocket(ws),
message: (ws, msg) => this.websocketMessage(ws, msg),
close: (ws) => this.closeWebsocket(ws)
}
});
2023-08-25 20:10:09 -04:00
this.messagePublisher.setServer(this.server);
2023-08-25 20:10:09 -04:00
console.log(`Listening on ${this.server.hostname}:${this.server.port}`);
}
2023-08-23 21:44:59 -04:00
2023-08-25 20:10:09 -04:00
private websocketMessage(
websocket: ServerWebSocket<SessionData>,
message: string | Uint8Array
) {
if (typeof message == 'string') {
const receivedMessage = parse<ServerMessage>(message);
receivedMessage.sessionData = websocket.data;
2023-08-23 21:44:59 -04:00
2023-08-25 20:10:09 -04:00
this.messageReceiver.addMessage(receivedMessage);
2023-08-23 21:44:59 -04:00
}
}
2023-08-25 20:10:09 -04:00
private closeWebsocket(websocket: ServerWebSocket<SessionData>) {
const { sessionId } = websocket.data;
2023-08-23 21:44:59 -04:00
2023-08-25 20:10:09 -04:00
const sessionEntities = this.sessions.get(sessionId)!.controllableEntities;
2023-08-25 20:10:09 -04:00
this.sessions.delete(sessionId);
2023-08-23 21:44:59 -04:00
2023-08-25 20:10:09 -04:00
if (!sessionEntities) return;
this.messagePublisher.addMessage({
type: MessageType.REMOVE_ENTITIES,
body: Array.from(sessionEntities)
});
2023-08-23 21:44:59 -04:00
}
2023-08-25 20:10:09 -04:00
private openWebsocket(websocket: ServerWebSocket<SessionData>) {
websocket.subscribe(Constants.GAME_TOPIC);
2023-08-25 20:10:09 -04:00
const { sessionId } = websocket.data;
if (this.sessions.has(sessionId)) {
return;
}
2023-08-25 20:10:09 -04:00
this.sessions.set(sessionId, {
sessionId,
controllableEntities: new Set()
});
const player = new Player(sessionId);
this.game.addEntity(player);
this.messagePublisher.addMessage({
type: MessageType.NEW_ENTITIES,
body: [
{
entityName: EntityNames.Player,
args: { playerId: sessionId, id: player.id }
}
]
});
this.sessions.get(sessionId)!.controllableEntities.add(player.id);
}
2023-08-25 18:48:17 -04:00
2023-08-25 20:10:09 -04:00
private fetchHandler(
req: Request,
server: Server
): Promise<Response> | Response {
2023-08-23 21:44:59 -04:00
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') {
2023-08-25 20:10:09 -04:00
if (this.sessions.size > Constants.MAX_PLAYERS)
2023-08-25 18:48:17 -04:00
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};`);
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 20:10:09 -04:00
return new Response('upgraded to ws', { 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 });
}
2023-08-25 20:10:09 -04:00
}