From 1bd6cbfe1b66a713d636d5e2448d678294402f90 Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 28 Aug 2026 11:29:49 +0200 Subject: [PATCH] =?UTF-8?q?Verbessere=20die=20Normalisierung=20politischer?= =?UTF-8?q?=20=C3=84mter,=20um=20sicherzustellen,=20dass=20nur=20ein=20akt?= =?UTF-8?q?uelles=20Amt=20pro=20Charakter=20erhalten=20bleibt=20und=20bei?= =?UTF-8?q?=20Ranggleichheit=20der=20j=C3=BCngste=20Eintrag=20beibehalten?= =?UTF-8?q?=20wird.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/worker/politics.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/worker/politics.rs b/src/worker/politics.rs index 0f4aa9b..4d40e4f 100755 --- a/src/worker/politics.rs +++ b/src/worker/politics.rs @@ -626,8 +626,8 @@ impl PoliticsWorker { .collect()) } - /// Bereinigt Bestandsdaten nach derselben Regel wie neue Wahlergebnisse. - /// Für mehrere Einträge desselben Amts bleibt der jüngste (größte ID) übrig. + /// Bereinigt Bestandsdaten auf genau ein aktuelles politisches Amt je Charakter. + /// Bei Ranggleichheit bleibt der jüngste Eintrag (größte ID) bestehen. fn normalize_existing_political_offices(pool: &ConnectionPool) -> Result<(), DbError> { let mut conn = pool .get() @@ -660,18 +660,13 @@ impl PoliticsWorker { } for offices in by_character.values() { - let highest_rank = offices.iter().map(|(_, _, rank)| *rank).max().unwrap_or(0); - let mut newest_by_type: HashMap = HashMap::new(); - for (office_id, office_type_id, _) in offices { - newest_by_type - .entry(*office_type_id) - .and_modify(|current| *current = (*current).max(*office_id)) - .or_insert(*office_id); - } + let retained_office_id = offices + .iter() + .max_by_key(|(office_id, _, rank)| (*rank, *office_id)) + .map(|(office_id, _, _)| *office_id); - for (office_id, office_type_id, rank) in offices { - let duplicate = newest_by_type.get(office_type_id) != Some(office_id); - if *rank < highest_rank || duplicate { + for (office_id, _, _) in offices { + if Some(*office_id) != retained_office_id { conn.execute("remove_political_office_by_id", &[office_id])?; } } @@ -682,7 +677,7 @@ impl PoliticsWorker { /// Wendet die Ein-Amts-Regel auf jedes soeben durch eine Wahl erzeugte Amt an. /// Höhere Ämter ersetzen niedrigere, Wiederwahlen ersetzen das alte gleiche - /// Amt und eine niedrigere Wahl wird nicht als zusätzliches Amt behalten. + /// Amt und es bleibt stets genau ein höchstrangiges Amt erhalten. fn normalize_elected_political_offices( pool: &ConnectionPool, elected_offices: Vec, @@ -726,7 +721,7 @@ impl PoliticsWorker { if existing_type_id == elected.office_type_id || existing_rank < new_rank { offices_to_remove.push(existing_id); - } else if existing_rank > new_rank { + } else if existing_rank >= new_rank { remove_new_office = true; break; }