Skip to main content

SimulationView

Struct SimulationView 

Source
pub struct SimulationView<'a> { /* private fields */ }

Implementations§

Source§

impl SimulationView<'_>

Source

pub const fn time(&self) -> SimTime

Source

pub fn army(&self, id: ArmyId) -> Result<Option<&Army>, CanwuError>

Examples found in repository?
examples/plugin.rs (line 22)
11fn set_stance(
12    view: &SimulationView<'_>,
13    context: &CommandContext,
14    payload: &Value,
15) -> Result<Vec<SystemDirective>, CanwuError> {
16    let army = ArmyId::new(
17        payload
18            .get("army")
19            .and_then(Value::as_u64)
20            .expect("payload was validated before the handler ran"),
21    );
22    let Some(army_state) = view.army(army)? else {
23        return Err(CanwuError::new(
24            ErrorCode::ArmyNotFound,
25            format!("army {army} was not found"),
26        ));
27    };
28    if context.issuer != Issuer::Actor(army_state.commander) {
29        return Err(CanwuError::new(
30            ErrorCode::InvalidAuthority,
31            "only the army commander may set its stance",
32        ));
33    }
34    Ok(vec![SystemDirective::SetComponent {
35        state: StateKey::new("military", "stance"),
36        entity: EntityRef::Army(army),
37        component: "stance".to_owned(),
38        value: payload["stance"].clone(),
39        summary: format!("Army {army} changed stance"),
40    }])
41}
Source

pub fn person(&self, id: PersonId) -> Result<Option<&Person>, CanwuError>

Source

pub fn government( &self, id: GovernmentId, ) -> Result<Option<&Government>, CanwuError>

Source

pub fn territory( &self, id: TerritoryId, ) -> Result<Option<&Territory>, CanwuError>

Source

pub fn route(&self, id: RouteId) -> Result<Option<&Route>, CanwuError>

Source

pub fn actor_knowledge( &self, actor: PersonId, ) -> Result<Option<&ActorKnowledge>, CanwuError>

Source

pub fn command( &self, id: CommandId, ) -> Result<Option<&CommandRecord>, CanwuError>

Resolves an exact command ID from the retained runtime journal in O(1).

A command that has already been sealed into a live archive is not available through this view and returns None.

Source

pub fn event(&self, id: EventId) -> Result<Option<&SimEvent>, CanwuError>

Resolves an exact event ID from the retained runtime journal in O(1).

An event that has already been sealed into a live archive is not available through this view and returns None.

Source

pub fn ingress( &self, id: IngressId, ) -> Result<Option<&IngressRecord>, CanwuError>

Source

pub fn domain_record( &self, reference: &DomainRecordRef, ) -> Result<Option<&DomainRecord>, CanwuError>

Source

pub fn typed_domain_record<T>( &self, reference: &TypedDomainRecordRef<T>, ) -> Result<Option<&DomainRecord>, CanwuError>

Source

pub fn proposed_domain_record( &self, reference: &DomainRecordRef, ) -> Result<Option<&DomainRecord>, CanwuError>

Source

pub fn proposed_typed_domain_record<T>( &self, reference: &TypedDomainRecordRef<T>, ) -> Result<Option<&DomainRecord>, CanwuError>

Source

pub fn reservation( &self, reservation: &ReservationRef, ) -> Result<Option<&ReservationAllocation>, CanwuError>

Examples found in repository?
examples/phased_boundary.rs (line 53)
48fn apply_grain(
49    view: &SimulationView<'_>,
50    _context: &BoundaryContext,
51) -> Result<BoundaryProposal, CanwuError> {
52    let reservation = ReservationRef::new("example-demand", "request", "daily-grain");
53    let allocation = view.reservation(&reservation)?.ok_or_else(|| {
54        CanwuError::new(
55            ErrorCode::InvalidBoundary,
56            "the declared grain reservation did not produce allocation evidence",
57        )
58    })?;
59    let territory = EntityRef::Territory(TerritoryId::new(1));
60    Ok(BoundaryProposal {
61        directives: vec![
62            BoundaryDirective::SetComponent {
63                state: StateKey::new("garrison", "grain"),
64                entity: territory.clone(),
65                component: "daily_grant".to_owned(),
66                value: Value::from(allocation.granted),
67                summary: format!(
68                    "Granted {} grain to the western garrison",
69                    allocation.granted
70                ),
71            },
72            BoundaryDirective::Emit {
73                event_type: "grain_allocated".to_owned(),
74                summary: "The daily grain allocation settled".to_owned(),
75                affected: vec![territory],
76            },
77        ],
78        ..BoundaryProposal::default()
79    })
80}
Source

pub fn random_range( &self, stream: &RandomStreamKey, upper_exclusive: u64, purpose: &str, ) -> Result<u64, CanwuError>

Source

pub fn component( &self, state: &StateKey, entity: &EntityRef, component: &str, ) -> Result<Option<&Value>, CanwuError>

Source

pub fn proposed_component( &self, state: &StateKey, entity: &EntityRef, component: &str, ) -> Result<Option<&Value>, CanwuError>

Auto Trait Implementations§

§

impl<'a> !Freeze for SimulationView<'a>

§

impl<'a> !RefUnwindSafe for SimulationView<'a>

§

impl<'a> !Sync for SimulationView<'a>

§

impl<'a> Send for SimulationView<'a>

§

impl<'a> Unpin for SimulationView<'a>

§

impl<'a> UnsafeUnpin for SimulationView<'a>

§

impl<'a> UnwindSafe for SimulationView<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.