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
289
290
291
292
293
294
295
296
use reqwest::Url;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[cfg(test)]
use mockito;

#[allow(dead_code)]
pub const DEFAULT_PORT: u16 = 21337;

#[derive(Debug)]
pub struct Client {
    session: reqwest::Client,
    base_url: Url,
}

/// The player's current deck in an active game.
///
/// See official ['API'] for more information.
///
/// ['API']: https://developer.riotgames.com/docs/lor#game-client-api_active-deck
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct StaticDecklist {
    pub deck_code: Option<String>,
    pub cards_in_deck: Option<HashMap<String, u8>>,
}

/// Screen information.
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct Screen {
    pub screen_width: u16,
    pub screen_height: u16,
}

/// Information about a rectangle on screen.
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct Rectangle {
    #[serde(rename = "GameID")]
    pub card_id: i32,
    pub card_code: String,
    pub top_left_x: u16,
    pub top_left_y: u16,
    pub width: u16,
    pub height: u16,
    pub local_player: bool,
}

/// The position of the cards in the collection, deck builder, Expedition drafts, and active games.
///
/// See official ['API'] for more information.
///
/// ['API']: https://developer.riotgames.com/docs/lor#game-client-api_card-positions
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct PositionalRectangles {
    pub player_name: Option<String>,
    pub opponent_name: Option<String>,
    pub game_state: String,
    pub screen: Screen,
    pub rectangles: Vec<Rectangle>,
}

/// The player's drafted cards during an Expedition.
///
/// See official ['API'] for more information.
///
/// ['API']: https://developer.riotgames.com/docs/lor#game-client-api_expeditions
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct ExpeditionsState {
    pub is_active: bool,
    pub state: String,
    pub record: Option<Vec<String>>,
    // Not implemented, type unknown
    pub draft_picks: Option<Vec<String>>,
    pub deck: Option<Vec<String>>,
    pub games: u8,
    pub wins: u8,
    pub losses: u8,
}

/// Result of the player's most recently completed game.
///
/// See official ['API'] for more information.
///
/// ['API']: https://developer.riotgames.com/docs/lor#game-client-api_game-result
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub struct GameResult {
    #[serde(rename = "GameID")]
    pub game_id: i32,
    pub local_player_won: bool,
}

impl Client {
    pub fn new(#[cfg(not(test))] port: u16) -> Self {
        #[cfg(test)]
        let url = &mockito::server_url();

        #[cfg(not(test))]
        let url = format!("http://localhost:{}", port);

        Client {
            session: reqwest::Client::builder().build().unwrap(),
            base_url: Url::parse(&url).unwrap(),
        }
    }

    pub async fn get_static_decklist(&self) -> Result<StaticDecklist, crate::Error> {
        Ok(self
            .session
            .get(self.base_url.join("/static-decklist").unwrap())
            .send()
            .await?
            .json::<StaticDecklist>()
            .await?)
    }

    pub async fn get_positional_rectangles(&self) -> Result<PositionalRectangles, crate::Error> {
        Ok(self
            .session
            .get(self.base_url.join("/positional-rectangles").unwrap())
            .send()
            .await?
            .json::<PositionalRectangles>()
            .await?)
    }

    pub async fn get_expeditions_state(&self) -> Result<ExpeditionsState, crate::Error> {
        Ok(self
            .session
            .get(self.base_url.join("/expeditions-state").unwrap())
            .send()
            .await?
            .json::<ExpeditionsState>()
            .await?)
    }

    pub async fn get_game_result(&self) -> Result<GameResult, crate::Error> {
        Ok(self
            .session
            .get(self.base_url.join("/game-result").unwrap())
            .send()
            .await?
            .json::<GameResult>()
            .await?)
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        Client, ExpeditionsState, GameResult, PositionalRectangles, Rectangle, Screen,
        StaticDecklist,
    };
    use mockito;

    #[tokio::test]
    async fn test_static_decklist() -> std::result::Result<(), crate::Error> {
        let body = StaticDecklist {
            deck_code: Some(
                "CEAAECABAMGA6EYXEYVS4NYIAECQCGY5FAVTCMRVAICACAYCBELDGBABAURCMKJW".to_string(),
            ),
            cards_in_deck: Some(
                [
                    ("01NX012".to_string(), 2),
                    ("01NX015".to_string(), 2),
                    ("01NX019".to_string(), 2),
                    ("01NX023".to_string(), 2),
                    ("01NX038".to_string(), 2),
                    ("01NX043".to_string(), 2),
                    ("01NX046".to_string(), 2),
                    ("01NX055".to_string(), 2),
                    ("01SI001".to_string(), 2),
                    ("01SI027".to_string(), 2),
                    ("01SI029".to_string(), 2),
                    ("01SI040".to_string(), 2),
                    ("01SI043".to_string(), 2),
                    ("01SI049".to_string(), 2),
                    ("01SI050".to_string(), 2),
                    ("01SI053".to_string(), 2),
                    ("01NX002".to_string(), 1),
                    ("01NX009".to_string(), 1),
                    ("01NX022".to_string(), 1),
                    ("01NX051".to_string(), 1),
                    ("01SI034".to_string(), 1),
                    ("01SI038".to_string(), 1),
                    ("01SI041".to_string(), 1),
                    ("01SI054".to_string(), 1),
                ]
                .iter()
                .cloned()
                .collect(),
            ),
        };

        let m = mockito::mock("GET", "/static-decklist")
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&body)?)
            .create();

        let client = Client::new();
        let res = client.get_static_decklist().await?;

        assert_eq!(body, res);
        m.assert();

        Ok(())
    }

    #[tokio::test]
    async fn test_positional_rectangles() -> std::result::Result<(), crate::Error> {
        let body = PositionalRectangles {
            player_name: Some("Player One".to_string()),
            opponent_name: Some("Player Two".to_string()),
            game_state: "InProgress".to_string(),
            screen: Screen {
                screen_width: 1920,
                screen_height: 1080,
            },
            rectangles: vec![Rectangle {
                card_id: 1427904082,
                card_code: "face".to_string(),
                top_left_x: 179,
                top_left_y: 481,
                width: 117,
                height: 117,
                local_player: true,
            }],
        };

        let m = mockito::mock("GET", "/positional-rectangles")
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&body)?)
            .create();

        let client = Client::new();
        let res = client.get_positional_rectangles().await?;

        assert_eq!(body, res);
        m.assert();

        Ok(())
    }

    #[tokio::test]
    async fn test_expeditions_state() -> std::result::Result<(), crate::Error> {
        let body = ExpeditionsState {
            is_active: false,
            state: "Inactive".to_string(),
            record: None,
            draft_picks: None,
            deck: None,
            games: 0,
            wins: 0,
            losses: 0,
        };

        let m = mockito::mock("GET", "/expeditions-state")
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&body)?)
            .create();

        let client = Client::new();
        let res = client.get_expeditions_state().await?;

        assert_eq!(body, res);
        m.assert();

        Ok(())
    }

    #[tokio::test]
    async fn test_game_result() -> std::result::Result<(), crate::Error> {
        let body = GameResult {
            game_id: -1,
            local_player_won: false,
        };

        let m = mockito::mock("GET", "/game-result")
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&body)?)
            .create();

        let client = Client::new();
        let res = client.get_game_result().await?;

        assert_eq!(body, res);
        m.assert();

        Ok(())
    }
}