From 3d42bce4134b63a29cf68224f9781ca9d7b3eb8f Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Fri, 24 Feb 2023 18:33:15 +0100 Subject: [PATCH] Delay player hand creation at the beginning of the game --- board-network/src/protocol.rs | 16 +++++++++++++++- board-server/src/room.rs | 24 ++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/board-network/src/protocol.rs b/board-network/src/protocol.rs index 07caf7d..5ce1e62 100644 --- a/board-network/src/protocol.rs +++ b/board-network/src/protocol.rs @@ -2,19 +2,32 @@ use crate::types::{Position2dRef, TileRef}; use board_shared::{position::Position2d, tile::Tile}; use serde::{Deserialize, Serialize}; +/// A message sent by the client to the server. #[derive(Debug, Deserialize, Serialize)] pub enum ClientMessage { /// Creates a new room and join it with the given player name. /// /// The server answers with a JoinedRoom message. CreateRoom(String), + /// Join an existing room with the given room name and player name. + /// + /// The server answers with a JoinedRoom message. JoinRoom(String, String), + /// Notify that the client has temporary left the game. Disconnected, + /// Start the game if the client has the permission. + StartGame, + /// 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), - TileTake(usize), + /// Try to remove a tile from the board to add it to the hand. + TileTake(#[serde(with = "Position2dRef")] Position2d), + /// Get the server to validate the current player moves. Validate, } +/// A message sent by the server to the client. #[derive(Clone, Debug, Deserialize, Serialize)] pub enum ServerMessage { /// Informs that a room has been joined. @@ -23,6 +36,7 @@ pub enum ServerMessage { players: Vec<(String, u32, bool)>, active_player: usize, }, + /// Notify that the room cannot be joined. JoinFailed(String), /// Notify that new player has joined the game. PlayerConnected(String), diff --git a/board-server/src/room.rs b/board-server/src/room.rs index 07109b9..ffbe549 100644 --- a/board-server/src/room.rs +++ b/board-server/src/room.rs @@ -21,6 +21,7 @@ pub struct Room { pub connections: HashMap, pub players: Vec, pub active_player: usize, + pub has_started: bool, pub board: Board, pub validated_board: Board, pub deck: RngDeck, @@ -51,13 +52,10 @@ impl Room { self.broadcast(ServerMessage::PlayerConnected(player_name.clone())); player_index = Some(self.players.len()); - let mut hand = Hand::default(); - hand.complete(&mut self.deck)?; - self.players.push(Player { name: player_name, score: 0, - hand, + hand: Hand::default(), ws: Some(tx.clone()), }); } @@ -100,6 +98,7 @@ impl Room { ClientMessage::CreateRoom(_) | ClientMessage::JoinRoom(_, _) => { eprintln!("[{}] Illegal client message {:?}", self.name, msg); } + ClientMessage::StartGame => self.on_start_game(), ClientMessage::TileUse(pos, tile_idx) => { if let Some(p) = self.connections.get(&addr) { if *p == self.active_player { @@ -119,6 +118,23 @@ impl Room { !self.connections.is_empty() } + fn on_start_game(&mut self) { + if self.has_started { + return; + } + + self.has_started = true; + self.deck = RngDeck::new_complete(); + + for p in &mut self.players { + p.hand + .complete(&mut self.deck) + .expect("Not enough tiles in deck"); + } + + self.broadcast(ServerMessage::PlayerTurn(self.active_player)); + } + fn on_tile_use(&mut self, pos: Position2d, tile_idx: usize) { let hand = &mut self.players[self.active_player].hand; if let Some(tile) = hand.remove(tile_idx) {