handle fuck off request

This commit is contained in:
griffi-gh 2024-02-21 04:21:47 +01:00
parent 00edf0d272
commit f0270e3ce5
4 changed files with 378 additions and 337 deletions

View file

@ -22,10 +22,12 @@ mod handshake;
mod world;
mod player;
pub use handshake::ConnectionRejectionReason;
use handshake::{
set_client_join_state_to_connected,
say_hello,
check_server_hello_response
check_server_hello_response,
check_server_fuck_off_response,
};
use world::{
inject_network_responses_into_manager_queue,
@ -129,6 +131,7 @@ pub fn update_networking() -> Workload {
).into_sequential_workload().run_if(if_just_connected),
(
check_server_hello_response,
check_server_fuck_off_response,
handle_disconnect,
).into_sequential_workload().run_if(is_join_state::<{ClientJoinState::Connected as u8}>),
(

View file

@ -1,4 +1,4 @@
use shipyard::{UniqueViewMut, View, IntoIter, AllStoragesViewMut};
use shipyard::{AllStoragesView, AllStoragesViewMut, IntoIter, Unique, UniqueView, UniqueViewMut, View};
use uflow::{client::Event as ClientEvent, SendMode};
use kubi_shared::networking::{
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
@ -8,6 +8,11 @@ use kubi_shared::networking::{
use crate::player::{spawn_local_player_multiplayer, spawn_remote_player_multiplayer};
use super::{UdpClient, NetworkEvent};
#[derive(Unique)]
pub struct ConnectionRejectionReason {
pub reason: String,
}
pub fn set_client_join_state_to_connected(
mut join_state: UniqueViewMut<ClientJoinState>
) {
@ -74,3 +79,33 @@ pub fn check_server_hello_response(
log::info!("Joined the server!");
}
pub fn check_server_fuck_off_response(
storages: AllStoragesView,
) {
//Check if we got the message and extract the init data from it
let Some(reason) = storages.borrow::<View<NetworkEvent>>().unwrap().iter().find_map(|event| {
let ClientEvent::Receive(data) = &event.0 else {
return None
};
if !event.is_message_of_type::<{ServerToClientMessageType::ServerFuckOff as u8}>() {
return None
}
let Ok(parsed_message) = postcard::from_bytes(data) else {
log::error!("Malformed message");
return None
};
let ServerToClientMessage::ServerFuckOff { reason } = parsed_message else {
unreachable!()
};
Some(reason)
}) else { return };
let mut client = storages.borrow::<UniqueViewMut<UdpClient>>().unwrap();
client.0.disconnect_now();
let mut join_state = storages.borrow::<UniqueViewMut<ClientJoinState>>().unwrap();
*join_state = ClientJoinState::Disconnected;
storages.add_unique(ConnectionRejectionReason { reason });
}

View file

@ -4,7 +4,7 @@ use shipyard::{IntoWorkload, NonSendSync, UniqueView, UniqueViewMut, Workload};
use crate::{
hui_integration::UiState,
loading_screen::loading_screen_base,
networking::ServerAddress,
networking::{ConnectionRejectionReason, ServerAddress},
prefabs::UiFontPrefab,
rendering::WindowSize,
state::{GameState, NextState}
@ -12,6 +12,8 @@ use crate::{
fn render_connecting_ui(
addr: UniqueView<ServerAddress>,
rejection: Option<UniqueView<ConnectionRejectionReason>>,
join_state: UniqueView<ClientJoinState>,
mut ui: NonSendSync<UniqueViewMut<UiState>>,
font: UniqueView<UiFontPrefab>,
size: UniqueView<WindowSize>,
@ -19,10 +21,11 @@ fn render_connecting_ui(
ui.hui.add(
loading_screen_base(vec![
Box::new(Text {
text: format!(
"Connecting to {}...",
addr.0,
).into(),
text: match (rejection, *join_state) {
(Some(err), _) => format!("Connection rejected by {}\n\n{}", addr.0, err.reason).into(),
(_, ClientJoinState::Disconnected) => format!("Lost connection to {}", addr.0).into(),
_ => format!("Connecting to {}...", addr.0).into(),
},
font: font.0,
text_size: 16,
..Default::default()