From a69dd4f47395fb45e5008d5cd4ba6f8c6e5ddc9a Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Thu, 23 Mar 2023 14:49:11 +0200 Subject: [PATCH] feat(client): user cache, and PubUserDetails event --- client/src/main.rs | 12 +++++++-- client/src/plugins/networking/mod.rs | 2 ++ .../networking/systems/events/receive/mod.rs | 8 ++++++ .../networking/systems/events/send/game.rs | 13 +++++++--- .../networking/systems/events/send/mod.rs | 7 +++-- .../networking/systems/events/send/user.rs | 26 +++++++++++++++++++ 6 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 client/src/plugins/networking/systems/events/send/user.rs diff --git a/client/src/main.rs b/client/src/main.rs index 7fd53be..cb630e0 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -19,7 +19,7 @@ use naia_bevy_client::{ Client, ClientConfig as NaiaClientConfig, Plugin as NaiaClientPlugin, ReceiveEvents, }; -use laurelin_shared::server::protocol::protocol; +use laurelin_shared::{server::protocol::protocol, types::user::UserPub}; mod cfg; mod constants; @@ -44,6 +44,11 @@ pub enum GameState { Game, } +#[derive(Resource)] +pub struct Global { + pub users_cache: Vec, +} + #[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)] struct MainLoop; @@ -93,7 +98,10 @@ fn main() { //app.add_plugin(ScriptingPlugin).add_plugin(lua::LuaPlugin); - app.insert_resource(cfg::CfgDirs( + app.insert_resource(Global { + users_cache: vec![], + }) + .insert_resource(cfg::CfgDirs( directories::ProjectDirs::from("com", "liljamo", "deckbuilder") .expect("failed to get project directories"), )); diff --git a/client/src/plugins/networking/mod.rs b/client/src/plugins/networking/mod.rs index 351de10..fe68a9d 100644 --- a/client/src/plugins/networking/mod.rs +++ b/client/src/plugins/networking/mod.rs @@ -19,10 +19,12 @@ impl Plugin for NetworkingPlugin { app.add_event::() .add_event::() .add_event::() + .add_event::() .add_systems(( send::game::create_event, send::game::all_forming_event, send::game::my_games_event, + send::user::pub_user_details_event, )) .add_systems( ( diff --git a/client/src/plugins/networking/systems/events/receive/mod.rs b/client/src/plugins/networking/systems/events/receive/mod.rs index e468144..7e6ad90 100644 --- a/client/src/plugins/networking/systems/events/receive/mod.rs +++ b/client/src/plugins/networking/systems/events/receive/mod.rs @@ -25,6 +25,7 @@ use crate::{ }, MenuState, }, + Global, }; pub fn connect_events(mut ev: EventReader, client: Client) { @@ -57,6 +58,7 @@ pub fn disconnect_events(mut ev: EventReader) { pub fn message_events( mut commands: Commands, mut ev: EventReader, + mut global: ResMut, mut cfg_user: ResMut, mut connect_data: ResMut, mut play_data: ResMut, @@ -99,6 +101,12 @@ pub fn message_events( play_data.my_games = response.games.unwrap(); play_data.waiting_for_my_games = false; } + DataRequestType::PubUserDetails => { + // TODO: handle possible error + global + .users_cache + .push(response.users.unwrap().get(0).unwrap().clone()); + } } } } diff --git a/client/src/plugins/networking/systems/events/send/game.rs b/client/src/plugins/networking/systems/events/send/game.rs index 6f92495..e83f2e6 100644 --- a/client/src/plugins/networking/systems/events/send/game.rs +++ b/client/src/plugins/networking/systems/events/send/game.rs @@ -16,18 +16,25 @@ use naia_bevy_client::Client; use super::data_request; pub struct GameCreateEvent; -data_request!(create_event, GameCreateEvent, DataRequestType::GameCreate); +data_request!( + create_event, + GameCreateEvent, + DataRequestType::GameCreate, + |ev| None +); pub struct GameAllFormingEvent; data_request!( all_forming_event, GameAllFormingEvent, - DataRequestType::GameAllForming + DataRequestType::GameAllForming, + |ev| None ); pub struct GameMyGamesEvent; data_request!( my_games_event, GameMyGamesEvent, - DataRequestType::GameMyGames + DataRequestType::GameMyGames, + |ev| None ); diff --git a/client/src/plugins/networking/systems/events/send/mod.rs b/client/src/plugins/networking/systems/events/send/mod.rs index 8464d62..cc97ffc 100644 --- a/client/src/plugins/networking/systems/events/send/mod.rs +++ b/client/src/plugins/networking/systems/events/send/mod.rs @@ -7,13 +7,16 @@ */ pub mod game; +pub mod user; macro_rules! data_request { - ($fni:ident, $event:ident, $reqtype:expr) => { + ($fni:ident, $event:ident, $reqtype:expr, |$ev:ident| $data:expr) => { pub fn $fni(mut ev: EventReader<$event>, mut client: Client) { - for _ in ev.iter() { + #[allow(unused_variables)] + for $ev in ev.iter() { client.send_message::(&DataRequest::new( $reqtype as u8, + $data, )); } } diff --git a/client/src/plugins/networking/systems/events/send/user.rs b/client/src/plugins/networking/systems/events/send/user.rs new file mode 100644 index 0000000..2a42506 --- /dev/null +++ b/client/src/plugins/networking/systems/events/send/user.rs @@ -0,0 +1,26 @@ +/* + * This file is part of laurelin/client + * Copyright (C) 2023 Jonni Liljamo + * + * Licensed under GPL-3.0-only. + * See LICENSE for licensing information. + */ + +use bevy::prelude::EventReader; +use laurelin_shared::server::{ + channels::DataRequestChannel, + messages::{DataRequest, DataRequestType}, +}; +use naia_bevy_client::Client; + +use super::data_request; + +pub struct PubUserDetailsEvent { + pub id: String, +} +data_request!( + pub_user_details_event, + PubUserDetailsEvent, + DataRequestType::PubUserDetails, + |ev| Some(ev.id.clone()) +); -- 2.44.1