Compare commits

...

2 commits

Author SHA1 Message Date
21b4e7fb3b rm rwlock 2024-12-13 14:38:08 -05:00
cae5c6802c update deps 2024-12-13 14:37:52 -05:00
2 changed files with 24 additions and 47 deletions

8
Cargo.lock generated
View file

@ -386,9 +386,9 @@ checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3"
[[package]]
name = "cc"
version = "1.2.3"
version = "1.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27f657647bcff5394bf56c7317665bbf790a137a50eaaa5c6bfbb9e27a518f2d"
checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf"
dependencies = [
"jobserver",
"libc",
@ -2441,9 +2441,9 @@ dependencies = [
[[package]]
name = "quanta"
version = "0.12.3"
version = "0.12.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e5167a477619228a0b284fac2674e3c388cba90631d7b7de620e6f1fcd08da5"
checksum = "773ce68d0bb9bc7ef20be3536ffe94e223e1f365bd374108b2659fac0c65cfe6"
dependencies = [
"crossbeam-utils",
"libc",

View file

@ -52,14 +52,14 @@ pub struct UserHandler {
name_generator: NameGenerator,
reserved_names: HashSet<String>,
offline_users: HashMap<String, Arc<RwLock<User>>>,
online_users: RwLock<HashMap<SocketAddr, Arc<RwLock<User>>>>,
online_users: HashMap<SocketAddr, Arc<RwLock<User>>>,
}
impl UserHandler {
/// Returns new UserHandler
pub fn new(state: Arc<AppState>) -> Self {
let offline_users = HashMap::<String, Arc<RwLock<User>>>::new();
let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
let online_users = HashMap::<SocketAddr, Arc<RwLock<User>>>::new();
UserHandler {
offline_users,
online_users,
@ -88,13 +88,7 @@ impl UserHandler {
}
async fn join_game(&self, game_id: String, addr: SocketAddr) {
let user = self
.online_users
.read()
.unwrap()
.get(&addr)
.unwrap()
.clone();
let user = self.online_users.get(&addr).unwrap().clone();
if let Err(e) = self
.state
@ -107,13 +101,7 @@ impl UserHandler {
}
async fn move_request(&self, move_request: PlayerMoveRequest, addr: SocketAddr) {
let player_user = self
.online_users
.read()
.unwrap()
.get(&addr)
.unwrap()
.clone();
let player_user = self.online_users.get(&addr).unwrap().clone();
if let Err(e) = self
.state
.tx_game_handler
@ -125,13 +113,7 @@ impl UserHandler {
}
async fn get_user(&self, new_game: NewGameRequest, addr: SocketAddr) {
let host = self
.online_users
.read()
.unwrap()
.get(&addr)
.unwrap()
.clone();
let host = self.online_users.get(&addr).unwrap().clone();
if let Err(e) = self
.state
.tx_game_handler
@ -145,8 +127,6 @@ impl UserHandler {
async fn get_player_user_id(&self, judge_decision: JudgeDecisionRequest, addr: SocketAddr) {
let player_user_id = self
.online_users
.read()
.unwrap()
.get(&addr)
.unwrap()
.read()
@ -168,7 +148,8 @@ impl UserHandler {
}
async fn pass_chat_message(&self, msg: String, addr: SocketAddr) {
let text = format! {"{0}: {1}", self.online_users.read().unwrap().get(&addr).unwrap().read().unwrap().name, msg};
let text =
format! {"{0}: {1}", self.online_users.get(&addr).unwrap().read().unwrap().name, msg};
if let Err(e) = self
.state
.tx_outgoing_message_handler
@ -198,7 +179,7 @@ impl UserHandler {
}
}
Addr(addr) => {
if let Some(user) = self.online_users.read().unwrap().get(&addr) {
if let Some(user) = self.online_users.get(&addr) {
tx = user.read().unwrap().tx.clone();
} else {
tracing::error!("Attempted to send message to offline user!");
@ -336,7 +317,7 @@ impl UserHandler {
let old_name;
if let Some(user) = self.online_users.read().unwrap().get(&addr) {
if let Some(user) = self.online_users.get(&addr) {
old_name = user.read().unwrap().name.clone();
// User abandons current name by requesting a new one
@ -350,7 +331,7 @@ impl UserHandler {
if self.offline_users.contains_key(&new_name) {
let buf;
// Copy over new tx
if let Some(online_user) = self.online_users.write().unwrap().remove(&addr) {
if let Some(online_user) = self.online_users.remove(&addr) {
if let Some(offline_user) = self.offline_users.remove(&new_name) {
offline_user.write().unwrap().tx = online_user.write().unwrap().tx.clone();
buf = offline_user;
@ -364,7 +345,7 @@ impl UserHandler {
}
// Move offline user object to online
self.online_users.write().unwrap().insert(addr, buf);
self.online_users.insert(addr, buf);
// Send welcome back messages
let msg = format! {
@ -406,7 +387,7 @@ impl UserHandler {
self.reserved_names.insert(new_name.clone());
// Change user's name
if let Some(user) = self.online_users.write().unwrap().get_mut(&addr) {
if let Some(user) = self.online_users.get_mut(&addr) {
user.write().unwrap().change_name(new_name.clone());
} else {
tracing::error!("Error updating username: Can't find user!");
@ -441,7 +422,7 @@ impl UserHandler {
)
.await;
// Update games this user is in
if let Some(user) = &self.online_users.read().unwrap().get(&addr) {
if let Some(user) = &self.online_users.get(&addr) {
let user_id = user.read().unwrap().uuid.to_string();
let msg = GameHandlerMessage::SendGameUserUpdate(user_id);
let tx = self.state.tx_game_handler.clone();
@ -458,7 +439,7 @@ impl UserHandler {
/// Broadcast updated user count
fn broadcast_user_count(&self) {
let tx = self.state.tx_outgoing_message_handler.clone();
let online_users: u32 = self.online_users.read().unwrap().len().try_into().unwrap();
let online_users: u32 = self.online_users.len().try_into().unwrap();
let msg = ServerToClientMessage::ServerOnlineUsers(ServerOnlineUsers { online_users });
tokio::spawn(async move { tx.send(Broadcast(msg)).await });
}
@ -467,8 +448,6 @@ impl UserHandler {
async fn user_cleanup(&mut self, addr: SocketAddr) {
let user_name = self
.online_users
.read()
.unwrap()
.get(&addr)
.unwrap()
.read()
@ -499,16 +478,14 @@ impl UserHandler {
}
/// Set user status to online
fn set_user_online(&self, addr: SocketAddr, user: Arc<RwLock<User>>) {
self.online_users.write().unwrap().insert(addr, user);
fn set_user_online(&mut self, addr: SocketAddr, user: Arc<RwLock<User>>) {
self.online_users.insert(addr, user);
}
/// Set user status to offline
fn set_user_offline(&mut self, user_name: String, addr: &SocketAddr) {
self.offline_users.insert(
user_name,
self.online_users.write().unwrap().remove(addr).unwrap(),
);
self.offline_users
.insert(user_name, self.online_users.remove(addr).unwrap());
}
/// Update chat stuff like users list
@ -527,7 +504,7 @@ impl UserHandler {
/// Generate chatroom join announcement
fn meta_announce_user_join(&self, addr: &SocketAddr) -> ChatMessage {
let msg = format!("{} joined.", {
if let Some(user) = self.online_users.read().unwrap().get(addr) {
if let Some(user) = self.online_users.get(addr) {
user.read().unwrap().name.clone()
} else {
"Error!".to_string()
@ -541,7 +518,7 @@ impl UserHandler {
// TODO: this may get expensive if there are many users
let mut names = vec![];
for user in self.online_users.read().unwrap().iter() {
for user in self.online_users.iter() {
names.push(user.1.read().unwrap().name.clone());
}