Skip to content

Commit

Permalink
Merge branch 'main' into better-notation
Browse files Browse the repository at this point in the history
  • Loading branch information
TomPlanche committed Jan 31, 2025
2 parents 140990a + 60a6407 commit 80e130c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
8 changes: 3 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,16 @@ impl App {
}

let addr = self.host_ip.as_ref().unwrap().to_string();
let addr_with_port = addr.to_string();

// ping the server to see if it's up

let s = UdpSocket::bind(addr_with_port.clone());
let s = UdpSocket::bind(addr.clone());
if s.is_err() {
eprintln!("\nServer is unreachable. Make sure you entered the correct IP and port.");
self.host_ip = None;
return;
}

self.game.opponent = Some(Opponent::new(addr_with_port, other_player_color));
self.game.opponent = Some(Opponent::new(addr, other_player_color));

if !self.hosting.unwrap() {
// If we are not hosting (joining) we set the selected color as the opposite of the opposite player color
Expand Down Expand Up @@ -256,7 +254,7 @@ impl App {
let mut config = match fs::read_to_string(config_path.clone()) {
Ok(content) => content
.parse::<Value>()
.unwrap_or_else(|_| Value::Table(Default::default())),
.unwrap_or_else(|_e| Value::Table(Default::default())),
Err(_) => Value::Table(Default::default()),
};

Expand Down
7 changes: 4 additions & 3 deletions src/game_logic/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,17 @@ impl Game {
}

let piece_type_from = self.game_board.get_piece_type(from);
let piece_type_to = self.game_board.get_piece_type(to);

// Check if moving a piece
let Some(piece_type_from) = piece_type_from else {
return;
};

// We increment the consecutive_non_pawn_or_capture if the piece type is a pawn or if there is no capture
self.game_board
.increment_consecutive_non_pawn_or_capture(piece_type_from, piece_type_to);
self.game_board.increment_consecutive_non_pawn_or_capture(
piece_type_from,
self.game_board.get_piece_type(to),
);

// We check if the move is a capture and add the piece to the taken pieces
self.game_board
Expand Down
44 changes: 21 additions & 23 deletions src/server/game_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ impl GameServer {

pub fn run(&self) {
let listener = TcpListener::bind("0.0.0.0:2308").expect("Failed to create listener");
listener
.set_nonblocking(true)
.expect("Failed to set listener to non-blocking");

// listener
// .set_nonblocking(true)
// .expect("Failed to set listener to non-blocking");

let state = self.clients.clone();
let stop_signal = self.stop_signal.clone();
Expand Down Expand Up @@ -89,7 +90,7 @@ impl GameServer {
}
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
// No connection ready, sleep briefly
thread::sleep(std::time::Duration::from_millis(100));
// thread::sleep(std::time::Duration::from_millis(100));
}
Err(e) => {
eprintln!("Failed to accept connection: {}", e);
Expand All @@ -104,27 +105,24 @@ fn handle_client(
stop_signal: Arc<AtomicBool>,
mut stream: TcpStream,
) {
let addr = stream.peer_addr().unwrap().to_string();

loop {
let mut buffer = [0; 5];
let addr = stream.peer_addr().unwrap().to_string();
let bytes_read = stream.read(&mut buffer).unwrap_or(0);

if bytes_read == 0 {
broadcast_message(state.clone(), "ended".to_string(), &addr);
remove_client(&state, &addr);
// we stop the server if one of the clients disconnects
stop_signal.store(true, Ordering::SeqCst);
break;
}

let request = String::from_utf8_lossy(&buffer[..]);
broadcast_message(state.clone(), format!("{}", request), &addr);

if request.trim() == "ended" {
remove_client(&state, &addr);
// We stop the server if one of the clients disconnects
stop_signal.store(true, Ordering::SeqCst);
break;
match stream.read(&mut buffer) {
Ok(0) => {
broadcast_message(state.clone(), "ended".to_string(), &addr);
remove_client(&state, &addr);
stop_signal.store(true, Ordering::SeqCst);
break;
}
Ok(bytes_read) => {
let request = String::from_utf8_lossy(&buffer[..bytes_read]);
broadcast_message(state.clone(), format!("{}", request), &addr);
}
Err(_e) => {
break;
}
}
}
}
Expand Down

0 comments on commit 80e130c

Please sign in to comment.