From effbdeed66e8de8e769b8ac069926ad1a9110e62 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Sun, 14 Feb 2021 00:06:14 +0100 Subject: rstnode: refactoring server and client components into rst-core * Add an inbox/ outbox system to server components * Define a data flow from Request -> computation -> Update * Create simple handlers to call server or client code for requests --- games/rstnode/rst-core/src/data.rs | 49 ++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 13 deletions(-) (limited to 'games/rstnode/rst-core/src/data.rs') 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>, + /// Router state + #[serde(skip)] + pub router: Option, /// Input buffer #[serde(skip)] - pub buffer: Vec, + pub buffer: VecDeque, } 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 { -- cgit v1.2.3