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
4 changes: 3 additions & 1 deletion src/server/Client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import WebSocket from "ws";
import { TokenPayload } from "../core/ApiSchemas";
import { Tick } from "../core/game/Game";
import { ClientID } from "../core/Schemas";
import { ClientID, Winner } from "../core/Schemas";

export class Client {
public lastPing: number = Date.now();

public hashes: Map<Tick, number> = new Map();

public reportedWinner: Winner | null = null;

constructor(
public readonly clientID: ClientID,
public readonly persistentID: string,
Expand Down
60 changes: 51 additions & 9 deletions src/server/GameServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export class GameServer {

private websockets: Set<WebSocket> = new Set();

private winnerVotes: Map<
string,
{ winner: ClientSendWinnerMessage; ips: Set<string> }
> = new Map();

Comment on lines +67 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

One-vote-per-IP is not enforced across competing winners

An IP can appear in multiple winner buckets if different clients from the same IP vote for different winners. This can produce conflicting “majorities” depending on arrival order.

Apply this to track and move an IP’s vote between winners:

   private winnerVotes: Map<
     string,
     { winner: ClientSendWinnerMessage; ips: Set<string> }
   > = new Map();
+  // Ensure one vote per IP across all winners.
+  private ipVotes: Map<string, string> = new Map();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private winnerVotes: Map<
string,
{ winner: ClientSendWinnerMessage; ips: Set<string> }
> = new Map();
private winnerVotes: Map<
string,
{ winner: ClientSendWinnerMessage; ips: Set<string> }
> = new Map();
// Ensure one vote per IP across all winners.
private ipVotes: Map<string, string> = new Map();
🤖 Prompt for AI Agents
In src/server/GameServer.ts around lines 67-71, the current winnerVotes Map
allows the same IP to be present in multiple winner buckets, so implement a
global IP-to-winner lookup and update logic: maintain a Map<string, string>
ipToWinner that maps each IP to the winner ID; when processing a new vote, check
ipToWinner for that IP — if it points to a different winner, remove the IP from
the previous winner's ips Set and update that winner's data, then add the IP to
the new winner's ips Set and update ipToWinner to the new winner; if the vote is
duplicate for the same winner, ignore; ensure entries for winners are
created/cleaned up when their ips Sets become non-empty/empty.

constructor(
public readonly id: string,
readonly log_: Logger,
Expand Down Expand Up @@ -184,6 +189,7 @@ export class GameServer {
}

client.lastPing = existing.lastPing;
client.reportedWinner = existing.reportedWinner;

this.activeClients = this.activeClients.filter((c) => c !== existing);
}
Expand Down Expand Up @@ -283,15 +289,7 @@ export class GameServer {
break;
}
case "winner": {
if (
this.outOfSyncClients.has(client.clientID) ||
this.kickedClients.has(client.clientID) ||
this.winner !== null
) {
return;
}
this.winner = clientMsg;
this.archiveGame();
this.handleWinner(client, clientMsg);
break;
}
default: {
Expand Down Expand Up @@ -793,4 +791,48 @@ export class GameServer {
outOfSyncClients,
};
}

private handleWinner(client: Client, clientMsg: ClientSendWinnerMessage) {
if (
this.outOfSyncClients.has(client.clientID) ||
this.kickedClients.has(client.clientID) ||
this.winner !== null ||
client.reportedWinner !== null
) {
return;
}
client.reportedWinner = clientMsg.winner;

// Add client vote
const winnerKey = JSON.stringify(clientMsg.winner);
if (!this.winnerVotes.has(winnerKey)) {
this.winnerVotes.set(winnerKey, { ips: new Set(), winner: clientMsg });
}
const potentialWinner = this.winnerVotes.get(winnerKey)!;
potentialWinner.ips.add(client.ip);

const activeUniqueIPs = new Set(this.activeClients.map((c) => c.ip));

const ratio = `${potentialWinner.ips.size}/${activeUniqueIPs.size}`;
this.log.info(
`recieved winner vote ${clientMsg.winner}, ${ratio} votes for this winner`,
{
clientID: client.clientID,
},
);

if (potentialWinner.ips.size * 2 < activeUniqueIPs.size) {
return;
}

// Vote succeeded
this.winner = potentialWinner.winner;
this.log.info(
`Winner determined by ${potentialWinner.ips.size}/${activeUniqueIPs.size} active IPs`,
{
winnerKey: winnerKey,
},
);
this.archiveGame();
}
}
Loading