aboutsummaryrefslogtreecommitdiff
path: root/src/data.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/data.rs')
-rw-r--r--src/data.rs48
1 files changed, 34 insertions, 14 deletions
diff --git a/src/data.rs b/src/data.rs
index 77a547255536..edec158d9963 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -1,18 +1,23 @@
//! Data structures for the game
+use crate::io::Io;
use async_std::sync::Arc;
+use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
- sync::atomic::{AtomicBool, AtomicU32, AtomicU16},
+ sync::atomic::{AtomicBool, AtomicU16, AtomicU32},
};
+pub type NodeId = usize;
+
/// A node is a computer on the network graph
///
/// It's owned by a player, and has some upgrade state, as well as
/// base stats.
+#[derive(Serialize, Deserialize)]
pub struct Node {
/// Each node has a unique ID by which it's addressed
- id: u16,
+ id: NodeId,
/// The current health
health: AtomicU32,
/// The max health
@@ -26,26 +31,37 @@ pub struct Node {
/// Active link states
link_states: Vec<Arc<Link>>,
/// Input buffer
+ #[serde(skip)]
buffer: Vec<Packet>,
}
+pub type LinkId = usize;
+
/// A one-to-one link between two nodes
+#[derive(Serialize, Deserialize)]
pub struct Link {
/// This link ID
- id: u16,
+ id: LinkId,
/// Node 1
- a: u16,
+ a: NodeId,
/// Node 2
- b: u16,
- length: u32,
+ b: NodeId,
+ /// The step length
+ length: usize,
/// Packets present on this link
+ #[serde(skip)]
pp: Vec<(Packet, AtomicU32)>,
+ /// Actual Rx, Tx pair
+ #[serde(skip)]
+ io: Io,
}
+pub type PacketId = usize;
+
/// A packet going across the network
pub struct Packet {
/// The packet ID
- id: u16,
+ id: PacketId,
/// Declare this packet to be removed
dead: AtomicBool,
/// Each packet is owned by a player
@@ -54,10 +70,13 @@ pub struct Packet {
data_: PacketType,
}
+pub type PlayerId = usize;
+
/// A player who's having fun
+#[derive(Serialize, Deserialize)]
pub struct Player {
/// A unique player ID (per match)
- id: u16,
+ id: PlayerId,
/// The player name
name: String,
/// Player color
@@ -67,19 +86,22 @@ pub struct Player {
}
/// Optionally, players can create teams
+#[derive(Serialize, Deserialize)]
pub struct Team {
/// Name of the team
name: String,
/// Unified color of the team
color: Color,
/// All team members by their ID
- roster: Vec<u16>
+ roster: Vec<u16>,
}
/// An RGB color without alpha
+#[derive(Serialize, Deserialize)]
pub struct Color(u8, u8, u8);
/// Describes ownership state
+#[derive(Serialize, Deserialize)]
pub enum Owner {
/// Nobody owns this
Neutral,
@@ -88,7 +110,7 @@ pub enum Owner {
}
/// Encodes upgrade level without numbers
-#[derive(Copy, Clone)]
+#[derive(Copy, Clone, Serialize, Deserialize)]
pub enum Level {
/// 1
One,
@@ -99,6 +121,7 @@ pub enum Level {
}
/// Describes upgrade state
+#[derive(Copy, Clone, Serialize, Deserialize)]
pub enum Upgrade {
/// A basic node
Base,
@@ -144,10 +167,7 @@ pub enum PacketType {
/// compute node. If the node manages to handle the packet,
/// without it getting dropped in the meantime), it yields a
/// specified reward, like a computation would.
- Payload {
- inner: Box<PacketType>,
- reward: u16
- },
+ Payload { inner: Box<PacketType>, reward: u16 },
/// A reset attack packet
///
/// When encountering a hostile node, it will make that node drop