let user_handler own offline_users
This commit is contained in:
parent
a70050fdbd
commit
3b2862b675
3 changed files with 12 additions and 16 deletions
|
@ -46,7 +46,6 @@ impl User {
|
||||||
|
|
||||||
/// Shared state
|
/// Shared state
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub offline_users: RwLock<HashMap<String, Arc<RwLock<User>>>>,
|
|
||||||
pub online_users: RwLock<HashMap<SocketAddr, Arc<RwLock<User>>>>,
|
pub online_users: RwLock<HashMap<SocketAddr, Arc<RwLock<User>>>>,
|
||||||
pub tx_broadcast: tokio::sync::broadcast::Sender<Message>,
|
pub tx_broadcast: tokio::sync::broadcast::Sender<Message>,
|
||||||
pub tx_game_handler: mpsc::Sender<GameHandlerMessage>,
|
pub tx_game_handler: mpsc::Sender<GameHandlerMessage>,
|
||||||
|
|
|
@ -82,11 +82,9 @@ async fn main() -> Result<()> {
|
||||||
let (tx_incoming_message_handler, mut rx_incoming_message_handler) = mpsc::channel(32);
|
let (tx_incoming_message_handler, mut rx_incoming_message_handler) = mpsc::channel(32);
|
||||||
let (tx_outgoing_message_handler, mut rx_outgoing_message_handler) = mpsc::channel(32);
|
let (tx_outgoing_message_handler, mut rx_outgoing_message_handler) = mpsc::channel(32);
|
||||||
let (tx_user_handler, mut rx_user_handler) = mpsc::channel(32);
|
let (tx_user_handler, mut rx_user_handler) = mpsc::channel(32);
|
||||||
let offline_users = RwLock::new(HashMap::<String, Arc<RwLock<User>>>::new());
|
|
||||||
let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
|
let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
|
||||||
|
|
||||||
let app_state = Arc::new(AppState {
|
let app_state = Arc::new(AppState {
|
||||||
offline_users,
|
|
||||||
online_users,
|
online_users,
|
||||||
tx_broadcast: tx_broadcast.clone(),
|
tx_broadcast: tx_broadcast.clone(),
|
||||||
tx_game_handler,
|
tx_game_handler,
|
||||||
|
|
|
@ -24,6 +24,7 @@ pub struct UserHandler {
|
||||||
users_by_id: HashMap<String, Arc<RwLock<User>>>,
|
users_by_id: HashMap<String, Arc<RwLock<User>>>,
|
||||||
name_generator: NameGenerator,
|
name_generator: NameGenerator,
|
||||||
reserved_names: HashSet<String>,
|
reserved_names: HashSet<String>,
|
||||||
|
offline_users: HashMap<String, Arc<RwLock<User>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum DmUserMethod {
|
pub enum DmUserMethod {
|
||||||
|
@ -50,7 +51,9 @@ pub enum SendUserMessage {
|
||||||
impl UserHandler {
|
impl UserHandler {
|
||||||
/// Returns new UserHandler
|
/// Returns new UserHandler
|
||||||
pub fn new(state: Arc<AppState>) -> Self {
|
pub fn new(state: Arc<AppState>) -> Self {
|
||||||
|
let offline_users = HashMap::<String, Arc<RwLock<User>>>::new();
|
||||||
UserHandler {
|
UserHandler {
|
||||||
|
offline_users,
|
||||||
state,
|
state,
|
||||||
users_by_id: HashMap::<String, Arc<RwLock<User>>>::new(),
|
users_by_id: HashMap::<String, Arc<RwLock<User>>>::new(),
|
||||||
reserved_names: HashSet::<String>::new(),
|
reserved_names: HashSet::<String>::new(),
|
||||||
|
@ -225,25 +228,20 @@ impl UserHandler {
|
||||||
|
|
||||||
if let Some(user) = self.state.online_users.read().unwrap().get(&addr) {
|
if let Some(user) = self.state.online_users.read().unwrap().get(&addr) {
|
||||||
old_name = user.read().unwrap().name.clone();
|
old_name = user.read().unwrap().name.clone();
|
||||||
|
|
||||||
|
// User abandons current name by requesting a new one
|
||||||
|
self.reserved_names.remove(&old_name);
|
||||||
} else {
|
} else {
|
||||||
tracing::error!("Nonexistent user attempting login");
|
tracing::error!("Nonexistent user attempting login");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resume user's old session if they exist as offline
|
// Resume user's old session if they exist as offline
|
||||||
if self
|
if self.offline_users.contains_key(&new_name) {
|
||||||
.state
|
|
||||||
.offline_users
|
|
||||||
.read()
|
|
||||||
.unwrap()
|
|
||||||
.contains_key(&new_name)
|
|
||||||
{
|
|
||||||
let buf;
|
let buf;
|
||||||
// Copy over new tx
|
// Copy over new tx
|
||||||
if let Some(online_user) = self.state.online_users.write().unwrap().remove(&addr) {
|
if let Some(online_user) = self.state.online_users.write().unwrap().remove(&addr) {
|
||||||
if let Some(offline_user) =
|
if let Some(offline_user) = self.offline_users.remove(&new_name) {
|
||||||
self.state.offline_users.write().unwrap().remove(&new_name)
|
|
||||||
{
|
|
||||||
offline_user.write().unwrap().tx = online_user.write().unwrap().tx.clone();
|
offline_user.write().unwrap().tx = online_user.write().unwrap().tx.clone();
|
||||||
buf = offline_user;
|
buf = offline_user;
|
||||||
} else {
|
} else {
|
||||||
|
@ -292,6 +290,7 @@ impl UserHandler {
|
||||||
Addr(addr),
|
Addr(addr),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
// Reserve name
|
// Reserve name
|
||||||
self.reserved_names.insert(new_name.clone());
|
self.reserved_names.insert(new_name.clone());
|
||||||
|
@ -362,7 +361,7 @@ impl UserHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clean up after a user when they disconnect
|
/// Clean up after a user when they disconnect
|
||||||
async fn user_cleanup(&self, addr: SocketAddr) {
|
async fn user_cleanup(&mut self, addr: SocketAddr) {
|
||||||
let user_name = self
|
let user_name = self
|
||||||
.state
|
.state
|
||||||
.online_users
|
.online_users
|
||||||
|
@ -403,8 +402,8 @@ impl UserHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set user status to offline
|
/// Set user status to offline
|
||||||
fn set_user_offline(&self, user_name: String, addr: &SocketAddr) {
|
fn set_user_offline(&mut self, user_name: String, addr: &SocketAddr) {
|
||||||
self.state.offline_users.write().unwrap().insert(
|
self.offline_users.insert(
|
||||||
user_name,
|
user_name,
|
||||||
self.state
|
self.state
|
||||||
.online_users
|
.online_users
|
||||||
|
|
Loading…
Add table
Reference in a new issue