/ src / player.rs
player.rs
 1  use egui::{Ui, TextEdit};
 2  use riven::{consts::PlatformRoute, RiotApi};
 3  
 4  use crate::{season::Season, AppError};
 5  
 6  #[derive(Default)]
 7  pub struct Player {
 8      pub name: String,
 9      pub tag: String,
10  }
11  impl Player {
12      pub fn input_field(&mut self, ui: &mut Ui, player_number: u8) {
13          ui.horizontal(|ui| {
14              ui.label(format!("Player {player_number}:"));
15              ui.add(
16                  TextEdit::singleline(&mut self.name)
17                      .hint_text("Game Name")
18              );
19              ui.add(
20                  TextEdit::singleline(&mut self.tag)
21                      .hint_text("Tagline")
22              );
23          });
24      }
25      
26      pub async fn get_season_match_ids(&mut self, api: &RiotApi, region: &PlatformRoute, season: &Season) -> Result<Vec<String>, AppError> {
27          let puuid = self.get_player_puuid(region, api).await?;
28          let mut matches = vec![];
29          let mut start = 0;
30  
31          loop {
32              let batch = api.match_v5().get_match_ids_by_puuid(
33                  region.to_regional(),
34                  &puuid,
35                  Some(100),
36                  season.end_date,
37                  None,
38                  season.start_date,
39                  Some(start),
40                  Some("ranked"),
41              ).await?;
42  
43              if batch.is_empty() {
44                  break;
45              }
46  
47              matches.extend(batch);
48              start += 100;
49          }
50          Ok(matches)
51      }
52  
53      async fn get_player_puuid(&mut self, region: &PlatformRoute, api: &RiotApi) -> Result<String, AppError> {
54          if let Some(account) = api.account_v1().get_by_riot_id(region.to_regional(), &self.name, &self.tag).await? {
55             Ok(account.puuid)
56          }
57          else {
58              Err(AppError::UnknownPlayer(self.to_string()))
59          }
60      }
61  }
62  impl ToString for Player {
63      fn to_string(&self) -> String {
64          format!("{}#{}", self.name, self.tag)
65      }
66  }