mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-08-17 05:34:37 +00:00
Added idle logic in gameserver
This commit is contained in:
@@ -35,6 +35,9 @@ export class GameServer {
|
|||||||
|
|
||||||
private maxGameDuration = 3 * 60 * 60 * 1000; // 3 hours
|
private maxGameDuration = 3 * 60 * 60 * 1000; // 3 hours
|
||||||
|
|
||||||
|
private disconnectionTimeout = 1 * 60 * 1000; // 1 minute
|
||||||
|
private inactivityTimeout = 3 * 60 * 1000; // 3 minute
|
||||||
|
|
||||||
private turns: Turn[] = [];
|
private turns: Turn[] = [];
|
||||||
private intents: Intent[] = [];
|
private intents: Intent[] = [];
|
||||||
public activeClients: Client[] = [];
|
public activeClients: Client[] = [];
|
||||||
@@ -164,6 +167,11 @@ export class GameServer {
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.isIdle = existing.isIdle;
|
||||||
|
client.lastAction = existing.lastAction;
|
||||||
|
client.lastPing = existing.lastPing;
|
||||||
|
|
||||||
existing.ws.removeAllListeners("message");
|
existing.ws.removeAllListeners("message");
|
||||||
this.activeClients = this.activeClients.filter((c) => c !== existing);
|
this.activeClients = this.activeClients.filter((c) => c !== existing);
|
||||||
}
|
}
|
||||||
@@ -191,6 +199,7 @@ export class GameServer {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
client.lastAction = Date.now();
|
||||||
this.addIntent(clientMsg.intent);
|
this.addIntent(clientMsg.intent);
|
||||||
}
|
}
|
||||||
if (clientMsg.type === "ping") {
|
if (clientMsg.type === "ping") {
|
||||||
@@ -353,6 +362,7 @@ export class GameServer {
|
|||||||
this.intents = [];
|
this.intents = [];
|
||||||
|
|
||||||
this.handleSynchronization();
|
this.handleSynchronization();
|
||||||
|
this.checkIdleStatus();
|
||||||
|
|
||||||
let msg = "";
|
let msg = "";
|
||||||
try {
|
try {
|
||||||
@@ -533,6 +543,29 @@ export class GameServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private checkIdleStatus() {
|
||||||
|
if (this.turns.length % 5 !== 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const now = Date.now();
|
||||||
|
for (const [clientID, client] of this.allClients) {
|
||||||
|
if (
|
||||||
|
client.isIdle === false &&
|
||||||
|
(now - client.lastPing > this.disconnectionTimeout ||
|
||||||
|
now - client.lastAction > this.inactivityTimeout)
|
||||||
|
) {
|
||||||
|
this.markClientIdle(client, true);
|
||||||
|
} else if (
|
||||||
|
client.isIdle &&
|
||||||
|
now - client.lastPing < this.disconnectionTimeout &&
|
||||||
|
now - client.lastAction < this.inactivityTimeout
|
||||||
|
) {
|
||||||
|
this.markClientIdle(client, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private markClientIdle(client: Client, isIdle: boolean) {
|
private markClientIdle(client: Client, isIdle: boolean) {
|
||||||
client.isIdle = isIdle;
|
client.isIdle = isIdle;
|
||||||
this.addIntent({
|
this.addIntent({
|
||||||
|
|||||||
Reference in New Issue
Block a user