aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-core/src/wire
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2021-02-06 19:40:53 +0100
committerKatharina Fey <kookie@spacekookie.de>2021-02-06 19:42:04 +0100
commitcf9392a33bb99ae581f818d3ddb8be1231521a02 (patch)
tree8295d8a4ed199c3263eadd8f1a508b98567a44f7 /games/rstnode/rst-core/src/wire
parent56d96b2f22bf6a61ff992b000215dc3a2c2448ad (diff)
rstnode: restructure project into workspace and sub-crates
Diffstat (limited to 'games/rstnode/rst-core/src/wire')
-rw-r--r--games/rstnode/rst-core/src/wire/action.rs33
-rw-r--r--games/rstnode/rst-core/src/wire/mod.rs68
-rw-r--r--games/rstnode/rst-core/src/wire/req.rs38
-rw-r--r--games/rstnode/rst-core/src/wire/resp.rs94
-rw-r--r--games/rstnode/rst-core/src/wire/update.rs72
5 files changed, 305 insertions, 0 deletions
diff --git a/games/rstnode/rst-core/src/wire/action.rs b/games/rstnode/rst-core/src/wire/action.rs
new file mode 100644
index 000000000000..22ab7ce6868e
--- /dev/null
+++ b/games/rstnode/rst-core/src/wire/action.rs
@@ -0,0 +1,33 @@
+use crate::data::{NodeId, Upgrade};
+use serde::{Deserialize, Serialize};
+
+/// All actions that a user can trigger via the UI
+#[derive(Serialize, Deserialize)]
+pub enum Action {
+ /// Cancel the running action
+ Cancel(NodeId),
+ /// Start a capture action
+ Capture { from: NodeId, to: NodeId },
+ /// Set the compute targets
+ Compute { from: NodeId, to: Vec<NodeId> },
+ /// Set to payload analysis mode
+ Payload(NodeId),
+ /// Send an exploit across the network
+ Reset {
+ from: NodeId,
+ to: NodeId,
+ exp: Exploit,
+ },
+ /// Try to upgrade the node to a level
+ Upgrade { node: NodeId, level: Upgrade },
+}
+
+/// A type of exploit a node can start running
+#[derive(Serialize, Deserialize)]
+pub enum Exploit {
+ Reset,
+ CNS,
+ Nitm,
+ Virus,
+ TakeOver,
+}
diff --git a/games/rstnode/rst-core/src/wire/mod.rs b/games/rstnode/rst-core/src/wire/mod.rs
new file mode 100644
index 000000000000..cd311383a83a
--- /dev/null
+++ b/games/rstnode/rst-core/src/wire/mod.rs
@@ -0,0 +1,68 @@
+//! Network formats and container messages
+
+mod action;
+pub use action::*;
+
+mod req;
+pub use req::*;
+
+mod resp;
+pub use resp::*;
+
+mod update;
+pub use update::*;
+
+use crate::{
+ data::{Color, Player},
+ map::Map,
+ Id,
+};
+use serde::{Deserialize, Serialize};
+
+/// An alias for a User's ID
+pub type UserId = usize;
+
+/// Represents a user payload
+#[derive(Copy, Clone, Serialize, Deserialize)]
+pub struct User {
+ /// The internal user ID
+ pub id: UserId,
+ /// The auth token provided by the client
+ pub token: Id,
+ /// Whether the scores will be tracked
+ pub registered: bool,
+}
+
+/// A more lobby specific abstraction for a user
+#[derive(Clone, Serialize, Deserialize)]
+pub struct LobbyUser {
+ /// The user ID
+ pub id: UserId,
+ /// Their nick name
+ pub name: String,
+ /// Are they ready?
+ pub ready: bool,
+ /// Are they the lobby admin?
+ pub admin: bool,
+ /// The colour they will be in the match
+ pub color: Color,
+}
+
+/// An alias for a Room ID
+pub type LobbyId = usize;
+
+/// Represent a lobby
+#[derive(Clone, Serialize, Deserialize)]
+pub struct Lobby {
+ /// The ID of the lobby
+ pub id: LobbyId,
+ /// A set of user IDs
+ pub players: Vec<LobbyUser>,
+ /// The name of the map
+ pub map: String,
+ /// Settings
+ pub settings: Vec<String>,
+}
+
+/// An alias for a match ID
+pub type MatchId = usize;
diff --git a/games/rstnode/rst-core/src/wire/req.rs b/games/rstnode/rst-core/src/wire/req.rs
new file mode 100644
index 000000000000..ffefcbdb6ac7
--- /dev/null
+++ b/games/rstnode/rst-core/src/wire/req.rs
@@ -0,0 +1,38 @@
+use super::{action::Action, LobbyId, MatchId, User};
+use serde::{Deserialize, Serialize};
+
+/// A message sent from the game client to the server
+#[derive(Serialize, Deserialize)]
+pub enum Request {
+ /// Register yourself with the game server
+ Register(String, String),
+
+ /// Login to your user session
+ ///
+ /// This user can't log into the system from another computer
+ Login(String, String),
+
+ /// Close your user session
+ Logout(User),
+
+ /// Start an anonymous session
+ Anonymous(String),
+
+ /// A user joins a game lobby
+ Join(User, LobbyId),
+
+ /// A user leaves a game lobby
+ Leave(User, LobbyId),
+
+ /// Mark a user as ready
+ Ready(User, LobbyId, bool),
+
+ /// Try to start the match
+ StartReq(User, LobbyId),
+
+ /// Send a move in the game
+ GameAction(User, MatchId, Action),
+
+ /// Leave the match (forfeit)
+ LeaveGame(User, MatchId),
+}
diff --git a/games/rstnode/rst-core/src/wire/resp.rs b/games/rstnode/rst-core/src/wire/resp.rs
new file mode 100644
index 000000000000..ef2e192a6044
--- /dev/null
+++ b/games/rstnode/rst-core/src/wire/resp.rs
@@ -0,0 +1,94 @@
+//! Response values that the server can reply with
+
+use super::{Lobby, LobbyId, User, UserId};
+use chrono::{DateTime, Utc};
+use serde::{Deserialize, Serialize};
+
+/// A response values from the server
+#[derive(Serialize, Deserialize)]
+pub enum Response {
+ /// Response to the register request
+ Register(Result<UserId, RegErr>),
+ /// Response to login request
+ Login(Result<User, AuthErr>),
+ /// Response to login request
+ Logout(Result<(), AuthErr>),
+ /// Get a list of available <name-room> pairs
+ Rooms(Vec<(String, LobbyId)>),
+ /// A user joins a game lobby
+ Join(Result<Lobby, LobbyErr>),
+ /// A user leaves a game lobby
+ Leave(Result<(), LobbyErr>),
+ /// Get the new set of ready states
+ Ready(LobbyUpdate),
+ /// Receiving a start request time
+ StartReq(DateTime<Utc>),
+ /// Response to the action with the state update
+ GameUpdate(UpdateState),
+ /// Leave the match (forfeit)
+ LeaveGame(Result<(), MatchErr>),
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum RegErr {
+ /// The password is way too bad
+ BadPassword,
+ /// THe username is already taken
+ UsernameTaken,
+ /// Other internal error, try again?
+ OtherError,
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum AuthErr {
+ /// Wrong password for the user
+ WrongPassword,
+ /// The requested user doesn't exist
+ UserNotFound,
+ /// No session currently exists
+ NoSossion,
+ /// Other internal error, try again?
+ OtherError,
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum LobbyErr {
+ /// The requested room is already full
+ RoomFull,
+ /// The room id is unknown
+ NoSuchRoom,
+ /// Previously not in room
+ NotInRoom,
+ /// Not everybody was ready
+ NotAllReady,
+ /// A request was sent by someone who isn't authorised
+ NotAuthorized,
+ /// Other internal error, try again?
+ OtherError,
+}
+
+#[derive(Serialize, Deserialize)]
+pub enum LobbyUpdate {
+ /// The set of ready users
+ Ready(Vec<UserId>),
+}
+
+/// The way the update was applied
+#[derive(Serialize, Deserialize)]
+pub enum UpdateState {
+ /// The update was applied seamlessly
+ Success,
+ /// The update was inserted, but had to be re-ordered with another update
+ Reordered,
+ /// The sent request was invalid and was not applied
+ Invalid,
+}
+
+/// An error that can occur in a match
+#[derive(Serialize, Deserialize)]
+pub enum MatchErr {
+ /// The provided player wasn't in the match (anymore?)
+ NotInMatch,
+ /// The requested match had already ended
+ MatchAlreadyEnded,
+}
diff --git a/games/rstnode/rst-core/src/wire/update.rs b/games/rstnode/rst-core/src/wire/update.rs
new file mode 100644
index 000000000000..a1b47ff07e50
--- /dev/null
+++ b/games/rstnode/rst-core/src/wire/update.rs
@@ -0,0 +1,72 @@
+//! Update to the game state
+
+use super::UserId;
+use crate::data::{NodeId, PacketId, Player, Upgrade};
+use serde::{Deserialize, Serialize};
+
+/// An update provided by the game server
+#[derive(Serialize, Deserialize)]
+pub enum Update {
+ /// Update made to a node
+ Node(NodeUpdate),
+ /// Update made to a link
+ Link(LinkUpdate),
+ /// Update made to a packet
+ Packet(PacketUpdate),
+ /// Update made to the user set
+ User(UserUpdate),
+ /// An error occured, can be non-fatal
+ Error(UpdateError),
+}
+
+/// Update made to a node
+#[derive(Serialize, Deserialize)]
+pub enum NodeUpdate {
+ /// The node owner changed
+ Owner(Player),
+ /// Represent a new upgrade state
+ Level { node: NodeId, new: Upgrade },
+ /// A new packet was consumed from a link
+ NewPacket(PacketId),
+ /// Remove a packet from the node buffer
+ SentPacket(PacketId),
+ /// Dropped a packet
+ DropPacket(PacketId),
+}
+
+/// Update made to a link
+#[derive(Serialize, Deserialize)]
+pub enum LinkUpdate {
+ /// Take a packet from a node's buffer
+ TakePacket(PacketId),
+ /// Give a packet to a node's buffer
+ GivePacket(PacketId),
+}
+
+/// Update made to a packet
+#[derive(Serialize, Deserialize)]
+pub enum PacketUpdate {
+ /// Advance a packet along one step along the link
+ Increment(PacketId),
+}
+
+/// Update made to the user set
+#[derive(Serialize, Deserialize)]
+pub enum UserUpdate {
+ UserLeft(UserId),
+}
+
+/// An error occured, can be non-fatal
+#[derive(Serialize, Deserialize)]
+pub enum UpdateError {
+ /// You are the last user in the match
+ LastUser,
+ /// The game crashed, so kick all
+ GameCrashed,
+ /// The server's time was behind the client time
+ ///
+ /// This means that newer events will be dropped from the map
+ /// state. This should prompt the client to warn the user this
+ /// has happened, then resync the time of the game states.
+ TimeAheadServer,
+}