2020-10-17 18:40:18 +11:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>pyRCV2 test</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<input type="file" id="bltFile">
|
|
|
|
<button onclick="clickBtn();">OK</button>
|
|
|
|
|
2020-10-17 22:20:13 +11:00
|
|
|
<table id="result"></table>
|
|
|
|
|
2020-10-17 18:40:18 +11:00
|
|
|
<script src="http://peterolson.github.com/BigRational.js/BigInt_BigRat.min.js"></script>
|
|
|
|
<script src="bundle.js"></script>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
async function clickBtn() {
|
2020-10-17 22:20:13 +11:00
|
|
|
// Read BLT file
|
2020-10-17 18:40:18 +11:00
|
|
|
let bltFile = document.getElementById('bltFile').files[0];
|
|
|
|
let text = await bltFile.text();
|
2020-10-17 22:20:13 +11:00
|
|
|
|
2020-10-17 18:40:18 +11:00
|
|
|
let election = py.pyRCV2.blt.readBLT(text);
|
2020-10-17 22:20:13 +11:00
|
|
|
|
|
|
|
// Create counter
|
|
|
|
let counter = py.pyRCV2.method.STVCCounter.STVCCounter(election);
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
let result = counter.reset();
|
|
|
|
|
|
|
|
// Initialise table rows
|
|
|
|
let tblResults = document.getElementById('result');
|
|
|
|
let candMap = new Map(); // Map Candidate -> rows
|
|
|
|
|
|
|
|
// Comment row
|
|
|
|
let elComment = document.createElement('tr');
|
|
|
|
let elTd = document.createElement('td');
|
|
|
|
elComment.appendChild(elTd);
|
|
|
|
tblResults.appendChild(elComment);
|
|
|
|
|
|
|
|
// Candidates
|
|
|
|
for (let candidate of election.candidates) {
|
|
|
|
let elTr1 = document.createElement('tr');
|
|
|
|
let elTr2 = document.createElement('tr');
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.setAttribute('rowspan', '2');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
elTd.innerText = candidate.py_name;
|
|
|
|
elTr1.appendChild(elTd);
|
|
|
|
|
|
|
|
tblResults.appendChild(elTr1);
|
|
|
|
tblResults.appendChild(elTr2);
|
|
|
|
|
|
|
|
candMap.set(candidate, [elTr1, elTr2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exhausted votes row
|
|
|
|
let elExhausted1 = document.createElement('tr');
|
|
|
|
let elExhausted2 = document.createElement('tr');
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.setAttribute('rowspan', '2');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
elTd.innerText = 'Exhausted';
|
|
|
|
elExhausted1.appendChild(elTd);
|
|
|
|
|
|
|
|
tblResults.appendChild(elExhausted1);
|
|
|
|
tblResults.appendChild(elExhausted2);
|
|
|
|
|
|
|
|
// Quota row
|
|
|
|
let elQuota = document.createElement('tr');
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
elTd.innerText = 'Quota';
|
|
|
|
elQuota.appendChild(elTd);
|
|
|
|
tblResults.appendChild(elQuota);
|
|
|
|
|
|
|
|
// Step election
|
|
|
|
result = counter.reset();
|
|
|
|
|
|
|
|
do {
|
|
|
|
// Display results
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.innerText = result.comment;
|
|
|
|
elComment.appendChild(elTd);
|
|
|
|
|
|
|
|
for (let [candidate, countCard] of result.candidates.impl) {
|
|
|
|
[elTr1, elTr2] = candMap.get(candidate);
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
if (!countCard.transfers.impl.equals(0)) {
|
|
|
|
elTd.innerText = countCard.transfers.pp(2);
|
|
|
|
}
|
|
|
|
elTr1.appendChild(elTd);
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
if (countCard.state == py.pyRCV2.model.CandidateState.WITHDRAWN) {
|
|
|
|
elTd.innerText = 'WD';
|
|
|
|
} else if (countCard.state == py.pyRCV2.model.CandidateState.ELECTED || countCard.state == py.pyRCV2.model.CandidateState.PROVISIONALLY_ELECTED) {
|
|
|
|
elTd.innerText = countCard.votes.pp(2);
|
|
|
|
elTd.style.fontWeight = 'bold';
|
|
|
|
} else if (countCard.state == py.pyRCV2.model.CandidateState.EXCLUDED) {
|
|
|
|
elTd.innerText = 'EX';
|
|
|
|
} else {
|
|
|
|
elTd.innerText = countCard.votes.pp(2);
|
|
|
|
}
|
|
|
|
elTr2.appendChild(elTd);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display exhausted votes
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
if (!result.exhausted.transfers.impl.equals(0)) {
|
|
|
|
elTd.innerText = result.exhausted.transfers.pp(2);
|
|
|
|
}
|
|
|
|
elExhausted1.appendChild(elTd);
|
|
|
|
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.innerText = result.exhausted.votes.pp(2);
|
|
|
|
elExhausted2.appendChild(elTd);
|
|
|
|
|
|
|
|
// Display quota
|
|
|
|
elTd = document.createElement('td');
|
|
|
|
elTd.style.borderTop = '1px solid black';
|
|
|
|
elTd.innerText = result.quota.pp(2);
|
|
|
|
elQuota.appendChild(elTd);
|
|
|
|
|
|
|
|
// Step election
|
|
|
|
result = counter.step();
|
|
|
|
if (py.isinstance(result, py.pyRCV2.model.CountCompleted)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (true);
|
2020-10-17 18:40:18 +11:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|