veloren/server/src/sys/mod.rs

84 lines
2.1 KiB
Rust
Raw Normal View History

2021-02-06 06:15:25 +00:00
pub mod agent;
pub mod entity_sync;
pub mod invite_timeout;
pub mod metrics;
pub mod msg;
pub mod object;
pub mod persistence;
2019-11-04 00:57:36 +00:00
pub mod sentinel;
pub mod subscription;
pub mod terrain;
pub mod terrain_sync;
pub mod waypoint;
2021-04-15 16:28:16 +00:00
pub mod wiring;
2021-04-04 03:04:02 +00:00
use common_ecs::{dispatch, run_now, System};
use common_systems::{melee, projectile};
use specs::DispatcherBuilder;
use std::{
marker::PhantomData,
time::{Duration, Instant},
};
2019-10-20 07:20:21 +00:00
pub type PersistenceScheduler = SysScheduler<persistence::Sys>;
pub fn add_server_systems(dispatch_builder: &mut DispatcherBuilder) {
2021-04-04 03:04:02 +00:00
dispatch::<melee::Sys>(dispatch_builder, &[&projectile::Sys::sys_name()]);
2021-04-06 23:20:26 +00:00
//Note: server should not depend on interpolation system
dispatch::<agent::Sys>(dispatch_builder, &[]);
dispatch::<terrain::Sys>(dispatch_builder, &[]);
dispatch::<waypoint::Sys>(dispatch_builder, &[]);
dispatch::<invite_timeout::Sys>(dispatch_builder, &[]);
dispatch::<persistence::Sys>(dispatch_builder, &[]);
dispatch::<object::Sys>(dispatch_builder, &[]);
2021-04-15 16:28:16 +00:00
dispatch::<wiring::Sys>(dispatch_builder, &[]);
}
2019-10-20 07:20:21 +00:00
pub fn run_sync_systems(ecs: &mut specs::World) {
// Setup for entity sync
// If I'm not mistaken, these two could be ran in parallel
run_now::<sentinel::Sys>(ecs);
run_now::<subscription::Sys>(ecs);
// Sync
run_now::<terrain_sync::Sys>(ecs);
run_now::<entity_sync::Sys>(ecs);
}
/// Used to schedule systems to run at an interval
pub struct SysScheduler<S> {
interval: Duration,
last_run: Instant,
_phantom: PhantomData<S>,
}
impl<S> SysScheduler<S> {
pub fn every(interval: Duration) -> Self {
Self {
interval,
last_run: Instant::now(),
_phantom: PhantomData,
}
}
pub fn should_run(&mut self) -> bool {
if self.last_run.elapsed() > self.interval {
self.last_run = Instant::now();
true
} else {
false
}
}
}
impl<S> Default for SysScheduler<S> {
fn default() -> Self {
Self {
interval: Duration::from_secs(30),
last_run: Instant::now(),
_phantom: PhantomData,
}
}
}