use crate::entity::EntityId;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct CurrentTick(
)` entry.
pub u64,
);
pub const DEFAULT_ARRIVAL_WINDOW_TICKS: u64 = 18_000;
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ArrivalLogRetention(pub u64);
impl Default for ArrivalLogRetention {
fn default() -> Self {
Self(DEFAULT_ARRIVAL_WINDOW_TICKS)
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ArrivalLog {
entries: Vec<(u64, EntityId)>,
}
impl ArrivalLog {
pub fn record(&mut self, tick: u64, stop: EntityId) {
self.entries.push((tick, stop));
}
#[must_use]
pub fn arrivals_in_window(&self, stop: EntityId, now: u64, window_ticks: u64) -> u64 {
if window_ticks == 0 {
return 0;
}
let lower = now.saturating_sub(window_ticks);
self.entries
.iter()
.filter(|(t, s)| *s == stop && *t >= lower && *t <= now)
.count() as u64
}
pub fn prune_before(&mut self, cutoff: u64) {
self.entries.retain(|(t, _)| *t >= cutoff);
}
#[must_use]
pub const fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn remap_entity_ids(&mut self, id_remap: &std::collections::HashMap<EntityId, EntityId>) {
self.entries
.retain_mut(|(_, stop)| match id_remap.get(stop) {
Some(&new) => {
*stop = new;
true
}
None => false,
});
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DestinationLog {
entries: Vec<(u64, EntityId)>,
}
impl DestinationLog {
pub fn record(&mut self, tick: u64, destination: EntityId) {
self.entries.push((tick, destination));
}
#[must_use]
pub fn destinations_in_window(&self, stop: EntityId, now: u64, window_ticks: u64) -> u64 {
if window_ticks == 0 {
return 0;
}
let lower = now.saturating_sub(window_ticks);
self.entries
.iter()
.filter(|(t, s)| *s == stop && *t >= lower && *t <= now)
.count() as u64
}
pub fn prune_before(&mut self, cutoff: u64) {
self.entries.retain(|(t, _)| *t >= cutoff);
}
#[must_use]
pub const fn len(&self) -> usize {
self.entries.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn remap_entity_ids(&mut self, id_remap: &std::collections::HashMap<EntityId, EntityId>) {
self.entries
.retain_mut(|(_, stop)| match id_remap.get(stop) {
Some(&new) => {
*stop = new;
true
}
None => false,
});
}
}