|
1 | 1 | use crate::api::FplClient; |
| 2 | +use crate::error::Result; |
2 | 3 | use crate::models::{Element, Position}; |
3 | 4 | use crate::utils::team_helpers::{create_team_map, find_team_ids_by_name}; |
4 | 5 | use owo_colors::OwoColorize; |
5 | 6 |
|
6 | | -pub async fn handle_availability(team: Option<String>, all: bool, limit: usize) { |
7 | | - match FplClient::fetch_bootstrap_static().await { |
8 | | - Ok(data) => { |
9 | | - let team_map = create_team_map(&data.teams); |
10 | | - let target_team_ids = if let Some(ref team_name) = team { |
11 | | - find_team_ids_by_name(&data.teams, team_name) |
| 7 | +pub async fn handle_availability(team: Option<String>, all: bool, limit: usize) -> Result<()> { |
| 8 | + let data = FplClient::fetch_bootstrap_static().await?; |
| 9 | + |
| 10 | + let team_map = create_team_map(&data.teams); |
| 11 | + let target_team_ids = if let Some(ref team_name) = team { |
| 12 | + find_team_ids_by_name(&data.teams, team_name) |
| 13 | + } else { |
| 14 | + Vec::new() |
| 15 | + }; |
| 16 | + |
| 17 | + let mut players: Vec<Element> = data |
| 18 | + .elements |
| 19 | + .into_iter() |
| 20 | + .filter(|player| { |
| 21 | + let team_match = if team.is_some() { |
| 22 | + target_team_ids.contains(&player.team) |
12 | 23 | } else { |
13 | | - Vec::new() |
| 24 | + true |
14 | 25 | }; |
15 | 26 |
|
16 | | - let mut players: Vec<Element> = data |
17 | | - .elements |
18 | | - .into_iter() |
19 | | - .filter(|player| { |
20 | | - let team_match = if team.is_some() { |
21 | | - target_team_ids.contains(&player.team) |
22 | | - } else { |
23 | | - true |
24 | | - }; |
25 | | - |
26 | | - if !team_match { |
27 | | - return false; |
28 | | - } |
29 | | - |
30 | | - // If not "all", only show players with news or non-available status |
31 | | - if !all && player.status == "a" && player.news.is_empty() { |
32 | | - return false; |
33 | | - } |
34 | | - |
35 | | - true |
36 | | - }) |
37 | | - .collect(); |
38 | | - |
39 | | - if players.is_empty() { |
40 | | - println!("No availability issues found."); |
41 | | - return; |
| 27 | + if !team_match { |
| 28 | + return false; |
42 | 29 | } |
43 | 30 |
|
44 | | - // Sort players by news_added (latest first) |
45 | | - players.sort_by(|a, b| b.news_added.cmp(&a.news_added)); |
46 | | - |
47 | | - println!( |
48 | | - "{:<4} {:<20} {:<16} {:<4} {:<10} {:<8} {:<18} {:<30}", |
49 | | - "ID", "Name", "Team", "Pos", "Status", "Chance", "News Added", "News" |
50 | | - ); |
51 | | - |
52 | | - for player in players.iter().take(limit) { |
53 | | - let team_name = team_map |
54 | | - .get(&player.team) |
55 | | - .map(|s| s.as_str()) |
56 | | - .unwrap_or("Unknown"); |
57 | | - |
58 | | - let status_desc = match player.status.as_str() { |
59 | | - "a" => "Available", |
60 | | - "i" => "Injured", |
61 | | - "d" => "Doubtful", |
62 | | - "s" => "Suspended", |
63 | | - "n" => "Not Available", |
64 | | - "u" => "Unavail", |
65 | | - _ => &player.status, |
66 | | - }; |
67 | | - |
68 | | - let chance_val = player.chance_of_playing_next_round; |
69 | | - let chance_str = chance_val |
70 | | - .map(|c| format!("{}%", c)) |
71 | | - .unwrap_or_else(|| "N/A".to_string()); |
72 | | - |
73 | | - let chance_padding = " ".repeat(8usize.saturating_sub(chance_str.len())); |
74 | | - |
75 | | - let colored_chance = match chance_val { |
76 | | - Some(75) => chance_str.yellow().to_string(), |
77 | | - Some(50) => chance_str.truecolor(255, 165, 0).to_string(), |
78 | | - Some(0) => chance_str.red().to_string(), |
79 | | - _ => chance_str, |
80 | | - }; |
81 | | - |
82 | | - let news_added = player |
83 | | - .news_added |
84 | | - .as_ref() |
85 | | - .map(|s| { |
86 | | - if s.len() >= 16 { |
87 | | - s[..16].replace('T', " ") |
88 | | - } else { |
89 | | - s.clone() |
90 | | - } |
91 | | - }) |
92 | | - .unwrap_or_else(|| "N/A".to_string()); |
93 | | - |
94 | | - println!( |
95 | | - "{:<4} {:<20} {:<16} {:<4} {:<10} {}{} {:<18} {:<30}", |
96 | | - player.id, |
97 | | - player.web_name, |
98 | | - team_name, |
99 | | - Position::from_element_type_id(player.element_type) |
100 | | - .map(|p| p.display_name().to_string()) |
101 | | - .unwrap_or_default(), |
102 | | - status_desc, |
103 | | - chance_padding, |
104 | | - colored_chance, |
105 | | - news_added, |
106 | | - player.news |
107 | | - ); |
| 31 | + // If not "all", only show players with news or non-available status |
| 32 | + if !all |
| 33 | + && player |
| 34 | + .status |
| 35 | + .is_available(player.chance_of_playing_next_round) |
| 36 | + && player.news.is_empty() |
| 37 | + { |
| 38 | + return false; |
108 | 39 | } |
109 | | - } |
110 | | - Err(e) => { |
111 | | - eprintln!("Error: {}", e); |
112 | | - } |
| 40 | + |
| 41 | + true |
| 42 | + }) |
| 43 | + .collect(); |
| 44 | + |
| 45 | + if players.is_empty() { |
| 46 | + println!("No availability issues found."); |
| 47 | + return Ok(()); |
113 | 48 | } |
| 49 | + |
| 50 | + // Sort players by news_added (latest first) |
| 51 | + players.sort_by(|a, b| b.news_added.cmp(&a.news_added)); |
| 52 | + |
| 53 | + println!( |
| 54 | + "{:<4} {:<20} {:<16} {:<4} {:<10} {:<8} {:<18} {:<30}", |
| 55 | + "ID", "Name", "Team", "Pos", "Status", "Chance", "News Added", "News" |
| 56 | + ); |
| 57 | + |
| 58 | + for player in players.iter().take(limit) { |
| 59 | + let team_name = team_map |
| 60 | + .get(&player.team) |
| 61 | + .map(|s| s.as_str()) |
| 62 | + .unwrap_or("Unknown"); |
| 63 | + |
| 64 | + let status_desc = player.status.display_name(); |
| 65 | + |
| 66 | + let chance_val = player.chance_of_playing_next_round; |
| 67 | + let chance_str = chance_val |
| 68 | + .map(|c| format!("{}%", c)) |
| 69 | + .unwrap_or_else(|| "N/A".to_string()); |
| 70 | + |
| 71 | + let chance_padding = " ".repeat(8usize.saturating_sub(chance_str.len())); |
| 72 | + |
| 73 | + let colored_chance = match chance_val { |
| 74 | + Some(75) => chance_str.yellow().to_string(), |
| 75 | + Some(50) => chance_str.truecolor(255, 165, 0).to_string(), |
| 76 | + Some(0) => chance_str.red().to_string(), |
| 77 | + _ => chance_str, |
| 78 | + }; |
| 79 | + |
| 80 | + let news_added = player |
| 81 | + .news_added |
| 82 | + .as_ref() |
| 83 | + .map(|s| { |
| 84 | + if s.len() >= 16 { |
| 85 | + s[..16].replace('T', " ") |
| 86 | + } else { |
| 87 | + s.clone() |
| 88 | + } |
| 89 | + }) |
| 90 | + .unwrap_or_else(|| "N/A".to_string()); |
| 91 | + |
| 92 | + println!( |
| 93 | + "{:<4} {:<20} {:<16} {:<4} {:<10} {}{} {:<18} {:<30}", |
| 94 | + player.id, |
| 95 | + player.web_name, |
| 96 | + team_name, |
| 97 | + Position::from_element_type_id(player.element_type) |
| 98 | + .map(|p| p.display_name().to_string()) |
| 99 | + .unwrap_or_default(), |
| 100 | + status_desc, |
| 101 | + chance_padding, |
| 102 | + colored_chance, |
| 103 | + news_added, |
| 104 | + player.news |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + Ok(()) |
114 | 109 | } |
0 commit comments