This repository has been archived on 2021-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
pyRCV2/html/worker.js

158 lines
5.0 KiB
JavaScript
Raw Normal View History

/*
pyRCV2: Preferential vote counting
Copyright © 2020 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/>.
*/
importScripts('vendor/BigInt_BigRat-a5f89e2.min.js', 'vendor/big-6.0.0.min.js', 'vendor/sjcl-1.0.8.min.js', 'bundle.js');
let stage, result, counter, ppDPs, tiesPrompt;
2020-12-27 23:28:16 +11:00
onmessage = function(evt) {
2020-12-27 23:28:16 +11:00
if (evt.data.type === 'init') {
// Set settings
if (evt.data.data.numbers === 'native') {
py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.Native);
}
if (evt.data.data.numbers === 'rational') {
py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.Rational);
}
if (evt.data.data.numbers === 'fixed') {
py.pyRCV2.numbers.set_numclass(py.pyRCV2.numbers.Fixed);
py.pyRCV2.numbers.set_dps(evt.data.data.fixedDPs);
}
ppDPs = evt.data.data.ppDPs;
2020-12-27 23:28:16 +11:00
let election = py.pyRCV2.blt.readBLT(evt.data.data.data);
let total_ballots = py.pyRCV2.numbers.Num(0);
for (let b of election.ballots) {
total_ballots.__iadd__(b.value);
}
2020-12-27 23:28:16 +11:00
// Create counter
if (evt.data.data.transfers === 'uig') {
counter = py.pyRCV2.method.gregory.UIGSTVCounter(election, evt.data.data.options);
2020-12-29 02:43:37 +11:00
} else if (evt.data.data.transfers === 'eg') {
counter = py.pyRCV2.method.gregory.EGSTVCounter(election, evt.data.data.options);
2021-01-08 19:16:56 +11:00
} else if (evt.data.data.transfers === 'meek') {
counter = py.pyRCV2.method.meek.MeekSTVCounter(election, evt.data.data.options);
2020-12-27 23:28:16 +11:00
} else {
counter = py.pyRCV2.method.gregory.WIGSTVCounter(election, evt.data.data.options);
2020-12-27 23:28:16 +11:00
}
if (evt.data.data.options['ties'] === 'backwards_random') {
counter.options['ties'] = [py.pyRCV2.ties.TiesBackwards(counter), py.pyRCV2.ties.TiesRandom(evt.data.data.seed)];
2021-01-05 01:27:30 +11:00
} else if (evt.data.data.options['ties'] === 'forwards_random') {
counter.options['ties'] = [py.pyRCV2.ties.TiesForwards(counter), py.pyRCV2.ties.TiesRandom(evt.data.data.seed)];
2020-12-27 23:28:16 +11:00
} else if (evt.data.data.options['ties'] === 'random') {
counter.options['ties'] = [py.pyRCV2.ties.TiesRandom(evt.data.data.seed)];
} else if (evt.data.data.options['ties'] === 'prompt') {
tiesPrompt = py.pyRCV2.ties.TiesPrompt();
counter.options['ties'] = [tiesPrompt];
}
postMessage({
'type': 'init',
'election': {
'name': election.py_name,
'candidates': election.candidates.map(c => c.py_name)
},
'total_ballots': total_ballots.pp(0),
'options': counter.describe_options(),
});
2020-12-27 23:28:16 +11:00
// Reset
stage = 1;
2021-01-08 19:16:56 +11:00
try {
result = counter.reset();
} catch (ex) {
handleException(ex);
return;
}
2020-12-27 23:28:16 +11:00
2021-01-08 19:16:56 +11:00
if (py.isinstance(result, py.pyRCV2.model.CountCompleted)) {
postMessage({'type': 'done', 'result': resultToJS(result)});
return;
} else {
postMessage({'type': 'result', 'result': resultToJS(result)});
}
2020-12-27 23:28:16 +11:00
stepElection();
} else if (evt.data.type === 'require_input') {
// Data received from the user
tiesPrompt.buffer = evt.data.input;
stepElection();
2020-12-24 00:04:30 +11:00
}
2020-12-27 23:28:16 +11:00
}
function stepElection() {
while (true) {
2020-12-27 23:28:16 +11:00
try {
result = counter.step();
} catch (ex) {
2021-01-08 19:16:56 +11:00
handleException(ex);
return;
2020-12-27 23:28:16 +11:00
}
2021-01-08 19:16:56 +11:00
stage += 1;
if (py.isinstance(result, py.pyRCV2.model.CountCompleted)) {
postMessage({'type': 'done', 'result': resultToJS(result)});
break;
} else {
postMessage({'type': 'result', 'result': resultToJS(result)});
}
}
}
2021-01-08 19:16:56 +11:00
function handleException(ex) {
if (py.isinstance(ex, py.pyRCV2.ties.RequireInput)) {
// Signals we require input to break a tie
postMessage({'type': 'require_input', 'message': ex.message});
} else if (py.isinstance(ex, py.pyRCV2.method.base_stv.STVException)) {
console.error(ex);
postMessage({'type': 'stv_exception', 'message': ex.message});
} else {
console.error(ex);
throw ex;
}
}
function resultToJS(result) {
return {
'stage': stage,
'comment': result.comment,
2021-01-09 04:33:13 +11:00
'logs': result.logs,
'candidates': result.candidates.py_items().map(([c, cc]) => [c.py_name, {
'transfers': cc.transfers.pp(ppDPs),
'votes': cc.votes.pp(ppDPs),
'state': cc.state,
'order_elected': cc.order_elected,
}]),
'exhausted': {
'transfers': result.exhausted.transfers.pp(ppDPs),
'votes': result.exhausted.votes.pp(ppDPs)
},
'loss_fraction': {
'transfers': result.loss_fraction.transfers.pp(ppDPs),
'votes': result.loss_fraction.votes.pp(ppDPs)
},
'total': result.total.pp(ppDPs),
'quota': result.quota.pp(ppDPs),
'vote_required_election': result.vote_required_election === null ? null : result.vote_required_election.pp(ppDPs),
};
}