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 world;
mod player; mod player;
pub use handshake::ConnectionRejectionReason;
use handshake::{ use handshake::{
set_client_join_state_to_connected, set_client_join_state_to_connected,
say_hello, say_hello,
check_server_hello_response check_server_hello_response,
check_server_fuck_off_response,
}; };
use world::{ use world::{
inject_network_responses_into_manager_queue, inject_network_responses_into_manager_queue,
@ -129,6 +131,7 @@ pub fn update_networking() -> Workload {
).into_sequential_workload().run_if(if_just_connected), ).into_sequential_workload().run_if(if_just_connected),
( (
check_server_hello_response, check_server_hello_response,
check_server_fuck_off_response,
handle_disconnect, handle_disconnect,
).into_sequential_workload().run_if(is_join_state::<{ClientJoinState::Connected as u8}>), ).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 uflow::{client::Event as ClientEvent, SendMode};
use kubi_shared::networking::{ use kubi_shared::networking::{
messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType}, messages::{ClientToServerMessage, ServerToClientMessage, ServerToClientMessageType},
@ -8,6 +8,11 @@ use kubi_shared::networking::{
use crate::player::{spawn_local_player_multiplayer, spawn_remote_player_multiplayer}; use crate::player::{spawn_local_player_multiplayer, spawn_remote_player_multiplayer};
use super::{UdpClient, NetworkEvent}; use super::{UdpClient, NetworkEvent};
#[derive(Unique)]
pub struct ConnectionRejectionReason {
pub reason: String,
}
pub fn set_client_join_state_to_connected( pub fn set_client_join_state_to_connected(
mut join_state: UniqueViewMut<ClientJoinState> mut join_state: UniqueViewMut<ClientJoinState>
) { ) {
@ -74,3 +79,33 @@ pub fn check_server_hello_response(
log::info!("Joined the server!"); 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::{ use crate::{
hui_integration::UiState, hui_integration::UiState,
loading_screen::loading_screen_base, loading_screen::loading_screen_base,
networking::ServerAddress, networking::{ConnectionRejectionReason, ServerAddress},
prefabs::UiFontPrefab, prefabs::UiFontPrefab,
rendering::WindowSize, rendering::WindowSize,
state::{GameState, NextState} state::{GameState, NextState}
@ -12,6 +12,8 @@ use crate::{
fn render_connecting_ui( fn render_connecting_ui(
addr: UniqueView<ServerAddress>, addr: UniqueView<ServerAddress>,
rejection: Option<UniqueView<ConnectionRejectionReason>>,
join_state: UniqueView<ClientJoinState>,
mut ui: NonSendSync<UniqueViewMut<UiState>>, mut ui: NonSendSync<UniqueViewMut<UiState>>,
font: UniqueView<UiFontPrefab>, font: UniqueView<UiFontPrefab>,
size: UniqueView<WindowSize>, size: UniqueView<WindowSize>,
@ -19,10 +21,11 @@ fn render_connecting_ui(
ui.hui.add( ui.hui.add(
loading_screen_base(vec![ loading_screen_base(vec![
Box::new(Text { Box::new(Text {
text: format!( text: match (rejection, *join_state) {
"Connecting to {}...", (Some(err), _) => format!("Connection rejected by {}\n\n{}", addr.0, err.reason).into(),
addr.0, (_, ClientJoinState::Disconnected) => format!("Lost connection to {}", addr.0).into(),
).into(), _ => format!("Connecting to {}...", addr.0).into(),
},
font: font.0, font: font.0,
text_size: 16, text_size: 16,
..Default::default() ..Default::default()