#![feature(if_let_guard)] use crate::game_handler::*; use anyhow::{Context, Result}; use axum::extract::ws::Message; use game_handler::*; use lib::*; use std::{ collections::{HashMap, HashSet}, fs::File, io::{BufRead, BufReader}, net::SocketAddr, sync::{Arc, RwLock}, }; use tokio::sync::mpsc::Sender; use tokio::sync::{broadcast, mpsc}; use user_handler::*; use uuid::Uuid; pub mod game_handler; pub mod message_handler; pub mod user_handler; pub mod websocket; /// User #[derive(Debug)] pub struct User { pub uuid: Uuid, pub name: String, pub tx: Sender, } impl User { /// Create a new user object from incoming data pub fn new(name: String, tx: Sender) -> User { User { uuid: Uuid::now_v7(), name, tx, } } pub fn change_name(&mut self, new_name: String) { self.name = new_name; } } /// Parse name list pub fn load_names(path: &str) -> Result> { let f = File::open(path).with_context(|| format!("Invalid names path: \"{}\"", path))?; let f = BufReader::new(f); let mut buf = vec![]; for line in f.lines() { buf.push(line?) } Ok(buf) } // Our shared state pub struct AppState { pub broadcast_tx: broadcast::Sender, pub users_tx: mpsc::Sender, pub messages_tx: mpsc::Sender<(SocketAddr, Message)>, pub games_tx: mpsc::Sender, pub first_names: Vec, pub last_names: Vec, pub reserved_names: RwLock>, pub users_by_id: RwLock>>>, pub online_users: RwLock>>>, pub offline_users: RwLock>>>, pub packs: CardPacks, pub packs_meta: CardPacksMeta, pub games: RwLock>>>, }