52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
importScripts('http://peterolson.github.com/BigRational.js/BigInt_BigRat.min.js', 'bundle.js');
|
|
|
|
onmessage = function(evt) {
|
|
let election = py.pyRCV2.blt.readBLT(evt.data);
|
|
postMessage({'type': 'init', 'election': {
|
|
'candidates': election.candidates.map(c => c.py_name)
|
|
}});
|
|
|
|
// Create counter
|
|
let counter = py.pyRCV2.method.STVCCounter.STVCCounter(election);
|
|
|
|
// Reset
|
|
let result = counter.reset();
|
|
|
|
postMessage({'type': 'result', 'result': {
|
|
'comment': result.comment,
|
|
'candidates': result.candidates.py_items().map(([c, cc]) => [c.py_name, {
|
|
'transfers': cc.transfers.pp(2),
|
|
'votes': cc.votes.pp(2),
|
|
'state': cc.state
|
|
}]),
|
|
'exhausted': {
|
|
'transfers': result.exhausted.transfers.pp(2),
|
|
'votes': result.exhausted.votes.pp(2)
|
|
},
|
|
'quota': result.quota.pp(2)
|
|
}});
|
|
|
|
// Step election
|
|
while (true) {
|
|
result = counter.step();
|
|
if (py.isinstance(result, py.pyRCV2.model.CountCompleted)) {
|
|
postMessage({'type': 'done'});
|
|
break;
|
|
} else {
|
|
postMessage({'type': 'result', 'result': {
|
|
'comment': result.comment,
|
|
'candidates': result.candidates.py_items().map(([c, cc]) => [c.py_name, {
|
|
'transfers': cc.transfers.pp(2),
|
|
'votes': cc.votes.pp(2),
|
|
'state': cc.state
|
|
}]),
|
|
'exhausted': {
|
|
'transfers': result.exhausted.transfers.pp(2),
|
|
'votes': result.exhausted.votes.pp(2)
|
|
},
|
|
'quota': result.quota.pp(2)
|
|
}});
|
|
}
|
|
}
|
|
}
|