From 0e4c6a94efca71381cae391ce0c534b6e0980e7a Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Tue, 7 Mar 2023 01:51:19 +0100 Subject: [PATCH] wtf --- kubi-server/src/auth.rs | 6 ++- kubi-server/src/chunk.rs | 4 +- kubi-shared/src/networking/state.rs | 3 +- kubi/src/camera/matrices.rs | 1 - kubi/src/networking.rs | 72 +++++++++++++++++++++++++---- kubi/src/world.rs | 3 -- kubi/src/world/loading.rs | 2 +- 7 files changed, 71 insertions(+), 20 deletions(-) diff --git a/kubi-server/src/auth.rs b/kubi-server/src/auth.rs index 5a376a0..7ecaba0 100644 --- a/kubi-server/src/auth.rs +++ b/kubi-server/src/auth.rs @@ -16,6 +16,8 @@ pub fn authenticate_players( password } } = event { + log::info!("ClientHello from {} with username {} and password {:?}", from, username, password); + // Handle password auth if let Some(server_password) = &config.server.password { if let Some(user_password) = &password { @@ -34,12 +36,12 @@ pub fn authenticate_players( } //Spawn the user - // TODO + //TODO Spawn the user on server side //Approve the user server.0.send_message(*from, ServerToClientMessage::ServerHello { init: InitData { - users: todo!() + users: vec![] //TODO create init data } }).map_err(log_error).ok(); } diff --git a/kubi-server/src/chunk.rs b/kubi-server/src/chunk.rs index 26210cd..c214e43 100644 --- a/kubi-server/src/chunk.rs +++ b/kubi-server/src/chunk.rs @@ -1,9 +1,9 @@ +//TODO server-side chunk manager + pub struct Chunk { - //TODO } pub struct ChunkManager { - //TODO } pub fn server_chunk_response( diff --git a/kubi-shared/src/networking/state.rs b/kubi-shared/src/networking/state.rs index dd9a8b5..98492c3 100644 --- a/kubi-shared/src/networking/state.rs +++ b/kubi-shared/src/networking/state.rs @@ -1,7 +1,8 @@ use shipyard::{Unique, Component}; // disconnected => connect => join => load => ingame -#[derive(Unique, Component, PartialEq, Eq, Clone, Copy)] +#[derive(Unique, Component, PartialEq, Eq, Clone, Copy, Debug)] +#[repr(u8)] pub enum ClientJoinState { /// Not connected yet Disconnected, diff --git a/kubi/src/camera/matrices.rs b/kubi/src/camera/matrices.rs index 5f2c003..109c3de 100644 --- a/kubi/src/camera/matrices.rs +++ b/kubi/src/camera/matrices.rs @@ -20,7 +20,6 @@ fn update_perspective_matrix( mut vm_camera: ViewMut, resize: View, ) { - //TODO update on launch let Some(&size) = resize.iter().next() else { return }; diff --git a/kubi/src/networking.rs b/kubi/src/networking.rs index 8c50747..7a763a4 100644 --- a/kubi/src/networking.rs +++ b/kubi/src/networking.rs @@ -1,4 +1,4 @@ -use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter}; +use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter, WorkloadModificator}; use glium::glutin::event_loop::ControlFlow; use std::net::SocketAddr; use kubi_udp::client::{Client, ClientConfig, ClientEvent}; @@ -6,7 +6,6 @@ use kubi_shared::networking::{ messages::{ClientToServerMessage, ServerToClientMessage}, state::ClientJoinState }; - use crate::{events::EventComponent, control_flow::SetControlFlow}; #[derive(Unique, Clone, Copy, PartialEq, Eq)] @@ -36,15 +35,17 @@ fn create_client( storages.add_unique(ClientJoinState::Disconnected); } -fn connect_client_if_needed( +fn connect_client( mut client: UniqueViewMut ) { - //NOTE: this used to be a condition function - //but that caused some issues for no reason - if client.0.has_not_made_connection_attempts() { - log::info!("Connect called"); - client.0.connect().unwrap(); - } + log::info!("Connect called"); + client.0.connect().unwrap(); +} + +fn should_connect( + client: UniqueView +) -> bool { + client.0.has_not_made_connection_attempts() } fn update_client( @@ -67,12 +68,49 @@ fn insert_client_events( })); } +fn set_client_join_state_to_connected( + mut join_state: UniqueViewMut +) { + log::info!("Setting ClientJoinState"); + *join_state = ClientJoinState::Connected; +} + +fn say_hello( + client: UniqueViewMut, +) { + log::info!("Authenticating"); + client.0.send_message(ClientToServerMessage::ClientHello { + username: "Sbeve".into(), + password: None + }).unwrap(); +} + +fn check_server_hello_response( + network_events: View, + mut join_state: UniqueViewMut +) { + for event in network_events.iter() { + if let ClientEvent::MessageReceived(ServerToClientMessage::ServerHello { init }) = &event.0 { + log::info!("Joined the server!"); + //TODO handle init data + *join_state = ClientJoinState::Joined; + } + } +} + pub fn update_networking() -> Workload { ( create_client.run_if_missing_unique::(), - connect_client_if_needed, + connect_client.run_if(should_connect), update_client, insert_client_events, + ( + set_client_join_state_to_connected, + say_hello, + ).into_workload().run_if(if_just_connected), + ( + check_server_hello_response, + ).into_workload().run_if(is_join_state::<{ClientJoinState::Connected as u8}>) ).into_workload() } @@ -90,6 +128,20 @@ pub fn disconnect_on_exit( } } +// conditions + +fn if_just_connected( + network_events: View, +) -> bool { + network_events.iter().any(|event| matches!(event.0, ClientEvent::Connected(_))) +} + +fn is_join_state( + join_state: UniqueView +) -> bool { + (*join_state as u8) == STATE +} + pub fn is_multiplayer( game_type: UniqueView ) -> bool { diff --git a/kubi/src/world.rs b/kubi/src/world.rs index 03d91c2..50664d2 100644 --- a/kubi/src/world.rs +++ b/kubi/src/world.rs @@ -18,9 +18,6 @@ use chunk::{Chunk, ChunkMesh, CHUNK_SIZE}; use tasks::ChunkTaskManager; use queue::BlockUpdateQueue; -//TODO separate world struct for render data -// because this is not send-sync - #[derive(Default, Unique)] pub struct ChunkStorage { pub chunks: HashMap diff --git a/kubi/src/world/loading.rs b/kubi/src/world/loading.rs index 0b7338c..ec5e7ca 100644 --- a/kubi/src/world/loading.rs +++ b/kubi/src/world/loading.rs @@ -34,7 +34,7 @@ pub fn update_chunks_if_player_moved( mut vm_world: UniqueViewMut, ) { //Check if the player actually moved - //TODO fix this also triggers on rotation, only activate when the player crosses the chnk border + //TODO fix this also triggers on rotation, only activate when the player crosses the chunk border let Some((_, transform)) = (&v_local_player, v_transform.inserted_or_modified()).iter().next() else { return };