Adding a wire layer

wip/yesman
Katharina Fey 4 years ago
parent 389b481607
commit 533abc6993
Signed by: kookie
GPG Key ID: F972AEEA2887D547
  1. 33
      src/wire/action.rs
  2. 75
      src/wire/mod.rs
  3. 39
      src/wire/req.rs
  4. 89
      src/wire/resp.rs
  5. 44
      src/wire/update.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<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,
}

@ -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<LobbyUser>,
/// The name of the map
map: String,
/// Settings
settings: Vec<String>,
}
/// 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<MatchUser>,
/// The active game map
map: Map,
}

@ -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),
}

@ -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<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, RoomErr>),
/// 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<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 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<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
}

@ -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,
}
Loading…
Cancel
Save