Refactor production calculation in DirectorWorker: Update the logic for determining the number of items to produce, ensuring it adheres to capacity constraints and maximum production limits. Add safety checks and logging for potential errors in production calculations, enhancing reliability and debugging capabilities.

This commit is contained in:
Torsten Schulz (local)
2026-01-12 14:01:18 +01:00
parent b858f5c385
commit 5475ae24f6

View File

@@ -500,11 +500,14 @@ impl DirectorWorker {
conn: &mut DbConnection,
plan: &ProductionPlan,
) -> Result<(), DbError> {
// WICHTIG: Kapazität direkt aus dem Plan berechnen (wurde gerade in der Schleife aktualisiert)
let free_capacity = Self::calc_free_capacity(plan);
let one_piece_cost = Self::calc_one_piece_cost(plan);
let max_money_production = Self::calc_max_money_production(plan, one_piece_cost);
let to_produce = (free_capacity.min(max_money_production)).clamp(0, 100);
// to_produce darf NIE größer sein als free_capacity, sonst passt es nicht ins Lager
// Zusätzlich: max. 100 Stück pro Produktion, und max. was das Geld erlaubt
let to_produce = free_capacity.min(max_money_production).min(100).max(0);
eprintln!(
"[DirectorWorker] Produktionsberechnung: free_capacity={}, one_piece_cost={}, max_money_production={}, to_produce={}, running_productions={}",
@@ -526,6 +529,15 @@ impl DirectorWorker {
return Ok(());
}
// Sicherheitsprüfung: to_produce darf niemals größer sein als free_capacity
if to_produce > free_capacity {
eprintln!(
"[DirectorWorker] FEHLER: to_produce ({}) > free_capacity ({})! Das sollte nicht passieren. Breche Produktion ab.",
to_produce, free_capacity
);
return Ok(());
}
let production_cost = to_produce as f64 * one_piece_cost;
if let Err(err) = self.base.change_falukant_user_money(