aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-core/src/data.rs
diff options
context:
space:
mode:
Diffstat (limited to 'games/rstnode/rst-core/src/data.rs')
-rw-r--r--games/rstnode/rst-core/src/data.rs49
1 files changed, 36 insertions, 13 deletions
diff --git a/games/rstnode/rst-core/src/data.rs b/games/rstnode/rst-core/src/data.rs
index d0494fdef3e2..44d4e2a1a456 100644
--- a/games/rstnode/rst-core/src/data.rs
+++ b/games/rstnode/rst-core/src/data.rs
@@ -4,10 +4,13 @@
use crate::io::Io;
use async_std::sync::Arc;
-use rand::seq::SliceRandom;
-use rand::thread_rng;
+use rand::{seq::SliceRandom, thread_rng};
+use ratman::Router;
use serde::{Deserialize, Serialize};
-use std::sync::atomic::{AtomicBool, AtomicU16, AtomicU32};
+use std::{
+ collections::VecDeque,
+ sync::atomic::{AtomicBool, AtomicU16, AtomicU32, Ordering},
+};
pub type NodeId = usize;
@@ -31,9 +34,12 @@ pub struct Node {
pub links: u8,
/// Active link states
pub link_states: Vec<Arc<Link>>,
+ /// Router state
+ #[serde(skip)]
+ pub router: Option<Router>,
/// Input buffer
#[serde(skip)]
- pub buffer: Vec<Packet>,
+ pub buffer: VecDeque<Packet>,
}
pub type LinkId = usize;
@@ -42,19 +48,19 @@ pub type LinkId = usize;
#[derive(Serialize, Deserialize)]
pub struct Link {
/// This link ID
- id: LinkId,
+ pub id: LinkId,
/// Node 1
- a: NodeId,
+ pub a: NodeId,
/// Node 2
- b: NodeId,
+ pub b: NodeId,
/// The step length
- length: usize,
+ pub length: usize,
/// Packets present on this link
#[serde(skip)]
- pp: Vec<(Packet, AtomicU32)>,
+ pub pp: Vec<(Packet, AtomicU32)>,
/// Actual Rx, Tx pair
#[serde(skip)]
- io: Io,
+ pub io: Io,
}
pub type PacketId = usize;
@@ -86,8 +92,27 @@ pub struct Player {
pub money: AtomicU16,
}
+// This is required because atomics can't safely be cloned
+impl Clone for Player {
+ fn clone(&self) -> Self {
+ let Self {
+ ref id,
+ ref name,
+ ref color,
+ ref money,
+ } = self;
+
+ Self {
+ id: id.clone(),
+ name: name.clone(),
+ color: color.clone(),
+ money: money.load(Ordering::Relaxed).into(),
+ }
+ }
+}
+
/// Optionally, players can create teams
-#[derive(Serialize, Deserialize)]
+#[derive(Clone, Serialize, Deserialize)]
pub struct Team {
/// Name of the team
name: String,
@@ -189,8 +214,6 @@ pub enum Owner {
Player(Player),
}
-
-
/// Encodes upgrade level without numbers
#[derive(Copy, Clone, Serialize, Deserialize)]
pub enum Level {