Skip to content

Commit

Permalink
Make retreat region public to avoid error deserializing combat state …
Browse files Browse the repository at this point in the history
…in post combat states

and clear public regions after combat.
  • Loading branch information
gereon77 committed Jun 22, 2024
1 parent b46d9d0 commit 5b15a1e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 55 deletions.
4 changes: 2 additions & 2 deletions agot-bg-game-server/src/client/GameSettingsComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ export default class GameSettingsComponent extends Component<GameSettingsCompone
All houses, except Targaryen, will replace their starting knight by a dragon. <small><i>
(Inspired by LordWazza)</i></small>
</Tooltip>}>
<label htmlFor="dragon-war-setting">Dragon war</label>
<label htmlFor="dragon-war-setting">Dragon War</label>
</OverlayTrigger>}
checked={this.gameSettings.dragonWar}
onChange={() => this.changeGameSettings(() => this.gameSettings.dragonWar = !this.gameSettings.dragonWar)}
Expand All @@ -836,7 +836,7 @@ export default class GameSettingsComponent extends Component<GameSettingsCompone
<Tooltip id="dragon-revenge-tooltip">
If a player has only one remaining non-dragon land unit and no more castles, it will turn into a dragon.
</Tooltip>}>
<label htmlFor="dragon-revenge-setting">Dragon revenge</label>
<label htmlFor="dragon-revenge-setting">Dragon Revenge</label>
</OverlayTrigger>}
checked={this.gameSettings.dragonRevenge}
onChange={() => this.changeGameSettings(() => this.gameSettings.dragonRevenge = !this.gameSettings.dragonRevenge)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1813,7 +1813,7 @@ export default class IngameGameState extends GameState<
return _.uniq(result);
}

addPublicVisibleRegions(regions: Region[]): void {
addPublicVisibleRegions(...regions: Region[]): void {
if (this.fogOfWar) {
const addedRegions = regions.filter(r => !this.publicVisibleRegions.includes(r));
this.publicVisibleRegions.push(...addedRegions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ export default class CombatGameState extends GameState<
[defender, {region: combatRegion, army: combatRegion.units.values, houseCard: null, tidesOfBattleCard: this.isTidesOfBattleCardsActive ? null : undefined}]
]);

this.ingameGameState.addPublicVisibleRegions(attackerComingFrom, combatRegion);

// Automatically declare attackers and defenders support
const possibleSupporters = this.getPossibleSupportingHouses();

Expand All @@ -196,8 +198,6 @@ export default class CombatGameState extends GameState<
if (!this.proceedNextSupportDeclaration(true)) {
this.proceedToChooseGeneral();
}

this.ingameGameState.addPublicVisibleRegions([attackerComingFrom, combatRegion]);
}

declareSupport(supportingHouse: House, supportedHouse: House | null, writeToGameLog: boolean): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,6 @@ export default class PostCombatGameState extends GameState<
if (this.doesVictoriousDefenderNeedToRetreat() && !this.originalLoser) {
// For the sake of simplicity, declare the winner the loser for the upcoming retreat phase.
// This way we do not have to complicate the code and handle the possible retreat of the winner in ResolveRetreatGameState.
this.combat.ingameGameState.log({
type: "arianne-martell-force-retreat",
house: this.loser.id,
enemyHouse: this.winner.id
});

this.originalLoser = this.loser;
this.loser = this.winner;
this.proceedRetreat();
Expand Down Expand Up @@ -426,6 +420,20 @@ export default class PostCombatGameState extends GameState<
});
}
});

// Hide combat areas after combat
this.combat.ingameGameState.publicVisibleRegions = [];
this.entireGame.users.values.filter(u => u.connected).forEach(u => {
this.entireGame.sendMessageToClients([u], {
type: "update-public-visible-regions",
regionsToMakeVisible: [],
ordersToMakeVisible: [],
clear: true,
applyChangesNow: !this.combat.ingameGameState.players.has(u)
});
});
this.combat.ingameGameState.updateVisibleRegions(true);

this.combat.resolveMarchOrderGameState.onResolveSingleMarchOrderGameStateFinish(this.attacker);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,49 +192,20 @@ export default class ResolveRetreatGameState extends GameState<

const from = this.postCombat.loserCombatData.region;

if (!this.ingame.fogOfWar) {
this.entireGame.broadcastToClients({
type: "move-units",
from: from.id,
to: this.retreatRegion.id,
units: armyLeft.map(u => u.id),
isRetreat: true
});
} else {
const to = this.retreatRegion as Region;
const userVisibilityList = this.entireGame.users.values.map(u => {
const p = this.ingame.players.tryGet(u, null);
const visibleRegions = this.ingame.getVisibleRegionsForPlayer(p);
return {
user: u,
seesFrom: visibleRegions.includes(from),
seesTo: visibleRegions.includes(to)
}
});

const usersWhoSeeFromAndTo = userVisibilityList.filter(item => item.seesFrom && item.seesTo).map(item => item.user);
this.entireGame.sendMessageToClients(usersWhoSeeFromAndTo, {
type: "move-units",
from: from.id,
to: to.id,
units: armyLeft.map(u => u.id)
});

const userWhoOnlySeeFrom = userVisibilityList.filter(dict => dict.seesFrom && !dict.seesTo).map(dict => dict.user);
this.entireGame.sendMessageToClients(userWhoOnlySeeFrom, {
type: "remove-units",
regionId: from.id,
unitIds: armyLeft.map(u => u.id),
animate: false
});

const userWhoOnlySeeTo = userVisibilityList.filter(dict => !dict.seesFrom && dict.seesTo).map(dict => dict.user);
this.entireGame.sendMessageToClients(userWhoOnlySeeTo, {
type: "add-units",
regionId: to.id,
units: armyLeft.map(u => u.serializeToClient())
});
}
// As the army units are necessary in the world
// to deserialize combat state properly,
// we add the chosen retreat region to public visible regions
// and clear this list afer combat.
this.ingame.addPublicVisibleRegions(this.retreatRegion);
this.ingame.updateVisibleRegions();

this.entireGame.broadcastToClients({
type: "move-units",
from: from.id,
to: this.retreatRegion.id,
units: armyLeft.map(u => u.id),
isRetreat: true
});
}

this.postCombat.onResolveRetreatFinish();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ export default class ArianneMartellHouseCardAbility extends HouseCardAbility {
}

forcesRetreatOfVictoriousDefender(postCombat: PostCombatGameState, house: House, houseCard: HouseCard): boolean {
return houseCard.id == "arianne-martell-asos" &&
const result = houseCard.id == "arianne-martell-asos" &&
postCombat.attacker == house &&
postCombat.loser == house &&
Math.abs(postCombat.combat.stats[0].total - postCombat.combat.stats[1].total) <= 2;

if (result) {
postCombat.combat.ingameGameState.log({
type: "arianne-martell-force-retreat",
house: postCombat.loser.id,
enemyHouse: postCombat.winner.id
});
}
return result;
}
}

0 comments on commit 5b15a1e

Please sign in to comment.