186 lines
6.6 KiB
Rust
186 lines
6.6 KiB
Rust
use std::fs;
|
|
|
|
use lib_efo::{
|
|
adjudication::{
|
|
AdjudicationConfig, AdjudicationDisposition, AdjudicationLevel, AdjudicationSpec,
|
|
ChosenEffect, StatName, apply_to_target, apply_to_world, preview, preview_at_potency,
|
|
},
|
|
entity::Entity,
|
|
epoch::EpochInfo,
|
|
stats::{EntityType, ListStats, Modification, StatPair},
|
|
};
|
|
use uuid::Uuid;
|
|
|
|
fn entity(id: u128, health: isize) -> Entity {
|
|
let mut entity = Entity::default();
|
|
entity.id = Uuid::from_u128(id);
|
|
entity.entity_type = EntityType::Town;
|
|
entity.change_stat_strict(
|
|
StatPair {
|
|
stat: ListStats::Health,
|
|
value: health.into(),
|
|
},
|
|
Modification::Replace,
|
|
);
|
|
entity.update_metastats();
|
|
entity
|
|
}
|
|
|
|
fn spec(config: &AdjudicationConfig, disposition: AdjudicationDisposition) -> AdjudicationSpec {
|
|
AdjudicationSpec {
|
|
level: AdjudicationLevel::Bad,
|
|
disposition,
|
|
source: Uuid::from_u128(1),
|
|
external_result: "d20=4".into(),
|
|
note: "The master rules that the walls absorb part of the shock.".into(),
|
|
effects: vec![ChosenEffect {
|
|
stat: StatName::Health,
|
|
delta: -40,
|
|
}],
|
|
policy_id: config.policy_id.clone(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn embedded_and_external_policies_validate() {
|
|
let config = AdjudicationConfig::embedded().expect("embedded");
|
|
let directory = tempfile::tempdir().expect("tempdir");
|
|
let path = directory.path().join("adjudication.json");
|
|
fs::write(&path, serde_json::to_vec_pretty(&config).expect("encode")).expect("write");
|
|
assert_eq!(AdjudicationConfig::load(&path).expect("external"), config);
|
|
}
|
|
|
|
#[test]
|
|
fn master_effects_are_never_invented_and_disposition_controls_application() {
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
for (disposition, expected_delta) in [
|
|
(AdjudicationDisposition::Applied, -40),
|
|
(AdjudicationDisposition::Dampened, -20),
|
|
(AdjudicationDisposition::Avoided, 0),
|
|
(AdjudicationDisposition::Destroyed, 0),
|
|
] {
|
|
let target = entity(2, 100);
|
|
let report = preview(&config, &spec(&config, disposition), &target, 3).expect("preview");
|
|
assert_eq!(report.changes.len(), 1);
|
|
assert_eq!(report.changes[0].applied_delta, expected_delta);
|
|
assert_eq!(report.changes[0].after, 100 + expected_delta);
|
|
assert_eq!(report.external_result, "d20=4");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn previews_are_stable_apply_exactly_and_refuse_stale_targets() {
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
let mut target = entity(2, 100);
|
|
let report = preview(
|
|
&config,
|
|
&spec(&config, AdjudicationDisposition::Dampened),
|
|
&target,
|
|
3,
|
|
)
|
|
.expect("preview");
|
|
assert_eq!(
|
|
report,
|
|
preview(
|
|
&config,
|
|
&spec(&config, AdjudicationDisposition::Dampened),
|
|
&target,
|
|
3,
|
|
)
|
|
.expect("replay")
|
|
);
|
|
apply_to_target(&report, &mut target).expect("apply");
|
|
assert_eq!(**target.get_stats().get_field(&ListStats::Health), 80);
|
|
assert!(apply_to_target(&report, &mut target).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn explicit_effects_are_clamped_and_invalid_master_input_is_rejected() {
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
let target = entity(2, 10);
|
|
let report = preview(
|
|
&config,
|
|
&spec(&config, AdjudicationDisposition::Applied),
|
|
&target,
|
|
0,
|
|
)
|
|
.expect("preview");
|
|
assert_eq!(report.changes[0].after, 0);
|
|
|
|
let mut duplicate = spec(&config, AdjudicationDisposition::Applied);
|
|
duplicate.effects.push(duplicate.effects[0]);
|
|
assert!(preview(&config, &duplicate, &target, 0).is_err());
|
|
let mut wrong_policy = spec(&config, AdjudicationDisposition::Applied);
|
|
wrong_policy.policy_id = "other".into();
|
|
assert!(preview(&config, &wrong_policy, &target, 0).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn omitted_effects_use_progressively_dampened_policy_defaults() {
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
let target = entity(2, 100);
|
|
let expected = [
|
|
(AdjudicationLevel::Excellent, 0),
|
|
(AdjudicationLevel::Good, -5),
|
|
(AdjudicationLevel::Mid, -10),
|
|
(AdjudicationLevel::Bad, -15),
|
|
(AdjudicationLevel::Worsen, -18),
|
|
(AdjudicationLevel::Apocalypse, -20),
|
|
];
|
|
for (level, delta) in expected {
|
|
let mut ruling = spec(&config, AdjudicationDisposition::Applied);
|
|
ruling.level = level;
|
|
ruling.effects.clear();
|
|
let report = preview(&config, &ruling, &target, 0).expect("default preview");
|
|
assert!(report.effects_were_defaulted);
|
|
assert_eq!(report.changes[0].applied_delta, delta);
|
|
assert_eq!(report.changes[0].after, 100 + delta);
|
|
}
|
|
|
|
let mut explicit = spec(&config, AdjudicationDisposition::Applied);
|
|
explicit.level = AdjudicationLevel::Excellent;
|
|
let report = preview(&config, &explicit, &target, 0).expect("explicit preview");
|
|
assert!(!report.effects_were_defaulted);
|
|
assert_eq!(report.changes[0].applied_delta, -40);
|
|
}
|
|
|
|
#[test]
|
|
fn source_attenuation_persists_and_destroyed_events_stop_diffusion() {
|
|
let config = AdjudicationConfig::embedded().expect("config");
|
|
let mut event = Entity::default();
|
|
event.id = Uuid::from_u128(1);
|
|
event.entity_type = EntityType::Event;
|
|
let target = entity(2, 100);
|
|
let mut world = EpochInfo::default();
|
|
world.events.push(event);
|
|
world.towns.push(target.clone());
|
|
|
|
let mut ruling = spec(&config, AdjudicationDisposition::Applied);
|
|
ruling.effects.clear();
|
|
let first = preview(&config, &ruling, &target, 0).expect("first preview");
|
|
assert_eq!(first.source_potency_after, 7500);
|
|
let world = apply_to_world(&world, &config, &first, EntityType::Town).expect("first apply");
|
|
assert_eq!(
|
|
world.event_adjudications[&ruling.source].potency_basis_points,
|
|
7500
|
|
);
|
|
|
|
let changed_target = &world.towns[0];
|
|
let second =
|
|
preview_at_potency(&config, &ruling, changed_target, 1, 7500).expect("attenuated preview");
|
|
assert_eq!(second.changes[0].applied_delta, -11);
|
|
|
|
ruling.disposition = AdjudicationDisposition::Destroyed;
|
|
ruling.external_result = "the source is destroyed".into();
|
|
let destroyed =
|
|
preview_at_potency(&config, &ruling, changed_target, 2, 7500).expect("destroyed preview");
|
|
let world =
|
|
apply_to_world(&world, &config, &destroyed, EntityType::Town).expect("destroyed apply");
|
|
assert!(world.event_adjudications[&ruling.source].destroyed);
|
|
assert_eq!(
|
|
world.event_adjudications[&ruling.source].potency_basis_points,
|
|
0
|
|
);
|
|
assert!(lib_efo::simulation::plan_diffusion(&world, ruling.source, 100, 25, None).is_err());
|
|
}
|