From 27f008432d8232236167e28dcc9966c4b1235cc8 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Tue, 3 Oct 2023 01:46:39 +0300 Subject: [PATCH] Apply suggestions from @UkoeHB --- benches/replication.rs | 2 +- src/server.rs | 27 +++++++++++++++++++-------- tests/common/mod.rs | 3 +-- tests/replication.rs | 12 ++++++------ tests/server_event.rs | 13 ++++++------- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/benches/replication.rs b/benches/replication.rs index 1dc71e49..8f270ccd 100644 --- a/benches/replication.rs +++ b/benches/replication.rs @@ -157,7 +157,7 @@ fn replication(c: &mut Criterion) { fn setup_app(app: &mut App) { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )) .replicate::(); diff --git a/src/server.rs b/src/server.rs index e75e8b95..f00b00c5 100644 --- a/src/server.rs +++ b/src/server.rs @@ -80,14 +80,23 @@ impl Plugin for ServerPlugin { ), ); - if let TickPolicy::MaxTickRate(max_tick_rate) = self.tick_policy { - let tick_time = Duration::from_millis(1000 / max_tick_rate as u64); - app.add_systems( - PostUpdate, - Self::increment_tick - .before(Self::diffs_sending_system) - .run_if(on_timer(tick_time)), - ); + match self.tick_policy { + TickPolicy::MaxTickRate(max_tick_rate) => { + let tick_time = Duration::from_millis(1000 / max_tick_rate as u64); + app.add_systems( + PostUpdate, + Self::increment_tick + .before(Self::diffs_sending_system) + .run_if(on_timer(tick_time)), + ); + } + TickPolicy::EveryFrame => { + app.add_systems( + PostUpdate, + Self::increment_tick.before(Self::diffs_sending_system), + ); + } + TickPolicy::Manual => (), } } } @@ -386,6 +395,8 @@ pub enum TickPolicy { /// /// By default it's 30 updates per second. MaxTickRate(u16), + /// Send updates from server every frame. + EveryFrame, /// [`ServerSet::Send`] should be manually configured. Manual, } diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 35006038..7b85944c 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -47,8 +47,7 @@ pub(super) fn connect(server_app: &mut App, client_app: &mut App) { server_app .insert_resource(server) - .insert_resource(server_transport) - .add_systems(Update, ServerPlugin::increment_tick); // Increment tick every frame per `TickPolicy::Manual`. + .insert_resource(server_transport); client_app .insert_resource(client) diff --git a/tests/replication.rs b/tests/replication.rs index 2a2f239d..27273c4d 100644 --- a/tests/replication.rs +++ b/tests/replication.rs @@ -13,7 +13,7 @@ fn acked_ticks_cleanup() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )); } @@ -38,7 +38,7 @@ fn tick_acks_receiving() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )); } @@ -63,7 +63,7 @@ fn spawn_replication() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )) .replicate::(); } @@ -100,7 +100,7 @@ fn insert_replication() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )) .replicate::() .replicate::() @@ -155,7 +155,7 @@ fn removal_replication() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )) .replicate::(); } @@ -199,7 +199,7 @@ fn despawn_replication() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )); } diff --git a/tests/server_event.rs b/tests/server_event.rs index 5549d806..56f42d4c 100644 --- a/tests/server_event.rs +++ b/tests/server_event.rs @@ -34,7 +34,7 @@ fn sending_receiving() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )) .add_server_event::(SendPolicy::Ordered); } @@ -79,7 +79,7 @@ fn sending_receiving_and_mapping() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )) .add_mapped_server_event::(SendPolicy::Ordered); } @@ -120,7 +120,7 @@ fn sending_receiving_reflect() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )) .register_type::() .add_server_reflect_event::( @@ -171,7 +171,7 @@ fn sending_receiving_and_mapping_reflect() { for app in [&mut server_app, &mut client_app] { app.add_plugins(( MinimalPlugins, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )) .register_type::() .add_mapped_server_reflect_event::(SendPolicy::Ordered); @@ -214,10 +214,9 @@ fn local_resending() { let mut app = App::new(); app.add_plugins(( TimePlugin, - ReplicationPlugins.set(ServerPlugin::new(TickPolicy::Manual)), + ReplicationPlugins.set(ServerPlugin::new(TickPolicy::EveryFrame)), )) - .add_server_event::(SendPolicy::Ordered) - .add_systems(Update, ServerPlugin::increment_tick); // Increment tick every frame per `TickPolicy::Manual`. + .add_server_event::(SendPolicy::Ordered); const DUMMY_CLIENT_ID: u64 = 1; for (mode, events_count) in [