From 7357062f92f1f7c6181228b7b97e37479869881d Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Tue, 21 Feb 2023 11:42:14 +0100 Subject: [PATCH] Add new messages --- board-network/src/lib.rs | 1 + board-network/src/protocol.rs | 23 ++++++++++++++++++++++- board-network/src/types.rs | 35 +++++++++++++++++++++++++++++++++++ board-server/README.md | 2 +- board-server/src/room.rs | 3 +++ 5 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 board-network/src/types.rs diff --git a/board-network/src/lib.rs b/board-network/src/lib.rs index 1b800ec..c087a5c 100644 --- a/board-network/src/lib.rs +++ b/board-network/src/lib.rs @@ -1 +1,2 @@ pub mod protocol; +pub mod types; diff --git a/board-network/src/protocol.rs b/board-network/src/protocol.rs index b1f1967..0d8337b 100644 --- a/board-network/src/protocol.rs +++ b/board-network/src/protocol.rs @@ -1,19 +1,40 @@ +use crate::types::{Position2dRef, TileRef}; +use board_shared::{position::Position2d, tile::Tile}; use serde::{Deserialize, Serialize}; #[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum ClientMessage { + /// Creates a new room and join it with the given player name. + /// + /// The server answers with a JoinedRoom message. CreateRoom(String), JoinRoom(String, String), Disconnected, + TileUse(#[serde(with = "Position2dRef")] Position2d, usize), + TileTake(usize), } #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)] pub enum ServerMessage { + /// Informs that a room has been joined. JoinedRoom { room_name: String, players: Vec<(String, u32, bool)>, + active_player: usize, }, + /// Notify that new player has joined the game. PlayerConnected(String), + /// Notify that new player has rejoined the game. PlayerReconnected(usize), + /// Notify that new player has temporary left the game. PlayerDisconnected(usize), -} \ No newline at end of file + /// Change the current player + PlayerTurn(usize), + /// Informs that a tile has been placed + TilePlaced( + #[serde(with = "Position2dRef")] Position2d, + #[serde(with = "TileRef")] Tile, + ), + /// Informs that a tile has been removed + TileRemoved(#[serde(with = "Position2dRef")] Position2d), +} diff --git a/board-network/src/types.rs b/board-network/src/types.rs new file mode 100644 index 0000000..7c5574a --- /dev/null +++ b/board-network/src/types.rs @@ -0,0 +1,35 @@ +use board_shared::position::Position2d; +use board_shared::tile::{Digit, Operator, Tile}; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +#[serde(remote = "Tile")] +pub enum TileRef { + Digit(#[serde(with = "DigitRef")] Digit), + Operator(#[serde(with = "OperatorRef")] Operator), + Equals, +} + +#[derive(Serialize, Deserialize)] +#[serde(remote = "Digit")] +pub struct DigitRef { + pub value: i8, + pub has_left_parenthesis: bool, + pub has_right_parenthesis: bool, +} + +#[derive(Serialize, Deserialize)] +#[serde(remote = "Operator")] +pub enum OperatorRef { + Add, + Subtract, + Multiply, + Divide, +} + +#[derive(Serialize, Deserialize)] +#[serde(remote = "Position2d")] +pub struct Position2dRef { + pub x: usize, + pub y: usize, +} diff --git a/board-server/README.md b/board-server/README.md index 224d97f..115f4f0 100644 --- a/board-server/README.md +++ b/board-server/README.md @@ -39,7 +39,7 @@ const socket = new WebSocket('ws://localhost:8080'); // Connection opened socket.addEventListener('open', (event) => { // Create a new room, and join it it immediately with the player name "player_name" - // The server will respond with a RoomCreated message which contains the room name + // The server will respond with a JoinedRoom message which contains the room name socket.send(JSON.stringify({ CreateRoom: 'player_name' })); }); diff --git a/board-server/src/room.rs b/board-server/src/room.rs index 7d981a8..a4697b1 100644 --- a/board-server/src/room.rs +++ b/board-server/src/room.rs @@ -15,6 +15,7 @@ pub struct Room { pub name: String, pub connections: HashMap, pub players: Vec, + pub active_player: usize, } impl Room { @@ -60,6 +61,7 @@ impl Room { .iter() .map(|p| (p.name.clone(), p.score, p.ws.is_some())) .collect(), + active_player: self.active_player, })?; Ok(()) @@ -71,6 +73,7 @@ impl Room { ClientMessage::CreateRoom(_) | ClientMessage::JoinRoom(_, _) => { eprintln!("[{}] Illegal client message {:?}", self.name, msg); } + _ => todo!(), } !self.connections.is_empty() }