diff options
author | Katharina Fey <kookie@spacekookie.de> | 2020-03-18 22:12:21 +0100 |
---|---|---|
committer | Katharina Fey <kookie@spacekookie.de> | 2020-03-18 22:12:21 +0100 |
commit | e458ac4c5293762f0afcfa8c2649995ce1b3ba09 (patch) | |
tree | 47c553e91a31da8014876bbfe4af1252869a9b67 /src | |
parent | eb86eb246e5a98867d0d8766a236d73b44bb46c7 (diff) |
Making updates to the serialisability of data
Diffstat (limited to 'src')
-rw-r--r-- | src/data.rs | 48 | ||||
-rw-r--r-- | src/main.rs | 30 | ||||
-rw-r--r-- | src/map.rs | 9 | ||||
-rw-r--r-- | src/stats.rs | 8 |
4 files changed, 72 insertions, 23 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 diff --git a/src/main.rs b/src/main.rs index 1b6a5773d25d..647fec9b18b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,35 @@ mod data; mod gens; +mod io; mod map; mod stats; +mod wire; -fn main() { - println!("Hello, world!"); +use ggez::{ + self, conf, + event::{self, EventHandler}, + Context, ContextBuilder, GameResult, +}; + +struct GameState; + +impl EventHandler for GameState { + fn update(&mut self, _: &mut Context) -> GameResult { + Ok(()) + } + + fn draw(&mut self, _: &mut Context) -> GameResult { + Ok(()) + } +} + +fn main() -> GameResult { + let cb = ContextBuilder::new("RstNode", "Katharina Fey") + .window_setup(conf::WindowSetup::default().title("RstNode").vsync(true)) + .window_mode(conf::WindowMode::default().dimensions(800.0, 600.0)); + + let (ctx, el) = &mut cb.build()?; + let mut game = GameState; // smash the state + event::run(ctx, el, &mut game) } diff --git a/src/map.rs b/src/map.rs index 31bc9a3dbe38..dd1fe1c6347e 100644 --- a/src/map.rs +++ b/src/map.rs @@ -1,9 +1,9 @@ //! Implements a map graph and world logic -use crate::data::{Node, Link}; -use std::collections::BTreeMap; +use crate::data::{Link, Node}; use async_std::sync::Arc; - +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; /// A map that people fight on /// @@ -11,9 +11,12 @@ use async_std::sync::Arc; /// the map nodes are placed, how much spacing there is, etc. All /// this information is encoded in the same structs because it's /// static, and just more convenient. +#[derive(Serialize, Deserialize)] pub struct Map { /// Node IDs mapped to node objects nodes: BTreeMap<u16, Arc<Node>>, /// Link IDs mapped to link objects links: BTreeMap<u16, Arc<Link>>, } + +impl Map {} diff --git a/src/stats.rs b/src/stats.rs index 99340f2989dc..1c651a98a0a8 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -5,7 +5,7 @@ //! Whenever there is some effect that can happen in the game, it's //! value (strength) will be derived from here. -pub type Money = u16; +pub type Money = usize; /// The cost of doing business pub mod costs { @@ -140,9 +140,9 @@ pub mod gains { // reference. If it made it to "max", it will then also // return an amount of money that can be gained (Cc(ref lvl), Compute { ref max, .. }) if incr_compute(lvl, packet) => match lvl { - One => *max, - Two => max * 2, - Three => max * 3, + One => *max as usize, + Two => (max * 2) as usize, + Three => (max * 3) as usize, }, // If in doubt, nada! |