From 533abc69936996be0cb333f730a9ed531d2d77d9 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Wed, 18 Mar 2020 22:13:03 +0100 Subject: Adding a wire layer --- src/wire/action.rs | 33 ++++++++++++++++++++ src/wire/mod.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++ src/wire/req.rs | 39 ++++++++++++++++++++++++ src/wire/resp.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/wire/update.rs | 44 +++++++++++++++++++++++++++ 5 files changed, 280 insertions(+) create mode 100644 src/wire/action.rs create mode 100644 src/wire/mod.rs create mode 100644 src/wire/req.rs create mode 100644 src/wire/resp.rs create mode 100644 src/wire/update.rs diff --git a/src/wire/action.rs b/src/wire/action.rs new file mode 100644 index 000000000000..8b25e58649ba --- /dev/null +++ b/src/wire/action.rs @@ -0,0 +1,33 @@ +use crate::data::{NodeId, Upgrade}; +use serde::{Serialize, Deserialize}; + +/// 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 }, + /// 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/src/wire/mod.rs b/src/wire/mod.rs new file mode 100644 index 000000000000..257333ee118c --- /dev/null +++ b/src/wire/mod.rs @@ -0,0 +1,75 @@ +//! Network formats and container messages + +mod action; +mod req; +mod resp; +mod update; + +use crate::{ + data::{Color, Player}, + map::Map, +}; +use serde::{Deserialize, Serialize}; + +/// An alias for a User's ID +pub type UserId = usize; + +/// Represents a user payload +#[derive(Serialize, Deserialize)] +pub struct User { + /// The internal user ID + id: UserId, + /// The auth token provided by the client + token: String, + /// Whether the scores will be tracked + registered: bool, +} + +/// A more lobby specific abstraction for a user +#[derive(Serialize, Deserialize)] +struct LobbyUser { + /// The user ID + id: UserId, + /// Their nick name + name: String, + /// Are they ready? + ready: bool, + /// The colour they will be in the match + color: Color, +} + +/// An alias for a Room ID +pub type LobbyId = usize; + +/// Represent a lobby +#[derive(Serialize, Deserialize)] +pub struct Lobby { + /// The ID of the lobby + id: LobbyId, + /// A set of user IDs + players: Vec, + /// The name of the map + map: String, + /// Settings + settings: Vec, +} + +/// An alias for a match ID +pub type MatchId = usize; + +/// Mapping users to a player in game +#[derive(Serialize, Deserialize)] +pub struct MatchUser { + user: User, + player: Player, +} + +#[derive(Serialize, Deserialize)] +pub struct Match { + /// The match id + id: MatchId, + /// The list of active players + players: Vec, + /// The active game map + map: Map, +} diff --git a/src/wire/req.rs b/src/wire/req.rs new file mode 100644 index 000000000000..fba64287006b --- /dev/null +++ b/src/wire/req.rs @@ -0,0 +1,39 @@ + +use super::{action::Action, User, LobbyId, MatchId}; +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/src/wire/resp.rs b/src/wire/resp.rs new file mode 100644 index 000000000000..b9f41fbe93e4 --- /dev/null +++ b/src/wire/resp.rs @@ -0,0 +1,89 @@ +//! 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), + /// Response to login request + Login(Result), + /// Response to login request + Logout(Result<(), AuthErr>), + /// Get a list of available pairs + Rooms(Vec<(String, LobbyId)>), + /// A user joins a game lobby + Join(Result), + /// A user leaves a game lobby + Leave(Result<(), RoomErr>), + /// Get the new set of ready states + Ready(RoomUpdate), + /// Receiving a start request time + StartReq(DateTime), + /// 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 RoomErr { + /// The requested room is already full + RoomFull, + /// The room id is unknown + NoSuchRoom, + /// Previously not in room + NotInRoom, +} + +#[derive(Serialize, Deserialize)] +pub enum RoomUpdate { + /// The set of ready users + Ready(Vec), +} + +/// 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/src/wire/update.rs b/src/wire/update.rs new file mode 100644 index 000000000000..27f2d0ddded3 --- /dev/null +++ b/src/wire/update.rs @@ -0,0 +1,44 @@ +//! Update to the game state + +use super::UserId; +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 {} + +/// Update made to a link +#[derive(Serialize, Deserialize)] +pub enum LinkUpdate {} + +/// Update made to a packet +#[derive(Serialize, Deserialize)] +pub enum PacketUpdate {} + +/// 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, +} -- cgit v1.2.3