Add new messages
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
pub mod protocol;
|
pub mod protocol;
|
||||||
|
pub mod types;
|
||||||
|
@@ -1,19 +1,40 @@
|
|||||||
|
use crate::types::{Position2dRef, TileRef};
|
||||||
|
use board_shared::{position::Position2d, tile::Tile};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
|
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||||
pub enum ClientMessage {
|
pub enum ClientMessage {
|
||||||
|
/// Creates a new room and join it with the given player name.
|
||||||
|
///
|
||||||
|
/// The server answers with a JoinedRoom message.
|
||||||
CreateRoom(String),
|
CreateRoom(String),
|
||||||
JoinRoom(String, String),
|
JoinRoom(String, String),
|
||||||
Disconnected,
|
Disconnected,
|
||||||
|
TileUse(#[serde(with = "Position2dRef")] Position2d, usize),
|
||||||
|
TileTake(usize),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||||
pub enum ServerMessage {
|
pub enum ServerMessage {
|
||||||
|
/// Informs that a room has been joined.
|
||||||
JoinedRoom {
|
JoinedRoom {
|
||||||
room_name: String,
|
room_name: String,
|
||||||
players: Vec<(String, u32, bool)>,
|
players: Vec<(String, u32, bool)>,
|
||||||
|
active_player: usize,
|
||||||
},
|
},
|
||||||
|
/// Notify that new player has joined the game.
|
||||||
PlayerConnected(String),
|
PlayerConnected(String),
|
||||||
|
/// Notify that new player has rejoined the game.
|
||||||
PlayerReconnected(usize),
|
PlayerReconnected(usize),
|
||||||
|
/// Notify that new player has temporary left the game.
|
||||||
PlayerDisconnected(usize),
|
PlayerDisconnected(usize),
|
||||||
|
/// 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),
|
||||||
}
|
}
|
35
board-network/src/types.rs
Normal file
35
board-network/src/types.rs
Normal file
@@ -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,
|
||||||
|
}
|
@@ -39,7 +39,7 @@ const socket = new WebSocket('ws://localhost:8080');
|
|||||||
// Connection opened
|
// Connection opened
|
||||||
socket.addEventListener('open', (event) => {
|
socket.addEventListener('open', (event) => {
|
||||||
// Create a new room, and join it it immediately with the player name "player_name"
|
// 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' }));
|
socket.send(JSON.stringify({ CreateRoom: 'player_name' }));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -15,6 +15,7 @@ pub struct Room {
|
|||||||
pub name: String,
|
pub name: String,
|
||||||
pub connections: HashMap<SocketAddr, usize>,
|
pub connections: HashMap<SocketAddr, usize>,
|
||||||
pub players: Vec<Player>,
|
pub players: Vec<Player>,
|
||||||
|
pub active_player: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Room {
|
impl Room {
|
||||||
@@ -60,6 +61,7 @@ impl Room {
|
|||||||
.iter()
|
.iter()
|
||||||
.map(|p| (p.name.clone(), p.score, p.ws.is_some()))
|
.map(|p| (p.name.clone(), p.score, p.ws.is_some()))
|
||||||
.collect(),
|
.collect(),
|
||||||
|
active_player: self.active_player,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -71,6 +73,7 @@ impl Room {
|
|||||||
ClientMessage::CreateRoom(_) | ClientMessage::JoinRoom(_, _) => {
|
ClientMessage::CreateRoom(_) | ClientMessage::JoinRoom(_, _) => {
|
||||||
eprintln!("[{}] Illegal client message {:?}", self.name, msg);
|
eprintln!("[{}] Illegal client message {:?}", self.name, msg);
|
||||||
}
|
}
|
||||||
|
_ => todo!(),
|
||||||
}
|
}
|
||||||
!self.connections.is_empty()
|
!self.connections.is_empty()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user