Merge branch 'main' into feat-set-demo-mode-at-runtime

This commit is contained in:
2025-12-31 16:51:46 -05:00
committed by GitHub
Unverified
3 changed files with 89 additions and 33 deletions

View File

@@ -14,7 +14,7 @@ pub struct Server {
}
impl Server {
pub fn new(admin_password: String, demo_mode: bool, tournament_type: String) -> Server {
pub fn new(admin_password: String, demo_mode: bool) -> Server {
Server {
clients: Arc::new(RwLock::new(HashMap::new())),
usernames: Arc::new(RwLock::new(HashMap::new())),
@@ -30,7 +30,7 @@ impl Server {
}
// Handler for CONNECT:<username>
pub async fn handle_connect(
pub async fn handle_connect_cmd(
&self,
addr: SocketAddr,
tx: UnboundedSender<Message>,
@@ -55,6 +55,8 @@ impl Server {
drop(clients_guard);
self.remove_observer_from_all_matches(addr).await;
// not taken
self.observers.write().await.remove(&addr);
self.usernames.write().await.insert(requested_username.clone(), addr);
@@ -71,6 +73,35 @@ impl Server {
Ok(())
}
pub async fn handle_disconnect_cmd(&self, addr: SocketAddr, tx: UnboundedSender<Message>) -> Result<(), anyhow::Error> {
let clients_guard = self.clients.read().await;
let client_opt = clients_guard.get(&addr).cloned();
if client_opt.is_none() {
return Err(anyhow::anyhow!("ERROR:INVALID:DISCONNECT"));
}
drop(clients_guard);
let mut client = client_opt.as_ref().unwrap().write().await;
self.usernames.write().await.remove(&client.username);
client.ready = false;
client.color = Color::None;
if client.current_match.is_some() {
let match_id = client.current_match.unwrap();
drop(client);
self.terminate_match(match_id).await;
}
self.clients.write().await.remove(&addr);
self.observers.write().await.insert(addr, tx.clone());
let _ = send(&tx, "DISCONNECT:ACK");
Ok(())
}
// Handler for READY
pub async fn handle_ready(
&self,
@@ -516,6 +547,7 @@ impl Server {
&self,
tx: UnboundedSender<Message>,
addr: SocketAddr,
tournament_type: String,
) -> Result<(), anyhow::Error> {
if !self.auth_check(addr).await {
return Err(anyhow::anyhow!("ERROR:INVALID:AUTH"));
@@ -548,7 +580,9 @@ impl Server {
let mut tournament_guard = self.tournament.write().await;
*tournament_guard = Some(Arc::new(RwLock::new(tourney)));
let _ = send(&tx, "TOURNAMENT:START:ACK");
let _ = crate::send(&tx, "TOURNAMENT:START:ACK");
self.broadcast_message_all_observers(&format!("TOURNAMENT:START:{}", tournament_type)).await;
Ok(())
}
@@ -670,6 +704,26 @@ impl Server {
Ok(())
}
pub async fn remove_observer_from_all_matches(&self, addr: SocketAddr) {
let matches_guard = self.matches.read().await;
for match_guard in matches_guard.values() {
let mut found = false;
let mut a_match = match_guard.write().await;
for i in 0..a_match.viewers.len() {
if a_match.viewers[i] == addr {
a_match.viewers.remove(i);
found = true;
break;
}
}
if found {
break;
}
}
}
pub async fn terminate_match(&self, match_id: u32) {
let matches_guard = self.matches.read().await;
let the_match = matches_guard.get(&match_id);