Allow joining an existing room and validating tile placement

This commit is contained in:
2023-02-23 21:00:00 +01:00
parent f1564ca6e3
commit ec6542aa52
7 changed files with 149 additions and 10 deletions

View File

@@ -2,7 +2,7 @@ use crate::types::{Position2dRef, TileRef};
use board_shared::{position::Position2d, tile::Tile};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive(Debug, Deserialize, Serialize)]
pub enum ClientMessage {
/// Creates a new room and join it with the given player name.
///
@@ -12,9 +12,10 @@ pub enum ClientMessage {
Disconnected,
TileUse(#[serde(with = "Position2dRef")] Position2d, usize),
TileTake(usize),
Validate,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum ServerMessage {
/// Informs that a room has been joined.
JoinedRoom {
@@ -22,6 +23,7 @@ pub enum ServerMessage {
players: Vec<(String, u32, bool)>,
active_player: usize,
},
JoinFailed(String),
/// Notify that new player has joined the game.
PlayerConnected(String),
/// Notify that new player has rejoined the game.
@@ -30,6 +32,8 @@ pub enum ServerMessage {
PlayerDisconnected(usize),
/// Change the current player
PlayerTurn(usize),
/// Update the current hand of the player
SyncHand(#[serde(with = "TileRef")] Tile), // TODO: Vec<Tile>
/// Informs that a tile has been placed
TilePlaced(
#[serde(with = "Position2dRef")] Position2d,
@@ -37,4 +41,5 @@ pub enum ServerMessage {
),
/// Informs that a tile has been removed
TileRemoved(#[serde(with = "Position2dRef")] Position2d),
TurnRejected(String),
}

View File

@@ -2,7 +2,7 @@ use board_shared::position::Position2d;
use board_shared::tile::{Digit, Operator, Tile};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(remote = "Tile")]
pub enum TileRef {
Digit(#[serde(with = "DigitRef")] Digit),
@@ -10,7 +10,7 @@ pub enum TileRef {
Equals,
}
#[derive(Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(remote = "Digit")]
pub struct DigitRef {
pub value: i8,
@@ -18,7 +18,7 @@ pub struct DigitRef {
pub has_right_parenthesis: bool,
}
#[derive(Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(remote = "Operator")]
pub enum OperatorRef {
Add,
@@ -27,7 +27,7 @@ pub enum OperatorRef {
Divide,
}
#[derive(Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(remote = "Position2d")]
pub struct Position2dRef {
pub x: usize,