Added code to mark a player AFK

This commit is contained in:
aqw42
2025-05-29 17:02:40 +02:00
parent 5446620788
commit 84fa28ba30
8 changed files with 42 additions and 2 deletions
+10 -1
View File
@@ -33,7 +33,8 @@ export type Intent =
| BuildUnitIntent
| EmbargoIntent
| QuickChatIntent
| MoveWarshipIntent;
| MoveWarshipIntent
| MarkIdleIntent;
export type AttackIntent = z.infer<typeof AttackIntentSchema>;
export type CancelAttackIntent = z.infer<typeof CancelAttackIntentSchema>;
@@ -56,6 +57,7 @@ export type TargetTroopRatioIntent = z.infer<
export type BuildUnitIntent = z.infer<typeof BuildUnitIntentSchema>;
export type MoveWarshipIntent = z.infer<typeof MoveWarshipIntentSchema>;
export type QuickChatIntent = z.infer<typeof QuickChatIntentSchema>;
export type MarkIdleIntent = z.infer<typeof MarkIdleIntentSchema>;
export type Turn = z.infer<typeof TurnSchema>;
export type GameConfig = z.infer<typeof GameConfigSchema>;
@@ -166,6 +168,7 @@ const BaseIntentSchema = z.object({
"attack",
"cancel_attack",
"spawn",
"mark_idle",
"boat",
"cancel_boat",
"name",
@@ -290,10 +293,16 @@ export const QuickChatIntentSchema = BaseIntentSchema.extend({
variables: z.record(SafeString).optional(),
});
export const MarkIdleIntentSchema = BaseIntentSchema.extend({
type: z.literal("mark_idle"),
isIdle: z.boolean(),
});
const IntentSchema = z.union([
AttackIntentSchema,
CancelAttackIntentSchema,
SpawnIntentSchema,
MarkIdleIntentSchema,
BoatAttackIntentSchema,
CancelBoatIntentSchema,
AllianceRequestIntentSchema,
+3
View File
@@ -15,6 +15,7 @@ import { DonateTroopsExecution } from "./DonateTroopExecution";
import { EmbargoExecution } from "./EmbargoExecution";
import { EmojiExecution } from "./EmojiExecution";
import { FakeHumanExecution } from "./FakeHumanExecution";
import { MarkIdleExecution } from "./MarkIdleExecution";
import { MoveWarshipExecution } from "./MoveWarshipExecution";
import { NoOpExecution } from "./NoOpExecution";
import { QuickChatExecution } from "./QuickChatExecution";
@@ -120,6 +121,8 @@ export class Executor {
intent.quickChatKey,
intent.variables ?? {},
);
case "mark_idle":
return new MarkIdleExecution(playerID, intent.isIdle);
default:
throw new Error(`intent type ${intent} not found`);
}
+3
View File
@@ -433,6 +433,9 @@ export interface Player {
largestClusterBoundingBox: { min: Cell; max: Cell } | null;
lastTileChange(): Tick;
isIdle(): boolean;
markIdle(isIdle: boolean): void;
hasSpawned(): boolean;
setHasSpawned(hasSpawned: boolean): void;
+1
View File
@@ -102,6 +102,7 @@ export interface PlayerUpdate {
smallID: number;
playerType: PlayerType;
isAlive: boolean;
isIdle: boolean;
tilesOwned: number;
gold: number;
population: number;
+3
View File
@@ -292,6 +292,9 @@ export class PlayerView {
hasSpawned(): boolean {
return this.data.hasSpawned;
}
isIdle(): boolean {
return this.data.isIdle;
}
}
export class GameView implements GameMap {
+10
View File
@@ -99,6 +99,7 @@ export class PlayerImpl implements Player {
public _outgoingLandAttacks: Attack[] = [];
private _hasSpawned = false;
private _isIdle = false;
constructor(
private mg: GameImpl,
@@ -136,6 +137,7 @@ export class PlayerImpl implements Player {
smallID: this.smallID(),
playerType: this.type(),
isAlive: this.isAlive(),
isIdle: this.isIdle(),
tilesOwned: this.numTilesOwned(),
gold: Number(this._gold),
population: this.population(),
@@ -925,6 +927,14 @@ export class PlayerImpl implements Player {
return this._lastTileChange;
}
isIdle(): boolean {
return this._isIdle;
}
markIdle(isIdle: boolean): void {
this._isIdle = isIdle;
}
hash(): number {
return (
simpleHash(this.id()) * (this.population() + this.numTilesOwned()) +
+3 -1
View File
@@ -4,7 +4,9 @@ import { Tick } from "../core/game/Game";
import { ClientID } from "../core/Schemas";
export class Client {
public lastPing: number;
public lastPing: number = Date.now();
public lastAction: number = Date.now();
public isIdle: boolean = false;
public hashes: Map<Tick, number> = new Map();
+9
View File
@@ -533,6 +533,15 @@ export class GameServer {
}
}
private markClientIdle(client: Client, isIdle: boolean) {
client.isIdle = isIdle;
this.addIntent({
type: "mark_idle",
clientID: client.clientID,
isIdle: isIdle,
});
}
private archiveGame() {
this.log.info("archiving game", {
gameID: this.id,