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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// SPDX-License-Identifier: Apache-2.0 or MIT

//! prelate-rs is an async-ready library wrapper around the [aoe4world] API.
//!
//! Use it to retrieve game statistics, player information, and general awesomeness from
//! aoe4world in your Rust applications.
//!
//! [aoe4world]: https://aoe4world.com/api

pub mod types;

mod pagination;

#[cfg(test)]
mod testutils;

use query::{GlobalGamesQuery, LeaderboardQuery, ProfileGamesQuery, ProfileQuery, SearchQuery};
use types::{leaderboards::Leaderboard, profile::ProfileId};

// Rexports
pub use chrono;
pub use futures;
pub use isocountry::CountryCode;
pub use strum;

/// Returns a [`ProfileQuery`]. Used to get profile for a player.
///
/// # Params
/// - `profile_id` is aoe4world the ID of the player.
pub fn profile(profile_id: impl Into<ProfileId>) -> ProfileQuery {
    ProfileQuery::default().with_profile_id(Some(profile_id.into()))
}

/// Returns a [`ProfileGamesQuery`]. Used to query the `/profile/{profile_id}/games` endpoint.
///
/// # Params
/// - `profile_id` is aoe4world the ID of the player whose games should be searched.
pub fn profile_games(profile_id: impl Into<ProfileId>) -> ProfileGamesQuery {
    ProfileGamesQuery::default().with_profile_id(Some(profile_id.into()))
}

/// Returns a [`GlobalGamesQuery`]. Used to query the `/games` endpoint.
///
/// # Examples
///
/// ## List Ranked 1v1 Games
///
/// In the following example, we collect the 100 most recent ranked 1v1 games into a [`Vec`]:
/// ```rust
/// # #[cfg(feature = "test-api")]
/// # tokio_test::block_on(async {
/// use prelate_rs::{futures::StreamExt, global_games, types::games::GameKind};
///
/// let stream = global_games()
///     .with_leaderboard(Some(vec![GameKind::Rm1v1]))
///     .get(100)
///     .await
///     .expect("query should succeed");
/// let games = stream.collect::<Vec<_>>().await;
///
/// for game in games {
///     // Do something with each game.
/// # game.expect("game should be valid");
/// }
/// # })
/// ```
pub fn global_games() -> GlobalGamesQuery {
    GlobalGamesQuery::default()
}

/// Returns a [`SearchQuery`]. Used to query the `/players/search` endpoint.
///
/// Note: the query must contain at least 3 characters.
///
/// # Params
/// - `query` is a search query (e.g. a player's username or part of a username).
///
/// # Examples
///
/// ## Fuzzy Search
///
/// In the following example, we collect the first 10 players who match the
/// search query `"jiglypuf"` into a [`Vec`]:
/// ```rust
/// # #[cfg(feature = "test-api")]
/// # tokio_test::block_on(async {
/// use prelate_rs::{futures::StreamExt, search};
///
/// let stream = search("jiglypuf")
///     .get(10)
///     .await
///     .expect("query should succeed");
/// let profiles = stream.collect::<Vec<_>>().await;
///
/// for profile in profiles {
///     // Do something with each profile.
/// # profile.expect("profile should be valid");
/// }
/// # })
/// ```
///
/// ## Exact Search
///
/// In the following example, we search for the player who matches exactly the
/// search query `"[DEBILS] HousedHorse"`:
/// ```rust
/// # #[cfg(feature = "test-api")]
/// # tokio_test::block_on(async {
/// use prelate_rs::{futures::StreamExt, search};
///
/// let mut stream = search("[DEBILS] HousedHorse")
///     .with_exact(Some(true))
///     .get(1)
///     .await
///     .expect("query should succeed");
/// let profile = stream
///     .next()
///     .await
///     .expect("there should be at least 1 matching profile");
///
/// // Do something with the profile.
/// # profile.expect("profile should be valid");
/// # })
/// ```
pub fn search(query: impl AsRef<str>) -> SearchQuery {
    SearchQuery::default().with_query(Some(query.as_ref().to_string()))
}

/// Returns a [`ProfileGamesQuery`]. Used to query the `/leaderboards/{leaderboard}` endpoint.
///
/// # Params
/// - `leaderboard` is the leaderboard to fetch.
pub fn leaderboard(leaderboard: impl Into<Leaderboard>) -> LeaderboardQuery {
    LeaderboardQuery::default().with_leaderboard(Some(leaderboard.into()))
}

pub mod query {
    //! Contains query builders to interact with the aoe4world API.
    //!
    //! Using these directly is possible, but it may be more ergonomic to use
    //! the provided functions at the top-level of the library.

    // Clippy complains about needless update in derived setters.
    #![allow(clippy::needless_update)]

    use anyhow::{bail, Result};
    use derive_setters::Setters;
    use futures::{Stream, StreamExt};
    use isocountry::CountryCode;
    use itertools::join;
    use url::Url;

    use crate::{
        pagination::{PaginatedRequest, PaginationClient},
        types::{
            games::{Game, GameKind, GamesOrder, GlobalGames, ProfileGames},
            leaderboards::{Leaderboard, LeaderboardEntry, LeaderboardPages},
            profile::{Profile, ProfileId},
            search::SearchResults,
        },
    };

    /// Constructs a query for the `/players/{profile_id}/games` endpoint.
    #[derive(Setters, Default)]
    #[setters(prefix = "with_")]
    #[setters(into)]
    pub struct ProfileGamesQuery {
        /// [`ProfileId`] to query.
        profile_id: Option<ProfileId>,
        /// Filter by [`Leaderboard`] .
        game_kind: Option<Vec<GameKind>>,
        /// Filter by [`Leaderboard`]. Same as [`GameKind`] but supports [`Leaderboard::RmSolo`] and [`Leaderboard::RmTeam`].
        leaderboard: Option<Vec<Leaderboard>>,
        /// Filter over an opponent's profile ID.
        opponent_profile_id: Option<ProfileId>,
        /// Filter over a list of opponent profile IDs.
        opponent_profile_ids: Option<Vec<ProfileId>>,
        /// Filter by time played since a specific date.
        since: Option<chrono::DateTime<chrono::Utc>>,
    }

    impl ProfileGamesQuery {
        /// Get the games for this profile.
        pub async fn get(self, limit: usize) -> Result<impl Stream<Item = Result<Game>>> {
            if self.profile_id.is_none() {
                bail!("missing profile_id")
            }

            let client = PaginationClient::<ProfileGames, Game>::with_limit(limit);
            let url = format!(
                "https://aoe4world.com/api/v0/players/{}/games",
                self.profile_id.unwrap()
            )
            .parse()?;
            let url = self.query_params(url);

            let pages = client
                .into_pages_concurrent(PaginatedRequest::new(url))
                .await?;
            Ok(pages.items().take(limit))
        }

        fn query_params(&self, mut url: Url) -> Url {
            let mut leaderboards = vec![];
            if let Some(ref leaderboard) = self.leaderboard {
                for g in leaderboard.iter().map(|g| g.to_string()) {
                    leaderboards.push(g)
                }
            }
            if let Some(ref game_kind) = self.game_kind {
                for g in game_kind.iter().map(|g| g.to_string()) {
                    leaderboards.push(g)
                }
            }
            if !leaderboards.is_empty() {
                url.query_pairs_mut()
                    .append_pair("leaderboard", join(leaderboards, ",").as_str());
            }
            if let Some(ref id) = self.opponent_profile_id {
                url.query_pairs_mut()
                    .append_pair("opponent_profile_id", id.to_string().as_str());
            }
            if let Some(ref ids) = self.opponent_profile_ids {
                url.query_pairs_mut()
                    .append_pair("opponent_profile_ids", join(ids, ",").as_str());
            }
            if let Some(ref since) = self.since {
                url.query_pairs_mut()
                    .append_pair("since", since.to_rfc3339().as_str());
            }
            url
        }
    }

    /// Constructs a query for the `/games` endpoint.
    #[derive(Setters, Default)]
    #[setters(prefix = "with_")]
    #[setters(into)]
    pub struct GlobalGamesQuery {
        /// Filter by game kind category.
        ///
        /// NOTE: this is named `leaderboard` but uses the [`GameKind`] enum.
        leaderboard: Option<Vec<GameKind>>,
        /// Filter over an opponent's profile ID.
        opponent_profile_id: Option<ProfileId>,
        /// Filter over a list of profile IDs.
        profile_ids: Option<Vec<ProfileId>>,
        /// Filter by time played since a specific date.
        since: Option<chrono::DateTime<chrono::Utc>>,
        /// Filter by time played since a specific date.
        order: Option<GamesOrder>,
    }

    impl GlobalGamesQuery {
        /// Get the games.
        pub async fn get(self, limit: usize) -> Result<impl Stream<Item = Result<Game>>> {
            let client = PaginationClient::<GlobalGames, Game>::with_limit(limit);

            let url = "https://aoe4world.com/api/v0/games".parse()?;
            let url = self.query_params(url);

            let pages = client
                .into_pages_concurrent(PaginatedRequest::new(url))
                .await?;
            Ok(pages.items().take(limit))
        }

        fn query_params(&self, mut url: Url) -> Url {
            if let Some(ref leaderboard) = self.leaderboard {
                url.query_pairs_mut()
                    .append_pair("leaderboard", join(leaderboard, ",").as_str());
            }
            if let Some(id) = self.opponent_profile_id {
                url.query_pairs_mut()
                    .append_pair("opponent_profile_id", id.to_string().as_str());
            }
            if let Some(ref ids) = self.profile_ids {
                url.query_pairs_mut()
                    .append_pair("profile_ids", join(ids, ",").as_str());
            }
            if let Some(ref since) = self.since {
                url.query_pairs_mut()
                    .append_pair("since", since.to_rfc3339().as_str());
            }
            if let Some(ref order) = self.order {
                url.query_pairs_mut()
                    .append_pair("order", order.to_string().as_str());
            }
            url
        }
    }

    /// Constructs a query for the `/players/{profile_id}` endpoint.
    #[derive(Setters, Default)]
    #[setters(prefix = "with_")]
    #[setters(into)]
    pub struct ProfileQuery {
        /// [`ProfileId`] to query.
        profile_id: Option<ProfileId>,
    }

    impl ProfileQuery {
        /// Get the profile.
        pub async fn get(self) -> Result<Profile> {
            if self.profile_id.is_none() {
                bail!("missing profile_id")
            }

            reqwest::get(format!(
                "https://aoe4world.com/api/v0/players/{}",
                self.profile_id.unwrap()
            ))
            .await?
            .json()
            .await
            .map_err(anyhow::Error::from)
        }
    }

    /// Constructs a query for the `/players/search` endpoint.
    #[derive(Setters, Default)]
    #[setters(prefix = "with_")]
    #[setters(into)]
    pub struct SearchQuery {
        /// Search query.
        query: Option<String>,
        /// Should the results exactly match the query.
        exact: Option<bool>,
    }

    impl SearchQuery {
        /// Get the search results.
        pub async fn get(self, limit: usize) -> Result<impl Stream<Item = Result<Profile>>> {
            if self.query.is_none() {
                bail!("missing search query");
            }
            if self.query.as_ref().unwrap().len() < 3 {
                bail!(
                    "search query must contain at least 3 characters, got {}",
                    self.query.as_ref().unwrap().len()
                );
            }

            let client = PaginationClient::<SearchResults, Profile>::with_limit(limit);

            let url = "https://aoe4world.com/api/v0/players/search".parse()?;
            let url = self.query_params(url);

            let pages = client
                .into_pages_concurrent(PaginatedRequest::new(url))
                .await?;
            Ok(pages.items().take(limit))
        }

        fn query_params(&self, mut url: Url) -> Url {
            if let Some(query) = &self.query {
                url.query_pairs_mut()
                    .append_pair("query", query.to_string().as_str());
            }
            if let Some(exact) = self.exact {
                url.query_pairs_mut()
                    .append_pair("exact", exact.to_string().as_str());
            }
            url
        }
    }

    /// Constructs a query for the `/leaderboards/leaderboard` endpoint.
    #[derive(Setters, Default)]
    #[setters(prefix = "with_")]
    #[setters(into)]
    pub struct LeaderboardQuery {
        /// [`ProfileId`] to query.
        leaderboard: Option<Leaderboard>,
        /// [`ProfileId`] to query.
        profile_id: Option<ProfileId>,
        /// Search query.
        query: Option<String>,
        /// Search by country.
        country: Option<CountryCode>,
    }

    impl LeaderboardQuery {
        /// Get the leaderboard data. Returns a stream of [`LeaderboardEntry`].
        pub async fn get(
            self,
            limit: usize,
        ) -> Result<impl Stream<Item = Result<LeaderboardEntry>>> {
            if self.leaderboard.is_none() {
                bail!("missing leaderboard");
            }

            let client = PaginationClient::<LeaderboardPages, LeaderboardEntry>::with_limit(limit);

            let url = format!(
                "https://aoe4world.com/api/v0/leaderboards/{}",
                self.leaderboard.unwrap()
            )
            .parse()?;
            let url = self.query_params(url);

            let pages = client
                .into_pages_concurrent(PaginatedRequest::new(url))
                .await?;
            Ok(pages.items().take(limit))
        }

        fn query_params(&self, mut url: Url) -> Url {
            if let Some(query) = &self.query {
                url.query_pairs_mut()
                    .append_pair("query", query.to_string().as_str());
            }
            if let Some(profile_id) = self.profile_id {
                url.query_pairs_mut()
                    .append_pair("profile_id", profile_id.to_string().as_str());
            }
            if let Some(country) = self.country {
                url.query_pairs_mut()
                    .append_pair("country", country.alpha2().to_lowercase().as_str());
            }
            url
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use futures::StreamExt;

    const HOUSEDHORSE_ID: u64 = 3176;
    const ONLY_CAMS_ID: u64 = 10433860;
    const ONLY_CAMS_NAME: &str = "🐪🐪🐪OnlyCams🐪🐪🐪";
    const DEBILS_NAME: &str = "DEBILS";

    #[cfg_attr(not(feature = "test-api"), ignore)]
    #[tokio::test]
    async fn profile_api_smoke() {
        profile(ONLY_CAMS_ID)
            .get()
            .await
            .expect("API call should succeed");

        profile(HOUSEDHORSE_ID)
            .get()
            .await
            .expect("API call should succeed");
    }

    #[cfg_attr(not(feature = "test-api"), ignore)]
    #[tokio::test(flavor = "multi_thread")]
    async fn player_games_api_smoke() {
        let g: Vec<_> = profile_games(ONLY_CAMS_ID)
            .get(100)
            .await
            .expect("API call should succeed")
            .collect()
            .await;
        assert_eq!(100, g.len());
        for (i, game) in g.iter().enumerate() {
            assert!(game.is_ok(), "game {i} not ok: {game:?}")
        }

        let g: Vec<_> = profile_games(HOUSEDHORSE_ID)
            .get(100)
            .await
            .expect("API call should succeed")
            .collect()
            .await;
        assert_eq!(100, g.len());
        for (i, game) in g.iter().enumerate() {
            assert!(game.is_ok(), "game {i} not ok: {game:?}")
        }

        let g: Vec<_> = profile_games(HOUSEDHORSE_ID)
            .get(1)
            .await
            .expect("API call should succeed")
            .collect()
            .await;
        assert_eq!(1, g.len());
        for (i, game) in g.iter().enumerate() {
            assert!(game.is_ok(), "game {i} not ok: {game:?}")
        }
    }

    #[cfg_attr(not(feature = "test-api"), ignore)]
    #[tokio::test(flavor = "multi_thread")]
    async fn global_games_api_smoke() {
        let g: Vec<_> = global_games()
            .get(100)
            .await
            .expect("API call should succeed")
            .collect()
            .await;
        println!("{:#?}", g);
        assert_eq!(100, g.len());
        for (i, game) in g.iter().enumerate() {
            assert!(game.is_ok(), "game {i} not ok: {game:?}")
        }
    }

    #[cfg_attr(not(feature = "test-api"), ignore)]
    #[tokio::test(flavor = "multi_thread")]
    async fn search_api_smoke() {
        let profiles: Vec<_> = search(ONLY_CAMS_NAME)
            .with_exact(Some(true))
            .get(100)
            .await
            .expect("API call should succeed")
            .collect()
            .await;
        assert!(profiles.len() <= 100);
        for (i, profile) in profiles.iter().enumerate() {
            assert!(profile.is_ok(), "profile {i} not ok: {profile:?}")
        }

        let profiles: Vec<_> = search(DEBILS_NAME)
            .with_exact(Some(false))
            .get(100)
            .await
            .expect("API call should succeed")
            .collect()
            .await;
        assert!(profiles.len() <= 100);
        for (i, profile) in profiles.iter().enumerate() {
            assert!(profile.is_ok(), "profile {i} not ok: {profile:?}")
        }
    }

    #[cfg_attr(not(feature = "test-api"), ignore)]
    #[tokio::test(flavor = "multi_thread")]
    async fn leaderboard_api_smoke() {
        let entries: Vec<_> = leaderboard(Leaderboard::RmSolo)
            .get(100)
            .await
            .expect("RmSolo leaderboard")
            .collect()
            .await;
        println!("{entries:?}");
        assert_eq!(100, entries.len(), "RmSolo len");
        for (i, entry) in entries.iter().enumerate() {
            assert!(entry.is_ok(), "RmSolo entry {i} not ok: {entry:?}")
        }

        let entries: Vec<_> = leaderboard(Leaderboard::RmTeam)
            .get(100)
            .await
            .expect("RmTeam leaderboard")
            .collect()
            .await;
        assert_eq!(100, entries.len(), "RmTeam len");
        for (i, entry) in entries.iter().enumerate() {
            assert!(entry.is_ok(), "RmTeam entry {i} not ok: {entry:?}")
        }

        let entries: Vec<_> = leaderboard(Leaderboard::RmTeam)
            .with_country(CountryCode::CAN)
            .get(10)
            .await
            .expect("RmTeam leaderboard Canada")
            .collect()
            .await;
        assert_eq!(10, entries.len(), "RmTeam Canada len");
        for (i, entry) in entries.iter().enumerate() {
            assert!(entry.is_ok(), "RmTeam Canada entry {i} not ok: {entry:?}")
        }
    }
}