function reassignParties(election) {
const nodes = sim.nodes();
function reassignNodes(nodes, shares) {
const pool = [];
var sumShares = shares.reduce((acc, cur) => acc + cur[1], 0);
shares = shares
.map(([party, share]) => [party, Math.round(share * nodes.length / sumShares)])
.sort((a, b) => b[1] - a[1]);
sumShares = shares.reduce((acc, cur) => acc + cur[1], 0);
if (sumShares < nodes.length) shares[0][1] += 1;
if (sumShares > nodes.length) shares[0][1] -= 1;
var fixed = shares.reduce((acc, cur) => acc + cur[1], 0)
shares.forEach(([party, share]) => {
let count = 0;
nodes.forEach(n => {
if (n.party === party) count += 1;
if (count > share) n.party = undefined;
})
})
shares.forEach(([party, share]) => {
let count = nodes.filter(n => n.party === party).length;
for (let i = 0; i < nodes.length && count < share; ++i) {
if (nodes[i].party === undefined) {
nodes[i].party = party;
nodes[i].cx = partyX(party)
count += 1;
}
}
})
return nodes;
}
const vs = currentElection.map(e => [e.party, e.voteShare]);
const ss = currentElection.map(e => [e.party, e.seats]);
const totalSeats = currentElection[0].totalSeats;
const newNodes = [
... reassignNodes(nodes.filter(n => n.group === 0), vs),
... reassignNodes(nodes.filter(n => n.group === 1), ss)
]
sim.nodes(newNodes);
}