2024-08-05 01:55:05 -04:00
|
|
|
#![feature(if_let_guard)]
|
|
|
|
|
2024-08-12 15:10:48 -04:00
|
|
|
use crate::game_handler::*;
|
2024-08-04 18:14:10 -04:00
|
|
|
use anyhow::{Context, Result};
|
2024-08-08 04:07:23 -04:00
|
|
|
use axum::extract::ws::Message;
|
2024-08-09 01:21:04 -04:00
|
|
|
use game_handler::GameHandlerMessage;
|
2024-08-04 18:14:10 -04:00
|
|
|
use lib::*;
|
|
|
|
use std::{
|
2024-08-05 00:08:29 -04:00
|
|
|
collections::{HashMap, HashSet},
|
2024-08-04 18:14:10 -04:00
|
|
|
fs::{read_to_string, File},
|
|
|
|
io::{BufRead, BufReader},
|
|
|
|
net::SocketAddr,
|
|
|
|
sync::{Arc, RwLock},
|
|
|
|
};
|
2024-08-08 04:07:23 -04:00
|
|
|
use tokio::sync::mpsc::Sender;
|
2024-08-06 02:23:31 -04:00
|
|
|
use tokio::sync::{broadcast, mpsc};
|
2024-08-08 04:07:23 -04:00
|
|
|
use user_handler::UserHandlerMessage;
|
2024-08-11 19:51:30 -04:00
|
|
|
use uuid::Uuid;
|
2024-08-08 05:29:32 -04:00
|
|
|
pub mod game_handler;
|
2024-08-08 04:07:23 -04:00
|
|
|
pub mod message_handler;
|
2024-08-07 05:30:30 -04:00
|
|
|
pub mod user_handler;
|
2024-08-08 04:07:23 -04:00
|
|
|
pub mod websocket;
|
2024-08-05 21:23:28 -04:00
|
|
|
|
|
|
|
/// User
|
2024-08-08 04:07:23 -04:00
|
|
|
#[derive(Debug)]
|
2024-08-05 21:23:28 -04:00
|
|
|
pub struct User {
|
2024-08-11 19:51:30 -04:00
|
|
|
pub uuid: Uuid,
|
2024-08-05 21:23:28 -04:00
|
|
|
pub name: String,
|
2024-08-08 04:07:23 -04:00
|
|
|
pub tx: Sender<String>,
|
2024-08-05 21:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl User {
|
|
|
|
/// Create a new user object from incoming data
|
2024-08-08 04:07:23 -04:00
|
|
|
pub fn new(name: String, tx: Sender<String>) -> User {
|
2024-08-11 19:51:30 -04:00
|
|
|
User {
|
|
|
|
uuid: Uuid::now_v7(),
|
|
|
|
name,
|
|
|
|
tx,
|
|
|
|
}
|
2024-08-05 21:23:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn change_name(&mut self, new_name: String) {
|
|
|
|
self.name = new_name;
|
|
|
|
}
|
|
|
|
}
|
2024-08-04 18:14:10 -04:00
|
|
|
|
|
|
|
/// Parse json for card data
|
|
|
|
pub fn load_cards_from_json(path: &str) -> Result<(CardPacks, CardPacksMeta)> {
|
|
|
|
let data: String =
|
|
|
|
read_to_string(path).with_context(|| format!("Invalid JSON path: \"{}\"", path))?;
|
|
|
|
let jayson: Vec<CardPack> = serde_json::from_str(&data)
|
|
|
|
.with_context(|| format!("The contents of \"{path}\" is not valid JSON."))?;
|
|
|
|
|
|
|
|
let mut official: HashMap<u8, CardSet> = HashMap::new();
|
|
|
|
let mut unofficial: HashMap<u8, CardSet> = HashMap::new();
|
|
|
|
|
|
|
|
let mut official_meta: Vec<CardPackMeta> = vec![];
|
|
|
|
let mut unofficial_meta: Vec<CardPackMeta> = vec![];
|
|
|
|
|
|
|
|
for set in jayson {
|
|
|
|
let mut num_white = 0;
|
|
|
|
let mut num_black = 0;
|
|
|
|
|
|
|
|
let mut newset = CardSet {
|
|
|
|
white: Option::None,
|
|
|
|
black: Option::None,
|
|
|
|
};
|
|
|
|
|
|
|
|
// No safe default for this so make it an Option
|
|
|
|
let mut pack: Option<u8> = Option::None;
|
|
|
|
|
|
|
|
if let Some(ref white) = set.white {
|
|
|
|
num_white = white.len();
|
|
|
|
if num_white > 0 {
|
|
|
|
pack = Some(white[0].pack);
|
|
|
|
newset.white = Some(set.white.unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref black) = set.black {
|
|
|
|
num_black = black.len();
|
|
|
|
if num_black > 0 {
|
|
|
|
pack = Some(black[0].pack);
|
|
|
|
newset.black = Some(set.black.unwrap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let meta = CardPackMeta {
|
|
|
|
name: set.name,
|
|
|
|
pack: pack.expect("No card pack number!"),
|
|
|
|
num_white,
|
|
|
|
num_black,
|
|
|
|
};
|
|
|
|
|
|
|
|
if set.official {
|
|
|
|
official_meta.push(meta);
|
|
|
|
official.insert(pack.unwrap(), newset);
|
|
|
|
} else {
|
|
|
|
unofficial_meta.push(meta);
|
|
|
|
unofficial.insert(pack.unwrap(), newset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
official.shrink_to_fit();
|
|
|
|
unofficial.shrink_to_fit();
|
|
|
|
|
|
|
|
official_meta.shrink_to_fit();
|
|
|
|
unofficial_meta.shrink_to_fit();
|
|
|
|
|
|
|
|
tracing::debug!("{} official", official.len());
|
|
|
|
tracing::debug!("{} official meta", official_meta.len());
|
|
|
|
tracing::debug!("{} unofficial", unofficial.len());
|
|
|
|
tracing::debug!("{} unofficial meta", unofficial_meta.len());
|
|
|
|
tracing::debug!("{:#?}", official_meta[0]);
|
|
|
|
tracing::debug!("{:#?}", unofficial_meta[0]);
|
|
|
|
|
|
|
|
let packs = CardPacks {
|
|
|
|
official,
|
|
|
|
unofficial,
|
|
|
|
};
|
|
|
|
|
|
|
|
let packs_meta = CardPacksMeta {
|
|
|
|
official_meta,
|
|
|
|
unofficial_meta,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok((packs, packs_meta))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse name list
|
|
|
|
pub fn load_names(path: &str) -> Result<Vec<String>> {
|
|
|
|
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 {
|
2024-08-08 04:07:23 -04:00
|
|
|
pub broadcast_tx: broadcast::Sender<String>,
|
|
|
|
pub users_tx: mpsc::Sender<UserHandlerMessage>,
|
|
|
|
pub messages_tx: mpsc::Sender<(SocketAddr, Message)>,
|
2024-08-09 01:21:04 -04:00
|
|
|
pub games_tx: mpsc::Sender<GameHandlerMessage>,
|
2024-08-05 21:23:28 -04:00
|
|
|
pub first_names: Vec<String>,
|
|
|
|
pub last_names: Vec<String>,
|
2024-08-05 00:08:29 -04:00
|
|
|
pub reserved_names: RwLock<HashSet<String>>,
|
2024-08-11 19:51:30 -04:00
|
|
|
pub user_uuid: RwLock<HashMap<Uuid, Arc<RwLock<User>>>>,
|
2024-08-04 18:14:10 -04:00
|
|
|
pub online_users: RwLock<HashMap<SocketAddr, Arc<RwLock<User>>>>,
|
|
|
|
pub offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>,
|
|
|
|
pub packs: CardPacks,
|
|
|
|
pub packs_meta: CardPacksMeta,
|
2024-08-09 01:21:04 -04:00
|
|
|
pub games: RwLock<HashMap<String, Arc<RwLock<Game>>>>,
|
2024-08-04 18:14:10 -04:00
|
|
|
}
|