1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::{collections::VecDeque, time::Instant};
pub struct TicksCounter {
ticks: u64,
time: Instant,
inner: VecDeque<u64>,
}
impl Default for TicksCounter {
fn default() -> Self {
Self {
time: Instant::now(),
ticks: Default::default(),
inner: Default::default(),
}
}
}
impl TicksCounter {
pub fn new_with_time(time: Instant) -> Self {
Self {
time,
..Self::default()
}
}
pub fn new_tick(&mut self) {
self.ticks += 1;
self.inner.push_front(
Instant::now()
.duration_since(self.time)
.as_millis()
.try_into()
.expect(
"Hey anyone who sees this as a panic message. Is the universe still there?",
),
);
if self.inner.len() > 100 {
self.inner.drain(50..);
}
}
pub fn tick_rate(&self) -> Option<f64> {
Some(20_000.0 / ((self.inner.get(0)? - self.inner.get(20)?) as f64))
}
pub fn tick_num(&self) -> u64 {
self.ticks
}
}