feat: add broadcasting to the server

This commit is contained in:
2026-08-17 19:11:26 +02:00
parent 1be6ece665
commit a096c990a9
2 changed files with 31 additions and 14 deletions

View File

@@ -51,6 +51,8 @@ pub enum ServerMessage {
board_view: BoardView, board_view: BoardView,
}, },
BoardState(BoardView),
AvailableCaptures { AvailableCaptures {
captures: Vec<CapturePath>, captures: Vec<CapturePath>,
}, },
@@ -83,6 +85,7 @@ pub enum ClientMessage {
TextMessage(String), TextMessage(String),
} }
#[derive(Clone)]
pub struct Message<T> pub struct Message<T>
where where
T: MessageLike, T: MessageLike,

View File

@@ -288,8 +288,6 @@ impl Server {
}; };
identity.identity.name = fixed_name identity.identity.name = fixed_name
// TODO! Validate if the name isn't already in use and append something to it if it is
} }
fn process_capture(&mut self, capture_path: CapturePath) { fn process_capture(&mut self, capture_path: CapturePath) {
@@ -317,6 +315,23 @@ impl Server {
self.send_message(addr, msg).await; self.send_message(addr, msg).await;
} }
async fn handle_readiness(&mut self, addr: SocketAddr) {
self.state.ready_count += 1;
info!(
"{} signaled readiness. Currently ready: {}/{}",
addr, self.state.ready_count, self.settings.max_connections
);
self.connections.get_mut(&addr).expect("Couldn't get the identity of the ready player. I guess this should never happen so i'm panicking like a little bitch").identity.is_ready = true;
if self.state.ready_count == self.settings.max_connections {
info!("All players are ready! Changing state to ServerStage::Game");
self.state.stage = ServerStage::Game;
self.start_game().await;
}
}
/* === Helper methods === */
async fn send_message( async fn send_message(
&mut self, &mut self,
addr: SocketAddr, addr: SocketAddr,
@@ -338,18 +353,17 @@ impl Server {
}; };
} }
async fn handle_readiness(&mut self, addr: SocketAddr) { async fn broadcast_message(&mut self, msg: Result<Message<ServerMessage>, postcard::Error>) {
self.state.ready_count += 1; match msg {
info!( Ok(msg) => {
"{} signaled readiness. Currently ready: {}/{}", for identity in self.connections.values_mut() {
addr, self.state.ready_count, self.settings.max_connections identity.sender.send(msg.clone()).await;
); }
self.connections.get_mut(&addr).expect("Couldn't get the identity of the ready player. I guess this should never happen so i'm panicking like a little bitch").identity.is_ready = true; }
Err(e) => {
if self.state.ready_count == self.settings.max_connections { error!("Error while broadcasting the message: {}", e);
info!("All players are ready! Changing state to ServerStage::Game"); return;
self.state.stage = ServerStage::Game; }
self.start_game().await;
} }
} }