Skip to content

Commit 9f97e2f

Browse files
committed
Changed PublicGameModifier enum to interface
1 parent def79e6 commit 9f97e2f

7 files changed

Lines changed: 43 additions & 56 deletions

File tree

src/client/PublicLobby.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
GameMapType,
77
GameMode,
88
HumansVsNations,
9-
PublicGameModifier,
9+
PublicGameModifiers,
1010
Quads,
1111
Trios,
1212
} from "../core/game/Game";
@@ -305,19 +305,19 @@ export class PublicLobby extends LitElement {
305305
}
306306

307307
private getModifierLabels(
308-
publicGameModifiers: PublicGameModifier[] | undefined,
308+
publicGameModifiers: PublicGameModifiers | undefined,
309309
): string[] {
310-
if (!publicGameModifiers || publicGameModifiers.length === 0) {
310+
if (!publicGameModifiers) {
311311
return [];
312312
}
313-
return publicGameModifiers.map((modifier) => {
314-
// Convert PascalCase to snake_case for localization key
315-
const key = modifier
316-
.replace(/([A-Z])/g, "_$1")
317-
.toLowerCase()
318-
.slice(1);
319-
return translateText(`public_game_modifier.${key}`);
320-
});
313+
const labels: string[] = [];
314+
if (publicGameModifiers.isRandomSpawn) {
315+
labels.push(translateText("public_game_modifier.random_spawn"));
316+
}
317+
if (publicGameModifiers.isCompact) {
318+
labels.push(translateText("public_game_modifier.compact_map"));
319+
}
320+
return labels;
321321
}
322322

323323
private lobbyClicked(lobby: GameInfo) {

src/core/Schemas.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
GameMode,
1616
GameType,
1717
HumansVsNations,
18-
PublicGameModifier,
1918
Quads,
2019
Trios,
2120
UnitType,
@@ -172,7 +171,12 @@ export const GameConfigSchema = z.object({
172171
gameType: z.enum(GameType),
173172
gameMode: z.enum(GameMode),
174173
gameMapSize: z.enum(GameMapSize),
175-
publicGameModifiers: z.enum(PublicGameModifier).array().optional(),
174+
publicGameModifiers: z
175+
.object({
176+
isCompact: z.boolean(),
177+
isRandomSpawn: z.boolean(),
178+
})
179+
.optional(),
176180
disableNations: z.boolean(),
177181
bots: z.number().int().min(0).max(400),
178182
infiniteGold: z.boolean(),

src/core/configuration/Config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
Gold,
88
Player,
99
PlayerInfo,
10-
PublicGameModifier,
10+
PublicGameModifiers,
1111
Team,
1212
TerraNullius,
1313
Tick,
@@ -59,7 +59,7 @@ export interface ServerConfig {
5959
stripePublishableKey(): string;
6060
allowedFlares(): string[] | undefined;
6161
enableMatchmaking(): boolean;
62-
getRandomPublicGameModifiers(): PublicGameModifier[];
62+
getRandomPublicGameModifiers(): PublicGameModifiers;
6363
supportsCompactMapForTeams(map: GameMapType): boolean;
6464
}
6565

src/core/configuration/DefaultConfig.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
Player,
1313
PlayerInfo,
1414
PlayerType,
15-
PublicGameModifier,
15+
PublicGameModifiers,
1616
Quads,
1717
TerrainType,
1818
TerraNullius,
@@ -224,17 +224,11 @@ export abstract class DefaultServerConfig implements ServerConfig {
224224
return false;
225225
}
226226

227-
getRandomPublicGameModifiers(): PublicGameModifier[] {
228-
const modifiers: PublicGameModifier[] = [];
229-
if (Math.random() < 0.1) {
230-
// 10% chance
231-
modifiers.push(PublicGameModifier.RandomSpawn);
232-
}
233-
if (Math.random() < 0.05) {
234-
// 5% chance
235-
modifiers.push(PublicGameModifier.CompactMap);
236-
}
237-
return modifiers;
227+
getRandomPublicGameModifiers(): PublicGameModifiers {
228+
return {
229+
isRandomSpawn: Math.random() < 0.1, // 10% chance
230+
isCompact: Math.random() < 0.05, // 5% chance
231+
};
238232
}
239233

240234
supportsCompactMapForTeams(map: GameMapType): boolean {

src/core/game/Game.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ export enum GameMapSize {
186186
Normal = "Normal",
187187
}
188188

189-
export enum PublicGameModifier {
190-
RandomSpawn = "RandomSpawn",
191-
CompactMap = "CompactMap",
189+
export interface PublicGameModifiers {
190+
isCompact: boolean;
191+
isRandomSpawn: boolean;
192192
}
193193

194194
export interface UnitInfo {

src/server/MapPlaylist.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
GameMode,
99
GameType,
1010
HumansVsNations,
11-
PublicGameModifier,
1211
Quads,
1312
Trios,
1413
} from "../core/game/Game";
@@ -93,41 +92,31 @@ export class MapPlaylist {
9392
const playerTeams =
9493
mode === GameMode.Team ? this.getTeamCount() : undefined;
9594

96-
let publicGameModifiers = config.getRandomPublicGameModifiers();
95+
let { isCompact, isRandomSpawn } = config.getRandomPublicGameModifiers();
9796

9897
// Duos, Trios, and Quads should not get random spawn (as it defeats the purpose)
99-
const disallowRandomSpawn =
100-
playerTeams === Duos || playerTeams === Trios || playerTeams === Quads;
101-
if (disallowRandomSpawn) {
102-
publicGameModifiers = publicGameModifiers.filter(
103-
(m) => m !== PublicGameModifier.RandomSpawn,
104-
);
98+
if (
99+
playerTeams === Duos ||
100+
playerTeams === Trios ||
101+
playerTeams === Quads
102+
) {
103+
isRandomSpawn = false;
105104
}
106105

107106
// Small maps (3rd player count < 50) don't get compact map in team games
108107
if (mode === GameMode.Team && !config.supportsCompactMapForTeams(map)) {
109-
publicGameModifiers = publicGameModifiers.filter(
110-
(m) => m !== PublicGameModifier.CompactMap,
111-
);
108+
isCompact = false;
112109
}
113110

114-
const isCompactMap = publicGameModifiers.includes(
115-
PublicGameModifier.CompactMap,
116-
);
117-
const isRandomSpawn = publicGameModifiers.includes(
118-
PublicGameModifier.RandomSpawn,
119-
);
120-
121111
// Create the default public game config (from your GameManager)
122112
return {
123113
donateGold: mode === GameMode.Team,
124114
donateTroops: mode === GameMode.Team,
125115
gameMap: map,
126-
maxPlayers: config.lobbyMaxPlayers(map, mode, playerTeams, isCompactMap),
116+
maxPlayers: config.lobbyMaxPlayers(map, mode, playerTeams, isCompact),
127117
gameType: GameType.Public,
128-
gameMapSize: isCompactMap ? GameMapSize.Compact : GameMapSize.Normal,
129-
publicGameModifiers:
130-
publicGameModifiers.length > 0 ? publicGameModifiers : undefined,
118+
gameMapSize: isCompact ? GameMapSize.Compact : GameMapSize.Normal,
119+
publicGameModifiers: { isCompact, isRandomSpawn },
131120
difficulty:
132121
playerTeams === HumansVsNations ? Difficulty.Hard : Difficulty.Easy,
133122
infiniteGold: false,
@@ -138,7 +127,7 @@ export class MapPlaylist {
138127
disableNations: mode === GameMode.Team && playerTeams !== HumansVsNations,
139128
gameMode: mode,
140129
playerTeams,
141-
bots: isCompactMap ? 100 : 400,
130+
bots: isCompact ? 100 : 400,
142131
spawnImmunityDuration: 5 * 10,
143132
disabledUnits: [],
144133
} satisfies GameConfig;

tests/util/TestServerConfig.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { JWK } from "jose";
22
import { GameEnv, ServerConfig } from "../../src/core/configuration/Config";
3-
import { GameMapType, PublicGameModifier } from "../../src/core/game/Game";
3+
import { GameMapType, PublicGameModifiers } from "../../src/core/game/Game";
44
import { GameID } from "../../src/core/Schemas";
55

66
export class TestServerConfig implements ServerConfig {
@@ -82,8 +82,8 @@ export class TestServerConfig implements ServerConfig {
8282
gitCommit(): string {
8383
throw new Error("Method not implemented.");
8484
}
85-
getRandomPublicGameModifiers(): PublicGameModifier[] {
86-
throw new Error("Method not implemented.");
85+
getRandomPublicGameModifiers(): PublicGameModifiers {
86+
return { isCompact: false, isRandomSpawn: false };
8787
}
8888
supportsCompactMapForTeams(map: GameMapType): boolean {
8989
throw new Error("Method not implemented.");

0 commit comments

Comments
 (0)