From 6a51098b6fa45369b124e3ed82cb71b867a5791a Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Mon, 13 Mar 2023 11:45:58 +0200 Subject: [PATCH] feat(server): store user cookies in memory --- server/src/main.rs | 8 +++++++- server/src/systems/event/mod.rs | 14 +++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/server/src/main.rs b/server/src/main.rs index 378d491..2ea3aa1 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -31,7 +31,9 @@ pub struct RuntimeTemp { } #[derive(Resource)] -pub struct Global {} +pub struct Global { + pub user_to_session_map: HashMap, +} fn main() { let mut server = App::new(); @@ -57,6 +59,10 @@ fn main() { .insert_resource(RuntimeTemp { afterauth_details: HashMap::new(), }) + // global data + .insert_resource(Global { + user_to_session_map: HashMap::new(), + }) // init system .add_startup_system(systems::init::init) // events diff --git a/server/src/systems/event/mod.rs b/server/src/systems/event/mod.rs index 8c6b914..419c658 100644 --- a/server/src/systems/event/mod.rs +++ b/server/src/systems/event/mod.rs @@ -23,7 +23,7 @@ use naia_bevy_server::{ Server, }; -use crate::{Config, RuntimeTemp}; +use crate::{Config, Global, RuntimeTemp}; pub(crate) fn auth_events( mut ev: EventReader, @@ -72,6 +72,7 @@ pub(crate) fn connect_events( mut ev: EventReader, mut server: Server, mut temp: ResMut, + mut global: ResMut, ) { for ConnectEvent(user_key) in ev.iter() { let address = server.user(user_key).address(); @@ -86,13 +87,20 @@ pub(crate) fn connect_events( cookie: aa_details.1.clone(), }, ); + + global + .user_to_session_map + .insert(*user_key, aa_details.1.clone()); + temp.afterauth_details.remove(&user_key); } } -pub(crate) fn disconnect_events(mut ev: EventReader) { - for DisconnectEvent(_user_key, user) in ev.iter() { +pub(crate) fn disconnect_events(mut ev: EventReader, mut global: ResMut) { + for DisconnectEvent(user_key, user) in ev.iter() { info!("Naia Server disconnected from: {:?}", user.address); + + global.user_to_session_map.remove(user_key); } } -- 2.44.1