@@ -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
3255function 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