Skip to content

Commit

Permalink
destructure and rearrange for flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Tijl Leenders committed Mar 24, 2024
1 parent 907add1 commit ce8c6b4
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions src/services/activity_placer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,33 @@ pub fn place(calendar: &mut Calendar, mut activities: Vec<Activity>) -> Vec<Acti
&activities[act_index_to_schedule].goal_id
[0..cmp::min(activities[act_index_to_schedule].goal_id.len(), 5)]
);
let best_hour_index_and_size: Option<(usize, usize)> =
activities[act_index_to_schedule].get_best_scheduling_index_and_length();
let best_hour_index: usize;
let best_size: usize;
if let Some(best_hour_index_and_size) = best_hour_index_and_size {
best_hour_index = best_hour_index_and_size.0;
best_size = best_hour_index_and_size.1;
if let Some((best_hour_index, best_size)) =
activities[act_index_to_schedule].get_best_scheduling_index_and_length()
{
println!(
"Best index:{:?} and size {:?}",
&best_hour_index, &best_size
);
println!("reserving {:?} hours...", best_size);
for duration_offset in 0..best_size {
Rc::make_mut(&mut calendar.hours[best_hour_index + duration_offset]);
calendar.hours[best_hour_index + duration_offset] = Rc::new(Hour::Occupied {
activity_index: act_index_to_schedule,
activity_title: activities[act_index_to_schedule].title.clone(),
activity_goalid: activities[act_index_to_schedule].goal_id.clone(),
});
calendar.update_budgets_for(
&activities[act_index_to_schedule].goal_id.clone(),
best_hour_index + duration_offset,
);
activities[act_index_to_schedule].duration_left -= 1;
}
if activities[act_index_to_schedule].duration_left == 0 {
activities[act_index_to_schedule].status = Status::Scheduled;
(activities[act_index_to_schedule]).release_claims();
}

dbg!(&calendar);
} else {
activities[act_index_to_schedule].release_claims();
if activities[act_index_to_schedule].activity_type == ActivityType::BudgetMinDay {
Expand All @@ -45,27 +61,6 @@ pub fn place(calendar: &mut Calendar, mut activities: Vec<Activity>) -> Vec<Acti
calendar.impossible_activities.push(impossible_activity);
continue;
}

println!("reserving {:?} hours...", best_size);
for duration_offset in 0..best_size {
Rc::make_mut(&mut calendar.hours[best_hour_index + duration_offset]);
calendar.hours[best_hour_index + duration_offset] = Rc::new(Hour::Occupied {
activity_index: act_index_to_schedule,
activity_title: activities[act_index_to_schedule].title.clone(),
activity_goalid: activities[act_index_to_schedule].goal_id.clone(),
});
calendar.update_budgets_for(
&activities[act_index_to_schedule].goal_id.clone(),
best_hour_index + duration_offset,
);
activities[act_index_to_schedule].duration_left -= 1;
}
if activities[act_index_to_schedule].duration_left == 0 {
activities[act_index_to_schedule].status = Status::Scheduled;
(activities[act_index_to_schedule]).release_claims();
}

dbg!(&calendar);
} else {
println!("Tried to schedule activity index None");
break;
Expand Down

0 comments on commit ce8c6b4

Please sign in to comment.