From 5475ae24f637a69dfe7f23fc5645620b993c0d26 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Mon, 12 Jan 2026 14:01:18 +0100 Subject: [PATCH] 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. --- src/worker/director.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/worker/director.rs b/src/worker/director.rs index 611a5a7..090e6bf 100644 --- a/src/worker/director.rs +++ b/src/worker/director.rs @@ -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(