cultivar/executor/src/api.rs

32 lines
689 B
Rust
Raw Normal View History

2023-04-01 14:02:42 +00:00
use crate::SIMS;
2023-04-01 05:52:41 +00:00
use cultivar_common::prelude::*;
use sp_runtime_interface::runtime_interface;
2023-04-01 14:02:42 +00:00
#[allow(dead_code)]
2023-04-01 05:52:41 +00:00
#[runtime_interface]
pub trait Api {
2023-04-01 14:02:42 +00:00
fn say(msg: &str) {
println!("{msg}");
2023-04-01 05:52:41 +00:00
}
2023-04-01 14:02:42 +00:00
fn walk(
sim_id: SimId,
entity_id: EntityId,
direction: Direction,
) -> Result<Result<Position, (Position, WalkError)>, SimError> {
if let Some(mut sim) = SIMS.write().sims.get_mut(&sim_id) {
2023-04-01 05:52:41 +00:00
let sim = sim.value_mut();
if let Some(entity) = sim.entities.get_mut(&entity_id) {
match entity {
2023-04-01 14:02:42 +00:00
Entity::Ferris(entity) => Ok(entity.walk(&sim.board, direction)),
2023-04-01 05:52:41 +00:00
}
2023-04-01 14:02:42 +00:00
} else {
Err(SimError::EntityNotFound)
2023-04-01 05:52:41 +00:00
}
2023-04-01 14:02:42 +00:00
} else {
Err(SimError::SimNotFound)
2023-04-01 05:52:41 +00:00
}
}
2023-04-01 14:02:42 +00:00
}