2021-06-11 18:09:26 +02:00
|
|
|
/* OpenTally: Open-source election vote counting
|
|
|
|
* Copyright © 2021 Lee Yingtong Li (RunasSudo)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-06-12 16:15:14 +02:00
|
|
|
use crate::election::{Candidate, CountState};
|
2021-06-12 16:56:18 +02:00
|
|
|
use crate::logger::smart_join;
|
2021-06-12 16:15:14 +02:00
|
|
|
use crate::numbers::Number;
|
2021-06-11 18:09:26 +02:00
|
|
|
use crate::stv::STVError;
|
|
|
|
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use wasm_bindgen::prelude::wasm_bindgen;
|
|
|
|
|
|
|
|
#[allow(unused_imports)]
|
|
|
|
use std::io::{stdin, stdout, Write};
|
|
|
|
|
2021-06-12 16:15:14 +02:00
|
|
|
#[derive(PartialEq)]
|
2021-06-12 19:15:15 +02:00
|
|
|
pub enum TieStrategy {
|
2021-06-11 18:09:26 +02:00
|
|
|
Forwards,
|
|
|
|
Backwards,
|
2021-06-12 19:15:15 +02:00
|
|
|
Random(String),
|
2021-06-11 18:09:26 +02:00
|
|
|
Prompt,
|
|
|
|
}
|
|
|
|
|
2021-06-12 19:15:15 +02:00
|
|
|
impl TieStrategy {
|
2021-06-12 16:15:14 +02:00
|
|
|
pub fn describe(&self) -> String {
|
2021-06-11 18:09:26 +02:00
|
|
|
match self {
|
2021-06-12 16:15:14 +02:00
|
|
|
Self::Forwards => "forwards",
|
|
|
|
Self::Backwards => "backwards",
|
2021-06-12 16:39:49 +02:00
|
|
|
Self::Random(_) => "random",
|
2021-06-12 16:15:14 +02:00
|
|
|
Self::Prompt => "prompt",
|
|
|
|
}.to_string()
|
|
|
|
}
|
|
|
|
|
2021-06-12 16:56:18 +02:00
|
|
|
pub fn choose_highest<'c, N: Number>(&self, state: &mut CountState<N>, candidates: &Vec<&'c Candidate>) -> Result<&'c Candidate, STVError> {
|
2021-06-12 16:15:14 +02:00
|
|
|
match self {
|
|
|
|
Self::Forwards => {
|
|
|
|
let mut candidates = candidates.clone();
|
|
|
|
candidates.sort_unstable_by(|a, b|
|
|
|
|
// Compare b to a to sort high-to-low
|
|
|
|
state.forwards_tiebreak.as_ref().unwrap().get(b).unwrap()
|
|
|
|
.cmp(&state.forwards_tiebreak.as_ref().unwrap().get(a).unwrap())
|
|
|
|
);
|
|
|
|
if state.forwards_tiebreak.as_ref().unwrap().get(candidates[0]).unwrap() == state.forwards_tiebreak.as_ref().unwrap().get(candidates[1]).unwrap() {
|
|
|
|
return Err(STVError::UnresolvedTie);
|
|
|
|
} else {
|
2021-06-12 16:56:18 +02:00
|
|
|
state.logger.log_literal(format!("Tie between {} broken forwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
|
2021-06-12 16:15:14 +02:00
|
|
|
return Ok(candidates[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::Backwards => {
|
|
|
|
let mut candidates = candidates.clone();
|
|
|
|
candidates.sort_unstable_by(|a, b|
|
|
|
|
state.backwards_tiebreak.as_ref().unwrap().get(b).unwrap()
|
|
|
|
.cmp(&state.backwards_tiebreak.as_ref().unwrap().get(a).unwrap())
|
|
|
|
);
|
|
|
|
if state.backwards_tiebreak.as_ref().unwrap().get(candidates[0]).unwrap() == state.backwards_tiebreak.as_ref().unwrap().get(candidates[1]).unwrap() {
|
|
|
|
return Err(STVError::UnresolvedTie);
|
|
|
|
} else {
|
2021-06-12 16:56:18 +02:00
|
|
|
state.logger.log_literal(format!("Tie between {} broken backwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
|
2021-06-12 16:15:14 +02:00
|
|
|
return Ok(candidates[0]);
|
|
|
|
}
|
|
|
|
}
|
2021-06-12 19:15:15 +02:00
|
|
|
Self::Random(_) => {
|
|
|
|
state.logger.log_literal(format!("Tie between {} broken at random.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
|
|
|
|
return Ok(candidates[state.random.as_mut().unwrap().next(candidates.len())]);
|
|
|
|
}
|
2021-06-11 18:09:26 +02:00
|
|
|
Self::Prompt => {
|
2021-06-12 16:56:18 +02:00
|
|
|
match prompt(candidates) {
|
|
|
|
Ok(c) => {
|
|
|
|
state.logger.log_literal(format!("Tie between {} broken by manual intervention.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
|
|
|
|
return Ok(c);
|
|
|
|
}
|
|
|
|
Err(e) => { return Err(e); }
|
|
|
|
}
|
2021-06-11 18:09:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 16:56:18 +02:00
|
|
|
pub fn choose_lowest<'c, N: Number>(&self, state: &mut CountState<N>, candidates: &Vec<&'c Candidate>) -> Result<&'c Candidate, STVError> {
|
2021-06-11 18:09:26 +02:00
|
|
|
match self {
|
2021-06-12 16:15:14 +02:00
|
|
|
Self::Forwards => {
|
|
|
|
let mut candidates = candidates.clone();
|
|
|
|
candidates.sort_unstable_by(|a, b|
|
|
|
|
state.forwards_tiebreak.as_ref().unwrap().get(a).unwrap()
|
|
|
|
.cmp(&state.forwards_tiebreak.as_ref().unwrap().get(b).unwrap())
|
|
|
|
);
|
|
|
|
if state.forwards_tiebreak.as_ref().unwrap().get(candidates[0]).unwrap() == state.forwards_tiebreak.as_ref().unwrap().get(candidates[1]).unwrap() {
|
|
|
|
return Err(STVError::UnresolvedTie);
|
|
|
|
} else {
|
2021-06-12 16:56:18 +02:00
|
|
|
state.logger.log_literal(format!("Tie between {} broken forwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
|
2021-06-12 16:15:14 +02:00
|
|
|
return Ok(candidates[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Self::Backwards => {
|
|
|
|
let mut candidates = candidates.clone();
|
|
|
|
candidates.sort_unstable_by(|a, b|
|
|
|
|
state.backwards_tiebreak.as_ref().unwrap().get(a).unwrap()
|
|
|
|
.cmp(&state.backwards_tiebreak.as_ref().unwrap().get(b).unwrap())
|
|
|
|
);
|
|
|
|
if state.backwards_tiebreak.as_ref().unwrap().get(candidates[0]).unwrap() == state.backwards_tiebreak.as_ref().unwrap().get(candidates[1]).unwrap() {
|
|
|
|
return Err(STVError::UnresolvedTie);
|
|
|
|
} else {
|
2021-06-12 16:56:18 +02:00
|
|
|
state.logger.log_literal(format!("Tie between {} broken backwards.", smart_join(&candidates.iter().map(|c| c.name.as_str()).collect())));
|
2021-06-12 16:15:14 +02:00
|
|
|
return Ok(candidates[0]);
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 18:09:26 +02:00
|
|
|
Self::Random(_seed) => {
|
2021-06-12 16:15:14 +02:00
|
|
|
return self.choose_highest(state, candidates);
|
2021-06-11 18:09:26 +02:00
|
|
|
}
|
|
|
|
Self::Prompt => {
|
2021-06-12 16:15:14 +02:00
|
|
|
return self.choose_highest(state, candidates);
|
2021-06-11 18:09:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
2021-06-12 16:15:14 +02:00
|
|
|
fn prompt<'c>(candidates: &Vec<&'c Candidate>) -> Result<&'c Candidate, STVError> {
|
2021-06-11 18:09:26 +02:00
|
|
|
println!("Multiple tied candidates:");
|
|
|
|
for (i, candidate) in candidates.iter().enumerate() {
|
|
|
|
println!("{}. {}", i + 1, candidate.name);
|
|
|
|
}
|
|
|
|
let mut buffer = String::new();
|
|
|
|
loop {
|
|
|
|
print!("Which candidate to select? [1-{}] ", candidates.len());
|
|
|
|
stdout().flush().expect("IO Error");
|
|
|
|
stdin().read_line(&mut buffer).expect("IO Error");
|
|
|
|
match buffer.trim().parse::<usize>() {
|
|
|
|
Ok(val) => {
|
|
|
|
if val >= 1 && val <= candidates.len() {
|
|
|
|
println!();
|
|
|
|
return Ok(candidates[val - 1]);
|
|
|
|
} else {
|
|
|
|
println!("Invalid selection");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
println!("Invalid selection");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
#[wasm_bindgen]
|
|
|
|
extern "C" {
|
|
|
|
fn read_user_input_buffer(s: &str) -> Option<String>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
2021-06-12 16:15:14 +02:00
|
|
|
fn prompt<'c>(candidates: &Vec<&'c Candidate>) -> Result<&'c Candidate, STVError> {
|
2021-06-11 18:09:26 +02:00
|
|
|
let mut message = String::from("Multiple tied candidates:\n");
|
|
|
|
for (i, candidate) in candidates.iter().enumerate() {
|
|
|
|
message.push_str(&format!("{}. {}\n", i + 1, candidate.name));
|
|
|
|
}
|
|
|
|
message.push_str(&format!("Which candidate to select? [1-{}] ", candidates.len()));
|
|
|
|
|
|
|
|
match read_user_input_buffer(&message) {
|
|
|
|
Some(response) => {
|
|
|
|
match response.trim().parse::<usize>() {
|
|
|
|
Ok(val) => {
|
|
|
|
if val >= 1 && val <= candidates.len() {
|
|
|
|
return Ok(candidates[val - 1]);
|
|
|
|
} else {
|
|
|
|
let _ = read_user_input_buffer(&message);
|
|
|
|
return Err(STVError::RequireInput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
let _ = read_user_input_buffer(&message);
|
|
|
|
return Err(STVError::RequireInput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
return Err(STVError::RequireInput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|