make users arcs
This commit is contained in:
parent
9f43f5333a
commit
96bef78efb
4 changed files with 40 additions and 18 deletions
|
@ -1,4 +1,5 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
/// Games update
|
/// Games update
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
@ -58,13 +59,12 @@ pub struct NewGameRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// New game request structure
|
/// New game request structure
|
||||||
use std::pin::Pin;
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct NewGameManifest<'user> {
|
pub struct NewGameManifest {
|
||||||
/// Game name
|
/// Game name
|
||||||
pub name: String,
|
pub name: String,
|
||||||
/// Game host
|
/// Game host
|
||||||
pub host: &'user Pin<Box<User>>,
|
pub host: Arc<Mutex<User>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Game join request structure
|
/// Game join request structure
|
||||||
|
|
|
@ -12,6 +12,7 @@ use futures::{SinkExt, StreamExt};
|
||||||
use lib::models::*;
|
use lib::models::*;
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use serde_json::to_string;
|
use serde_json::to_string;
|
||||||
|
use std::sync::Mutex;
|
||||||
use std::{net::SocketAddr, sync::Arc};
|
use std::{net::SocketAddr, sync::Arc};
|
||||||
pub mod message_handler;
|
pub mod message_handler;
|
||||||
use crate::message_handler::*;
|
use crate::message_handler::*;
|
||||||
|
@ -72,7 +73,7 @@ async fn handle_new_user(
|
||||||
addr: &SocketAddr,
|
addr: &SocketAddr,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// Create
|
// Create
|
||||||
let new_user = Box::pin(generate_new_user(&state));
|
let new_user = Arc::new(Mutex::new(generate_new_user(&state)));
|
||||||
tracing::debug!("User created at ptr: {:p}", new_user);
|
tracing::debug!("User created at ptr: {:p}", new_user);
|
||||||
tracing::debug!("User borrowed ptr: {:p}", *&new_user);
|
tracing::debug!("User borrowed ptr: {:p}", *&new_user);
|
||||||
|
|
||||||
|
@ -122,9 +123,9 @@ fn generate_new_user(state: &Arc<AppState>) -> User {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generate message to notify client of user changes
|
/// Generate message to notify client of user changes
|
||||||
fn client_self_user_update(new_user: &User) -> String {
|
fn client_self_user_update(new_user: &Arc<Mutex<User>>) -> String {
|
||||||
to_string::<UserUpdate>(&UserUpdate {
|
to_string::<UserUpdate>(&UserUpdate {
|
||||||
username: new_user.name.clone(),
|
username: new_user.lock().unwrap().name.clone(),
|
||||||
})
|
})
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
@ -135,7 +136,7 @@ fn chat_meta_update(state: &Arc<AppState>) -> String {
|
||||||
let mut names = vec![];
|
let mut names = vec![];
|
||||||
|
|
||||||
for user in state.online_users.lock().unwrap().iter() {
|
for user in state.online_users.lock().unwrap().iter() {
|
||||||
names.push(user.1.name.clone());
|
names.push(user.1.lock().unwrap().name.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
to_string::<ChatUpdate>(&ChatUpdate {
|
to_string::<ChatUpdate>(&ChatUpdate {
|
||||||
|
@ -178,7 +179,7 @@ fn games_update(state: &Arc<AppState>) -> String {
|
||||||
fn announce_join(state: &Arc<AppState>, addr: &SocketAddr) -> String {
|
fn announce_join(state: &Arc<AppState>, addr: &SocketAddr) -> String {
|
||||||
let msg = format!(
|
let msg = format!(
|
||||||
"{} joined.",
|
"{} joined.",
|
||||||
state.online_users.lock().unwrap().get(addr).unwrap().name
|
state.online_users.lock().unwrap().get(addr).unwrap().lock().unwrap().name
|
||||||
);
|
);
|
||||||
|
|
||||||
tracing::debug!("{}", &msg);
|
tracing::debug!("{}", &msg);
|
||||||
|
|
|
@ -57,10 +57,10 @@ fn handle_new_game(
|
||||||
let binding = state.online_users.lock().unwrap();
|
let binding = state.online_users.lock().unwrap();
|
||||||
let user = binding.get(&addr).unwrap();
|
let user = binding.get(&addr).unwrap();
|
||||||
|
|
||||||
tracing::debug!("{:#?} from {}", new_game, user.name);
|
tracing::debug!("{:#?} from {}", new_game, user.lock().unwrap().name);
|
||||||
let manifest = NewGameManifest {
|
let manifest = NewGameManifest {
|
||||||
name: new_game.name,
|
name: new_game.name,
|
||||||
host: user,
|
host: user.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// create game
|
// create game
|
||||||
|
@ -90,7 +90,7 @@ fn handle_chat_message(
|
||||||
tx: &Sender<String>,
|
tx: &Sender<String>,
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let msg = format! {"{0}: {1}", state.online_users.lock().unwrap().get(&addr).unwrap().name, chat_message.text};
|
let msg = format! {"{0}: {1}", state.online_users.lock().unwrap().get(&addr).unwrap().lock().unwrap().name, chat_message.text};
|
||||||
tracing::debug!("{msg}");
|
tracing::debug!("{msg}");
|
||||||
tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg })?)?;
|
tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg })?)?;
|
||||||
|
|
||||||
|
@ -110,6 +110,8 @@ fn handle_user_log_in(
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get(&addr)
|
.get(&addr)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
.name
|
.name
|
||||||
.clone();
|
.clone();
|
||||||
let new_name = user_log_in.username.clone();
|
let new_name = user_log_in.username.clone();
|
||||||
|
@ -144,6 +146,8 @@ fn handle_user_log_in(
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get_mut(&addr)
|
.get_mut(&addr)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
.change_name(user_log_in.username);
|
.change_name(user_log_in.username);
|
||||||
|
|
||||||
let msg = format! {
|
let msg = format! {
|
||||||
|
@ -183,7 +187,15 @@ fn handle_close(
|
||||||
if let Some(cf) = close_frame {
|
if let Some(cf) = close_frame {
|
||||||
tracing::debug!(
|
tracing::debug!(
|
||||||
"Close received from {0} with code: {1} and reason: {2}",
|
"Close received from {0} with code: {1} and reason: {2}",
|
||||||
state.online_users.lock().unwrap().get(&addr).unwrap().name,
|
state
|
||||||
|
.online_users
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.get(&addr)
|
||||||
|
.unwrap()
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.name,
|
||||||
cf.code,
|
cf.code,
|
||||||
cf.reason
|
cf.reason
|
||||||
)
|
)
|
||||||
|
@ -194,7 +206,15 @@ fn handle_close(
|
||||||
let msg = ChatMessage {
|
let msg = ChatMessage {
|
||||||
text: format!(
|
text: format!(
|
||||||
"{0} left.",
|
"{0} left.",
|
||||||
state.online_users.lock().unwrap().get(&addr).unwrap().name
|
state
|
||||||
|
.online_users
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.get(&addr)
|
||||||
|
.unwrap()
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.name
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -206,6 +226,8 @@ fn handle_close(
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get(&addr)
|
.get(&addr)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
.name
|
.name
|
||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ use std::{
|
||||||
fs::{read_to_string, File},
|
fs::{read_to_string, File},
|
||||||
io::{BufRead, BufReader},
|
io::{BufRead, BufReader},
|
||||||
net::SocketAddr,
|
net::SocketAddr,
|
||||||
pin::Pin,
|
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
use tokio::sync::broadcast;
|
use tokio::sync::broadcast;
|
||||||
|
@ -44,8 +43,8 @@ fn load_names(path: &str) -> Result<Vec<String>> {
|
||||||
// Our shared state
|
// Our shared state
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
// We require unique usernames. This tracks which usernames have been taken.
|
// We require unique usernames. This tracks which usernames have been taken.
|
||||||
online_users: Mutex<HashMap<SocketAddr, Pin<Box<User>>>>,
|
online_users: Mutex<HashMap<SocketAddr, Arc<Mutex<User>>>>,
|
||||||
offline_users: Mutex<HashMap<String, Pin<Box<User>>>>,
|
offline_users: Mutex<HashMap<String, Arc<Mutex<User>>>>,
|
||||||
// Channel used to send messages to all connected clients.
|
// Channel used to send messages to all connected clients.
|
||||||
tx: broadcast::Sender<String>,
|
tx: broadcast::Sender<String>,
|
||||||
// Master card decks
|
// Master card decks
|
||||||
|
@ -71,8 +70,8 @@ async fn main() -> Result<()> {
|
||||||
// Set up application state for use with with_state().
|
// Set up application state for use with with_state().
|
||||||
// Main Broadcast Channel
|
// Main Broadcast Channel
|
||||||
let (tx, _rx) = broadcast::channel(100);
|
let (tx, _rx) = broadcast::channel(100);
|
||||||
let online_users = Mutex::new(HashMap::<SocketAddr, Pin<Box<User>>>::new());
|
let online_users = Mutex::new(HashMap::<SocketAddr, Arc<Mutex<User>>>::new());
|
||||||
let offline_users = Mutex::new(HashMap::<String, Pin<Box<User>>>::new());
|
let offline_users = Mutex::new(HashMap::<String, Arc<Mutex<User>>>::new());
|
||||||
let all_cards = load_cards_from_json("data/cah-cards-full.json")?;
|
let all_cards = load_cards_from_json("data/cah-cards-full.json")?;
|
||||||
let games = Mutex::new(vec![]);
|
let games = Mutex::new(vec![]);
|
||||||
let first_names = load_names("data/first.txt")?;
|
let first_names = load_names("data/first.txt")?;
|
||||||
|
|
Loading…
Add table
Reference in a new issue