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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Models for the endpoint "Server Activity".
//!
//! About the endpoint "Server Activity",
//! see the [API document](https://tetr.io/about/api/#generalactivity).
use crate::model::prelude::*;
/// An array of user activity over the last 2 days.
/// A user is seen as active if they logged in or received XP within the last 30 minutes.
#[derive(Clone, Debug, Deserialize)]
#[non_exhaustive]
pub struct ServerActivity {
/// The array of plot points, newest points first.
pub activity: Vec<u32>,
}
impl ServerActivity {
/// Returns the peak point of the activity.
///
/// If the activity is empty, `None` is returned.
pub fn peak(&self) -> Option<u32> {
self.activity.iter().max().copied()
}
/// Returns the index of the peak point of the activity.
///
/// If several points are equally maximum, the first one is returned.
/// If the activity is empty, `None` is returned.
pub fn peak_index(&self) -> Option<usize> {
self.activity
.iter()
.enumerate()
.max_by_key(|(_, &v)| v)
.map(|(i, _)| i)
}
/// Returns the trough point of the activity.
///
/// If the activity is empty, `None` is returned.
pub fn trough(&self) -> Option<u32> {
self.activity.iter().min().copied()
}
/// Returns the index of the trough point of the activity.
///
/// If several points are equally minimum, the first one is returned.
/// If the activity is empty, `None` is returned.
pub fn trough_index(&self) -> Option<usize> {
self.activity
.iter()
.enumerate()
.min_by_key(|(_, &v)| v)
.map(|(i, _)| i)
}
/// Returns the average of the activity.
///
/// If the activity is empty, `None` is returned.
pub fn average(&self) -> Option<f64> {
let len = self.activity.len() as f64;
if 0.0 < len {
Some(self.activity.iter().sum::<u32>() as f64 / len)
} else {
None
}
}
}
impl AsRef<ServerActivity> for ServerActivity {
fn as_ref(&self) -> &Self {
self
}
}