use std::{collections::HashMap, net::SocketAddr, sync::Arc}; use rand::RngExt; use tokio::sync::{ mpsc::{error::SendError, UnboundedSender}, RwLock, }; use tokio_tungstenite::tungstenite::Message; use tracing::error; use crate::{ tournaments::Tournament, types::{Client, Color, Match}, }; pub mod server; pub mod tournaments; pub mod types; pub type Clients = Arc>>>>; pub type Usernames = Arc>>; pub type Observers = Arc>>>; pub type Matches = Arc>>>>; pub type Reservations = Arc>>; pub type WrappedTournament = Arc>>>>; pub const SERVER_PLAYER_USERNAME: &str = "The Server"; // pub const SERVER_PLAYER_ADDR: &str = "127.0.0.1:6666"; pub async fn broadcast_message(observers: &Observers, msg: &str) { let observer_guard = observers.read().await; for tx in observer_guard.values() { let _ = send(tx, msg); } } pub async fn gen_match_id(matches: &Matches) -> u32 { let matches_guard = matches.read().await; let mut result = rand::rng().random_range(100000..=999999); while matches_guard.get(&result).is_some() { result = rand::rng().random_range(100000..=999999); } result } pub fn random_move(board: &[Vec]) -> usize { let mut random = rand::rng().random_range(0..7); while board[random][5] != Color::None { random = rand::rng().random_range(0..7); } random } pub fn send(tx: &UnboundedSender, text: &str) -> Result<(), SendError> { tx.send(Message::text(text)) }