/*
* This file is part of laurelin/server
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
*/
use bevy_ecs::prelude::*;
use bevy_log::{info, warn};
use bevy_quinnet::{
server::{ConnectionEvent, ConnectionLostEvent, Server},
shared::ClientId,
};
use crate::Global;
mod message;
pub use message::handle_client_messages;
pub fn handle_server_events(
mut global: ResMut<Global>,
mut server: ResMut<Server>,
mut connection_events: EventReader<ConnectionEvent>,
mut connection_lost_events: EventReader<ConnectionLostEvent>,
) {
for client in connection_events.iter() {
handle_connect(&client.id, &mut global);
}
for client in connection_lost_events.iter() {
handle_disconnect(&client.id, &mut global);
}
}
fn handle_connect(client_id: &ClientId, global: &mut Global) {
if let Some(user_id) = global.client_to_user_map.get(&client_id) {
warn!(
"Recieved a connection event from an already connected user: {}",
client_id
);
} else {
info!("{} connected", client_id);
}
}
fn handle_disconnect(client_id: &ClientId, global: &mut Global) {
if let Some(user_id) = global.client_to_user_map.remove(&client_id) {
info!("{} disconnected", client_id);
} else {
warn!(
"Received a disconnect event from an unknown source, possibly a client sending the disconnect event twice: {}",
client_id
);
}
}
/*
use bevy_ecs::{
event::EventReader,
system::{Res, ResMut},
};
use bevy_log::info;
use laurelin_shared::{
api::user::{login, register, ResponseLogin, ResponseRegister},
server::{
channels::AfterAuthChannel,
messages::{AfterAuth, Auth},
},
};
use naia_bevy_server::{
events::{AuthEvents, ConnectEvent, DisconnectEvent, ErrorEvent, TickEvent},
Server,
};
use crate::{Config, Global, RuntimeTemp};
mod message;
pub use message::message_events;
pub 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>() {
// TODO: accept a third type of authentication: existing session
// the client passes a cookie that it had stored,
// and we verify it with the api, returning the very same aa details.
match auth.username {
Some(username) => {
// register
let wrapped =
register(&config.api_address, &username, &auth.email, &auth.password);
match wrapped.response {
ResponseRegister::Error(_) => server.reject_connection(&user_key),
ResponseRegister::Ok(user) => {
server.accept_connection(&user_key);
temp.afterauth_details
.insert(user_key, (user, wrapped.cookie));
}
}
}
None => {
// login
let wrapped = login(&config.api_address, &auth.email, &auth.password);
match wrapped.response {
ResponseLogin::Error(_) => server.reject_connection(&user_key),
ResponseLogin::Ok(user) => {
server.accept_connection(&user_key);
temp.afterauth_details
.insert(user_key, (user, wrapped.cookie));
}
}
}
}
}
}
}
pub 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();
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 {
id: aa_details.0.id.to_string().clone(),
username: aa_details.0.username.clone(),
cookie: aa_details.1.clone(),
},
);
global
.user_to_session_map
.insert(*user_key, aa_details.1.clone());
temp.afterauth_details.remove(user_key);
}
}
pub 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);
}
}
pub fn error_events(mut ev: EventReader<ErrorEvent>) {
for ErrorEvent(error) in ev.iter() {
info!("Naia Server Error: {:?}", error);
}
}
pub 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 mut _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() {
// ...
//}
}
}
*/