use std::{collections::HashMap, time::SystemTime};
use crate::{
crops::crop_registry,
persistence::{load_farm, save_farm},
};
pub fn harvest() {
let mut farm = load_farm();
let registry = crop_registry();
let mut harvested: HashMap<String, u16> = HashMap::new();
for plot in &mut farm.plots {
let Some(ref crop_id) = plot.planted_crop else {
continue;
};
let Some(ref planted_at) = plot.planted_at else {
continue;
};
let crop = ®istry[crop_id];
let grow_time = crop.grow_time;
let age = planted_at.elapsed().unwrap().as_secs();
if age < grow_time as u64 {
continue;
}
let crop_count = farm
.inventory
.crops
.get_or_insert_with(HashMap::new)
.entry(crop_id.clone())
.or_insert(0);
*crop_count += 1;
*harvested.entry(crop_id.to_string()).or_insert(0) += 1;
plot.planted_crop = None;
plot.planted_at = None;
}
farm.last_updated = SystemTime::now();
match save_farm(&farm) {
true => {
if harvested.is_empty() {
println!(" No crops to harvest");
} else {
println!(" Harvested:");
for (crop, amount) in harvested {
println!(" +{amount} {crop}")
}
}
}
false => {
usefulog::err("failed to save farm");
std::process::exit(1);
}
}
}