mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-28 11:10:44 +01:00
feat: add broadcasting current turn and board view
This commit is contained in:
@@ -2,6 +2,7 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
|
||||
use log::{debug, error, info, warn};
|
||||
use tokio::sync::mpsc::{self, Receiver, Sender};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::{
|
||||
logic::{
|
||||
@@ -42,10 +43,15 @@ pub enum ClientData {
|
||||
identity: NetworkIdentity,
|
||||
board_view: BoardView,
|
||||
},
|
||||
GameEnd(GameResult),
|
||||
|
||||
AvailableCaptures(Vec<CapturePath>),
|
||||
AvailableMoves(Vec<MovePath>),
|
||||
|
||||
BoardView(BoardView),
|
||||
CurrentTurn(Uuid),
|
||||
|
||||
TextMessage(String),
|
||||
GameEnd(GameResult),
|
||||
}
|
||||
|
||||
pub struct Client {
|
||||
@@ -169,8 +175,12 @@ impl Client {
|
||||
|
||||
ServerMessage::AvailableCaptures { captures } => todo!(),
|
||||
ServerMessage::AvailableMoves { moves } => todo!(),
|
||||
ServerMessage::BroadcastBoardState { board } => todo!(),
|
||||
ServerMessage::BroadcastCurrentTurn { active_player } => todo!(),
|
||||
ServerMessage::BroadcastBoardState { board } => {
|
||||
let _ = update_sender.send(ClientData::BoardView(board)).await;
|
||||
},
|
||||
ServerMessage::BroadcastCurrentTurn { active_player } => {
|
||||
let _ = update_sender.send(ClientData::CurrentTurn(active_player)).await;
|
||||
},
|
||||
ServerMessage::GameEnd { result } => todo!(),
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -51,8 +51,6 @@ pub enum ServerMessage {
|
||||
board_view: BoardView,
|
||||
},
|
||||
|
||||
BoardState(BoardView),
|
||||
|
||||
AvailableCaptures {
|
||||
captures: Vec<CapturePath>,
|
||||
},
|
||||
@@ -61,8 +59,9 @@ pub enum ServerMessage {
|
||||
},
|
||||
|
||||
BroadcastBoardState {
|
||||
board: Board,
|
||||
board: BoardView,
|
||||
},
|
||||
|
||||
BroadcastCurrentTurn {
|
||||
active_player: Uuid,
|
||||
},
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
pub mod client;
|
||||
pub mod connection;
|
||||
pub mod message;
|
||||
mod network_identity;
|
||||
pub mod network_identity;
|
||||
pub mod server;
|
||||
|
||||
@@ -257,6 +257,7 @@ impl Server {
|
||||
}
|
||||
|
||||
self.game_master = Some(game_master);
|
||||
self.on_player_action().await;
|
||||
}
|
||||
|
||||
/* ======= Processing messages :D helper functions ======== */
|
||||
@@ -290,20 +291,22 @@ impl Server {
|
||||
identity.identity.name = fixed_name
|
||||
}
|
||||
|
||||
fn process_capture(&mut self, capture_path: CapturePath) {
|
||||
async fn process_capture(&mut self, capture_path: CapturePath) {
|
||||
if let Some(gm) = self.game_master.as_mut() {
|
||||
if gm.capture(&capture_path) {
|
||||
debug!("Capture valid!");
|
||||
self.on_player_action().await;
|
||||
} else {
|
||||
debug!("Invalid capture!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn process_move(&mut self, from: Position, to: Position) {
|
||||
async fn process_move(&mut self, from: Position, to: Position) {
|
||||
if let Some(gm) = self.game_master.as_mut() {
|
||||
if gm.move_pawn(from, to) {
|
||||
debug!("Move was valid!");
|
||||
self.on_player_action().await;
|
||||
} else {
|
||||
debug!("Move was invalid!");
|
||||
}
|
||||
@@ -330,6 +333,37 @@ impl Server {
|
||||
}
|
||||
}
|
||||
|
||||
/* === Sending messages === */
|
||||
|
||||
/// Wrapper around broadcast_current_turn and broadcast_board_view. Should be used after every
|
||||
/// players move
|
||||
async fn on_player_action(&mut self) {
|
||||
self.broadcast_board_view().await;
|
||||
self.broadcast_current_turn().await;
|
||||
}
|
||||
|
||||
async fn broadcast_board_view(&mut self) {
|
||||
if let Some(gm) = &self.game_master {
|
||||
let msg = Message::new(ServerMessage::BroadcastBoardState {
|
||||
board: gm.get_board_view(),
|
||||
});
|
||||
self.broadcast_message(msg).await;
|
||||
} else {
|
||||
error!("Cannot broadcast board view as game manager is not set!");
|
||||
}
|
||||
}
|
||||
|
||||
async fn broadcast_current_turn(&mut self) {
|
||||
if let Some(gm) = &self.game_master {
|
||||
let msg = Message::new(ServerMessage::BroadcastCurrentTurn {
|
||||
active_player: gm.get_current_turn(),
|
||||
});
|
||||
self.broadcast_message(msg).await;
|
||||
} else {
|
||||
error!("Cannot broadcast current turn as game master doesn't exists!!!!!!!!!");
|
||||
}
|
||||
}
|
||||
|
||||
/* === Helper methods === */
|
||||
|
||||
async fn send_message(
|
||||
|
||||
Reference in New Issue
Block a user