From 2249f5207e4c76d340044164863f26f8fe272e9e Mon Sep 17 00:00:00 2001 From: NewHappyRabbit <31893343+NewHappyRabbit@users.noreply.github.com> Date: Mon, 17 Feb 2025 21:32:36 +0200 Subject: [PATCH] Public lobbies map will now be picked from a randomized playlist, assuring each map is played at least once, without duplicates. --- src/core/PseudoRandom.ts | 9 +++++++++ src/server/GameManager.ts | 10 +++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/core/PseudoRandom.ts b/src/core/PseudoRandom.ts index e24815b07..4fa8619f8 100644 --- a/src/core/PseudoRandom.ts +++ b/src/core/PseudoRandom.ts @@ -47,4 +47,13 @@ export class PseudoRandom { chance(odds: number): boolean { return this.nextInt(0, odds) == 0; } + + shuffleArray(array: any[]) { + for (let i = array.length - 1; i >= 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } + + return array; + } } diff --git a/src/server/GameManager.ts b/src/server/GameManager.ts index 49de68163..8c5b77647 100644 --- a/src/server/GameManager.ts +++ b/src/server/GameManager.ts @@ -9,6 +9,7 @@ import { PseudoRandom } from "../core/PseudoRandom"; export class GameManager { private lastNewLobby: number = 0; + private mapsPlaylist: GameMapType[] = []; private games: GameServer[] = []; @@ -77,6 +78,13 @@ export class GameManager { } } + private getNextMap(): GameMapType { + if (this.mapsPlaylist.length == 0) { + this.mapsPlaylist = this.random.shuffleArray(Object.values(GameMapType)); + } + return this.mapsPlaylist.shift(); + } + tick() { const lobbies = this.gamesByPhase(GamePhase.Lobby); const active = this.gamesByPhase(GamePhase.Active); @@ -87,7 +95,7 @@ export class GameManager { this.lastNewLobby = now; lobbies.push( new GameServer(generateID(), now, true, this.config, { - gameMap: this.random.randElement(Object.values(GameMapType)), + gameMap: this.getNextMap(), gameType: GameType.Public, difficulty: Difficulty.Medium, disableBots: false,