Restore missing Anarchy cubes in existing saves

This commit is contained in:
coyotte508 2026-10-18 20:30:00 +02:00
commit 4bade66adf
6 changed files with 89 additions and 3 deletions

View file

@ -340,6 +340,38 @@ fn anarchy() -> Incident {
.build()
}
pub(crate) fn restore_missing_anarchy_cubes(game: &mut Game) {
// Older engines could remove another player's Anarchy effect during research.
// Its victory points survived: each point still represents one unused cube.
for (player, p) in game.players.iter().enumerate() {
let points: f32 = p
.special_victory_points
.iter()
.filter(|v| v.origin == EventOrigin::Incident(44))
.map(|v| v.points)
.sum();
if !points.is_finite() || points < 1.0 || points.fract() != 0.0 {
continue;
}
let existing: usize = game
.permanent_effects
.iter()
.filter_map(|e| match e {
PermanentEffect::Anarchy(a) if a.player == player => Some(a.advances_lost),
_ => None,
})
.sum();
let missing = (points as usize).saturating_sub(existing);
if missing > 0 {
game.permanent_effects
.push(PermanentEffect::Anarchy(Anarchy {
player,
advances_lost: missing,
}));
}
}
}
pub(crate) fn anarchy_replaces_event_marker(game: &Game, player: usize, advance: Advance) -> bool {
advance.info(game).government.is_some()
&& game.permanent_effects.iter().any(|effect| {

View file

@ -7,6 +7,7 @@ use crate::content::ability;
use crate::content::ability::Ability;
use crate::content::custom_actions::SpecialAction;
use crate::content::effects::PermanentEffect;
use crate::content::incidents::trojan::restore_missing_anarchy_cubes;
use crate::content::persistent_events::PersistentEventState;
use crate::game::{Game, GameContext, GameOptions, GameState, UIElement};
use crate::leader::Leader;
@ -157,6 +158,7 @@ pub fn from_data(data: GameData, cache: Cache, context: GameContext) -> Game {
for player_data in data.players {
game.players.push(player_from_data(player_data, &game));
}
restore_missing_anarchy_cubes(&mut game);
let all = game.cache.get_abilities().clone();
for player_index in 0..game.players.len() {
initialize_player(player_index, &mut game, &all);

View file

@ -411,3 +411,5 @@ Full-game replay no longer reserves room below chat for the hidden last-turn rec
Viewer 0.1.148 / engine 0.4.102 uses Anarchy's remaining cubes when forecasting government research events. Eligible government advances no longer show an event warning; ordinary research still does, and the general warning is hidden when available choices have different outcomes. The journal also records the unchanged event counter. Anarchy resolution now preserves other players' cubes and does not add an event marker for card-granted advances that already bypass the counter. No historical games are rewritten.
Verified with 832 frontend/engine tests, Anarchy and Dogma Rust regressions, locale validation, type checking, production build, and a browser preview of Dogma with one event marker remaining. Rule source: the printed Revolution: Anarchy card in the English event deck scan linked in `docs/card-text-audit.json`.
Viewer 0.1.149 / engine 0.4.103 restores missing Anarchy cubes when loading saves affected by the old cross-player removal bug. Surviving Anarchy victory points give the exact unused cube count; recovery adds only missing cubes per player and preserves existing effects and scores. This repairs both research previews and execution without replaying historical moves. Verified against a private backup of the reported game, including Dogma research and Undo, plus 835 frontend/engine tests, targeted Rust Anarchy tests, all locales, type checking and production build.

View file

@ -1,12 +1,12 @@
{
"name": "@boardgamers/clash-web-client",
"version": "0.1.148",
"version": "0.1.149",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@boardgamers/clash-web-client",
"version": "0.1.148",
"version": "0.1.149",
"dependencies": {
"@boardgamers/protocol": "0.9.0",
"lucide-svelte": "^0.577.0",

View file

@ -1,6 +1,6 @@
{
"name": "@boardgamers/clash-web-client",
"version": "0.1.148",
"version": "0.1.149",
"private": true,
"type": "module",
"scripts": {

View file

@ -71,6 +71,8 @@ test('the warning returns only after the last Anarchy cube has been used', () =>
test('another player researching government neither uses nor removes your Anarchy cubes', () => {
const game = afterAnarchy();
game.players[1].special_victory_points = [{ points: 2, origin: { Incident: 44 }, attribution: 'Events' }];
game.players[0].special_victory_points = [];
game.permanent_effects = [{ Anarchy: { player: 1, advances_lost: 2 } }];
assert.equal(view(game).advances.find((a) => a.id === 'Dogma')!.triggersEvent, true);
game.players[0].incident_tokens = 2;
@ -81,6 +83,7 @@ test('another player researching government neither uses nor removes your Anarch
test('Anarchy uses only the researching player’s cube when multiple players are affected', () => {
const game = afterAnarchy();
game.players[1].special_victory_points = [{ points: 2, origin: { Incident: 44 }, attribution: 'Events' }];
const other = { Anarchy: { player: 1, advances_lost: 2 } };
game.permanent_effects.unshift(other);
const after = research(game, 'Dogma');
@ -99,3 +102,50 @@ test('a card-granted government advance uses an Anarchy cube without adding an e
assert.equal(game.players[0].incident_tokens, 1);
assert.deepEqual(game.permanent_effects ?? [], []);
});
test('a saved Anarchy point restores its missing cube for previews and actual research', () => {
const game = afterAnarchy();
delete game.permanent_effects;
assert.equal(view(game).advances.find((a) => a.id === 'Dogma')!.triggersEvent, false);
const after = research(game, 'Dogma');
assert.equal(after.players[0].incident_tokens, 1);
assert.deepEqual(after.incidents_left, game.incidents_left);
assert.equal(after.rng, game.rng);
assert.deepEqual(after.permanent_effects ?? [], []);
assert.deepEqual(after.players[0].special_victory_points ?? [], []);
assert.equal(view(after).advances.find((a) => a.id === 'Devotion')!.triggersEvent, true);
const undone = move(after, 'Undo');
assert.equal(undone.players[0].special_victory_points[0].points, 1);
assert.equal(view(undone).advances.find((a) => a.id === 'Dogma')!.triggersEvent, false);
const redone = move(undone, 'Redo');
assert.equal(redone.players[0].incident_tokens, 1);
assert.deepEqual(redone.permanent_effects ?? [], []);
assert.deepEqual(redone.players[0].special_victory_points ?? [], []);
});
test('restoring missing Anarchy cubes is per player and does not duplicate surviving cubes on reload', () => {
const game = afterAnarchy(3);
game.permanent_effects[0].Anarchy.advances_lost = 1;
game.players[1].special_victory_points = [{ points: 2, origin: { Incident: 44 }, attribution: 'Events' }];
game.permanent_effects.push({ Anarchy: { player: 1, advances_lost: 2 } }, 'TrojanHorse');
const loaded = JSON.parse(engine.stripSecret(JSON.stringify(game), 0));
const cubes = (g: any, seat: number) =>
g.permanent_effects.reduce(
(sum: number, e: any) => sum + (e.Anarchy?.player === seat ? e.Anarchy.advances_lost : 0),
0,
);
assert.equal(cubes(loaded, 0), 3);
assert.equal(cubes(loaded, 1), 2);
assert.ok(loaded.permanent_effects.includes('TrojanHorse'));
const reloaded = JSON.parse(engine.stripSecret(JSON.stringify(loaded), 0));
assert.deepEqual(reloaded.permanent_effects, loaded.permanent_effects);
assert.deepEqual(reloaded.players[0].special_victory_points, game.players[0].special_victory_points);
});
test('unrelated event points do not restore Anarchy cubes', () => {
const game = afterAnarchy();
delete game.permanent_effects;
game.players[0].special_victory_points[0].origin = { Incident: 42 };
assert.equal(view(game).advances.find((a) => a.id === 'Dogma')!.triggersEvent, true);
});