mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: remove max_connections from server config
This commit is contained in:
@@ -26,6 +26,12 @@ use crate::{
|
|||||||
settings::{general_settings::SettingsLike, server_settings::ServerSettings},
|
settings::{general_settings::SettingsLike, server_settings::ServerSettings},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const MAX_PLAYABLE_CONNECTIONS: usize = 2; // DON'T EVER CHANGE THIS AS GAME OF CHECKERS HAS ONLY TWO PLAYERS, U
|
||||||
|
// UNDERSTAND????
|
||||||
|
//
|
||||||
|
// There will be a spectator mode, hence the name __PLAYABLE__ connection. You will be able to have
|
||||||
|
// more connections, but they won't be able to play
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum ServerStage {
|
enum ServerStage {
|
||||||
Lobby,
|
Lobby,
|
||||||
@@ -143,7 +149,7 @@ impl Server {
|
|||||||
tokio::select! {
|
tokio::select! {
|
||||||
new_client_opt = self.welcoming_reciever.as_mut().expect("Welcoming reciever was not set. Was the server started?").recv() => {
|
new_client_opt = self.welcoming_reciever.as_mut().expect("Welcoming reciever was not set. Was the server started?").recv() => {
|
||||||
if let Some((addr, sender)) = new_client_opt {
|
if let Some((addr, sender)) = new_client_opt {
|
||||||
if self.state.stage == ServerStage::Lobby && self.connections.keys().len() < self.settings.max_connections && !self.settings.allow_spectators {
|
if self.state.stage == ServerStage::Lobby && self.connections.keys().len() < MAX_PLAYABLE_CONNECTIONS && !self.settings.allow_spectators {
|
||||||
let identity = NetworkServerIdentity::new(sender);
|
let identity = NetworkServerIdentity::new(sender);
|
||||||
self.connections.insert(addr, identity);
|
self.connections.insert(addr, identity);
|
||||||
self.request_handshake(addr).await;
|
self.request_handshake(addr).await;
|
||||||
@@ -216,7 +222,7 @@ impl Server {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if names.len() != self.settings.max_connections {
|
if names.len() != MAX_PLAYABLE_CONNECTIONS {
|
||||||
error!("Tried starting the game, but could not gather enough ready player names");
|
error!("Tried starting the game, but could not gather enough ready player names");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -312,11 +318,11 @@ impl Server {
|
|||||||
self.state.ready_count += 1;
|
self.state.ready_count += 1;
|
||||||
info!(
|
info!(
|
||||||
"{} signaled readiness. Currently ready: {}/{}",
|
"{} signaled readiness. Currently ready: {}/{}",
|
||||||
addr, self.state.ready_count, self.settings.max_connections
|
addr, self.state.ready_count, MAX_PLAYABLE_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; // TODO! This sometimes happens for some reason (when there is a game going, both player leave and someone tries to join)
|
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; // TODO! This sometimes happens for some reason (when there is a game going, both player leave and someone tries to join)
|
||||||
|
|
||||||
if self.state.ready_count == self.settings.max_connections {
|
if self.state.ready_count == MAX_PLAYABLE_CONNECTIONS {
|
||||||
info!("All players are ready! Changing state to ServerStage::Game");
|
info!("All players are ready! Changing state to ServerStage::Game");
|
||||||
self.state.stage = ServerStage::Game;
|
self.state.stage = ServerStage::Game;
|
||||||
self.start_game().await;
|
self.start_game().await;
|
||||||
@@ -472,7 +478,7 @@ impl Server {
|
|||||||
self.state.ready_count -= 1;
|
self.state.ready_count -= 1;
|
||||||
info!(
|
info!(
|
||||||
"Currently {}/{} players are ready",
|
"Currently {}/{} players are ready",
|
||||||
self.state.ready_count, self.settings.max_connections
|
self.state.ready_count, MAX_PLAYABLE_CONNECTIONS
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
self.connections.remove(&addr);
|
self.connections.remove(&addr);
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ use crate::settings::general_settings::{DEFAULT_URL, SettingsLike};
|
|||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct ServerSettings {
|
pub struct ServerSettings {
|
||||||
pub addr: SocketAddr,
|
pub addr: SocketAddr,
|
||||||
pub max_connections: usize,
|
|
||||||
pub allow_spectators: bool,
|
pub allow_spectators: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,8 +18,6 @@ impl Default for ServerSettings {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
addr: DEFAULT_URL,
|
addr: DEFAULT_URL,
|
||||||
max_connections: 2, // Maybe this should be a const somewhere in the server code? It
|
|
||||||
// makes no sense to have different value than this
|
|
||||||
allow_spectators: false,
|
allow_spectators: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user