Skip to content

Commit fe8c0f9

Browse files
authored
Fix clients able to join above max players (#2547)
Resolves #698 ## Description: This PR fixes clients being able to join games which are already full. This caused several bugs and glitches, like incorrect team sizes in team games, and being stuck in spectator mode. This fix checks for number of active clients in GameServer, and if it sees that the lobby is full, it does not put the client in the game, and sends an error with error key being "full-lobby" ClientGameRunner then checks to see this error, and overrides the default implementation of showing a popup. Instead it will leave the lobby for the user by dispatching a leave-lobby event into the document, and the user can then reqeue into a new game. Here is a video showcasing how full games are handled. https://github.com/user-attachments/assets/dc6220ea-590f-4bd1-8ca5-38c0d24ae792 ## Note on testing I wasn't able to figure out how to properly overwrite lobbyMaxPlayers from the default config using the devconfig, so the video shows just a hardcoded version of defaultconfig, therefore testing solo is probably not really possible. I just changed the function in defaultconfig for my testing to this: ```ts lobbyMaxPlayers( map: GameMapType, mode: GameMode, numPlayerTeams: TeamCountConfig | undefined, ): number { return 1; } ``` ## Notes This PR does not necessarily resolve all cases which cause 698, as for example joining too late while there is still space is not changed at all. For most public games, this shouldn't be an issue as the timer is long enough for a majority to be filled up before the timer hits 0. Additionally, spectating ongoing games should work fine, but as local server spectating is buggy in general, I was not able to test and confirm this 100%. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: Lavodan
1 parent 8f3e09c commit fe8c0f9

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/client/ClientGameRunner.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,25 @@ export function joinLobby(
116116
).then((r) => r.start());
117117
}
118118
if (message.type === "error") {
119-
showErrorModal(
120-
message.error,
121-
message.message,
122-
lobbyConfig.gameID,
123-
lobbyConfig.clientID,
124-
true,
125-
false,
126-
"error_modal.connection_error",
127-
);
119+
if (message.error === "full-lobby") {
120+
document.dispatchEvent(
121+
new CustomEvent("leave-lobby", {
122+
detail: { lobby: lobbyConfig.gameID },
123+
bubbles: true,
124+
composed: true,
125+
}),
126+
);
127+
} else {
128+
showErrorModal(
129+
message.error,
130+
message.message,
131+
lobbyConfig.gameID,
132+
lobbyConfig.clientID,
133+
true,
134+
false,
135+
"error_modal.connection_error",
136+
);
137+
}
128138
}
129139
};
130140
transport.connect(onconnect, onmessage);

src/server/GameServer.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ export class GameServer {
137137
});
138138
return;
139139
}
140+
140141
// Log when lobby creator joins private game
141142
if (client.clientID === this.lobbyCreatorID) {
142143
this.log.info("Lobby creator joined", {
@@ -207,6 +208,23 @@ export class GameServer {
207208
this.activeClients = this.activeClients.filter((c) => c !== existing);
208209
}
209210

211+
if (
212+
this.gameConfig.maxPlayers &&
213+
this.activeClients.length >= this.gameConfig.maxPlayers
214+
) {
215+
this.log.warn(`cannot add client, game full`, {
216+
clientID: client.clientID,
217+
});
218+
219+
client.ws.send(
220+
JSON.stringify({
221+
type: "error",
222+
error: "full-lobby",
223+
} satisfies ServerErrorMessage),
224+
);
225+
return;
226+
}
227+
210228
// Client connection accepted
211229
this.activeClients.push(client);
212230
client.lastPing = Date.now();

0 commit comments

Comments
 (0)