DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

5b6558cd0dc6791d7504f28428324a5022a78178 — Jonni Liljamo 1 year, 9 months ago 77d69bb
feat(client, server): finally handle AfterAuth properly
M client/src/plugins/networking/systems/events.rs => client/src/plugins/networking/systems/events.rs +0 -5
@@ 58,16 58,11 @@ pub fn disconnect_events(mut ev: EventReader<DisconnectEvent>) {
}

pub fn message_events(mut ev: EventReader<MessageEvents>, mut cfg_user: ResMut<CfgUser>) {
    println!("1");
    for events in ev.iter() {
        println!("2");
        for aa_message in events.read::<AfterAuthChannel, AfterAuth>() {
            println!("3");
            // TODO: move handling to a separate function?
            cfg_user.username = aa_message.username;
            cfg_user.cookie = aa_message.cookie;
            println!("got username: {}", cfg_user.username);
            println!("got cookie: {}", cfg_user.cookie);
        }
    }
}

M server/src/main.rs => server/src/main.rs +13 -1
@@ 6,12 6,14 @@
 * See LICENSE for licensing information.
 */

use std::collections::HashMap;

use bevy_app::{App, ScheduleRunnerPlugin};
use bevy_core::CorePlugin;
use bevy_ecs::system::Resource;
use bevy_log::{info, LogPlugin};

use naia_bevy_server::{Plugin as ServerPlugin, ServerConfig};
use naia_bevy_server::{Plugin as ServerPlugin, ServerConfig, UserKey};

use laurelin_shared::server::protocol::protocol;



@@ 22,6 24,12 @@ pub struct Config {
    pub api_address: String,
}

/// Temporary runtime data
#[derive(Resource)]
pub struct RuntimeTemp {
    pub afterauth_details: HashMap<UserKey, (String, String)>,
}

#[derive(Resource)]
pub struct Global {}



@@ 42,6 50,10 @@ fn main() {
        .add_plugin(ServerPlugin::new(ServerConfig::default(), protocol()))
        // config
        .insert_resource(Config { api_address })
        // temp runtime data
        .insert_resource(RuntimeTemp {
            afterauth_details: HashMap::new(),
        })
        // init system
        .add_startup_system(systems::init::init)
        // events

M server/src/systems/event/mod.rs => server/src/systems/event/mod.rs +25 -19
@@ 6,7 6,10 @@
 * See LICENSE for licensing information.
 */

use bevy_ecs::{event::EventReader, system::Res};
use bevy_ecs::{
    event::EventReader,
    system::{Res, ResMut},
};
use bevy_log::info;
use laurelin_shared::{
    api::user::{login, register, ResponseLogin, ResponseRegister},


@@ 20,12 23,13 @@ use naia_bevy_server::{
    Server,
};

use crate::Config;
use crate::{Config, RuntimeTemp};

pub(crate) fn auth_events(
    mut ev: EventReader<AuthEvents>,
    mut server: Server,
    config: Res<Config>,
    mut temp: ResMut<RuntimeTemp>,
) {
    for events in ev.iter() {
        for (user_key, auth) in events.read::<Auth>() {


@@ 38,14 42,8 @@ pub(crate) fn auth_events(
                        ResponseRegister::Error(_) => server.reject_connection(&user_key),
                        ResponseRegister::Ok(user) => {
                            server.accept_connection(&user_key);

                            server.send_message::<AfterAuthChannel, AfterAuth>(
                                &user_key,
                                &AfterAuth {
                                    username: user.username,
                                    cookie: wrapped.cookie,
                                },
                            )
                            temp.afterauth_details
                                .insert(user_key, (user.username, wrapped.cookie));
                        }
                    }
                }


@@ 56,14 54,8 @@ pub(crate) fn auth_events(
                        ResponseLogin::Error(_) => server.reject_connection(&user_key),
                        ResponseLogin::Ok(user) => {
                            server.accept_connection(&user_key);

                            server.send_message::<AfterAuthChannel, AfterAuth>(
                                &user_key,
                                &AfterAuth {
                                    username: user.username,
                                    cookie: wrapped.cookie,
                                },
                            )
                            temp.afterauth_details
                                .insert(user_key, (user.username, wrapped.cookie));
                        }
                    }
                }


@@ 72,10 64,24 @@ pub(crate) fn auth_events(
    }
}

pub(crate) fn connect_events(mut ev: EventReader<ConnectEvent>, mut server: Server) {
pub(crate) fn connect_events(
    mut ev: EventReader<ConnectEvent>,
    mut server: Server,
    temp: Res<RuntimeTemp>,
) {
    for ConnectEvent(user_key) in ev.iter() {
        let address = server.user(user_key).address();
        info!("Naia Server connected to: {:?}", address);

        // TODO: handle
        let aa_details = temp.afterauth_details.get(user_key).unwrap();
        server.send_message::<AfterAuthChannel, AfterAuth>(
            &user_key,
            &AfterAuth {
                username: aa_details.0.clone(),
                cookie: aa_details.1.clone(),
            },
        )
    }
}