Skip to content

Commit

Permalink
Backport changes from vocho-gui
Browse files Browse the repository at this point in the history
  • Loading branch information
miestasmia committed Jun 12, 2019
1 parent 8b1ff71 commit dab1a39
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 57 deletions.
111 changes: 71 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vocho-term",
"version": "1.2.2",
"version": "1.3.0",
"description": "",
"main": "src/index.js",
"scripts": {
Expand All @@ -17,8 +17,10 @@
"eslint": "^5.16.0"
},
"dependencies": {
"ansi-styles": "^4.0.0",
"blessed": "git+https://github.com/CreativeCactus/blessed.git",
"editor-widget": "^1.1.1",
"table": "^5.4.0",
"vocho-lib": "git+https://github.com/tejoesperanto/vocho-lib.git"
}
}
80 changes: 66 additions & 14 deletions src/calc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const lib = require('vocho-lib');
const table = require('table').table;
const style = require('ansi-styles');

const commaSeparatedRegex = /[^\s,]/g;

Expand All @@ -15,7 +17,7 @@ module.exports = function performElection (electionType, candidatesStr, ballotsS
const ballots = ballotsStr
.trim()
.split(/\r?\n/g)
.map(b => b.trim())
.map(b => b.replace(/\s/g, ''))
.filter(b => b.length)
.map(b => {
if (b === 'blanka') { b = ''; }
Expand All @@ -24,26 +26,76 @@ module.exports = function performElection (electionType, candidatesStr, ballotsS

global.disableDebug = true;
try {
let results;

if (electionType === 'RP') {
const results = lib.RankedPairs([...candidates], ballots, ignoredCandidates, tieBreaker);
results = lib.RankedPairs([...candidates], ballots, ignoredCandidates, tieBreaker);
} else if (electionType === 'STV') {
results = lib.STV(places, [...candidates], ballots, ignoredCandidates, tieBreaker);
}

let resultsText = `${results.ballots} balotiloj kalkulitaj, ${results.blankBallots} blanka(j)`;

if (ignoredCandidates.length) {
resultsText += `\n\nIgnorataj kandidatoj: ${ignoredCandidates.join(', ')}`;
}

let resultsStr = `${results.ballots} balotiloj (${results.blankBallots} blanka(j))\n`;
if (electionType === 'RP') {
if (results.disqualifiedCandidates.length) {
resultsStr += `Neelektitaj laŭ §2.6: ${results.disqualifiedCandidates.join(', ')}\n`;
resultsText += `\n\nNeelektitaj laŭ §2.6: ${results.disqualifiedCandidates.join(', ')}\n`;
}

const comparedPairsTableData = [[]];
for (let th of [ 'Paro', 'Gajnanto', 'Diferenco' ]) {
comparedPairsTableData[0].push(`${style.bold.open}${th}${style.bold.close}`);
}

for (let [pairName, pair] of results.rankedPairs) {
const cand1 = pairName[0];
const cand2 = pairName[1];

comparedPairsTableData.push([
`${cand1} (${pair[cand1]}) kontraŭ ${cand2} (${pair[cand2]})`,
pair['winner'],
Math.abs(pair['diff'])
]);
}
resultsStr += `Venkinto: ${results.winner}`;
return resultsStr;

resultsText += '\n\nKomparitaj paroj:\n' + table(comparedPairsTableData);

const graphTableData = [[]];
for (let th of [ 'De', 'Al' ]) {
graphTableData[0].push(`${style.bold.open}${th}${style.bold.close}`);
}

for (let [from, to] of Object.entries(results.graph)) {
graphTableData.push([
from, to.join(', ')
]);
}

resultsText += '\n\nGrafeo:\n' + table(graphTableData);

resultsText += `\n\nVenkinto: ${results.winner}`;
} else if (electionType === 'STV') {
const results = lib.STV(places, [...candidates], ballots, ignoredCandidates, tieBreaker);

const resultsStr =
`${results.ballots} balotiloj (${results.blankBallots} blanka(j))
Venkintoj (laŭ ordo de elektiĝo): ${results.winners.join(', ')}`;
return resultsStr;
} else {
throw new Error(`Nekonata voĉdonsistemo ${electionType}`);
resultsText += `\nElektiĝkvoto: ${results.quota.toFixed(3)}`;

for (let i = 0; i < results.rounds.length; i++) {
const round = results.rounds[i];

resultsText += `\n\n${style.bold.open}Vico ${i + 1}${style.bold.close}`;

if (round.elected.length) {
resultsText += `\nElektitaj: ${round.elected.join(', ')}`;
} else if (round.eliminated) {
resultsText += '\nMalelektita: ' + round.eliminated;
}
}

resultsText += '\n\nVenkintoj (laŭ ordo de elektiĝo):\n' + results.winners.join(', ');
}

return resultsText;
} catch (e) {
if (!e || !('type' in e)) { throw e; }
switch (e.type) {
Expand Down
5 changes: 3 additions & 2 deletions src/main-box.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ module.exports = function setUpMainBox (mainBox, prompt) {
width: '50%',
border: 'line',
scrollable: true,
mouse: true,
content: defaultResultsValue
});

Expand Down Expand Up @@ -237,9 +238,9 @@ module.exports = function setUpMainBox (mainBox, prompt) {
prompt.setLabel('Necesas egalecrompanto!');
let promptText = 'La egalecrompanto mem enskribu sian balotilon ĉi-sube.';
if (currentElectionType === 'RP') {
promptText += '\nEkz. A=B>C>D=E';
promptText += '\nEkz. A>B>D>C';
} else if (currentElectionType === 'STV') {
promptText += '\nEkz. ABCDEF';
promptText += '\nEkz. ABCD';
}
promptText += '\nValidaj kandidatoj:\n' + e.candidates.join(', ') + '\n';

Expand Down

0 comments on commit dab1a39

Please sign in to comment.