use tui::style::Color;
use mlbt_api::plays::{Play, Zone};
use crate::components::util::convert_color;
pub const HOME_PLATE_WIDTH: f64 = 17.0; const X_COORDS: [f64; 3] = [-8.5, 17.0 / -6.0, 17.0 / 6.0];
pub const DEFAULT_SZ_BOT: f64 = 1.5; pub const DEFAULT_SZ_TOP: f64 = 3.3;
#[derive(Debug, PartialEq)]
pub struct Coordinate(pub f64, pub f64);
pub struct StrikeZone {
pub colors: Vec<Color>,
#[allow(dead_code)]
pub strike_zone_bot: f64,
#[allow(dead_code)]
pub strike_zone_top: f64,
}
impl Default for StrikeZone {
fn default() -> Self {
StrikeZone {
colors: StrikeZone::all_white(),
strike_zone_bot: DEFAULT_SZ_BOT,
strike_zone_top: DEFAULT_SZ_TOP,
}
}
}
impl From<&Play> for StrikeZone {
fn from(play: &Play) -> Self {
let colors = match play.matchup.batter_hot_cold_zones.as_ref() {
Some(z) => StrikeZone::transform_zones(z),
None => return StrikeZone::default(),
};
if colors.len() < 9 {
return StrikeZone::default();
}
StrikeZone::new(colors)
}
}
impl StrikeZone {
pub fn new(colors: Vec<Color>) -> Self {
StrikeZone {
colors,
strike_zone_bot: DEFAULT_SZ_BOT,
strike_zone_top: DEFAULT_SZ_TOP,
}
}
fn transform_zones(zones: &[Zone]) -> Vec<Color> {
zones
.iter()
.map(|z| convert_color(z.color.clone()))
.collect()
}
pub fn build_coords(strike_zone_bot: f64, strike_zone_top: f64) -> Vec<Coordinate> {
let y_chunk = (strike_zone_top - strike_zone_bot) / 3.0;
let y_coords = [
strike_zone_bot + (2.0 * y_chunk),
strike_zone_bot + y_chunk,
strike_zone_bot,
];
y_coords
.iter()
.flat_map(|y| X_COORDS.iter().map(move |x| Coordinate(*x, *y)))
.collect()
}
fn all_white() -> Vec<Color> {
(0..9).map(|_| Color::Rgb(255, 255, 255)).collect()
}
}
#[test]
fn test_all_white() {
let hm = StrikeZone::default();
let good = vec![
Color::Rgb(255, 255, 255),
Color::Rgb(255, 255, 255),
Color::Rgb(255, 255, 255),
Color::Rgb(255, 255, 255),
Color::Rgb(255, 255, 255),
Color::Rgb(255, 255, 255),
Color::Rgb(255, 255, 255),
Color::Rgb(255, 255, 255),
Color::Rgb(255, 255, 255),
];
assert_eq!(hm.colors, good);
}
#[test]
fn test_new() {
let hm = StrikeZone::new(vec![]);
let good = vec![];
assert_eq!(hm.colors, good);
}
#[test]
fn test_coords() {
let bot = 1.5 * 12.0;
let top = 3.3 * 12.0;
let coords = StrikeZone::build_coords(bot, top);
let w = vec![
Coordinate(-8.5, 32.4),
Coordinate(17.0 / -6.0, 32.4),
Coordinate(17.0 / 6.0, 32.4),
Coordinate(-8.5, 25.2),
Coordinate(17.0 / -6.0, 25.2),
Coordinate(17.0 / 6.0, 25.2),
Coordinate(-8.5, 18.0),
Coordinate(17.0 / -6.0, 18.0),
Coordinate(17.0 / 6.0, 18.0),
];
assert_eq!(w, coords);
}