Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const GamemodeToArenaClass: Record<DiepGamemodeID, (typeof ArenaEntity) | null>
* Used for determining which endpoints go to the default.
*/
export default class GameServer {
/** All game servers. */
public static games: GameServer[] = [];
/** Stores total player count. */
public static globalPlayerCount = 0;
/** Whether or not the game server is running. */
Expand Down Expand Up @@ -119,7 +121,7 @@ export default class GameServer {
const _add = this.clients.add;
this.clients.add = (client: Client) => {
GameServer.globalPlayerCount += 1;
this.broadcastPlayerCount();
GameServer.broadcastPlayerCount();

return _add.call(this.clients, client);
}
Expand All @@ -128,7 +130,7 @@ export default class GameServer {
let success = _delete.call(this.clients, client);
if (success) {
GameServer.globalPlayerCount -= 1;
this.broadcastPlayerCount();
GameServer.broadcastPlayerCount();
this.clientsAwaitingSpawn.delete(client);
}

Expand All @@ -137,7 +139,7 @@ export default class GameServer {
const _clear = this.clients.clear;
this.clients.clear = () => {
GameServer.globalPlayerCount -= this.clients.size;
this.broadcastPlayerCount();
GameServer.broadcastPlayerCount();
this.clientsAwaitingSpawn.clear();

return _clear.call(this.clients);
Expand All @@ -152,16 +154,22 @@ export default class GameServer {
this._tickInterval = setInterval(() => {
if (this.clients.size) this.tickLoop(); // Don't tick empty games
}, config.mspt);

GameServer.games.push(this);
}

/** Returns a WebSocketServer Writer Broadcast Stream. */
public broadcast() {
return new WSSWriterStream(this);
}

/** Broadcasts a player count packet. */
public broadcastPlayerCount() {
this.broadcast().vu(ClientBound.PlayerCount).vu(GameServer.globalPlayerCount).send();
public static broadcastPlayerCount() {
for (const game of GameServer.games) {
game.broadcast().vu(ClientBound.PlayerCount).vu(GameServer.globalPlayerCount).send();
}
}

/** Sends a notification to all clients connected to this game server. */
public broadcastMessage(text: string, color = 0x000000, time = 5000, id = "") {
this.broadcast().u8(ClientBound.Notification).stringNT(text).u32(color).float(time).stringNT(id).send();
Expand Down
12 changes: 4 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export const bannedClients = new Set<string>();
const connections = new Map<string, number>();
const allClients = new Set<Client>();
const app = App({});
const games: GameServer[] = [];

app.ws("/*", {
compression: SHARED_COMPRESSOR,
Expand All @@ -63,7 +62,7 @@ app.ws("/*", {
return ws.close();
}
connections.set(ipAddress, conns + 1);
const game = games.find(({ gamemode }) => gamemode === ws.getUserData().gamemode);
const game = GameServer.games.find(({ gamemode }) => gamemode === ws.getUserData().gamemode);
if (!game) {
return ws.close();
}
Expand Down Expand Up @@ -99,7 +98,7 @@ app.get("/*", (res, req) => {
res.writeStatus("200 OK").end(JSON.stringify(TankDefinitions));
return;
case "/servers":
res.writeStatus("200 OK").end(JSON.stringify(games.map(({ gamemode, name }) => ({ gamemode, name }))));
res.writeStatus("200 OK").end(JSON.stringify(GameServer.games.map(({ gamemode, name }) => ({ gamemode, name }))));
return;
case "/commands":
res.writeStatus("200 OK").end(JSON.stringify(config.enableCommands ? Object.values(commandDefinitions) : []));
Expand Down Expand Up @@ -160,14 +159,11 @@ app.listen(PORT, (success) => {
//
// NOTES(0): As of now, both servers run on the same process (and thread) here
const ffa = new GameServer(FFAArena, "FFA");
const sbx = new GameServer(SandboxArena, "Sandbox");

games.push(ffa, sbx);

const sandbox = new GameServer(SandboxArena, "Sandbox");

util.saveToLog("Servers up", "All servers booted up.", 0x37F554);
util.log("Dumping endpoint -> gamemode routing table");
for (const game of games) console.log("> " + `localhost:${config.serverPort}/${game.gamemode}`.padEnd(40, " ") + " -> " + game.name);
for (const game of GameServer.games) console.log("> " + `localhost:${config.serverPort}/${game.gamemode}`.padEnd(40, " ") + " -> " + game.name);
});

process.on("uncaughtException", (error) => {
Expand Down
Loading