mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-24 11:25:05 +00:00
allow up to 3 of same ip to join game
This commit is contained in:
+28
-16
@@ -53,7 +53,7 @@ export class GameServer {
|
||||
public readonly createdAt: number,
|
||||
public readonly isPublic: boolean,
|
||||
private config: ServerConfig,
|
||||
public gameConfig: GameConfig
|
||||
public gameConfig: GameConfig,
|
||||
) {}
|
||||
|
||||
public updateGameConfig(gameConfig: GameConfig): void {
|
||||
@@ -89,15 +89,27 @@ export class GameServer {
|
||||
persistentID: client.persistentID,
|
||||
gameID: this.id,
|
||||
});
|
||||
|
||||
if (
|
||||
this.activeClients.filter(
|
||||
(c) => c.ip == client.ip && c.clientID != client.clientID,
|
||||
).length >= 3
|
||||
) {
|
||||
console.log(
|
||||
`cannot add client ${client.clientID}, already have 3 ips (${client.ip})`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove stale client if this is a reconnect
|
||||
const existing = this.activeClients.find(
|
||||
(c) => c.clientID == client.clientID || client.ip == c.ip
|
||||
(c) => c.clientID == client.clientID,
|
||||
);
|
||||
if (existing != null) {
|
||||
existing.ws.removeAllListeners("message");
|
||||
}
|
||||
this.activeClients = this.activeClients.filter(
|
||||
(c) => c.clientID != client.clientID
|
||||
(c) => c.clientID != client.clientID,
|
||||
);
|
||||
this.activeClients.push(client);
|
||||
client.lastPing = Date.now();
|
||||
@@ -113,7 +125,7 @@ export class GameServer {
|
||||
}
|
||||
try {
|
||||
const clientMsg: ClientMessage = ClientMessageSchema.parse(
|
||||
JSON.parse(message)
|
||||
JSON.parse(message),
|
||||
);
|
||||
if (this.allClients.has(clientMsg.clientID)) {
|
||||
const client = this.allClients.get(clientMsg.clientID);
|
||||
@@ -133,7 +145,7 @@ export class GameServer {
|
||||
this.addIntent(clientMsg.intent);
|
||||
} else {
|
||||
console.warn(
|
||||
`${this.id}: client ${clientMsg.clientID} sent to wrong game`
|
||||
`${this.id}: client ${clientMsg.clientID} sent to wrong game`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -146,14 +158,14 @@ export class GameServer {
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(
|
||||
`error handline websocket request in game server: ${error}`
|
||||
`error handline websocket request in game server: ${error}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
client.ws.on("close", () => {
|
||||
console.log(`${this.id}: client ${client.clientID} disconnected`);
|
||||
this.activeClients = this.activeClients.filter(
|
||||
(c) => c.clientID != client.clientID
|
||||
(c) => c.clientID != client.clientID,
|
||||
);
|
||||
});
|
||||
client.ws.on("error", (error: Error) => {
|
||||
@@ -190,7 +202,7 @@ export class GameServer {
|
||||
|
||||
this.endTurnIntervalID = setInterval(
|
||||
() => this.endTurn(),
|
||||
this.config.turnIntervalMs()
|
||||
this.config.turnIntervalMs(),
|
||||
);
|
||||
this.activeClients.forEach((c) => {
|
||||
console.log(`${this.id}: sending start message to ${c.clientID}`);
|
||||
@@ -209,8 +221,8 @@ export class GameServer {
|
||||
type: "start",
|
||||
turns: this.turns.slice(lastTurn),
|
||||
config: this.gameConfig,
|
||||
})
|
||||
)
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -227,7 +239,7 @@ export class GameServer {
|
||||
ServerTurnMessageSchema.parse({
|
||||
type: "turn",
|
||||
turn: pastTurn,
|
||||
})
|
||||
}),
|
||||
);
|
||||
this.activeClients.forEach((c) => {
|
||||
c.ws.send(msg);
|
||||
@@ -244,12 +256,12 @@ export class GameServer {
|
||||
}
|
||||
});
|
||||
console.log(
|
||||
`${this.id}: ending game ${this.id} with ${this.turns.length} turns`
|
||||
`${this.id}: ending game ${this.id} with ${this.turns.length} turns`,
|
||||
);
|
||||
try {
|
||||
if (this.allClients.size > 0) {
|
||||
const playerRecords: PlayerRecord[] = Array.from(
|
||||
this.allClients.values()
|
||||
this.allClients.values(),
|
||||
).map((client) => ({
|
||||
ip: client.ip,
|
||||
clientID: client.clientID,
|
||||
@@ -264,8 +276,8 @@ export class GameServer {
|
||||
this.turns,
|
||||
this._startTime,
|
||||
Date.now(),
|
||||
this.winner
|
||||
)
|
||||
this.winner,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
console.log(`${this.id}: no clients joined, not archiving game`);
|
||||
@@ -301,7 +313,7 @@ export class GameServer {
|
||||
for (const client of this.activeClients) {
|
||||
if (now - client.lastPing > 60_000) {
|
||||
console.log(
|
||||
`${this.id}: no pings from ${client.clientID}, terminating connection`
|
||||
`${this.id}: no pings from ${client.clientID}, terminating connection`,
|
||||
);
|
||||
if (client.ws.readyState === WebSocket.OPEN) {
|
||||
client.ws.close(1000, "no heartbeats received, closing connection");
|
||||
|
||||
Reference in New Issue
Block a user