Include a serde feature in the shared crate

This commit is contained in:
2024-03-23 13:36:28 +01:00
parent 3d93bf0fda
commit f1962503b8
22 changed files with 134 additions and 281 deletions

View File

@@ -1,2 +1 @@
pub mod protocol;
pub mod types;

View File

@@ -1,4 +1,6 @@
use crate::types::{BoardRef, Position2dRef, TileRef};
use board_shared::board::Board;
use board_shared::position::Position2d;
use board_shared::tile::Tile;
use serde::{Deserialize, Serialize};
/// A message sent by the client to the server.
@@ -19,11 +21,11 @@ pub enum ClientMessage {
/// Try to place a tile from the hand on the board.
///
/// The server will validate the move and answer with a TilePlaced if the message is valid.
TileUse(Position2dRef, usize),
TileUse(Position2d, usize),
/// Try to place an equal sign on the board.
TilePlaceEqual(Position2dRef),
TilePlaceEqual(Position2d),
/// Try to remove a tile from the board to add it to the hand.
TileTake(Position2dRef),
TileTake(Position2d),
/// Get the server to validate the current player moves.
Validate,
}
@@ -35,7 +37,7 @@ pub enum ServerMessage {
JoinedRoom {
room_name: String,
players: Vec<(String, u32, bool)>,
board: BoardRef,
board: Board,
active_player: usize,
has_started: bool,
},
@@ -50,13 +52,13 @@ pub enum ServerMessage {
/// Change the current player
PlayerTurn(usize),
/// Update the current hand of the player
SyncHand(Vec<TileRef>),
SyncHand(Vec<Tile>),
/// Update the score of the n-th player.
SyncScore(usize, u32),
/// Informs that a tile has been placed
TilePlaced(Position2dRef, TileRef),
TilePlaced(Position2d, Tile),
/// Informs that a tile has been removed
TileRemoved(Position2dRef),
TileRemoved(Position2d),
TurnRejected(String),
/// Notify that the game has ended.
GameOver,

View File

@@ -1,134 +0,0 @@
use board_shared::{
board::Board,
position::{Grid2d, Position2d},
tile::{Digit, Operator, Tile},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct BoardRef {
pub tiles: Vec<Option<TileRef>>,
pub width: usize,
pub height: usize,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum TileRef {
Digit(DigitRef),
Operator(OperatorRef),
Equals,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct DigitRef {
pub value: i8,
pub has_left_parenthesis: bool,
pub has_right_parenthesis: bool,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum OperatorRef {
Add,
Subtract,
Multiply,
Divide,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Position2dRef {
pub x: usize,
pub y: usize,
}
impl From<&Board> for BoardRef {
fn from(value: &Board) -> Self {
Self {
tiles: value
.iter()
.map(|tile| tile.map(Into::into))
.collect::<Vec<_>>(),
width: value.width(),
height: value.height(),
}
}
}
impl From<Tile> for TileRef {
fn from(value: Tile) -> Self {
match value {
Tile::Digit(digit) => TileRef::Digit(digit.into()),
Tile::Operator(operator) => TileRef::Operator(operator.into()),
Tile::Equals => TileRef::Equals,
}
}
}
impl From<TileRef> for Tile {
fn from(value: TileRef) -> Self {
match value {
TileRef::Digit(digit) => Tile::Digit(digit.into()),
TileRef::Operator(operator) => Tile::Operator(operator.into()),
TileRef::Equals => Tile::Equals,
}
}
}
impl From<Digit> for DigitRef {
fn from(value: Digit) -> Self {
Self {
value: value.value,
has_left_parenthesis: value.has_left_parenthesis,
has_right_parenthesis: value.has_right_parenthesis,
}
}
}
impl From<DigitRef> for Digit {
fn from(value: DigitRef) -> Self {
Self {
value: value.value,
has_left_parenthesis: value.has_left_parenthesis,
has_right_parenthesis: value.has_right_parenthesis,
}
}
}
impl From<Operator> for OperatorRef {
fn from(value: Operator) -> Self {
match value {
Operator::Add => OperatorRef::Add,
Operator::Subtract => OperatorRef::Subtract,
Operator::Multiply => OperatorRef::Multiply,
Operator::Divide => OperatorRef::Divide,
}
}
}
impl From<OperatorRef> for Operator {
fn from(value: OperatorRef) -> Self {
match value {
OperatorRef::Add => Operator::Add,
OperatorRef::Subtract => Operator::Subtract,
OperatorRef::Multiply => Operator::Multiply,
OperatorRef::Divide => Operator::Divide,
}
}
}
impl From<Position2d> for Position2dRef {
fn from(value: Position2d) -> Self {
Self {
x: value.x,
y: value.y,
}
}
}
impl From<Position2dRef> for Position2d {
fn from(value: Position2dRef) -> Self {
Self {
x: value.x,
y: value.y,
}
}
}