irelia_cli/
in_game.rs

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! Module for the `LoL` `in_game` API, docs have been copied from their [official counterparts](https://developer.riotgames.com/docs/lol#game-client-api)
//!
//! All types are all generated from the official JSON snippets

pub mod types;

use serde::de::DeserializeOwned;

use crate::{Error, RequestClient};

use self::types::{
    Abilities, ActivePlayer, AllGameData, AllPlayer, Events, FullRunes, GameData, Item, Runes,
    Scores, SummonerSpells, TeamID,
};

/// The only url the in game API can be used on
pub const URL: &str = "127.0.0.1:2999";

/// Struct that represents a connection to the in game api client
/// Because the URL is constant, this is a zero sized struct to help organize code
pub struct GameClient;

impl GameClient {
    #[must_use]
    pub fn new() -> GameClient {
        GameClient
    }

    #[must_use]
    /// Returns the url, which is currently static
    pub fn url(&self) -> &str {
        URL
    }

    //noinspection SpellCheckingInspection
    /// Get all available data.
    ///
    /// A sample response can be found [here](https://static.developer.riotgames.com/docs/lol/liveclientdata_sample.json).
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn all_game_data(
        &self,
        request_client: &RequestClient,
    ) -> Result<AllGameData, Error> {
        self.live_client("allgamedata", None, request_client).await
    }

    //noinspection SpellCheckingInspection
    /// Get all data about the active player.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn active_player(
        &self,
        request_client: &RequestClient,
    ) -> Result<ActivePlayer, Error> {
        self.live_client("activeplayer", None, request_client).await
    }

    //noinspection SpellCheckingInspection
    /// Returns the player name.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn active_player_name(
        &self,
        request_client: &RequestClient,
    ) -> Result<String, Error> {
        self.live_client("activeplayername", None, request_client)
            .await
    }

    //noinspection SpellCheckingInspection
    /// Get Abilities for the active player.    
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn active_player_abilities(
        &self,
        request_client: &RequestClient,
    ) -> Result<Abilities, Error> {
        self.live_client("activeplayerabilities", None, request_client)
            .await
    }

    //noinspection SpellCheckingInspection
    /// Retrieve the full list of runes for the active player.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn active_player_runes(
        &self,
        request_client: &RequestClient,
    ) -> Result<FullRunes, Error> {
        self.live_client("activeplayerrunes", None, request_client)
            .await
    }

    //noinspection SpellCheckingInspection
    /// Retrieve the list of heroes in the game and their stats.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn player_list(
        &self,
        team: Option<TeamID>,
        request_client: &RequestClient,
    ) -> Result<Vec<AllPlayer>, Error> {
        let team = team.map_or_else(
            || "",
            |team| match team {
                TeamID::ALL => "?teamID=ALL",
                TeamID::UNKNOWN => "?teamID=UNKNOWN",
                TeamID::ORDER => "?teamID=ORDER",
                TeamID::CHAOS => "?teamID=CHAOS",
                TeamID::NEUTRAL => "?teamID=NEUTRAL",
            },
        );

        let endpoint = format!("playerlist{team}");
        self.live_client(&endpoint, None, request_client).await
    }

    //noinspection SpellCheckingInspection
    /// Retrieve the list of the current scores for the player.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn player_scores(
        &self,
        summoner: &str,
        request_client: &RequestClient,
    ) -> Result<Scores, Error> {
        self.live_client("playerscores", Some(summoner), request_client)
            .await
    }

    //noinspection SpellCheckingInspection
    /// Retrieve the list of the summoner spells for the player.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn player_summoner_spells(
        &self,
        summoner: &str,
        request_client: &RequestClient,
    ) -> Result<SummonerSpells, Error> {
        self.live_client("playersummonerspells", Some(summoner), request_client)
            .await
    }

    //noinspection SpellCheckingInspection
    /// Retrieve the basic runes of any player.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn player_main_runes(
        &self,
        summoner: &str,
        request_client: &RequestClient,
    ) -> Result<Runes, Error> {
        self.live_client("playermainrunes", Some(summoner), request_client)
            .await
    }

    //noinspection SpellCheckingInspection
    /// Retrieve the list of items for the player.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn player_items(
        &self,
        summoner: &str,
        request_client: &RequestClient,
    ) -> Result<Vec<Item>, Error> {
        self.live_client("playeritems", Some(summoner), request_client)
            .await
    }

    //noinspection SpellCheckingInspection
    /// Get a list of events that have occurred in the game.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn event_data(
        &self,
        event_id: Option<i32>,
        request_client: &RequestClient,
    ) -> Result<Events, Error> {
        let event_id = if let Some(id) = event_id {
            format!("?eventID={id}")
        } else {
            String::new()
        };
        let endpoint = format!("eventdata{event_id}");
        self.live_client(&endpoint, None, request_client).await
    }

    //noinspection SpellCheckingInspection
    /// Basic data about the game.
    ///
    /// # Errors
    /// This will return an error if the game API is not running
    pub async fn game_stats(&self, request_client: &RequestClient) -> Result<GameData, Error> {
        self.live_client("gamestats", None, request_client).await
    }

    //noinspection SpellCheckingInspection
    async fn live_client<R>(
        &self,
        endpoint: &str,
        summoner: Option<&str>,
        request_client: &RequestClient,
    ) -> Result<R, Error>
    where
        R: DeserializeOwned,
    {
        let endpoint = if let Some(summoner) = summoner {
            format!("/liveclientdata/{endpoint}?summonerName={summoner}")
        } else {
            format!("/liveclientdata/{endpoint}")
        };

        request_client
            .request_template(URL, &endpoint, "GET", None::<()>, None, |bytes| {
                serde_json::from_slice(&bytes).map_err(Error::SerdeJsonError)
            })
            .await
    }
}

impl Default for GameClient {
    fn default() -> Self {
        GameClient
    }
}