2023-03-08 14:10:48 -06:00
|
|
|
use shipyard::{UniqueView, UniqueViewMut, NonSendSync};
|
|
|
|
use uflow::{server::Event as ServerEvent, SendMode};
|
|
|
|
use kubi_shared::networking::messages::{
|
|
|
|
ClientToServerMessage,
|
|
|
|
ServerToClientMessage,
|
|
|
|
InitData,
|
|
|
|
C_CLIENT_HELLO
|
|
|
|
};
|
|
|
|
use crate::{
|
|
|
|
server::{ServerEvents, UdpServer, IsMessageOfType},
|
|
|
|
config::ConfigTable, util::log_error
|
|
|
|
};
|
2023-02-13 21:27:27 -06:00
|
|
|
|
|
|
|
pub fn authenticate_players(
|
2023-03-08 14:10:48 -06:00
|
|
|
mut server: NonSendSync<UniqueViewMut<UdpServer>>,
|
2023-02-13 21:27:27 -06:00
|
|
|
events: UniqueView<ServerEvents>,
|
|
|
|
config: UniqueView<ConfigTable>
|
|
|
|
) {
|
|
|
|
for event in &events.0 {
|
2023-03-08 14:10:48 -06:00
|
|
|
// if let ServerEvent::MessageReceived {
|
|
|
|
// from,
|
|
|
|
// message: ClientToServerMessage::ClientHello {
|
|
|
|
// username,
|
|
|
|
// password
|
|
|
|
// }
|
|
|
|
// } = event {
|
|
|
|
|
|
|
|
let ServerEvent::Receive(client_addr, data) = event else{
|
|
|
|
continue
|
|
|
|
};
|
|
|
|
let Some(client) = server.0.client(client_addr) else {
|
|
|
|
log::error!("Client doesn't exist");
|
|
|
|
continue
|
|
|
|
};
|
|
|
|
if !event.is_message_of_type::<C_CLIENT_HELLO>() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
let Ok(parsed_message) = postcard::from_bytes(data) else {
|
|
|
|
log::error!("Malformed message 00");
|
|
|
|
continue
|
|
|
|
};
|
|
|
|
let ClientToServerMessage::ClientHello { username, password } = parsed_message else {
|
|
|
|
unreachable!()
|
|
|
|
};
|
|
|
|
|
|
|
|
log::info!("ClientHello; username={} password={:?}", username, password);
|
2023-03-06 18:51:19 -06:00
|
|
|
|
2023-03-08 14:10:48 -06:00
|
|
|
// Handle password auth
|
|
|
|
if let Some(server_password) = &config.server.password {
|
|
|
|
if let Some(user_password) = &password {
|
|
|
|
if server_password != user_password {
|
|
|
|
let res = postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
|
|
|
|
reason: "Passwords don't match".into()
|
|
|
|
}).unwrap().into_boxed_slice();
|
|
|
|
client.borrow_mut().send(
|
|
|
|
res, 0, SendMode::Reliable
|
|
|
|
);
|
2023-02-13 21:27:27 -06:00
|
|
|
continue
|
|
|
|
}
|
2023-03-08 14:10:48 -06:00
|
|
|
} else {
|
|
|
|
let res = postcard::to_allocvec(&ServerToClientMessage::ServerFuckOff {
|
|
|
|
reason: "This server is password protected".into()
|
|
|
|
}).unwrap().into_boxed_slice();
|
|
|
|
client.borrow_mut().send(
|
|
|
|
res, 0, SendMode::Reliable
|
|
|
|
);
|
|
|
|
continue
|
2023-02-13 21:27:27 -06:00
|
|
|
}
|
2023-03-08 14:10:48 -06:00
|
|
|
}
|
2023-02-13 21:31:17 -06:00
|
|
|
|
2023-03-08 14:10:48 -06:00
|
|
|
//Spawn the user
|
|
|
|
//TODO Spawn the user on server side
|
2023-02-13 21:52:11 -06:00
|
|
|
|
2023-03-08 14:10:48 -06:00
|
|
|
//Approve the user
|
|
|
|
let res = postcard::to_allocvec(&ServerToClientMessage::ServerHello {
|
|
|
|
init: InitData {
|
|
|
|
users: vec![] //TODO create init data
|
|
|
|
}
|
|
|
|
}).unwrap().into_boxed_slice();
|
|
|
|
client.borrow_mut().send(
|
|
|
|
res, 0, SendMode::Reliable
|
|
|
|
);
|
2023-03-08 14:13:50 -06:00
|
|
|
|
|
|
|
log::info!("{username} joined the game!")
|
2023-02-13 21:27:27 -06:00
|
|
|
}
|
|
|
|
}
|