1pub mod client;
2pub mod coordinator;
3pub mod net;
4pub mod ship;
5
6use mt_net::{ActionPlan, BagMsg, Rules, VariableHuman};
7use rkyv::{
8 Archive, Deserialize, Serialize,
9 api::high::{HighSerializer, HighValidator},
10 bytecheck::CheckBytes,
11 de::Pool,
12 rancor::Strategy,
13 ser::allocator::ArenaHandle,
14 util::AlignedVec,
15};
16
17#[derive(Debug, Clone, PartialEq, Archive, Serialize, Deserialize, Hash, Eq, PartialOrd, Ord)]
18pub enum ShipKind {
19 Rat(String),
20 Wind(String),
21}
22
23pub type ShipName = i128;
24
25#[derive(Debug, Clone, Serialize, Deserialize, Archive, PartialEq, Eq)]
26pub struct NetworkShipAddress {
27 ip: [u8; 4],
28 port: u16,
29 ship: ShipName,
30 pub kind: ShipKind,
31}
32
33#[derive(Debug, Archive, Clone, Default, Serialize, Deserialize)]
34pub enum Action {
35 #[default]
36 Sail,
37 Shoot {
38 target: Vec<NetworkShipAddress>,
39 id: u32,
40 },
41 Catch {
42 source: NetworkShipAddress,
43 id: u32,
44 },
45}
46
47#[derive(Clone, Debug)]
48pub struct Variable {
49 pub ship: ShipName,
50 pub strategy: Option<Action>,
51}
52
53pub fn get_strategies(
54 haystack: &Rules,
55 rat_ship: &str,
56 variable: String,
57 indirect_parent_rat: Option<&str>,
58) -> Vec<ActionPlan> {
59 match haystack.raw().get(&variable) {
60 None => vec![ActionPlan::default()],
61 Some(plans) => {
62 let directly = plans
64 .iter()
65 .filter(|plan| plan.ship == rat_ship)
66 .filter_map(|el| el.strategy.clone())
67 .collect::<Vec<_>>();
68
69 let mut indirect = plans
71 .iter()
72 .filter(|plan| indirect_parent_rat.is_none_or(|parent_rat| plan.ship == parent_rat))
73 .filter_map(|plan| match plan.strategy.as_ref()? {
74 ActionPlan::Sail => None,
75 ActionPlan::Shoot { target, id } => target
76 .iter()
77 .find(|shoot_target| *shoot_target == rat_ship)
78 .map(|_| ActionPlan::Catch {
79 source: plan.ship.clone(),
80 id: *id,
81 }),
82 ActionPlan::Catch { source, id } => {
83 if source == rat_ship {
84 Some(ActionPlan::Shoot {
85 target: vec![source.clone()],
86 id: *id,
87 })
88 } else {
89 None
90 }
91 }
92 })
93 .collect::<Vec<_>>();
94
95 indirect.extend(directly);
96 indirect
97 }
98 }
99}
100
101#[async_trait::async_trait]
102pub trait Ship: Send + Sync + 'static {
103 async fn ask_for_action(&self, variable_name: &str) -> anyhow::Result<(Action, bool)>;
105
106 async fn wait_for_wind(&self) -> anyhow::Result<Vec<WindData>>;
109
110 fn get_cannon(&self) -> &impl Cannon;
111}
112
113#[derive(Archive, Serialize, Deserialize, Debug, Clone, Copy, Default)]
114pub enum VariableType {
115 #[default]
116 StaticOnly, U8,
118 I32,
119 F32,
120 F64,
121}
122
123impl From<u8> for VariableType {
124 fn from(value: u8) -> Self {
125 match value {
126 1 => Self::U8,
127 2 => Self::I32,
128 3 => Self::F32,
129 4 => Self::F64,
130 _ => Self::default(),
131 }
132 }
133}
134
135impl From<VariableType> for u8 {
136 fn from(value: VariableType) -> Self {
137 match value {
138 VariableType::StaticOnly => 0,
139 VariableType::U8 => 1,
140 VariableType::I32 => 2,
141 VariableType::F32 => 3,
142 VariableType::F64 => 4,
143 }
144 }
145}
146
147use rkyv::rancor::Error as RkyvError;
148
149pub trait Sendable: Sized + Send + Sync + 'static
152where
153 Self: for<'b> Serialize<HighSerializer<AlignedVec, ArenaHandle<'b>, RkyvError>>,
154{
155}
156impl<T> Sendable for T
158where
159 T: Sized + Send + Sync + 'static,
160 T: for<'b> Serialize<HighSerializer<AlignedVec, ArenaHandle<'b>, RkyvError>>,
161{
162}
163
164#[async_trait::async_trait]
165pub trait Cannon: Send + Sync + 'static {
166 async fn shoot<'b, T: Sendable>(
170 &self,
171 targets: &'b [crate::NetworkShipAddress],
172 id: u32,
173 data: &T,
174 variable_type: VariableType,
175 variable_name: &str,
176 ) -> anyhow::Result<()>;
177
178 async fn catch<T>(&self, id: u32) -> anyhow::Result<Vec<T>>
182 where
183 T: Send,
184 T: Archive,
185 T::Archived: for<'a> CheckBytes<HighValidator<'a, rkyv::rancor::Error>>
186 + Deserialize<T, Strategy<Pool, rkyv::rancor::Error>>;
187
188 async fn catch_dyn(&self, id: u32) -> anyhow::Result<Vec<(String, VariableType, String)>>;
189}
190
191#[derive(Clone, Debug, Default, Copy, Archive, Serialize, Deserialize, PartialEq)]
192pub struct TimeMsg {
193 pub sec: i32,
194 pub nanosec: u32,
195}
196
197#[derive(Clone, Debug, Default, PartialEq, Archive, Serialize, Deserialize)]
198pub struct Header {
199 pub seq: u32,
200 pub stamp: TimeMsg,
201 pub frame_id: String,
202}
203
204pub type WindData = BagMsg;
205
206#[async_trait::async_trait]
207pub trait Coordinator: Send + Sync + 'static {
208 async fn rat_action_request_queue(
209 &self,
210 ship: String,
211 ) -> anyhow::Result<tokio::sync::broadcast::Receiver<String>>;
212
213 async fn blow_wind(&self, ship: String, data: Vec<WindData>) -> anyhow::Result<()>;
214
215 async fn rat_action_send(
216 &self,
217 ship: String,
218 action: ActionPlan,
219 lock_until_ack: bool,
220 ) -> anyhow::Result<()>;
221}