Compare commits
No commits in common. "21b4e7fb3bf7426a21e4ee81fdc8060ddb1d6a42" and "e1c0cf185ca2dd24b8b7ea84b776e060a0fa1753" have entirely different histories.
21b4e7fb3b
...
e1c0cf185c
2 changed files with 47 additions and 24 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -386,9 +386,9 @@ checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3"
|
|||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.4"
|
||||
version = "1.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9157bbaa6b165880c27a4293a474c91cdcf265cc68cc829bf10be0964a391caf"
|
||||
checksum = "27f657647bcff5394bf56c7317665bbf790a137a50eaaa5c6bfbb9e27a518f2d"
|
||||
dependencies = [
|
||||
"jobserver",
|
||||
"libc",
|
||||
|
@ -2441,9 +2441,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "quanta"
|
||||
version = "0.12.4"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "773ce68d0bb9bc7ef20be3536ffe94e223e1f365bd374108b2659fac0c65cfe6"
|
||||
checksum = "8e5167a477619228a0b284fac2674e3c388cba90631d7b7de620e6f1fcd08da5"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
"libc",
|
||||
|
|
|
@ -52,14 +52,14 @@ pub struct UserHandler {
|
|||
name_generator: NameGenerator,
|
||||
reserved_names: HashSet<String>,
|
||||
offline_users: HashMap<String, Arc<RwLock<User>>>,
|
||||
online_users: HashMap<SocketAddr, Arc<RwLock<User>>>,
|
||||
online_users: RwLock<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 = HashMap::<SocketAddr, Arc<RwLock<User>>>::new();
|
||||
let online_users = RwLock::new(HashMap::<SocketAddr, Arc<RwLock<User>>>::new());
|
||||
UserHandler {
|
||||
offline_users,
|
||||
online_users,
|
||||
|
@ -88,7 +88,13 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
async fn join_game(&self, game_id: String, addr: SocketAddr) {
|
||||
let user = self.online_users.get(&addr).unwrap().clone();
|
||||
let user = self
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.clone();
|
||||
|
||||
if let Err(e) = self
|
||||
.state
|
||||
|
@ -101,7 +107,13 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
async fn move_request(&self, move_request: PlayerMoveRequest, addr: SocketAddr) {
|
||||
let player_user = self.online_users.get(&addr).unwrap().clone();
|
||||
let player_user = self
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.clone();
|
||||
if let Err(e) = self
|
||||
.state
|
||||
.tx_game_handler
|
||||
|
@ -113,7 +125,13 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
async fn get_user(&self, new_game: NewGameRequest, addr: SocketAddr) {
|
||||
let host = self.online_users.get(&addr).unwrap().clone();
|
||||
let host = self
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.clone();
|
||||
if let Err(e) = self
|
||||
.state
|
||||
.tx_game_handler
|
||||
|
@ -127,6 +145,8 @@ 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()
|
||||
|
@ -148,8 +168,7 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
async fn pass_chat_message(&self, msg: String, addr: SocketAddr) {
|
||||
let text =
|
||||
format! {"{0}: {1}", self.online_users.get(&addr).unwrap().read().unwrap().name, msg};
|
||||
let text = format! {"{0}: {1}", self.online_users.read().unwrap().get(&addr).unwrap().read().unwrap().name, msg};
|
||||
if let Err(e) = self
|
||||
.state
|
||||
.tx_outgoing_message_handler
|
||||
|
@ -179,7 +198,7 @@ impl UserHandler {
|
|||
}
|
||||
}
|
||||
Addr(addr) => {
|
||||
if let Some(user) = self.online_users.get(&addr) {
|
||||
if let Some(user) = self.online_users.read().unwrap().get(&addr) {
|
||||
tx = user.read().unwrap().tx.clone();
|
||||
} else {
|
||||
tracing::error!("Attempted to send message to offline user!");
|
||||
|
@ -317,7 +336,7 @@ impl UserHandler {
|
|||
|
||||
let old_name;
|
||||
|
||||
if let Some(user) = self.online_users.get(&addr) {
|
||||
if let Some(user) = self.online_users.read().unwrap().get(&addr) {
|
||||
old_name = user.read().unwrap().name.clone();
|
||||
|
||||
// User abandons current name by requesting a new one
|
||||
|
@ -331,7 +350,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.remove(&addr) {
|
||||
if let Some(online_user) = self.online_users.write().unwrap().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;
|
||||
|
@ -345,7 +364,7 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
// Move offline user object to online
|
||||
self.online_users.insert(addr, buf);
|
||||
self.online_users.write().unwrap().insert(addr, buf);
|
||||
|
||||
// Send welcome back messages
|
||||
let msg = format! {
|
||||
|
@ -387,7 +406,7 @@ impl UserHandler {
|
|||
self.reserved_names.insert(new_name.clone());
|
||||
|
||||
// Change user's name
|
||||
if let Some(user) = self.online_users.get_mut(&addr) {
|
||||
if let Some(user) = self.online_users.write().unwrap().get_mut(&addr) {
|
||||
user.write().unwrap().change_name(new_name.clone());
|
||||
} else {
|
||||
tracing::error!("Error updating username: Can't find user!");
|
||||
|
@ -422,7 +441,7 @@ impl UserHandler {
|
|||
)
|
||||
.await;
|
||||
// Update games this user is in
|
||||
if let Some(user) = &self.online_users.get(&addr) {
|
||||
if let Some(user) = &self.online_users.read().unwrap().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();
|
||||
|
@ -439,7 +458,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.len().try_into().unwrap();
|
||||
let online_users: u32 = self.online_users.read().unwrap().len().try_into().unwrap();
|
||||
let msg = ServerToClientMessage::ServerOnlineUsers(ServerOnlineUsers { online_users });
|
||||
tokio::spawn(async move { tx.send(Broadcast(msg)).await });
|
||||
}
|
||||
|
@ -448,6 +467,8 @@ impl UserHandler {
|
|||
async fn user_cleanup(&mut self, addr: SocketAddr) {
|
||||
let user_name = self
|
||||
.online_users
|
||||
.read()
|
||||
.unwrap()
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.read()
|
||||
|
@ -478,14 +499,16 @@ impl UserHandler {
|
|||
}
|
||||
|
||||
/// Set user status to online
|
||||
fn set_user_online(&mut self, addr: SocketAddr, user: Arc<RwLock<User>>) {
|
||||
self.online_users.insert(addr, user);
|
||||
fn set_user_online(&self, addr: SocketAddr, user: Arc<RwLock<User>>) {
|
||||
self.online_users.write().unwrap().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.remove(addr).unwrap());
|
||||
self.offline_users.insert(
|
||||
user_name,
|
||||
self.online_users.write().unwrap().remove(addr).unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
/// Update chat stuff like users list
|
||||
|
@ -504,7 +527,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.get(addr) {
|
||||
if let Some(user) = self.online_users.read().unwrap().get(addr) {
|
||||
user.read().unwrap().name.clone()
|
||||
} else {
|
||||
"Error!".to_string()
|
||||
|
@ -518,7 +541,7 @@ impl UserHandler {
|
|||
// TODO: this may get expensive if there are many users
|
||||
let mut names = vec![];
|
||||
|
||||
for user in self.online_users.iter() {
|
||||
for user in self.online_users.read().unwrap().iter() {
|
||||
names.push(user.1.read().unwrap().name.clone());
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue