From dfcae52675dc9372a0ee45fda5350876ebdd9ae5 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Mon, 3 Apr 2023 08:55:06 +0300 Subject: [PATCH] feat(client): change users_cache to a HashMap, instead of a Vec --- client/src/main.rs | 4 +- client/src/plugins/menu/ui/play/ui.rs | 52 +++++-------------- .../networking/systems/events/receive/mod.rs | 2 +- .../networking/systems/events/send/user.rs | 18 ++----- 4 files changed, 20 insertions(+), 56 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index 3fd0fd8..b0f24fd 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -52,7 +52,7 @@ pub enum GameState { #[derive(Resource)] pub struct Global { - pub users_cache: Vec, + pub users_cache: HashMap, /// stores ids of users currently in queue to be gotten pub users_cache_queue: Vec, pub games_cache: HashMap, @@ -111,7 +111,7 @@ fn main() { //app.add_plugin(ScriptingPlugin).add_plugin(lua::LuaPlugin); app.insert_resource(Global { - users_cache: vec![], + users_cache: HashMap::new(), users_cache_queue: vec![], games_cache: HashMap::new(), games_cache_queue: vec![], diff --git a/client/src/plugins/menu/ui/play/ui.rs b/client/src/plugins/menu/ui/play/ui.rs index 52de51e..6553303 100644 --- a/client/src/plugins/menu/ui/play/ui.rs +++ b/client/src/plugins/menu/ui/play/ui.rs @@ -137,11 +137,7 @@ pub fn ui( "Host: {}", global .users_cache - .iter() - .filter(|u| u.id == global.cur_game.as_ref().unwrap().host_id) - .cloned() - .collect::>() - .first() + .get(&global.cur_game.as_ref().unwrap().host_id) .unwrap_or(&UserPub { id: "".to_string(), created_at: "".to_string(), @@ -154,18 +150,15 @@ pub fn ui( "Guest: {}", global .users_cache - .iter() - .filter(|u| &u.id - == global + .get( + global .cur_game .as_ref() .unwrap() .guest_id .as_ref() - .unwrap_or(&"".to_string())) - .cloned() - .collect::>() - .first() + .unwrap_or(&"".to_string()) + ) .unwrap_or(&UserPub { id: "".to_string(), created_at: "".to_string(), @@ -187,11 +180,7 @@ pub fn ui( "Host: {}", global .users_cache - .iter() - .filter(|u| u.id == global.cur_game.as_ref().unwrap().host_id) - .cloned() - .collect::>() - .first() + .get(&global.cur_game.as_ref().unwrap().host_id) .unwrap_or(&UserPub { id: "".to_string(), created_at: "".to_string(), @@ -204,18 +193,15 @@ pub fn ui( "Guest: {}", global .users_cache - .iter() - .filter(|u| &u.id - == global + .get( + global .cur_game .as_ref() .unwrap() .guest_id .as_ref() - .unwrap_or(&"".to_string())) - .cloned() - .collect::>() - .first() + .unwrap_or(&"".to_string()) + ) .unwrap_or(&UserPub { id: "".to_string(), created_at: "".to_string(), @@ -271,11 +257,7 @@ fn browse_forming( "Host: {}", global .users_cache - .iter() - .filter(|u| u.id == game.host_id) - .cloned() - .collect::>() - .first() + .get(&game.host_id) .unwrap_or(&UserPub { id: "".to_string(), created_at: "".to_string(), @@ -346,11 +328,7 @@ fn browse_inprogress( "Host: {}", global .users_cache - .iter() - .filter(|u| u.id == game.host_id) - .cloned() - .collect::>() - .first() + .get(&game.host_id) .unwrap_or(&UserPub { id: "".to_string(), created_at: "".to_string(), @@ -409,11 +387,7 @@ fn browse_finished( "Host: {}", global .users_cache - .iter() - .filter(|u| u.id == game.host_id) - .cloned() - .collect::>() - .first() + .get(&game.host_id) .unwrap_or(&UserPub { id: "".to_string(), created_at: "".to_string(), diff --git a/client/src/plugins/networking/systems/events/receive/mod.rs b/client/src/plugins/networking/systems/events/receive/mod.rs index b3a39f9..b0a46b8 100644 --- a/client/src/plugins/networking/systems/events/receive/mod.rs +++ b/client/src/plugins/networking/systems/events/receive/mod.rs @@ -149,7 +149,7 @@ pub fn message_events( let user: UserPub = serde_json::from_str(response.data.unwrap().get(0).unwrap()).unwrap(); global.users_cache_queue.retain(|id| id != &user.id); - global.users_cache.push(user); + global.users_cache.insert(user.id.clone(), user); } } } diff --git a/client/src/plugins/networking/systems/events/send/user.rs b/client/src/plugins/networking/systems/events/send/user.rs index 614a3dd..b892544 100644 --- a/client/src/plugins/networking/systems/events/send/user.rs +++ b/client/src/plugins/networking/systems/events/send/user.rs @@ -7,12 +7,9 @@ */ use bevy::prelude::{EventReader, ResMut}; -use laurelin_shared::{ - server::{ - channels::DataRequestChannel, - messages::{DataRequest, DataRequestType}, - }, - types::user::UserPub, +use laurelin_shared::server::{ + channels::DataRequestChannel, + messages::{DataRequest, DataRequestType}, }; use naia_bevy_client::Client; @@ -29,14 +26,7 @@ pub fn pub_user_details_event( ) { for ev in ev.iter() { // check if already in cache OR in cache queue - if !global - .users_cache - .iter() - .filter(|&u| u.id == ev.id) - .cloned() - .collect::>() - .is_empty() - { + if global.users_cache.contains_key(&ev.id) { return; } -- 2.44.1