Skip to content

Commit e483c82

Browse files
committed
Select only powers that are in end game
We were selecting powers to play as randomly, but sometimes they'd lose mid game, making the rest of the game very boring.
1 parent 3cccbbc commit e483c82

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

ai_animation/src/gameState.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,36 @@ enum AvailableMaps {
2020

2121
/**
2222
* Return a random power from the PowerENUM for the player to control
23+
* Only returns powers that have more than 2 supply centers in the last phase
2324
*/
24-
function getRandomPower(): PowerENUM {
25-
const values = Object.values(PowerENUM).filter(power =>
25+
function getRandomPower(gameData?: GameSchemaType): PowerENUM {
26+
const allPowers = Object.values(PowerENUM).filter(power =>
2627
power !== PowerENUM.GLOBAL && power !== PowerENUM.EUROPE
2728
);
28-
const idx = Math.floor(Math.random() * values.length);
29-
return values[idx];
29+
30+
// If no game data provided, return any random power
31+
if (!gameData || !gameData.phases || gameData.phases.length === 0) {
32+
const idx = Math.floor(Math.random() * allPowers.length);
33+
return allPowers[idx];
34+
}
35+
36+
// Get the last phase to check supply centers
37+
const lastPhase = gameData.phases[gameData.phases.length - 1];
38+
39+
// Filter powers that have more than 2 supply centers
40+
const eligiblePowers = allPowers.filter(power => {
41+
const centers = lastPhase.state?.centers?.[power];
42+
return centers && centers.length > 2;
43+
});
44+
45+
// If no powers have more than 2 centers, fall back to any power
46+
if (eligiblePowers.length === 0) {
47+
const idx = Math.floor(Math.random() * allPowers.length);
48+
return allPowers[idx];
49+
}
50+
51+
const idx = Math.floor(Math.random() * eligiblePowers.length);
52+
return eligiblePowers[idx];
3053
}
3154

3255
function loadFileFromServer(filePath: string): Promise<string> {
@@ -155,8 +178,8 @@ class GameState {
155178
playBtn.disabled = false;
156179
speedSelector.disabled = false;
157180

158-
// Set the poewr if the game specifies it, else random.
159-
this.currentPower = this.gameData.power !== undefined ? this.gameData.power : getRandomPower();
181+
// Set the power if the game specifies it, else random.
182+
this.currentPower = this.gameData.power !== undefined ? this.gameData.power : getRandomPower(this.gameData);
160183

161184

162185
const momentsFilePath = `./games/${this.gameId}/moments.json`;

0 commit comments

Comments
 (0)