Sync tile placements for all players

This commit is contained in:
2023-03-10 10:33:36 +01:00
parent 4fbe817f71
commit 126d5026cd
4 changed files with 124 additions and 14 deletions

View File

@@ -20,6 +20,8 @@ pub enum ClientMessage {
///
/// The server will validate the move and answer with a TilePlaced if the message is valid.
TileUse(Position2dRef, usize),
/// Try to place an equal sign on the board.
TilePlaceEqual(Position2dRef),
/// Try to remove a tile from the board to add it to the hand.
TileTake(Position2dRef),
/// Get the server to validate the current player moves.

View File

@@ -12,21 +12,21 @@ pub struct BoardRef {
pub height: usize,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum TileRef {
Digit(DigitRef),
Operator(OperatorRef),
Equals,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct DigitRef {
pub value: i8,
pub has_left_parenthesis: bool,
pub has_right_parenthesis: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum OperatorRef {
Add,
Subtract,
@@ -34,7 +34,7 @@ pub enum OperatorRef {
Divide,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct Position2dRef {
pub x: usize,
pub y: usize,
@@ -63,6 +63,16 @@ impl From<Tile> for TileRef {
}
}
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 {
@@ -73,6 +83,16 @@ impl From<Digit> for DigitRef {
}
}
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 {
@@ -84,6 +104,17 @@ impl From<Operator> for OperatorRef {
}
}
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 {