Skip to content

Commit

Permalink
Apply suggestions from @UkoeHB
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 2, 2023
1 parent 108c817 commit 27f0084
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion benches/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<DummyComponent>();

Expand Down
27 changes: 19 additions & 8 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => (),
}
}
}
Expand Down Expand Up @@ -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,
}
Expand Down
3 changes: 1 addition & 2 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions tests/replication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
));
}

Expand All @@ -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)),
));
}

Expand All @@ -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::<TableComponent>();
}
Expand Down Expand Up @@ -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::<TableComponent>()
.replicate::<SparseSetComponent>()
Expand Down Expand Up @@ -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::<TableComponent>();
}
Expand Down Expand Up @@ -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)),
));
}

Expand Down
13 changes: 6 additions & 7 deletions tests/server_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<DummyEvent>(SendPolicy::Ordered);
}
Expand Down Expand Up @@ -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::<DummyEvent>(SendPolicy::Ordered);
}
Expand Down Expand Up @@ -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::<ReflectedValue>()
.add_server_reflect_event::<ReflectEvent, ReflectEventSerializer, ReflectEventDeserializer>(
Expand Down Expand Up @@ -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::<ReflectedValue>()
.add_mapped_server_reflect_event::<ReflectEvent, ReflectEventSerializer, ReflectEventDeserializer>(SendPolicy::Ordered);
Expand Down Expand Up @@ -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::<DummyEvent>(SendPolicy::Ordered)
.add_systems(Update, ServerPlugin::increment_tick); // Increment tick every frame per `TickPolicy::Manual`.
.add_server_event::<DummyEvent>(SendPolicy::Ordered);

const DUMMY_CLIENT_ID: u64 = 1;
for (mode, events_count) in [
Expand Down

0 comments on commit 27f0084

Please sign in to comment.