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,