Skip to content

Commit

Permalink
Merge pull request #572 from gereon77/fast-track-planning-when-no-units
Browse files Browse the repository at this point in the history
Automatically set ready for players who don't have units left
  • Loading branch information
Longwelwind committed Apr 22, 2020
2 parents f41ba12 + b2de42a commit efb4191
Showing 1 changed file with 35 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ export default class PlanningGameState extends GameState<IngameGameState> {
});

this.planningRestrictions = planningRestrictions;
}

isOrderAvailable(house: House, order: Order): boolean {
return this.getAvailableOrders(house).includes(order);
// Automatically set ready for houses which don't have units left
this.game.houses.forEach(h => {
if (this.getPossibleRegionsForOrders(h).length == 0) {
this.setReady(this.ingameGameState.getControllerOfHouse(h));
}
});
}

onPlayerMessage(player: Player, message: ClientMessage): void {
Expand Down Expand Up @@ -99,30 +102,30 @@ export default class PlanningGameState extends GameState<IngameGameState> {
})
}
} else if (message.type == "ready") {
if (this.readyPlayers.includes(player)) {
return;
}

if (!this.canReady(player.house).status) {
return;
}
this.setReady(player);
}
}

this.readyPlayers.push(player);
private setReady(player: Player): void {
if (this.readyPlayers.includes(player)) {
return;
}

// Check if all player are ready to go the action entireGame state
if (this.readyPlayers.length == this.ingameGameState.players.values.length) {
this.ingameGameState.proceedToActionGameState(this.placedOrders as BetterMap<Region, Order>, this.planningRestrictions);
} else {
this.entireGame.broadcastToClients({
type: "player-ready",
userId: player.user.id
});
}
if (!this.canReady(player.house).status) {
return;
}
}

getPossibleRegionsForOrders(house: House): Region[] {
return this.game.world.getControlledRegions(house).filter(r => r.units.size > 0);
this.readyPlayers.push(player);

// Check if all player are ready to go the action entireGame state
if (this.readyPlayers.length == this.ingameGameState.players.values.length) {
this.ingameGameState.proceedToActionGameState(this.placedOrders as BetterMap<Region, Order>, this.planningRestrictions);
} else {
this.entireGame.broadcastToClients({
type: "player-ready",
userId: player.user.id
});
}
}

serializeToClient(admin: boolean, player: Player | null): SerializedPlanningGameState {
Expand All @@ -147,7 +150,15 @@ export default class PlanningGameState extends GameState<IngameGameState> {
* Common
*/

canReady(house: House): {status: boolean; reason: string} {
getPossibleRegionsForOrders(house: House): Region[] {
return this.game.world.getControlledRegions(house).filter(r => r.units.size > 0);
}

isOrderAvailable(house: House, order: Order): boolean {
return this.getAvailableOrders(house).includes(order);
}

canReady(house: House): {status: boolean; reason: string} {
const possibleRegions = this.getPossibleRegionsForOrders(house);

if (possibleRegions.every(r => this.placedOrders.has(r)))
Expand Down

0 comments on commit efb4191

Please sign in to comment.