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
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
//! Interpreting shard info calls.

use crate::{
    data,
    error::{ApiError, Result},
    EndpointResult,
};

/// Shard info raw result.
#[derive(serde_derive::Deserialize, Clone, Debug)]
pub(crate) struct Response {
    ok: i32,
    shards: Vec<ShardResponse>,
}

#[derive(serde_derive::Deserialize, Clone, Debug)]
struct ShardResponse {
    users: u32,
    name: String,
    tick: f64,
    rooms: u32,
}

/// Structure describing information about a single game shard.
#[derive(Clone, Debug)]
pub struct ShardInfo {
    /// The name of this shard, useful for all shard-specific API calls.
    pub name: String,
    /// The total number of open rooms in this shard.
    pub room_count: u32,
    /// The total number of users spawned in this shard (TODO: confirm this is what this is).
    pub user_count: u32,
    /// The average millisecond tick this shard has for some past period of time (TODO: more detail).
    pub tick_avg_milliseconds: f64,
    /// Phantom data in order to allow adding any additional fields in the future.
    _non_exhaustive: (),
}

impl AsRef<str> for ShardInfo {
    fn as_ref(&self) -> &str {
        &self.name
    }
}

impl EndpointResult for Vec<ShardInfo> {
    type RequestResult = Response;
    type ErrorResult = data::ApiError;

    fn from_raw(raw: Response) -> Result<Vec<ShardInfo>> {
        let Response { ok, shards } = raw;

        if ok != 1 {
            return Err(ApiError::NotOk(ok).into());
        }

        Ok(shards
            .into_iter()
            .map(|response| {
                let ShardResponse {
                    users,
                    name,
                    tick,
                    rooms,
                } = response;
                ShardInfo {
                    name: name,
                    room_count: rooms,
                    user_count: users,
                    tick_avg_milliseconds: tick,
                    _non_exhaustive: (),
                }
            })
            .collect())
    }
}

#[cfg(test)]
mod tests {
    use super::ShardInfo;
    use crate::EndpointResult;
    use serde_json;

    fn test_parse(json: serde_json::Value) {
        let response = serde_json::from_value(json).unwrap();

        let _ = Vec::<ShardInfo>::from_raw(response).unwrap();
    }

    #[test]
    fn parse_sample() {
        test_parse(json! ({
            "shards": [
                {
                    "users": 1246,
                    "rooms": 28858,
                    "name": "shard0",
                    "tick": 5726.411601456489
                },
                {
                    "users": 584,
                    "rooms": 4816,
                    "name": "shard1",
                    "tick": 2153.4476171877614
                }
            ],
            "ok": 1
        }));
    }
}