Skip to content

Commit a0a7fa7

Browse files
committed
encapsulate isSpawnPhase calculation into default config for reusability
1 parent 6b9dd7f commit a0a7fa7

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

src/core/configuration/Config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ export interface Config {
9393
instantBuild(): boolean;
9494
isRandomSpawn(): boolean;
9595
numSpawnPhaseTurns(): number;
96-
numSingleplayerGracePeriodTurns(): number;
96+
numGracePeriodTurns(): number;
97+
isSpawnPhase(
98+
ticks: number,
99+
gameType: GameType,
100+
firstHumanSpawnTick?: number,
101+
): boolean;
97102
userSettings(): UserSettings;
98103
playerTeams(): TeamCountConfig;
99104

src/core/configuration/DefaultConfig.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,13 +633,31 @@ export class DefaultConfig implements Config {
633633
boatMaxNumber(): number {
634634
return 3;
635635
}
636+
636637
numSpawnPhaseTurns(): number {
637638
return this._gameConfig.gameType === GameType.Singleplayer ? 100 : 300;
638639
}
639640
// Amount of ticks for player to change spawn placement in singleplayer
640-
numSingleplayerGracePeriodTurns(): number {
641+
numGracePeriodTurns(): number {
641642
return 15;
642643
}
644+
isSpawnPhase(
645+
ticks: number,
646+
gameType: GameType,
647+
firstHumanSpawnTick?: number,
648+
): boolean {
649+
if (ticks > this.numSpawnPhaseTurns()) {
650+
return false;
651+
}
652+
if (gameType !== GameType.Singleplayer) {
653+
return true;
654+
}
655+
if (!firstHumanSpawnTick) {
656+
return true;
657+
}
658+
return ticks <= firstHumanSpawnTick + this.numGracePeriodTurns();
659+
}
660+
643661
numBots(): number {
644662
return this.bots();
645663
}

src/core/game/GameImpl.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
Execution,
1515
Game,
1616
GameMode,
17-
GameType,
1817
GameUpdates,
1918
HumansVsNations,
2019
MessageType,
@@ -341,24 +340,17 @@ export class GameImpl implements Game {
341340
}
342341

343342
inSpawnPhase(): boolean {
344-
if (this._ticks > this.config().numSpawnPhaseTurns()) {
345-
return false;
346-
}
347-
if (this.config().gameConfig().gameType === GameType.Singleplayer) {
348-
if (!this.firstHumanSpawnTick) {
349-
this.firstHumanSpawnTick = Array.from(this._players.values()).some(
350-
(player) => player.type() === PlayerType.Human && player.hasSpawned(),
351-
)
352-
? this._ticks
353-
: 0;
354-
} else {
355-
return (
356-
this._ticks <=
357-
this.firstHumanSpawnTick +
358-
this.config().numSingleplayerGracePeriodTurns()
359-
);
360-
}
343+
if (!this.firstHumanSpawnTick) {
344+
const humanSpawned = Array.from(this._players.values()).some(
345+
(p) => p.type() === PlayerType.Human && p.hasSpawned(),
346+
);
347+
if (humanSpawned) this.firstHumanSpawnTick = this._ticks;
361348
}
349+
return this._config.isSpawnPhase(
350+
this.ticks(),
351+
this.config().gameConfig().gameType,
352+
this.firstHumanSpawnTick,
353+
);
362354
return true;
363355
}
364356

src/core/game/GameView.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { WorkerClient } from "../worker/WorkerClient";
99
import {
1010
Cell,
1111
EmojiMessage,
12-
GameType,
1312
GameUpdates,
1413
Gold,
1514
NameViewData,
@@ -658,25 +657,17 @@ export class GameView implements GameMap {
658657
return this.lastUpdate.tick;
659658
}
660659
inSpawnPhase(): boolean {
661-
if (this.ticks() > this._config.numSpawnPhaseTurns()) {
662-
return false;
663-
}
664-
if (this._config.gameConfig().gameType === GameType.Singleplayer) {
665-
if (!this.firstHumanSpawnTick) {
666-
this.firstHumanSpawnTick = Array.from(this._players.values()).some(
667-
(player) => player.type() === PlayerType.Human && player.hasSpawned(),
668-
)
669-
? this.ticks()
670-
: 0;
671-
} else {
672-
return (
673-
this.ticks() <=
674-
this.firstHumanSpawnTick +
675-
this._config.numSingleplayerGracePeriodTurns()
676-
);
677-
}
660+
if (!this.firstHumanSpawnTick) {
661+
const humanSpawned = this.playerViews().some(
662+
(p) => p.type() === PlayerType.Human && p.hasSpawned(),
663+
);
664+
if (humanSpawned) this.firstHumanSpawnTick = this.ticks();
678665
}
679-
return true;
666+
return this.config().isSpawnPhase(
667+
this.ticks(),
668+
this.config().gameConfig().gameType,
669+
this.firstHumanSpawnTick,
670+
);
680671
}
681672
config(): Config {
682673
return this._config;

0 commit comments

Comments
 (0)