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; }