From 0d5a94da230e1065e869421ef05fdfa1b91bda0f Mon Sep 17 00:00:00 2001 From: Elias Stepanik Date: Thu, 3 Apr 2025 11:07:42 +0200 Subject: [PATCH] Removed a lot of things that overcomplicate things. I will first try to focus on basic networking. --- NOTES.md | 6 + client/src/app.rs | 7 +- client/src/helper/database.rs | 119 ++++++++++++++++++ client/src/helper/mod.rs | 1 + client/src/main.rs | 2 + .../plugins/environment/environment_plugin.rs | 8 +- .../environment/systems/environment_system.rs | 20 ++- client/src/plugins/mod.rs | 2 +- publish_server.bat | 4 +- server/src/lib.rs | 36 +----- 10 files changed, 164 insertions(+), 41 deletions(-) create mode 100644 client/src/helper/database.rs diff --git a/NOTES.md b/NOTES.md index e69de29..cbf079f 100644 --- a/NOTES.md +++ b/NOTES.md @@ -0,0 +1,6 @@ +https://spacetimedb.com/docs/getting-started +https://spacetimedb.com/docs/modules/rust/quickstart +https://spacetimedb.com/docs/sdks/rust/quickstart + +https://winter.dev/articles/physics-engine +https://spacetimedb.com/docs/unity/part-4 \ No newline at end of file diff --git a/client/src/app.rs b/client/src/app.rs index b8e06f6..c765f6f 100644 --- a/client/src/app.rs +++ b/client/src/app.rs @@ -3,8 +3,12 @@ use crate::helper::egui_dock::{ reset_camera_viewport, set_camera_viewport, set_gizmo_mode, show_ui_system, UiState, }; use bevy::prelude::*; +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::module_bindings::DbConnection; pub struct AppPlugin; @@ -23,8 +27,9 @@ impl Plugin for AppPlugin { app.add_plugins(crate::plugins::camera::camera_plugin::CameraPlugin); app.add_plugins(crate::plugins::ui::ui_plugin::UiPlugin); - app.add_plugins(crate::plugins::environment::environment_plugin::EnvironmentPlugin); + app.add_systems(Startup, setup_database); + app.add_systems(Update, (debug_gizmos, toggle_ui_system)); app.add_systems( diff --git a/client/src/helper/database.rs b/client/src/helper/database.rs new file mode 100644 index 0000000..6d9bb0b --- /dev/null +++ b/client/src/helper/database.rs @@ -0,0 +1,119 @@ + +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 8aa673e..660483c 100644 --- a/client/src/helper/mod.rs +++ b/client/src/helper/mod.rs @@ -1,2 +1,3 @@ pub mod debug_gizmos; pub mod egui_dock; +pub mod database; diff --git a/client/src/main.rs b/client/src/main.rs index 2e10c8e..e32951f 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,6 +1,8 @@ mod app; mod helper; mod plugins; +mod module_bindings; + use crate::app::AppPlugin; use bevy::gizmos::{AppGizmoBuilder, GizmoPlugin}; use bevy::log::info; diff --git a/client/src/plugins/environment/environment_plugin.rs b/client/src/plugins/environment/environment_plugin.rs index 8e8c895..3bd2a9f 100644 --- a/client/src/plugins/environment/environment_plugin.rs +++ b/client/src/plugins/environment/environment_plugin.rs @@ -2,8 +2,12 @@ use bevy::app::{App, Plugin, Startup}; use bevy::color::palettes::basic::{GREEN, YELLOW}; use bevy::color::palettes::css::RED; use bevy::prelude::*; - +use crate::plugins::environment::systems::environment_system::*; pub struct EnvironmentPlugin; impl Plugin for EnvironmentPlugin { - fn build(&self, app: &mut App) {} + fn build(&self, app: &mut App) { + app.add_systems(Startup, init); + app.add_systems(FixedUpdate, fixed_update); + app.add_systems(Update, fixed_update); + } } diff --git a/client/src/plugins/environment/systems/environment_system.rs b/client/src/plugins/environment/systems/environment_system.rs index ff71782..e9f4e22 100644 --- a/client/src/plugins/environment/systems/environment_system.rs +++ b/client/src/plugins/environment/systems/environment_system.rs @@ -1,10 +1,18 @@ -use bevy::color::palettes::basic::*; -use bevy::color::palettes::css::{BEIGE, MIDNIGHT_BLUE, ORANGE, ORANGE_RED, SEA_GREEN}; -use bevy::math::*; -use bevy::pbr::{CascadeShadowConfigBuilder, NotShadowCaster}; + use bevy::prelude::*; +use crate::helper::database::DbConnectionResource; -pub fn setup(mut commands: Commands) { - // Insert the octree into the ECS + + + +pub fn init(mut commands: Commands, db_connection: Res) { } + +pub fn fixed_update(time: Res