hypixel 0.1.0

Rust wrapper for the Hypixel public API.
Documentation
use serde::Deserialize;
use crate::Error;
use crate::api::http;
use crate::handler::player_data::Client;

/// This represents the response from the Hypixel player data endpoint.
#[derive(Deserialize)]
pub struct PlayerResponse {
    /// This is the actual player data.
    pub player: Player
}

/// This represents the player object from Hypixel's player data
/// endpoint.
#[derive(Debug, Deserialize)]
pub struct Player {
    /// UUID of the player, you're probably not going to use this.
    pub uuid: String,
    /// Display name of the player.
    #[serde(rename = "displayname")]
    pub display_name: Option<String>,
    /// Rank of the player, in String representation.
    pub rank: Option<String>,
    /// Package rank of the player.
    #[serde(rename = "packageRank")]
    pub package_rank: Option<String>,
    /// New package rank of the player, whatever that means.
    #[serde(rename = "newPackageRank")]
    pub new_package_rank: Option<String>,
    /// First login time stamp.
    #[serde(rename = "firstLogin")]
    pub first_login: usize,
    /// Last login time stamp.
    #[serde(rename = "lastLogin")]
    pub last_login: usize,
    /// Last log out time stamp.
    #[serde(rename = "lastLogout")]
    pub last_logout: usize,
    /// One time achievements player has.
    #[serde(rename = "achievementsOneTime")]
    pub one_time_achievements: Vec<String>,
    /// Known aliases. Includes current name.
    #[serde(rename = "knownAliases")]
    pub known_aliases: Vec<String>,
    /// Player name.
    #[serde(rename = "playername")]
    pub name: String,
}

impl Player {
    /// Returns a list of friend objects that include a small amount of data.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of the Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run 
    /// for friend in player.friends(&client).await? {
    ///     let friend = client.player_from_uuid(&friend.uuid_receiver).await?;
    ///     
    ///     println!("Found: {}", friend.name);
    /// }
    /// ``` 
    /// 
    /// # Errors
    /// 
    /// Returns a `FailedResponse` error if the API key is invalid.
    pub async fn friends(&self, client: &Client)-> Result<Vec<Friend>, Error> {
        let res = http::fetch::<FriendsResponse>(
            &format!("friends?uuid={}&key={}", &self.uuid, client.key())
        ).await?;   

        Ok(res.friends)
    }

    /// Returns a list of recent games played by the player. This is empty if no recent
    /// games were played.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of the Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// let games = player.recent_games(&client).await?;
    /// // Something...
    /// ```
    /// 
    /// # Errors
    /// 
    /// Returns a `FailedResponse` error if the API key is invalid. 
    pub async fn recent_games(&self, client: &Client) -> Result<Vec<Game>, Error> {
        let res = http::fetch::<RecentGamesResponse>(
            &format!("recentgames?uuid={}&key={}", &self.uuid, client.key())
        ).await?;

        Ok(res.games)
    }

    /// Return the ranked skywars data of this player. 
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of the Client struct.
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// let data = player.ranked_skywars(&client).await?;
    /// // Something...
    /// ```
    /// 
    /// # Errors
    /// 
    /// Returns a `FailedResponse` error if the API key is invalid or no data could
    /// be found. 
    pub async fn ranked_skywars(&self, client: &Client) -> Result<RankedSkywars, Error> {
        let res = http::fetch::<RankedSkywarsResponse>(
            &format!("player/ranked/skywars?uuid={}&key={}", &self.uuid, client.key())
        ).await?;

        Ok(res.result)
    }

    /// Returns a Hypixel guild from a player. This can be nothing if it does 
    /// not match any existing guilds.
    /// 
    /// # Arguments
    /// 
    /// * `client` - Instance of the Client struct.
    /// 
    /// # Errors
    /// 
    /// Returns a `FailedResponse` error if the API key is invalid.
    pub async fn guild(&self, client: &Client) -> Result<Option<Guild>, Error> {
        let res = http::fetch::<GuildResponse>(
            &format!("guild?player={}&key={}", &self.uuid, client.key())
        ).await?;

        Ok(res.guild)
    }
}

/// This represents the response from the Hypixel player friends endpoint.
#[derive(Deserialize)]
pub struct FriendsResponse {
    /// Actual data.
    #[serde(rename = "records")]
    pub friends: Vec<Friend>
}

/// This represents a friend object from the Hypixel player friends endpoint.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Friend {
    /// UUID of request sender.
    pub uuid_sender: String,
    /// UUID of request receiver.
    pub uuid_receiver: String,
    /// Time stamp.
    pub started: usize
}

/// This represents the response from the Hypixel player recent games endpoint
#[derive(Deserialize)]
pub struct RecentGamesResponse {
    pub games: Vec<Game>
}

/// This represents a game object from the recent games endpoint.
#[derive(Debug, Deserialize)]
pub struct Game {
    /// When the game was played.
    pub date: usize,
    /// Game type.
    #[serde(rename = "gameType")]
    pub game_type: String,
    /// Game mode.
    pub mode: String,
    /// Map name of the game.
    pub map: String,
    /// When the game ended.
    pub ended: usize
}

/// Represents response from Hypixel player ranked skywars endpoint.
#[derive(Deserialize)]
pub struct RankedSkywarsResponse {
    /// Actual data.
    pub result: RankedSkywars
}

/// This represents the result object from the ranked skywars Hypixel endpoint.
#[derive(Debug, Deserialize)]
pub struct RankedSkywars {
    /// This is a String that includes the current month and year.
    pub key: String,
    /// Postition of the player.
    pub position: usize,
    /// Score of the player.
    pub score: usize
}

/// Represents response from the Hypixel guild endpoint. 
#[derive(Deserialize)]
pub struct GuildResponse {
    /// Actual data.
    pub guild: Option<Guild>
}

/// This represents the guild object from the Hypixel guild endpoint.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Guild {
    /// Name of the clan.
    pub name: String,
    /// Coins the clan posesses.
    pub coins: usize,
    /// All time coins.
    pub coins_ever: usize,
    /// When the guild was created.
    pub created: usize,
    /// List of guild members.
    pub members: Vec<GuildMember>,
    /// Guild experience.
    pub exp: usize,
    /// Experience for each game type.
    pub guild_exp_by_game_type: GuildExpDist
}

/// This represents a guild member contained in the Guild struct.
#[derive(Debug, Deserialize)]
pub struct GuildMember {
    /// UUID of the member.
    pub uuid: String,
    /// Rank of the member in the guild.
    pub rank: String,
    /// When the player joined the guild.
    pub joined: usize
}

/// This represents the experience distribution for different game modes for a guild.
/// Each field corresponds to their game mode.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub struct GuildExpDist {
    pub build_battle: usize,
    pub prototype: usize,
    pub walls: usize,
    pub battleground: usize,
    pub murder_mystery: usize,
    pub arena: usize,
    pub duels: usize,
    pub skyblock: usize,
    pub housing: usize,
    pub paintball: usize,
    pub vampirez: usize,
    pub pit: usize,
    pub mcgo: usize,
    pub walls3: usize,
    pub super_smash: usize,
    pub tntgames: usize,
    pub quakecraft: usize,
    pub skywars: usize,
    pub bedwars: usize,
    pub survival_games: usize,
    pub wool_games: usize,
    pub legacy: usize,
    pub replay: usize,
    pub speed_uhc: usize,
    pub smp: usize,
    pub gingerbread: usize,
    pub uhc: usize,
    pub arcade: usize
}