-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.js
127 lines (111 loc) · 3.61 KB
/
core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
(function () {
angular.module("core", ["stvCounter"])
.controller("mainController", function (stvCounter) {
var ctrl = this;
ctrl.countVotes = function () {
ctrl.ballots = parseBallotsText(ctrl.ballotForm.text);
ctrl.seats = ctrl.ballotForm.seats;
ctrl.elected = stvCounter.count(ctrl.seats, ctrl.ballots);
ctrl.rounds = stvCounter.rounds;
ctrl.quota = stvCounter.quota;
ctrl.tie = ctrl.elected.length < ctrl.seats;
}
ctrl.toggleBallotFormHelp = function () {
ctrl.ballotForm.showHelp = !ctrl.ballotForm.showHelp;
}
ctrl.toggleGeneratedExampleForm = function () {
ctrl.generatedExampleForm.show = !ctrl.generatedExampleForm.show;
}
ctrl.fillAndreaExample = function () {
var ballots = getAndreaExample();
fillForm(ballots, 2);
}
ctrl.fillSnacksExample = function () {
var ballots = getSnacksExample();
fillForm(ballots, 3);
}
ctrl.fillGeneratedExample = function () {
var candidates = ctrl.generatedExampleForm.candidatesText.split(/\r?\n/);
var ballots = [];
for (var i = 0; i < ctrl.generatedExampleForm.votes; i++) {
ballots.push(shuffle(candidates).slice(0));
}
fillForm(ballots, ctrl.generatedExampleForm.seats)
}
function fillForm(ballots, seats) {
ctrl.ballotForm.text = convertBallotsToText(ballots);
ctrl.ballotForm.seats = seats;
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function convertBallotsToText(ballots) {
var text = "";
ballots.forEach(function (ballot) {
ballot.forEach(function (candidate) {
text += candidate + "\n";
});
text += "\n"
});
return text.trim();
}
function parseBallotsText(text) {
var ballots = [];
var ballotTexts = text.split(/\r?\n\r?\n/);
ballotTexts.forEach(function (ballotText) {
var ballot = ballotText.split(/\r?\n/);
ballot = ballot.map(function (candidate) { return getCandidateName(candidate); })
ballots.push(ballot);
});
return ballots;
}
function getCandidateName(candidate) {
return candidate.trim().replace(/\w\S*/g, function (name) {
return name.charAt(0).toUpperCase() + name.substr(1).toLowerCase();
})
}
function getAndreaExample() {
var ballots = [];
for (var i = 0; i < 16; i++) {
ballots.push(["Andrea", "Brad", "Carter", "Delilah"]);
}
for (var i = 0; i < 24; i++) {
ballots.push(["Andrea", "Carter", "Brad", "Delilah"]);
}
for (var i = 0; i < 17; i++) {
ballots.push(["Delilah", "Andrea", "Brad", "Carter"]);
}
return ballots;
}
function getSnacksExample() {
var ballots = [];
for (var i = 0; i < 4; i++) {
ballots.push(["Oranges"]);
}
for (var i = 0; i < 2; i++) {
ballots.push(["Pears", "Oranges"]);
}
for (var i = 0; i < 8; i++) {
ballots.push(["Chocolate", "Strawberries"]);
}
for (var i = 0; i < 4; i++) {
ballots.push(["Chocolate", "Sweets"]);
}
for (var i = 0; i < 1; i++) {
ballots.push(["Strawberries"]);
}
for (var i = 0; i < 1; i++) {
ballots.push(["Sweets"]);
}
return ballots;
}
})
})()