Send the complete board when joining a room

This commit is contained in:
2023-03-07 11:48:47 +01:00
parent 091aa81e70
commit 60e4713f7f
4 changed files with 100 additions and 20 deletions

View File

@@ -1,5 +1,4 @@
use crate::types::{Position2dRef, TileRef};
use board_shared::{position::Position2d, tile::Tile};
use crate::types::{BoardRef, Position2dRef, TileRef};
use serde::{Deserialize, Serialize};
/// A message sent by the client to the server.
@@ -20,9 +19,9 @@ 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(#[serde(with = "Position2dRef")] Position2d, usize),
TileUse(Position2dRef, usize),
/// Try to remove a tile from the board to add it to the hand.
TileTake(#[serde(with = "Position2dRef")] Position2d),
TileTake(Position2dRef),
/// Get the server to validate the current player moves.
Validate,
}
@@ -34,6 +33,7 @@ pub enum ServerMessage {
JoinedRoom {
room_name: String,
players: Vec<(String, u32, bool)>,
board: BoardRef,
active_player: usize,
has_started: bool,
},
@@ -48,13 +48,10 @@ pub enum ServerMessage {
/// Change the current player
PlayerTurn(usize),
/// Update the current hand of the player
SyncHand(#[serde(with = "TileRef")] Tile), // TODO: Vec<Tile>
SyncHand(Vec<TileRef>),
/// Informs that a tile has been placed
TilePlaced(
#[serde(with = "Position2dRef")] Position2d,
#[serde(with = "TileRef")] Tile,
),
TilePlaced(Position2dRef, TileRef),
/// Informs that a tile has been removed
TileRemoved(#[serde(with = "Position2dRef")] Position2d),
TileRemoved(Position2dRef),
TurnRejected(String),
}

View File

@@ -1,17 +1,25 @@
use board_shared::position::Position2d;
use board_shared::tile::{Digit, Operator, Tile};
use board_shared::{
board::Board,
position::{Grid2d, Position2d},
tile::{Digit, Operator, Tile},
};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(remote = "Tile")]
pub struct BoardRef {
pub tiles: Vec<Option<TileRef>>,
pub width: usize,
pub height: usize,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum TileRef {
Digit(#[serde(with = "DigitRef")] Digit),
Operator(#[serde(with = "OperatorRef")] Operator),
Digit(DigitRef),
Operator(OperatorRef),
Equals,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(remote = "Digit")]
pub struct DigitRef {
pub value: i8,
pub has_left_parenthesis: bool,
@@ -19,7 +27,6 @@ pub struct DigitRef {
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(remote = "Operator")]
pub enum OperatorRef {
Add,
Subtract,
@@ -28,8 +35,69 @@ pub enum OperatorRef {
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(remote = "Position2d")]
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<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<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<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,
}
}
}