From 8db41b3a1e2ad2024db3d194fa35b4c231b2f3b5 Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Fri, 24 Mar 2023 11:51:07 +0200 Subject: [PATCH] feat(client): revamp user details event, send it when needed --- .../networking/systems/events/receive/mod.rs | 28 ++++++++- .../networking/systems/events/send/user.rs | 59 +++++++++++++++---- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/client/src/plugins/networking/systems/events/receive/mod.rs b/client/src/plugins/networking/systems/events/receive/mod.rs index f6a366b..054446f 100644 --- a/client/src/plugins/networking/systems/events/receive/mod.rs +++ b/client/src/plugins/networking/systems/events/receive/mod.rs @@ -28,6 +28,8 @@ use crate::{ Global, }; +use super::send::user::PubUserDetailsEvent; + pub fn connect_events(mut ev: EventReader, client: Client) { for _ in ev.iter() { if let Ok(server_address) = client.server_address() { @@ -58,6 +60,7 @@ pub fn disconnect_events(mut ev: EventReader) { pub fn message_events( mut commands: Commands, mut ev: EventReader, + mut pud_ev_w: EventWriter, mut global: ResMut, mut cfg_user: ResMut, mut connect_data: ResMut, @@ -93,12 +96,33 @@ pub fn message_events( } DataRequestType::GameAllForming => { // TODO: handle possible error - play_data.all_forming = response.games.unwrap(); + let all_forming = response.games.unwrap(); + + for game in &all_forming { + pud_ev_w.send(PubUserDetailsEvent { + id: game.host_id.clone(), + }); + } + + play_data.all_forming = all_forming; play_data.waiting_for_all_forming = false; } DataRequestType::GameMyGames => { // TODO: handle possible error - play_data.my_games = response.games.unwrap(); + let my_games = response.games.unwrap(); + + for game in &my_games { + pud_ev_w.send(PubUserDetailsEvent { + id: game.host_id.clone(), + }); + if let Some(guest_id) = &game.guest_id { + pud_ev_w.send(PubUserDetailsEvent { + id: guest_id.clone(), + }); + } + } + + play_data.my_games = my_games; play_data.waiting_for_my_games = false; } DataRequestType::PubUserDetails => { diff --git a/client/src/plugins/networking/systems/events/send/user.rs b/client/src/plugins/networking/systems/events/send/user.rs index 2a42506..614a3dd 100644 --- a/client/src/plugins/networking/systems/events/send/user.rs +++ b/client/src/plugins/networking/systems/events/send/user.rs @@ -6,21 +6,58 @@ * See LICENSE for licensing information. */ -use bevy::prelude::EventReader; -use laurelin_shared::server::{ - channels::DataRequestChannel, - messages::{DataRequest, DataRequestType}, +use bevy::prelude::{EventReader, ResMut}; +use laurelin_shared::{ + server::{ + channels::DataRequestChannel, + messages::{DataRequest, DataRequestType}, + }, + types::user::UserPub, }; use naia_bevy_client::Client; -use super::data_request; +use crate::Global; pub struct PubUserDetailsEvent { pub id: String, } -data_request!( - pub_user_details_event, - PubUserDetailsEvent, - DataRequestType::PubUserDetails, - |ev| Some(ev.id.clone()) -); + +pub fn pub_user_details_event( + mut ev: EventReader, + mut client: Client, + mut global: ResMut, +) { + 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() + { + return; + } + + if !global + .users_cache_queue + .iter() + .filter(|&id| id == &ev.id) + .cloned() + .collect::>() + .is_empty() + { + return; + } + + // add to cache queue + global.users_cache_queue.push(ev.id.clone()); + + // send request + client.send_message::(&DataRequest::new( + DataRequestType::PubUserDetails as u8, + Some(ev.id.clone()), + )); + } +} -- 2.44.1