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 std::sync::{Arc, Mutex};
|
||||
|
||||
/// Games update
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
|
@ -58,13 +59,12 @@ pub struct NewGameRequest {
|
|||
}
|
||||
|
||||
/// New game request structure
|
||||
use std::pin::Pin;
|
||||
#[derive(Debug)]
|
||||
pub struct NewGameManifest<'user> {
|
||||
pub struct NewGameManifest {
|
||||
/// Game name
|
||||
pub name: String,
|
||||
/// Game host
|
||||
pub host: &'user Pin<Box<User>>,
|
||||
pub host: Arc<Mutex<User>>,
|
||||
}
|
||||
|
||||
/// Game join request structure
|
||||
|
|
|
@ -12,6 +12,7 @@ use futures::{SinkExt, StreamExt};
|
|||
use lib::models::*;
|
||||
use rand::seq::SliceRandom;
|
||||
use serde_json::to_string;
|
||||
use std::sync::Mutex;
|
||||
use std::{net::SocketAddr, sync::Arc};
|
||||
pub mod message_handler;
|
||||
use crate::message_handler::*;
|
||||
|
@ -72,7 +73,7 @@ async fn handle_new_user(
|
|||
addr: &SocketAddr,
|
||||
) -> Result<()> {
|
||||
// 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 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
|
||||
fn client_self_user_update(new_user: &User) -> String {
|
||||
fn client_self_user_update(new_user: &Arc<Mutex<User>>) -> String {
|
||||
to_string::<UserUpdate>(&UserUpdate {
|
||||
username: new_user.name.clone(),
|
||||
username: new_user.lock().unwrap().name.clone(),
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
@ -135,7 +136,7 @@ fn chat_meta_update(state: &Arc<AppState>) -> String {
|
|||
let mut names = vec![];
|
||||
|
||||
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 {
|
||||
|
@ -178,7 +179,7 @@ fn games_update(state: &Arc<AppState>) -> String {
|
|||
fn announce_join(state: &Arc<AppState>, addr: &SocketAddr) -> String {
|
||||
let msg = format!(
|
||||
"{} joined.",
|
||||
state.online_users.lock().unwrap().get(addr).unwrap().name
|
||||
state.online_users.lock().unwrap().get(addr).unwrap().lock().unwrap().name
|
||||
);
|
||||
|
||||
tracing::debug!("{}", &msg);
|
||||
|
|
|
@ -57,10 +57,10 @@ fn handle_new_game(
|
|||
let binding = state.online_users.lock().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 {
|
||||
name: new_game.name,
|
||||
host: user,
|
||||
host: user.clone(),
|
||||
};
|
||||
|
||||
// create game
|
||||
|
@ -90,7 +90,7 @@ fn handle_chat_message(
|
|||
tx: &Sender<String>,
|
||||
addr: SocketAddr,
|
||||
) -> 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}");
|
||||
tx.send(to_string::<ChatMessage>(&ChatMessage { text: msg })?)?;
|
||||
|
||||
|
@ -110,6 +110,8 @@ fn handle_user_log_in(
|
|||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.lock()
|
||||
.unwrap()
|
||||
.name
|
||||
.clone();
|
||||
let new_name = user_log_in.username.clone();
|
||||
|
@ -144,6 +146,8 @@ fn handle_user_log_in(
|
|||
.unwrap()
|
||||
.get_mut(&addr)
|
||||
.unwrap()
|
||||
.lock()
|
||||
.unwrap()
|
||||
.change_name(user_log_in.username);
|
||||
|
||||
let msg = format! {
|
||||
|
@ -183,7 +187,15 @@ fn handle_close(
|
|||
if let Some(cf) = close_frame {
|
||||
tracing::debug!(
|
||||
"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.reason
|
||||
)
|
||||
|
@ -194,7 +206,15 @@ fn handle_close(
|
|||
let msg = ChatMessage {
|
||||
text: format!(
|
||||
"{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()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.lock()
|
||||
.unwrap()
|
||||
.name
|
||||
.clone();
|
||||
|
||||
|
|
|
@ -8,7 +8,6 @@ use std::{
|
|||
fs::{read_to_string, File},
|
||||
io::{BufRead, BufReader},
|
||||
net::SocketAddr,
|
||||
pin::Pin,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
use tokio::sync::broadcast;
|
||||
|
@ -44,8 +43,8 @@ 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.
|
||||
online_users: Mutex<HashMap<SocketAddr, Pin<Box<User>>>>,
|
||||
offline_users: Mutex<HashMap<String, Pin<Box<User>>>>,
|
||||
online_users: Mutex<HashMap<SocketAddr, Arc<Mutex<User>>>>,
|
||||
offline_users: Mutex<HashMap<String, Arc<Mutex<User>>>>,
|
||||
// Channel used to send messages to all connected clients.
|
||||
tx: broadcast::Sender<String>,
|
||||
// Master card decks
|
||||
|
@ -71,8 +70,8 @@ async fn main() -> Result<()> {
|
|||
// Set up application state for use with with_state().
|
||||
// Main Broadcast Channel
|
||||
let (tx, _rx) = broadcast::channel(100);
|
||||
let online_users = Mutex::new(HashMap::<SocketAddr, Pin<Box<User>>>::new());
|
||||
let offline_users = Mutex::new(HashMap::<String, Pin<Box<User>>>::new());
|
||||
let online_users = Mutex::new(HashMap::<SocketAddr, Arc<Mutex<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 games = Mutex::new(vec![]);
|
||||
let first_names = load_names("data/first.txt")?;
|
||||
|
|
Loading…
Add table
Reference in a new issue