DEVELOPMENT ENVIRONMENT

~liljamo/deck-builder

9033f4ee833928da4589d933d3b3f041b78f1799 — Jonni Liljamo 1 year, 9 months ago 0fe4ea4
wip(client, server): handle auth and connecting
M client/src/plugins/menu/ui/connect/ui.rs => client/src/plugins/menu/ui/connect/ui.rs +88 -67
@@ 6,10 6,18 @@
 * See LICENSE for licensing information.
 */

use bevy::ecs::{system::ResMut, system::Resource};
use bevy::ecs::{
    system::Resource,
    system::{Res, ResMut},
};
use bevy_inspector_egui::bevy_egui::{egui, EguiContext};
use laurelin_shared::server::messages::Auth;
use naia_bevy_client::Client;

use crate::util::egui::{menuwindow, password};
use crate::{
    cfg::CfgDev,
    util::egui::{menuwindow, password},
};

#[derive(Default, Resource)]
pub struct ConnectScreenData {


@@ 22,7 30,7 @@ pub struct ConnectScreenData {
    pub error: String,
}

#[derive(Default)]
#[derive(Default, PartialEq)]
pub enum ConnectState {
    #[default]
    Login,


@@ 31,84 39,97 @@ pub enum ConnectState {
    Registering,
}

pub fn ui(mut egui_context: ResMut<EguiContext>, mut data: ResMut<ConnectScreenData>) {
pub fn ui(
    mut egui_context: ResMut<EguiContext>,
    mut data: ResMut<ConnectScreenData>,
    mut client: Client,
    cfg_dev: Res<CfgDev>,
) {
    menuwindow(
        egui_context.ctx_mut(),
        "Connect",
        &egui::Vec2::new(400., 600.),
        |ui| {
            match data.state {
                ConnectState::Login => {
                    ui.horizontal(|ui| {
                        ui.label("Email:");
                        ui.text_edit_singleline(&mut data.email)
                    });
                    ui.horizontal(|ui| {
                        ui.label("Password:");
                        ui.add(password(&mut data.password));
                    });
        |ui| match data.state {
            ConnectState::Login => {
                ui.horizontal(|ui| {
                    ui.label("Email:");
                    ui.text_edit_singleline(&mut data.email)
                });
                ui.horizontal(|ui| {
                    ui.label("Password:");
                    ui.add(password(&mut data.password));
                });

                if !data.error.is_empty() {
                    ui.label(egui::RichText::new(&data.error).color(egui::Color32::RED));
                }

                    if !data.error.is_empty() {
                        ui.label(egui::RichText::new(&data.error).color(egui::Color32::RED));
                ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
                    if ui.button("Login").clicked() {
                        data.error.clear();
                        data.state = ConnectState::LoggingIn;

                        client.auth(Auth::new(None, &data.email, &data.password));
                        client.connect(&cfg_dev.server_address)
                    }
                });

                ui.vertical_centered(|ui| {
                    ui.label("I don't have an account:");
                    if ui.link("Register").clicked() {
                        *data = ConnectScreenData::default();
                        data.state = ConnectState::Register;
                    }
                });
            }
            ConnectState::Register => {
                ui.horizontal(|ui| {
                    ui.label("Username:");
                    ui.text_edit_singleline(&mut data.username)
                });
                ui.horizontal(|ui| {
                    ui.label("Email:");
                    ui.text_edit_singleline(&mut data.email)
                });
                ui.horizontal(|ui| {
                    ui.label("Password:");
                    ui.add(password(&mut data.password));
                });
                ui.horizontal(|ui| {
                    ui.label("Confirm password:");
                    ui.add(password(&mut data.password_confirm));
                });

                if !data.error.is_empty() {
                    ui.label(egui::RichText::new(&data.error).color(egui::Color32::RED));
                }

                ui.add_enabled_ui(data.password == data.password_confirm, |ui| {
                    ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
                        if ui.button("Login").clicked() {
                        if ui.button("Register").clicked() {
                            data.error.clear();
                            // yoink
                        }
                    });
                            data.state = ConnectState::Registering;

                    ui.vertical_centered(|ui| {
                        ui.label("I don't have an account:");
                        if ui.link("Register").clicked() {
                            *data = ConnectScreenData::default();
                            data.state = ConnectState::Register;
                            client.auth(Auth::new(
                                Some(data.username.clone()),
                                &data.email,
                                &data.password,
                            ));
                            client.connect(&cfg_dev.server_address)
                        }
                    });
                }
                ConnectState::Register => {
                    ui.horizontal(|ui| {
                        ui.label("Username:");
                        ui.text_edit_singleline(&mut data.username)
                    });
                    ui.horizontal(|ui| {
                        ui.label("Email:");
                        ui.text_edit_singleline(&mut data.email)
                    });
                    ui.horizontal(|ui| {
                        ui.label("Password:");
                        ui.add(password(&mut data.password));
                    });
                    ui.horizontal(|ui| {
                        ui.label("Confirm password:");
                        ui.add(password(&mut data.password_confirm));
                    });
                });

                    if !data.error.is_empty() {
                        ui.label(egui::RichText::new(&data.error).color(egui::Color32::RED));
                ui.vertical_centered(|ui| {
                    ui.label("I have an account:");
                    if ui.link("Login").clicked() {
                        *data = ConnectScreenData::default();
                        data.state = ConnectState::Login;
                    }

                    ui.add_enabled_ui(data.password == data.password_confirm, |ui| {
                        ui.with_layout(egui::Layout::right_to_left(egui::Align::TOP), |ui| {
                            if ui.button("Register").clicked() {
                                data.error.clear();
                                // yoink
                            }
                        });
                    });

                    ui.vertical_centered(|ui| {
                        ui.label("I have an account:");
                        if ui.link("Login").clicked() {
                            *data = ConnectScreenData::default();
                            data.state = ConnectState::Login;
                        }
                    });
                }
                _ => {
                    ui.spinner();
                }
                });
            }
            _ => {
                ui.spinner();
            }
        },
    );

M client/src/plugins/networking/systems/events.rs => client/src/plugins/networking/systems/events.rs +33 -3
@@ 6,8 6,38 @@
 * See LICENSE for licensing information.
 */

pub fn connect_events() {}
use bevy::prelude::*;
use naia_bevy_client::{
    events::{ConnectEvent, DisconnectEvent, RejectEvent},
    Client,
};

pub fn reject_events() {}
use crate::plugins::menu::ui::connect::{ConnectScreenData, ConnectState};

pub fn disconnect_events() {}
pub fn connect_events(mut ev: EventReader<ConnectEvent>, client: Client) {
    for _ in ev.iter() {
        info!("?");
        if let Ok(server_address) = client.server_address() {
            info!("Laurelin client connected to: '{}'", server_address);
        }
    }
}

pub fn reject_events(mut ev: EventReader<RejectEvent>, mut data: ResMut<ConnectScreenData>) {
    for _ in ev.iter() {
        warn!("Laurelin client rejected from connecting to server!");

        data.error = "rejected from connecting".to_string();
        data.state = if data.state == ConnectState::LoggingIn {
            ConnectState::Login
        } else {
            ConnectState::Register
        }
    }
}

pub fn disconnect_events(mut ev: EventReader<DisconnectEvent>) {
    for _ in ev.iter() {
        info!("Laurelin client disconnected from server.");
    }
}

M server/src/systems/event/mod.rs => server/src/systems/event/mod.rs +2 -0
@@ 17,9 17,11 @@ pub(crate) fn auth_events(mut ev: EventReader<AuthEvents>, mut server: Server) {
            match auth.username {
                Some(username) => {
                    // register
                    server.reject_connection(&user_key);
                }
                None => {
                    // login
                    server.reject_connection(&user_key);
                }
            }
        }