src/server/GameServer.ts

This commit is contained in:
Scott Anderson
2025-05-14 01:57:48 -04:00
parent a930a8c231
commit 17f2559572
+27 -27
View File
@@ -71,36 +71,36 @@ export class GameServer {
}
public updateGameConfig(gameConfig: Partial<GameConfig>): void {
if (typeof gameConfig.gameMap !== "undefined") {
if (gameConfig.gameMap !== undefined) {
this.gameConfig.gameMap = gameConfig.gameMap;
}
if (typeof gameConfig.difficulty !== "undefined") {
if (gameConfig.difficulty !== undefined) {
this.gameConfig.difficulty = gameConfig.difficulty;
}
if (typeof gameConfig.disableNPCs !== "undefined") {
if (gameConfig.disableNPCs !== undefined) {
this.gameConfig.disableNPCs = gameConfig.disableNPCs;
}
if (gameConfig.bots != null) {
if (gameConfig.bots !== undefined) {
this.gameConfig.bots = gameConfig.bots;
}
if (typeof gameConfig.infiniteGold !== "undefined") {
if (gameConfig.infiniteGold !== undefined) {
this.gameConfig.infiniteGold = gameConfig.infiniteGold;
}
if (typeof gameConfig.infiniteTroops !== "undefined") {
if (gameConfig.infiniteTroops !== undefined) {
this.gameConfig.infiniteTroops = gameConfig.infiniteTroops;
}
if (typeof gameConfig.instantBuild !== "undefined") {
if (gameConfig.instantBuild !== undefined) {
this.gameConfig.instantBuild = gameConfig.instantBuild;
}
if (typeof gameConfig.gameMode !== "undefined") {
if (gameConfig.gameMode !== undefined) {
this.gameConfig.gameMode = gameConfig.gameMode;
}
if (gameConfig.disabledUnits != null) {
if (gameConfig.disabledUnits !== undefined) {
this.gameConfig.disabledUnits = gameConfig.disabledUnits;
}
if (gameConfig.playerTeams != null) {
if (gameConfig.playerTeams !== undefined) {
this.gameConfig.playerTeams = gameConfig.playerTeams;
}
}
@@ -120,9 +120,9 @@ export class GameServer {
});
if (
this.gameConfig.gameType == GameType.Public &&
this.gameConfig.gameType === GameType.Public &&
this.activeClients.filter(
(c) => c.ip == client.ip && c.clientID != client.clientID,
(c) => c.ip === client.ip && c.clientID !== client.clientID,
).length >= 3
) {
this.log.warn("cannot add client, already have 3 ips", {
@@ -134,9 +134,9 @@ export class GameServer {
// Remove stale client if this is a reconnect
const existing = this.activeClients.find(
(c) => c.clientID == client.clientID,
(c) => c.clientID === client.clientID,
);
if (existing != null) {
if (existing !== undefined) {
if (client.persistentID !== existing.persistentID) {
this.log.error("persistent ids do not match", {
clientID: client.clientID,
@@ -149,7 +149,7 @@ export class GameServer {
}
existing.ws.removeAllListeners("message");
this.activeClients = this.activeClients.filter(
(c) => c.clientID != client.clientID,
(c) => c.clientID !== client.clientID,
);
}
this.activeClients.push(client);
@@ -167,8 +167,8 @@ export class GameServer {
} catch (error) {
throw Error(`error parsing schema for ${ipAnonymize(client.ip)}`);
}
if (clientMsg.type == "intent") {
if (clientMsg.intent.clientID != client.clientID) {
if (clientMsg.type === "intent") {
if (clientMsg.intent.clientID !== client.clientID) {
this.log.warn(
`client id mismatch, client: ${client.clientID}, intent: ${clientMsg.intent.clientID}`,
);
@@ -176,14 +176,14 @@ export class GameServer {
}
this.addIntent(clientMsg.intent);
}
if (clientMsg.type == "ping") {
if (clientMsg.type === "ping") {
this.lastPingUpdate = Date.now();
client.lastPing = Date.now();
}
if (clientMsg.type == "hash") {
if (clientMsg.type === "hash") {
client.hashes.set(clientMsg.turnNumber, clientMsg.hash);
}
if (clientMsg.type == "winner") {
if (clientMsg.type === "winner") {
this.winner = clientMsg;
this.allPlayersStats = clientMsg.allPlayersStats;
}
@@ -203,7 +203,7 @@ export class GameServer {
persistentID: client.persistentID,
});
this.activeClients = this.activeClients.filter(
(c) => c.clientID != client.clientID,
(c) => c.clientID !== client.clientID,
);
});
client.ws.on("error", (error: Error) => {
@@ -444,9 +444,9 @@ export class GameServer {
}
const noRecentPings = now > this.lastPingUpdate + 20 * 1000;
const noActive = this.activeClients.length == 0;
const noActive = this.activeClients.length === 0;
if (this.gameConfig.gameType != GameType.Public) {
if (this.gameConfig.gameType !== GameType.Public) {
if (this._hasStarted) {
if (noActive && noRecentPings) {
this.log.info("private game complete", {
@@ -464,7 +464,7 @@ export class GameServer {
const msSinceCreation = now - this.createdAt;
const lessThanLifetime = msSinceCreation < this.config.gameCreationRate();
const notEnoughPlayers =
this.gameConfig.gameType == GameType.Public &&
this.gameConfig.gameType === GameType.Public &&
this.gameConfig.maxPlayers &&
this.activeClients.length < this.gameConfig.maxPlayers;
if (lessThanLifetime && notEnoughPlayers) {
@@ -498,7 +498,7 @@ export class GameServer {
}
public isPublic(): boolean {
return this.gameConfig.gameType == GameType.Public;
return this.gameConfig.gameType === GameType.Public;
}
public kickClient(clientID: ClientID): void {
@@ -530,7 +530,7 @@ export class GameServer {
if (this.activeClients.length <= 1) {
return;
}
if (this.turns.length % 10 != 0 || this.turns.length < 10) {
if (this.turns.length % 10 !== 0 || this.turns.length < 10) {
// Check hashes every 10 turns
return;
}
@@ -540,7 +540,7 @@ export class GameServer {
const { mostCommonHash, outOfSyncClients } =
this.findOutOfSyncClients(lastHashTurn);
if (outOfSyncClients.length == 0) {
if (outOfSyncClients.length === 0) {
this.turns[lastHashTurn].hash = mostCommonHash;
return;
}