/*
* This file is part of laurelin/client
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy::prelude::*;
use iyes_loopless::state::NextState;
use laurelin_shared::server::{channels::AfterAuthChannel, messages::AfterAuth};
use naia_bevy_client::{
events::{ClientTickEvent, ConnectEvent, DisconnectEvent, MessageEvents, RejectEvent},
Client,
};
use crate::{
cfg::CfgUser,
plugins::menu::{
ui::connect::{ConnectScreenData, ConnectState},
MenuState,
},
};
pub fn connect_events(
mut commands: Commands,
mut ev: EventReader<ConnectEvent>,
client: Client,
mut data: ResMut<ConnectScreenData>,
) {
for _ in ev.iter() {
if let Ok(server_address) = client.server_address() {
info!("Laurelin client connected to: '{}'", server_address);
data.state = ConnectState::Login;
commands.insert_resource(NextState(MenuState::Menu));
}
}
}
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.");
}
}
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() {}
}