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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//! 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
/// Types returned by the in game API
pub mod types;
use types::ActivePlayerOrNull;
use self::types::{
Abilities, ActivePlayer, AllGameData, AllPlayer, Events, GameData, Item, Runes, Scores,
SummonerSpells, TeamID,
};
use crate::in_game::sealed::GameClientInternal;
use std::future::Future;
use std::net::{Ipv4Addr, SocketAddrV4};
/// The only url the in game API can be used on
pub const URL: SocketAddrV4 = SocketAddrV4::new(Ipv4Addr::LOCALHOST, 2999);
/// Trait that represents a connection to the in game api client
pub trait GameClient: GameClientInternal {
/// This makes a head request to the in game API
///
/// Please note, when using invalid methods (such as `HEAD`) with an Operation ID
/// The API will always return 200 with an empty body, at least in my testing
/// This can be used to see if the API is alive and if you're outside the loading screen
///
/// # Errors
/// This will return an error if it is unable to connect to the server
fn head(
&self,
endpoint: &str,
) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send {
self.socketv4_raw_request_template(
URL,
endpoint,
"HEAD",
None,
None,
crate::requests::RequestFmt::MsgPack,
)
}
//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
fn all_game_data(&self) -> impl Future<Output = Result<AllGameData, Self::Error>> + Send {
self.live_client("allgamedata", None)
}
//noinspection SpellCheckingInspection
/// Get all data about the active player.
///
/// # Errors
/// This will return an error if the game API is not running
fn active_player(
&self,
) -> impl Future<Output = Result<Option<ActivePlayer>, Self::Error>> + Send {
async {
let active_player: ActivePlayerOrNull = self.live_client("activeplayer", None).await?;
Ok(active_player.into_option())
}
}
//noinspection SpellCheckingInspection
/// Returns the player name.
///
/// # Errors
/// This will return an error if the game API is not running
fn active_player_name(&self) -> impl Future<Output = Result<String, Self::Error>> + Send {
self.live_client("activeplayername", None)
}
//noinspection SpellCheckingInspection
/// Get Abilities for the active player.
///
/// # Errors
/// This will return an error if the game API is not running
fn active_player_abilities(
&self,
) -> impl Future<Output = Result<Abilities, Self::Error>> + Send {
self.live_client("activeplayerabilities", None)
}
//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
fn active_player_runes(&self) -> impl Future<Output = Result<Runes, Self::Error>> + Send {
self.live_client("activeplayerrunes", None)
}
//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
fn player_list(
&self,
team: Option<TeamID>,
) -> impl Future<Output = Result<Box<[AllPlayer]>, Self::Error>> + Send {
let endpoint = team.map_or_else(
|| "playerlist",
|team| match team {
TeamID::All => "playerlist?teamID=ALL",
TeamID::Unknown => "playerlist?teamID=UNKNOWN",
TeamID::Order => "playerlist?teamID=ORDER",
TeamID::Chaos => "playerlist?teamID=CHAOS",
TeamID::Neutral => "playerlist?teamID=NEUTRAL",
},
);
self.live_client(endpoint, None)
}
//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
fn player_scores(
&self,
riot_id: impl AsRef<str> + Send,
) -> impl Future<Output = Result<Scores, Self::Error>> + Send {
async move {
self.live_client("playerscores", Some(riot_id.as_ref()))
.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
fn player_summoner_spells(
&self,
riot_id: impl AsRef<str> + Send,
) -> impl Future<Output = Result<SummonerSpells, Self::Error>> + Send {
async move {
self.live_client("playersummonerspells", Some(riot_id.as_ref()))
.await
}
}
//noinspection SpellCheckingInspection
/// Retrieve the basic runes of any player.
///
/// # Errors
/// This will return an error if the game API is not running
fn player_main_runes(
&self,
riot_id: impl AsRef<str> + Send,
) -> impl Future<Output = Result<Runes, Self::Error>> + Send {
async move {
self.live_client("playermainrunes", Some(riot_id.as_ref()))
.await
}
}
//noinspection SpellCheckingInspection
/// Retrieve the list of items for the player.
///
/// # Errors
/// This will return an error if the game API is not running
fn player_items(
&self,
riot_id: impl AsRef<str> + Send,
) -> impl Future<Output = Result<Box<[Item]>, Self::Error>> + Send {
async move {
self.live_client("playeritems", Some(riot_id.as_ref()))
.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
fn event_data(
&self,
event_id: Option<i32>,
) -> impl Future<Output = Result<Events, Self::Error>> + Send {
let event_id = event_id.map_or(String::new(), |id| format!("?eventID={id}"));
let endpoint = format!("eventdata{event_id}");
async move { self.live_client(&endpoint, None).await }
}
//noinspection SpellCheckingInspection
/// Basic data about the game.
///
/// # Errors
/// This will return an error if the game API is not running
fn game_stats(&self) -> impl Future<Output = Result<GameData, Self::Error>> + Send {
self.live_client("gamestats", None)
}
}
mod sealed {
use super::URL;
use crate::utils::requests::RequestClientTrait;
use serde::de::DeserializeOwned;
use std::future::Future;
pub trait GameClientInternal: RequestClientTrait + Sync {
fn live_client<R: DeserializeOwned>(
&self,
endpoint: &str,
riot_id: Option<&str>,
) -> impl Future<Output = Result<R, Self::Error>> + Send {
async move {
let endpoint = riot_id.map_or_else(
|| format!("/liveclientdata/{endpoint}"),
|riot_id| format!("/liveclientdata/{endpoint}?riotId={riot_id}"),
);
let buf = self
.socketv4_request_template(
URL,
&endpoint,
"GET",
None,
None,
crate::requests::RequestFmt::MsgPack,
)
.await?;
Ok(Self::decode_response_bytes(
buf,
crate::requests::RequestFmt::MsgPack,
)?)
}
}
#[cfg(feature = "replay")]
/// Internal abstraction over `request_client`, this lets me cut out anything that only applies here,
/// and reduce the usage in oneliners
///
/// # Errors
/// This will return an error if there is not an active replay running
fn replay<R>(
&self,
endpoint: &'static str,
method: &'static str,
body: Option<impl serde::Serialize + Send>,
) -> impl Future<Output = Result<R, Self::Error>> + Send
where
R: DeserializeOwned,
{
async move {
let body = Self::encode_body(body, crate::requests::RequestFmt::MsgPack)?;
let buffer = self
.socketv4_request_template(
URL,
endpoint,
method,
body,
None,
crate::requests::RequestFmt::MsgPack,
)
.await?;
Ok(Self::decode_response_bytes(
buffer,
crate::requests::RequestFmt::MsgPack,
)?)
}
}
}
impl<T> GameClientInternal for T
where
T: RequestClientTrait + Sync,
Self: Sync,
{
}
}
impl<T> GameClient for T where T: GameClientInternal {}