DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

4eab707e2d2a94f8ab14a66f5b3ec18e3495b2e6 — Jonni Liljamo 1 year, 9 months ago dcdb263
feat(server, client): handle tick events, sorta
M client/src/plugins/networking/mod.rs => client/src/plugins/networking/mod.rs +2 -1
@@ 17,6 17,7 @@ impl Plugin for NetworkingPlugin {
        app.add_system_to_stage(CoreStage::PreUpdate, systems::events::connect_events)
            .add_system_to_stage(CoreStage::PreUpdate, systems::events::reject_events)
            .add_system_to_stage(CoreStage::PreUpdate, systems::events::disconnect_events)
            .add_system_to_stage(CoreStage::PreUpdate, systems::events::message_events);
            .add_system_to_stage(CoreStage::PreUpdate, systems::events::message_events)
            .add_system_to_stage(CoreStage::PreUpdate, systems::events::tick_events);
    }
}

M client/src/plugins/networking/systems/events.rs => client/src/plugins/networking/systems/events.rs +14 -1
@@ 10,7 10,7 @@ use bevy::prelude::*;
use iyes_loopless::state::NextState;
use laurelin_shared::server::{channels::AfterAuthChannel, messages::AfterAuth};
use naia_bevy_client::{
    events::{ConnectEvent, DisconnectEvent, MessageEvents, RejectEvent},
    events::{ClientTickEvent, ConnectEvent, DisconnectEvent, MessageEvents, RejectEvent},
    Client,
};



@@ 58,11 58,24 @@ 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);
        }
    }
}

pub fn tick_events(mut ev: EventReader<ClientTickEvent>, mut client: Client) {
    if !client.is_connected() {
        return;
    }

    for ClientTickEvent(tick) in ev.iter() {}
}

M server/src/main.rs => server/src/main.rs +5 -1
@@ 22,6 22,9 @@ pub struct Config {
    pub api_address: String,
}

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

fn main() {
    let mut server = App::new();



@@ 45,7 48,8 @@ fn main() {
        .add_system(systems::event::auth_events)
        .add_system(systems::event::connect_events)
        .add_system(systems::event::disconnect_events)
        .add_system(systems::event::error_events);
        .add_system(systems::event::error_events)
        .add_system(systems::event::tick_events);

    info!("Laurelin server starting");
    server.run();

M server/src/systems/event/mod.rs => server/src/systems/event/mod.rs +23 -1
@@ 16,7 16,7 @@ use laurelin_shared::{
    },
};
use naia_bevy_server::{
    events::{AuthEvents, ConnectEvent, DisconnectEvent, ErrorEvent},
    events::{AuthEvents, ConnectEvent, DisconnectEvent, ErrorEvent, TickEvent},
    Server,
};



@@ 90,3 90,25 @@ pub(crate) fn error_events(mut ev: EventReader<ErrorEvent>) {
        info!("Naia Server Error: {:?}", error);
    }
}

pub(crate) fn tick_events(mut ev: EventReader<TickEvent>, mut server: Server) {
    let mut has_ticked = false;

    for TickEvent(_server_tick) in ev.iter() {
        has_ticked = true;

        // Handle messages coming from clients
        //let messages = server.receive_tick_buffer_messages(server_tick);
    }

    if has_ticked {
        // Update scopes of entities
        // Not sure if this will come into play,
        // but see naia/demos/bevy/server/src/systems/events.rs::tick_events
        //for (_, user_key, entity) in server.scope_checks() {
        // ...
        //}

        server.send_all_updates();
    }
}