DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

6a51098b6fa45369b124e3ed82cb71b867a5791a — Jonni Liljamo 1 year, 9 months ago 5760a73
feat(server): store user cookies in memory
2 files changed, 18 insertions(+), 4 deletions(-)

M server/src/main.rs
M server/src/systems/event/mod.rs
M server/src/main.rs => server/src/main.rs +7 -1
@@ 31,7 31,9 @@ pub struct RuntimeTemp {
}

#[derive(Resource)]
pub struct Global {}
pub struct Global {
    pub user_to_session_map: HashMap<UserKey, String>,
}

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

M server/src/systems/event/mod.rs => server/src/systems/event/mod.rs +11 -3
@@ 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<AuthEvents>,


@@ 72,6 72,7 @@ pub(crate) fn connect_events(
    mut ev: EventReader<ConnectEvent>,
    mut server: Server,
    mut temp: ResMut<RuntimeTemp>,
    mut global: ResMut<Global>,
) {
    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<DisconnectEvent>) {
    for DisconnectEvent(_user_key, user) in ev.iter() {
pub(crate) fn disconnect_events(mut ev: EventReader<DisconnectEvent>, mut global: ResMut<Global>) {
    for DisconnectEvent(user_key, user) in ev.iter() {
        info!("Naia Server disconnected from: {:?}", user.address);

        global.user_to_session_map.remove(user_key);
    }
}