cleanup -- don't break the spaghetti edition
This commit is contained in:
parent
5237b81cab
commit
9f38eafcb1
5 changed files with 40 additions and 48 deletions
|
@ -3,6 +3,7 @@
|
|||
use anyhow::{Context, Result};
|
||||
use lib::*;
|
||||
use rand::prelude::IteratorRandom;
|
||||
use rand::prelude::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
|
@ -14,9 +15,29 @@ use std::{
|
|||
};
|
||||
use tokio::sync::broadcast;
|
||||
pub mod websocket;
|
||||
pub mod meta;
|
||||
pub mod user;
|
||||
use crate::user::*;
|
||||
|
||||
/// User
|
||||
#[derive(Default, Debug, Eq, PartialEq, Hash)]
|
||||
pub struct User {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl User {
|
||||
/// Create a new user object from incoming data
|
||||
pub fn new(state: &Arc<AppState>) -> User {
|
||||
User {
|
||||
name: format!(
|
||||
"{} {}",
|
||||
state.first_names.choose(&mut rand::thread_rng()).unwrap(),
|
||||
state.last_names.choose(&mut rand::thread_rng()).unwrap(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn change_name(&mut self, new_name: String) {
|
||||
self.name = new_name;
|
||||
}
|
||||
}
|
||||
|
||||
/// Card Set
|
||||
#[derive(Debug)]
|
||||
|
@ -332,18 +353,13 @@ pub fn load_names(path: &str) -> Result<Vec<String>> {
|
|||
|
||||
// Our shared state
|
||||
pub struct AppState {
|
||||
// We require unique usernames. This tracks which usernames have been taken.
|
||||
pub tx: broadcast::Sender<String>,
|
||||
pub first_names: Vec<String>,
|
||||
pub last_names: Vec<String>,
|
||||
pub reserved_names: RwLock<HashSet<String>>,
|
||||
pub online_users: RwLock<HashMap<SocketAddr, Arc<RwLock<User>>>>,
|
||||
pub offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>,
|
||||
// Channel used to send messages to all connected clients.
|
||||
pub tx: broadcast::Sender<String>,
|
||||
// Master card decks
|
||||
pub packs: CardPacks,
|
||||
pub packs_meta: CardPacksMeta,
|
||||
// Games list
|
||||
pub games: RwLock<Vec<Game>>,
|
||||
// chatrooms: Mutex<HashMap<String, ChatRoom<'user>>>,
|
||||
pub first_names: Vec<String>,
|
||||
pub last_names: Vec<String>,
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::websocket::*;
|
||||
use anyhow::{Context, Result};
|
||||
use axum::{routing::get, Router};
|
||||
use server::user::*;
|
||||
use server::*;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
|
@ -23,30 +22,29 @@ async fn main() -> Result<()> {
|
|||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
// Set up application state for use with with_state().
|
||||
// Main Broadcast Channel
|
||||
// Set up state
|
||||
let (tx, _rx) = broadcast::channel(100);
|
||||
let first_names = load_names("data/first.txt")?;
|
||||
let last_names = load_names("data/last.txt")?;
|
||||
let reserved_names = RwLock::new(HashSet::<String>::new());
|
||||
let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
|
||||
let offline_users = RwLock::new(HashMap::<String, Arc<RwLock<User>>>::new());
|
||||
let (packs, packs_meta) = load_cards_from_json("data/cah-cards-full.json")?;
|
||||
let games = RwLock::new(vec![]);
|
||||
let first_names = load_names("data/first.txt")?;
|
||||
let last_names = load_names("data/last.txt")?;
|
||||
|
||||
let app_state = Arc::new(AppState {
|
||||
tx,
|
||||
first_names,
|
||||
last_names,
|
||||
reserved_names,
|
||||
online_users,
|
||||
offline_users,
|
||||
tx,
|
||||
packs,
|
||||
packs_meta,
|
||||
games,
|
||||
first_names,
|
||||
last_names,
|
||||
});
|
||||
|
||||
// set routes and apply state
|
||||
// Router
|
||||
let app = Router::new()
|
||||
.route("/websocket", get(websocket_connection_handler))
|
||||
.nest_service("/", ServeDir::new("dist"))
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::meta::*;
|
||||
use crate::user::*;
|
||||
use crate::websocket::meta::*;
|
||||
use crate::websocket::user::*;
|
||||
use crate::AppState;
|
||||
use crate::Game;
|
||||
use crate::NewGameManifest;
|
||||
|
@ -17,6 +17,8 @@ use lib::*;
|
|||
use serde_json::{from_str, to_string};
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
use tokio::sync::broadcast::Sender;
|
||||
pub mod meta;
|
||||
pub mod user;
|
||||
|
||||
/// Establish the WebSocket connection
|
||||
pub async fn websocket_connection_handler(
|
||||
|
|
|
@ -1,39 +1,16 @@
|
|||
use crate::meta::*;
|
||||
use crate::websocket::meta::*;
|
||||
use crate::AppState;
|
||||
use crate::User;
|
||||
use anyhow::Result;
|
||||
use axum::extract::ws::{Message, WebSocket};
|
||||
use futures::stream::SplitSink;
|
||||
use futures::SinkExt;
|
||||
use lib::*;
|
||||
use rand::prelude::SliceRandom;
|
||||
use serde_json::to_string;
|
||||
use std::net::SocketAddr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::RwLock;
|
||||
|
||||
/// User
|
||||
#[derive(Default, Debug, Eq, PartialEq, Hash)]
|
||||
pub struct User {
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl User {
|
||||
/// Create a new user object from incoming data
|
||||
pub fn new(state: &Arc<AppState>) -> User {
|
||||
User {
|
||||
name: format!(
|
||||
"{} {}",
|
||||
state.first_names.choose(&mut rand::thread_rng()).unwrap(),
|
||||
state.last_names.choose(&mut rand::thread_rng()).unwrap(),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn change_name(&mut self, new_name: String) {
|
||||
self.name = new_name;
|
||||
}
|
||||
}
|
||||
|
||||
/// Generate message to notify client of user changes
|
||||
pub fn user_client_self_update(new_user: &Arc<RwLock<User>>) -> String {
|
||||
to_string::<UserUpdate>(&UserUpdate {
|
||||
|
@ -81,4 +58,3 @@ pub async fn user_handle_new(
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue