Allow queuing free research after finishing round three
This commit is contained in:
parent
74319b6e89
commit
d246f98e65
5 changed files with 125 additions and 14 deletions
|
|
@ -73,9 +73,54 @@ pub fn prune_objectives(game: &mut Game) {
|
|||
for index in invalid.into_iter().rev() { game.early_choices.remove(index); }
|
||||
}
|
||||
|
||||
fn advance_key(game: &Game) -> String {
|
||||
format!("{}:status/advance", game.age)
|
||||
}
|
||||
|
||||
fn advance_offer(game: &Game, player: usize) -> Option<Offer> {
|
||||
if game.age >= game.options.length.ages() { return None; }
|
||||
let status = game.events.iter().find_map(|event| match &event.event_type {
|
||||
PersistentEventType::StatusPhase(phase) => Some((phase, event)),
|
||||
_ => None,
|
||||
});
|
||||
let eligible = match status {
|
||||
Some((StatusPhaseState::CompleteObjectives, _)) => true,
|
||||
Some((StatusPhaseState::FreeAdvance, event)) => !event.players_used.contains(&player),
|
||||
Some(_) => false,
|
||||
None => {
|
||||
let order = game.human_players_sorted(game.starting_player_index);
|
||||
game.round == 3 && order.iter().position(|p| *p == player)
|
||||
.zip(order.iter().position(|p| *p == game.current_player_index))
|
||||
.is_some_and(|(own, current)| own < current)
|
||||
}
|
||||
};
|
||||
if !eligible { return None; }
|
||||
let p = game.player(player);
|
||||
let mut options: Vec<_> = game.cache.get_advances().values()
|
||||
.filter(|a| p.can_advance_free(a.advance, game))
|
||||
.map(|a| OptionChoice { label: a.name.clone(), description: a.description.clone(),
|
||||
response: EventResponse::SelectAdvance(a.advance) }).collect();
|
||||
options.sort_by(|a, b| a.label.cmp(&b.label));
|
||||
if options.is_empty() { return None; }
|
||||
let key = advance_key(game);
|
||||
let selected = game.early_choices.iter().find(|c| c.player == player &&
|
||||
(c.key == key || c.key.starts_with(&format!("{}:", game.age)) && c.key.ends_with(":FreeAdvance/advance")))
|
||||
.map(|c| c.response.clone());
|
||||
Some(Offer { key, title: "Free Advance".into(), options, selected })
|
||||
}
|
||||
|
||||
pub fn offers(game: &Game, player: usize) -> Vec<Offer> {
|
||||
if player >= game.players.len() || !game.player(player).is_human()
|
||||
|| game.dropped_players.contains(&player) || game.civilization_draft.is_some()
|
||||
|| matches!(game.state, crate::game::GameState::Finished) { return vec![]; }
|
||||
let mut result = current_offers(game, player);
|
||||
if let Some(offer) = advance_offer(game, player) { result.push(offer); }
|
||||
result
|
||||
}
|
||||
|
||||
/// Expose only the current step, never future cards, random results, or another
|
||||
/// player's hand. Earlier players may still change the available options.
|
||||
pub fn offers(game: &Game, player: usize) -> Vec<Offer> {
|
||||
fn current_offers(game: &Game, player: usize) -> Vec<Offer> {
|
||||
if player >= game.players.len() || !game.player(player).is_human()
|
||||
|| game.dropped_players.contains(&player) || game.civilization_draft.is_some() || matches!(game.state, crate::game::GameState::Finished) {
|
||||
return vec![];
|
||||
|
|
@ -108,13 +153,6 @@ pub fn offers(game: &Game, player: usize) -> Vec<Offer> {
|
|||
result.push(Offer { key, title, options, selected });
|
||||
};
|
||||
match &event.event_type {
|
||||
PersistentEventType::StatusPhase(StatusPhaseState::FreeAdvance) => {
|
||||
let mut options: Vec<_> = game.cache.get_advances().values()
|
||||
.filter(|a| p.can_advance_free(a.advance, game))
|
||||
.map(|a| OptionChoice { label: a.name.clone(), description: a.description.clone(), response: EventResponse::SelectAdvance(a.advance) }).collect();
|
||||
options.sort_by(|a,b| a.label.cmp(&b.label));
|
||||
add("advance", "Free Advance".into(), options);
|
||||
}
|
||||
PersistentEventType::StatusPhase(StatusPhaseState::RazeSize1City) => {
|
||||
let mut options: Vec<_> = p.cities.iter().filter(|c| c.size() == 1)
|
||||
.map(|c| option(c.position.to_string(), EventResponse::SelectPositions(vec![c.position]))).collect();
|
||||
|
|
@ -155,6 +193,9 @@ pub fn can_save(game: &Game, player: usize, key: &str, response: Option<&EventRe
|
|||
|
||||
pub fn save(game: &mut Game, player: usize, key: &str, response: Option<EventResponse>) -> Result<(), String> {
|
||||
if !can_save(game, player, key, response.as_ref()) { return Err("This early choice is no longer available".into()); }
|
||||
if key == advance_key(game) {
|
||||
game.early_choices.retain(|c| c.player != player || !c.key.ends_with(":FreeAdvance/advance"));
|
||||
}
|
||||
game.early_choices.retain(|c| c.player != player || (c.key != key && !matches!((&c.response, &response),
|
||||
(EventResponse::SelectHandCards(a), Some(EventResponse::SelectHandCards(b))) if !a.is_empty() && a == b && key.contains(":objective:"))));
|
||||
if let Some(response) = response { game.early_choices.push(SavedChoice { player, key: key.into(), response }); }
|
||||
|
|
@ -164,6 +205,11 @@ pub fn save(game: &mut Game, player: usize, key: &str, response: Option<EventRes
|
|||
}
|
||||
|
||||
pub fn expire_current_event(game: &mut Game) {
|
||||
if game.events.last().is_some_and(|e| matches!(e.event_type,
|
||||
PersistentEventType::StatusPhase(StatusPhaseState::FreeAdvance))) {
|
||||
let key = advance_key(game);
|
||||
game.early_choices.retain(|c| c.key != key);
|
||||
}
|
||||
if game.events.last().is_some_and(|e| matches!(e.event_type, PersistentEventType::StatusPhase(StatusPhaseState::CompleteObjectives))) {
|
||||
let prefix = format!("{}:objective:", game.age);
|
||||
game.early_choices.retain(|c| !c.key.starts_with(&prefix));
|
||||
|
|
@ -194,10 +240,12 @@ pub fn take_ready(game: &mut Game) -> Option<EventResponse> {
|
|||
if rs.iter().any(|r| r.name == "Pay to gain the Action Card")) => (game.events.len()-1, "offer".into()),
|
||||
_ => return None,
|
||||
};
|
||||
let key = if suffix == "first" { format!("{}:first", game.age) } else if let Some(name) = suffix.strip_prefix("objective:") {
|
||||
let key = if suffix == "advance" { advance_key(game) } else if suffix == "first" { format!("{}:first", game.age) } else if let Some(name) = suffix.strip_prefix("objective:") {
|
||||
objective_key(game, name)
|
||||
} else { format!("{}{suffix}", scope(game, index)?) };
|
||||
let at = game.early_choices.iter().position(|c| c.player == player && c.key == key)?;
|
||||
let legacy_key = format!("{}{suffix}", scope(game, index)?);
|
||||
let at = game.early_choices.iter().position(|c| c.player == player &&
|
||||
(c.key == key || suffix == "advance" && c.key == legacy_key))?;
|
||||
let response = &game.early_choices[at].response;
|
||||
let valid = match (&handler.request, response) {
|
||||
(PersistentEventRequest::SelectPlayer(r), EventResponse::SelectPlayer(p)) => r.choices.contains(p),
|
||||
|
|
|
|||
|
|
@ -560,3 +560,13 @@ Verified 890 frontend tests, targeted payment, Roads, Rome and Great Person Rust
|
|||
tests, Svelte checks, locales, production build and engine compatibility checks.
|
||||
The broader advances suite retains its known unrelated cultural-influence log
|
||||
snapshot mismatch. Existing game history is not repaired.
|
||||
|
||||
### 0.1.173 / engine 0.4.118 — Queue free research after turn three
|
||||
|
||||
The Research menu offers the next free advance as soon as a player has finished
|
||||
round three, while later players finish their turns. The private, editable choice
|
||||
survives entry into status phase and resolves only when that player's real research
|
||||
request is reached. Invalid choices ask again; earlier rounds and the final age do
|
||||
not offer this queue. Previously saved status-phase choices remain compatible.
|
||||
Verified 894 frontend/engine tests, locales, Svelte checks, production build and
|
||||
engine compatibility checks.
|
||||
|
|
|
|||
4
web-client/package-lock.json
generated
4
web-client/package-lock.json
generated
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@boardgamers/clash-web-client",
|
||||
"version": "0.1.172",
|
||||
"version": "0.1.173",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@boardgamers/clash-web-client",
|
||||
"version": "0.1.172",
|
||||
"version": "0.1.173",
|
||||
"dependencies": {
|
||||
"@boardgamers/protocol": "0.9.0",
|
||||
"lucide-svelte": "^0.577.0",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/clash-web-client",
|
||||
"version": "0.1.172",
|
||||
"version": "0.1.173",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ test('early objective choices stay private and complete only when that player is
|
|||
const offer = view(raw, 1).earlyChoices.find((o: any) => o.title === 'Wood Supplies');
|
||||
assert.ok(offer, 'other player can queue a qualifying status objective');
|
||||
const pick = offer.options.find((o: any) => o.response.SelectHandCards.length);
|
||||
for (const other of view(raw, 1).earlyChoices) {
|
||||
for (const other of view(raw, 1).earlyChoices.filter((o: any) => o.key.includes(':objective:'))) {
|
||||
raw = queue(raw, 1, other.key, other.key === offer.key ? pick.response : { SelectHandCards: [] });
|
||||
}
|
||||
assert.ok(!JSON.parse(raw).players[1].completed_objectives?.length);
|
||||
|
|
@ -231,3 +231,56 @@ test('disabling keep-government restores the normal payment prompt', () => {
|
|||
assert.equal(engine.currentPlayer(state),1);
|
||||
assert.equal(JSON.parse(state).events.at(-1).handler.request.Payment[0].name,'Pay to change government');
|
||||
});
|
||||
|
||||
test('free research can be queued after round three and survives entry into status phase', () => {
|
||||
const before = fixture('status_phase/free_advance');
|
||||
// Player 1 went first and has finished; player 0 is taking the last turn.
|
||||
assert.equal(JSON.parse(before).starting_player_index, 1);
|
||||
assert.equal(engine.currentPlayer(before), 0);
|
||||
const offer = view(before, 1).earlyChoices.find((o: any) => o.key.endsWith('/advance'));
|
||||
assert.ok(offer);
|
||||
assert.ok(!view(before, 0).earlyChoices.some((o: any) => o.key.endsWith('/advance')));
|
||||
const response = { SelectAdvance: 'Storage' };
|
||||
assert.ok(offer.options.some((o: any) => o.response.SelectAdvance === 'Storage'));
|
||||
const queued = queue(before, 1, offer.key, response);
|
||||
assert.deepEqual(JSON.parse(queued).players, JSON.parse(before).players);
|
||||
assert.deepEqual(view(queued, 1).earlyChoices.find((o: any) => o.key === offer.key).selected, response);
|
||||
assert.equal(JSON.parse(engine.stripSecret(queued, 0)).early_choices, undefined);
|
||||
const after = apply(queued, { Playing: 'EndTurn' }, 0);
|
||||
assert.ok(JSON.parse(after).players[1].advances.includes('Storage'));
|
||||
assert.equal(engine.currentPlayer(after), 0);
|
||||
assert.equal(JSON.parse(after).early_choices, undefined);
|
||||
});
|
||||
|
||||
test('round-three research is absent before finishing, in earlier rounds and in the final age', () => {
|
||||
for (const changes of [{ round: 2 }, { age: 6 }, { current_player_index: 1 }]) {
|
||||
const game = { ...JSON.parse(fixture('status_phase/free_advance')), ...changes };
|
||||
assert.ok(!view(JSON.stringify(game), 1).earlyChoices.some((o: any) => o.key.endsWith('/advance')));
|
||||
}
|
||||
});
|
||||
|
||||
test('an invalid pre-status research choice asks again instead of granting another advance', () => {
|
||||
let raw = fixture('status_phase/free_advance');
|
||||
const offer = view(raw, 1).earlyChoices.find((o: any) => o.key.endsWith('/advance'));
|
||||
raw = queue(raw, 1, offer.key, { SelectAdvance: 'Storage' });
|
||||
const changed = JSON.parse(raw);
|
||||
changed.players[1].advances.push('Storage');
|
||||
const after = apply(JSON.stringify(changed), { Playing: 'EndTurn' }, 0);
|
||||
assert.equal(engine.currentPlayer(after), 1);
|
||||
assert.equal(JSON.parse(after).early_choices, undefined);
|
||||
assert.equal(JSON.parse(after).players[1].advances.filter((a: string) => a === 'Storage').length, 1);
|
||||
});
|
||||
|
||||
test('previously saved status-phase research keys still resolve', () => {
|
||||
const raw = research(), game = JSON.parse(raw);
|
||||
game.early_choices = [{
|
||||
player: 0, key: `${game.age}:${game.round}:0:status:FreeAdvance/advance`,
|
||||
response: { SelectAdvance: 'Philosophy' },
|
||||
}];
|
||||
const restored = JSON.stringify(game);
|
||||
assert.deepEqual(view(restored, 0).earlyChoices.find((o: any) => o.key.endsWith('/advance')).selected,
|
||||
{ SelectAdvance: 'Philosophy' });
|
||||
const after = apply(restored, { Response: { SelectAdvance: 'Storage' } }, 1);
|
||||
assert.ok(JSON.parse(after).players[0].advances.includes('Philosophy'));
|
||||
assert.equal(JSON.parse(after).early_choices, undefined);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue