From 8f86a7997fb373f308c7728a304eac457922c19e Mon Sep 17 00:00:00 2001 From: Elias Stepanik Date: Fri, 4 Apr 2025 04:01:57 +0200 Subject: [PATCH] Basic Client server combo --- client/Cargo.toml | 3 +- client/src/app.rs | 2 +- client/src/helper/database.rs | 119 ------------------ client/src/helper/mod.rs | 1 - .../plugins/camera/systems/camera_system.rs | 12 ++ .../environment/systems/environment_system.rs | 9 +- client/src/plugins/mod.rs | 4 +- client/src/plugins/network/mod.rs | 2 + client/src/plugins/network/network_plugin.rs | 13 ++ .../src/plugins/network/systems/callbacks.rs | 50 ++++++++ .../src/plugins/network/systems/connection.rs | 31 +++++ .../src/plugins/network/systems/database.rs | 61 +++++++++ client/src/plugins/network/systems/mod.rs | 4 + .../plugins/network/systems/subscriptions.rs | 23 ++++ publish_server.bat | 2 +- server/src/lib.rs | 49 +++++++- server/src/types/mod.rs | 2 + server/src/types/player.rs | 31 +++++ server/src/types/vec3.rs | 20 +++ watch_logs.bat | 2 + 20 files changed, 304 insertions(+), 136 deletions(-) delete mode 100644 client/src/helper/database.rs create mode 100644 client/src/plugins/network/mod.rs create mode 100644 client/src/plugins/network/network_plugin.rs create mode 100644 client/src/plugins/network/systems/callbacks.rs create mode 100644 client/src/plugins/network/systems/connection.rs create mode 100644 client/src/plugins/network/systems/database.rs create mode 100644 client/src/plugins/network/systems/mod.rs create mode 100644 client/src/plugins/network/systems/subscriptions.rs create mode 100644 server/src/types/mod.rs create mode 100644 server/src/types/player.rs create mode 100644 server/src/types/vec3.rs create mode 100644 watch_logs.bat diff --git a/client/Cargo.toml b/client/Cargo.toml index 26a7438..cece379 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -17,4 +17,5 @@ bevy_render = "0.15.0" bevy_window = "0.15.0" egui_dock = "0.14.0" spacetimedb-sdk = "1.0" -hex = "0.4" \ No newline at end of file +hex = "0.4" +random_word = { version = "0.5.0", features = ["en"] } \ No newline at end of file diff --git a/client/src/app.rs b/client/src/app.rs index c765f6f..149e815 100644 --- a/client/src/app.rs +++ b/client/src/app.rs @@ -7,7 +7,7 @@ use crate::helper::*; use bevy_egui::EguiSet; use bevy_render::extract_resource::ExtractResourcePlugin; use spacetimedb_sdk::{credentials, DbContext, Error, Event, Identity, Status, Table, TableWithPrimaryKey}; -use crate::helper::database::setup_database; +use crate::plugins::network::systems::database::setup_database; use crate::module_bindings::DbConnection; pub struct AppPlugin; diff --git a/client/src/helper/database.rs b/client/src/helper/database.rs deleted file mode 100644 index 6d9bb0b..0000000 --- a/client/src/helper/database.rs +++ /dev/null @@ -1,119 +0,0 @@ - -use bevy::prelude::{Commands, Resource}; -use bevy::utils::info; -use spacetimedb_sdk::{credentials, DbContext, Error, Event, Identity, Status, Table, TableWithPrimaryKey}; -use crate::module_bindings::*; - -/// The URI of the SpacetimeDB instance hosting our chat module. -const HOST: &str = "http://192.168.178.10:3000"; - -/// The database name we chose when we published our module. -const DB_NAME: &str = "horror-game"; - -#[derive(Resource)] -pub struct DbConnectionResource(pub(crate) DbConnection); - -pub fn setup_database(mut commands: Commands) { - // Call your connection function and insert the connection as a resource. - let ctx = connect_to_db(); - - register_callbacks(&ctx); - - // Subscribe to SQL queries in order to construct a local partial replica of the database. - subscribe_to_tables(&ctx); - - // Spawn a thread, where the connection will process messages and invoke callbacks. - ctx.run_threaded(); - - - commands.insert_resource(DbConnectionResource(ctx)); -} - - -/// Register subscriptions for all rows of both tables. -fn subscribe_to_tables(ctx: &DbConnection) { - ctx.subscription_builder() - .on_applied(on_sub_applied) - .on_error(on_sub_error) - .subscribe(["SELECT * FROM physics_world"]); -} - -/// Our `on_subscription_applied` callback: -/// sort all past messages and print them in timestamp order. -fn on_sub_applied(ctx: &SubscriptionEventContext) { - println!("Fully connected and all subscriptions applied."); - println!("Use /name to set your name, or type a message!"); -} - -/// Or `on_error` callback: -/// print the error, then exit the process. -fn on_sub_error(_ctx: &ErrorContext, err: Error) { - eprintln!("Subscription failed: {}", err); - std::process::exit(1); -} - -fn connect_to_db() -> DbConnection { - DbConnection::builder() - // Register our `on_connect` callback, which will save our auth token. - .on_connect(on_connected) - // Register our `on_connect_error` callback, which will print a message, then exit the process. - /*.on_connect_error(on_connect_error)*/ - // Our `on_disconnect` callback, which will print a message, then exit the process. - .on_disconnect( on_disconnected) - // If the user has previously connected, we'll have saved a token in the `on_connect` callback. - // In that case, we'll load it and pass it to `with_token`, - // so we can re-authenticate as the same `Identity`. - .with_token(creds_store().load().expect("Error loading credentials")) - // Set the database name we chose when we called `spacetime publish`. - .with_module_name(DB_NAME) - // Set the URI of the SpacetimeDB host that's running our database. - .with_uri(HOST) - // Finalize configuration and connect! - .build() - .expect("Failed to connect") -} - -/// Register all the callbacks our app will use to respond to database events. -fn register_callbacks(ctx: &DbConnection) { - - /*// When a new message is received, print it. - ctx.db.message().on_insert(on_message_inserted);*/ - - /*// When we fail to set our name, print a warning. - ctx.reducers.on_set_name(on_name_set);*/ - - // When we fail to send a message, print a warning. - /*ctx.reducers.on_send_message(on_message_sent);*/ -} - - - -fn creds_store() -> credentials::File { - credentials::File::new("quickstart-chat") -} - -/// Our `on_connect` callback: save our credentials to a file. -fn on_connected(_ctx: &DbConnection, _identity: Identity, token: &str) { - if let Err(e) = creds_store().save(token) { - eprintln!("Failed to save credentials: {:?}", e); - } -} - - - -/// Our `on_connect_error` callback: print the error, then exit the process. -fn on_connect_error(_ctx: &ErrorContext, err: Error) { - eprintln!("Connection error: {:?}", err); - std::process::exit(1); -} - -/// Our `on_disconnect` callback: print a note, then exit the process. -fn on_disconnected(_ctx: &ErrorContext, err: Option) { - if let Some(err) = err { - eprintln!("Disconnected: {}", err); - std::process::exit(1); - } else { - println!("Disconnected."); - std::process::exit(0); - } -} \ No newline at end of file diff --git a/client/src/helper/mod.rs b/client/src/helper/mod.rs index 660483c..8aa673e 100644 --- a/client/src/helper/mod.rs +++ b/client/src/helper/mod.rs @@ -1,3 +1,2 @@ pub mod debug_gizmos; pub mod egui_dock; -pub mod database; diff --git a/client/src/plugins/camera/systems/camera_system.rs b/client/src/plugins/camera/systems/camera_system.rs index 64c80f0..04ac0f5 100644 --- a/client/src/plugins/camera/systems/camera_system.rs +++ b/client/src/plugins/camera/systems/camera_system.rs @@ -4,6 +4,9 @@ use bevy::math::Vec3; use bevy::prelude::*; use bevy_render::camera::{Exposure, PhysicalCameraParameters, Projection}; use bevy_window::CursorGrabMode; +use random_word::Lang; +use crate::module_bindings::set_name; +use crate::plugins::network::systems::database::DbConnectionResource; #[derive(Component)] pub struct CameraController { @@ -54,6 +57,7 @@ pub fn camera_controller_system( mut windows: Query<&mut Window>, mut query: Query<(&mut Transform, &mut CameraController)>, mut app_exit_events: EventWriter, + mut ctx: ResMut, ) { let mut window = windows.single_mut(); let (mut transform, mut controller) = query.single_mut(); @@ -92,6 +96,12 @@ pub fn camera_controller_system( } } + let word = random_word::get(Lang::En); + + if keyboard_input.just_pressed(KeyCode::KeyQ) { + ctx.0.reducers.set_name(word.to_string()).unwrap(); + } + // ==================== // 3) Handle Keyboard Movement (WASD, Space, Shift) // ==================== @@ -147,6 +157,8 @@ pub fn camera_controller_system( } } + + // ======================= // 7) Exit on Escape // ======================= diff --git a/client/src/plugins/environment/systems/environment_system.rs b/client/src/plugins/environment/systems/environment_system.rs index e9f4e22..5fcf3b5 100644 --- a/client/src/plugins/environment/systems/environment_system.rs +++ b/client/src/plugins/environment/systems/environment_system.rs @@ -1,18 +1,15 @@ use bevy::prelude::*; -use crate::helper::database::DbConnectionResource; - - -pub fn init(mut commands: Commands, db_connection: Res) { +pub fn init(mut commands: Commands) { } -pub fn fixed_update(time: Res