irelia_cli/
in_game.rs

1//! Module for the `LoL` `in_game` API, docs have been copied from their [official counterparts](https://developer.riotgames.com/docs/lol#game-client-api)
2//!
3//! All types are all generated from the official JSON snippets
4
5pub mod types;
6
7use serde::de::DeserializeOwned;
8
9use crate::{Error, RequestClient};
10
11use self::types::{
12    Abilities, ActivePlayer, AllGameData, AllPlayer, Events, FullRunes, GameData, Item, Runes,
13    Scores, SummonerSpells, TeamID,
14};
15
16/// The only url the in game API can be used on
17pub const URL: &str = "127.0.0.1:2999";
18
19/// Struct that represents a connection to the in game api client
20/// Because the URL is constant, this is a zero sized struct to help organize code
21pub struct GameClient;
22
23impl GameClient {
24    #[must_use]
25    pub fn new() -> GameClient {
26        GameClient
27    }
28
29    #[must_use]
30    /// Returns the url, which is currently static
31    pub fn url(&self) -> &str {
32        URL
33    }
34
35    //noinspection SpellCheckingInspection
36    /// Get all available data.
37    ///
38    /// A sample response can be found [here](https://static.developer.riotgames.com/docs/lol/liveclientdata_sample.json).
39    ///
40    /// # Errors
41    /// This will return an error if the game API is not running
42    pub async fn all_game_data(
43        &self,
44        request_client: &RequestClient,
45    ) -> Result<AllGameData, Error> {
46        self.live_client("allgamedata", None, request_client).await
47    }
48
49    //noinspection SpellCheckingInspection
50    /// Get all data about the active player.
51    ///
52    /// # Errors
53    /// This will return an error if the game API is not running
54    pub async fn active_player(
55        &self,
56        request_client: &RequestClient,
57    ) -> Result<ActivePlayer, Error> {
58        self.live_client("activeplayer", None, request_client).await
59    }
60
61    //noinspection SpellCheckingInspection
62    /// Returns the player name.
63    ///
64    /// # Errors
65    /// This will return an error if the game API is not running
66    pub async fn active_player_name(
67        &self,
68        request_client: &RequestClient,
69    ) -> Result<String, Error> {
70        self.live_client("activeplayername", None, request_client)
71            .await
72    }
73
74    //noinspection SpellCheckingInspection
75    /// Get Abilities for the active player.    
76    ///
77    /// # Errors
78    /// This will return an error if the game API is not running
79    pub async fn active_player_abilities(
80        &self,
81        request_client: &RequestClient,
82    ) -> Result<Abilities, Error> {
83        self.live_client("activeplayerabilities", None, request_client)
84            .await
85    }
86
87    //noinspection SpellCheckingInspection
88    /// Retrieve the full list of runes for the active player.
89    ///
90    /// # Errors
91    /// This will return an error if the game API is not running
92    pub async fn active_player_runes(
93        &self,
94        request_client: &RequestClient,
95    ) -> Result<FullRunes, Error> {
96        self.live_client("activeplayerrunes", None, request_client)
97            .await
98    }
99
100    //noinspection SpellCheckingInspection
101    /// Retrieve the list of heroes in the game and their stats.
102    ///
103    /// # Errors
104    /// This will return an error if the game API is not running
105    pub async fn player_list(
106        &self,
107        team: Option<TeamID>,
108        request_client: &RequestClient,
109    ) -> Result<Vec<AllPlayer>, Error> {
110        let team = team.map_or_else(
111            || "",
112            |team| match team {
113                TeamID::ALL => "?teamID=ALL",
114                TeamID::UNKNOWN => "?teamID=UNKNOWN",
115                TeamID::ORDER => "?teamID=ORDER",
116                TeamID::CHAOS => "?teamID=CHAOS",
117                TeamID::NEUTRAL => "?teamID=NEUTRAL",
118            },
119        );
120
121        let endpoint = format!("playerlist{team}");
122        self.live_client(&endpoint, None, request_client).await
123    }
124
125    //noinspection SpellCheckingInspection
126    /// Retrieve the list of the current scores for the player.
127    ///
128    /// # Errors
129    /// This will return an error if the game API is not running
130    pub async fn player_scores(
131        &self,
132        summoner: &str,
133        request_client: &RequestClient,
134    ) -> Result<Scores, Error> {
135        self.live_client("playerscores", Some(summoner), request_client)
136            .await
137    }
138
139    //noinspection SpellCheckingInspection
140    /// Retrieve the list of the summoner spells for the player.
141    ///
142    /// # Errors
143    /// This will return an error if the game API is not running
144    pub async fn player_summoner_spells(
145        &self,
146        summoner: &str,
147        request_client: &RequestClient,
148    ) -> Result<SummonerSpells, Error> {
149        self.live_client("playersummonerspells", Some(summoner), request_client)
150            .await
151    }
152
153    //noinspection SpellCheckingInspection
154    /// Retrieve the basic runes of any player.
155    ///
156    /// # Errors
157    /// This will return an error if the game API is not running
158    pub async fn player_main_runes(
159        &self,
160        summoner: &str,
161        request_client: &RequestClient,
162    ) -> Result<Runes, Error> {
163        self.live_client("playermainrunes", Some(summoner), request_client)
164            .await
165    }
166
167    //noinspection SpellCheckingInspection
168    /// Retrieve the list of items for the player.
169    ///
170    /// # Errors
171    /// This will return an error if the game API is not running
172    pub async fn player_items(
173        &self,
174        summoner: &str,
175        request_client: &RequestClient,
176    ) -> Result<Vec<Item>, Error> {
177        self.live_client("playeritems", Some(summoner), request_client)
178            .await
179    }
180
181    //noinspection SpellCheckingInspection
182    /// Get a list of events that have occurred in the game.
183    ///
184    /// # Errors
185    /// This will return an error if the game API is not running
186    pub async fn event_data(
187        &self,
188        event_id: Option<i32>,
189        request_client: &RequestClient,
190    ) -> Result<Events, Error> {
191        let event_id = if let Some(id) = event_id {
192            format!("?eventID={id}")
193        } else {
194            String::new()
195        };
196        let endpoint = format!("eventdata{event_id}");
197        self.live_client(&endpoint, None, request_client).await
198    }
199
200    //noinspection SpellCheckingInspection
201    /// Basic data about the game.
202    ///
203    /// # Errors
204    /// This will return an error if the game API is not running
205    pub async fn game_stats(&self, request_client: &RequestClient) -> Result<GameData, Error> {
206        self.live_client("gamestats", None, request_client).await
207    }
208
209    //noinspection SpellCheckingInspection
210    async fn live_client<R>(
211        &self,
212        endpoint: &str,
213        summoner: Option<&str>,
214        request_client: &RequestClient,
215    ) -> Result<R, Error>
216    where
217        R: DeserializeOwned,
218    {
219        let endpoint = if let Some(summoner) = summoner {
220            format!("/liveclientdata/{endpoint}?summonerName={summoner}")
221        } else {
222            format!("/liveclientdata/{endpoint}")
223        };
224
225        request_client
226            .request_template(URL, &endpoint, "GET", None::<()>, None, |bytes| {
227                serde_json::from_slice(&bytes).map_err(Error::SerdeJsonError)
228            })
229            .await
230    }
231}
232
233impl Default for GameClient {
234    fn default() -> Self {
235        GameClient
236    }
237}