-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdele.js
186 lines (180 loc) · 6.61 KB
/
dele.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Generated by CoffeeScript 1.12.7
(function() {
var lcount, logit, ready;
lcount = 1;
logit = function(msg, chapa) {
var c, color, plus, pre;
if (chapa == null) {
chapa = "none";
}
plus = "";
pre = "";
if (chapa !== "none") {
if (typeof chapa === "object") {
plus = (function() {
var j, len, results1;
results1 = [];
for (j = 0, len = chapa.length; j < len; j++) {
c = chapa[j];
results1.push("<a class='ui grey circular label'>" + c + "</a>");
}
return results1;
})();
plus.toString(' ');
plus = "<a class='ui green label'>+</a>Chapas " + plus;
} else {
color = chapa === "!" ? "red" : chapa === "+" ? "green" : "grey";
plus = (function() {
switch (false) {
case chapa !== "none":
return "";
case chapa !== "!":
return "<a class='ui " + color + " label'>" + chapa + "</a>";
default:
return "<a class='ui " + color + " circular label'>" + chapa + "</a>";
}
})();
}
pre = lcount + ": ";
lcount += 1;
}
return $("#log").append("<div class='item'><div class='content'>" + pre + plus + " " + msg + "</div></div>");
};
ready = function() {
var b_a, b_d, b_i, p_d, p_i, p_r, p_rd, p_rs, v_insc, v_vags;
p_i = $("#initial");
p_d = $("#data");
p_r = $("#result");
p_rs = $("#simple");
p_rd = $("#log");
v_insc = $("#chapas").val();
v_vags = $("#vagas").val();
b_i = $("#initial-finish");
b_d = $("#data-finish");
b_a = $("#about-box");
$('.ui.accordion').accordion();
b_a.click(function() {
return $('.ui.modal').modal('show');
});
b_i.click(function() {
var i, j, ref;
v_vags = $("#vagas").val();
v_insc = $("#chapas").val();
if ((0 < v_vags && v_vags < 501) && (0 < v_insc && v_insc < 501)) {
for (i = j = 1, ref = v_insc; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
p_d.before('<div class="ui labeled input"><div class="ui blue label">Chapa ' + i + '</div><input type="number" min="0" max="10000" id="votesfor" name="votesfor[' + i + ']"></input></div><br>');
}
return $.tab('change tab', 'dat');
}
});
return b_d.click(function() {
var a_vots, brn, chapa_votes, color, j, k, l, label, len, len1, m, maxp, maxv, nls, p, quoc, r_vags, results, total, v, v_inva, v_min, v_used, vags;
$.tab('change tab', 'res');
v_vags = parseInt($("#vagas").val());
v_insc = parseInt($("#chapas").val());
v_inva = 0;
a_vots = $("input[id='votesfor']").map(function() {
return parseInt($(this).val() || 0);
}).get();
results = $("input[id='votesfor']").map(function() {
return 0;
}).get();
total = a_vots.reduce(function(t, s) {
return t + s;
});
brn = parseInt($("#vbrancos").val() || 0);
nls = parseInt($("#vnulos").val() || 0);
logit("Total de chapas inscritas: " + v_insc);
logit("Total de vagas em disputa: " + v_vags);
logit("Votos em branco: " + brn);
logit("Votos nulos: " + nls);
logit("Votos nas chapas: " + a_vots);
logit("Total de votos: " + (total + brn + nls));
v_min = (function() {
switch (false) {
case v_insc - v_inva !== 2:
return Math.ceil(total * 0.1);
case !(v_insc - v_inva > 2):
return Math.ceil(total * 0.05);
default:
return 1;
}
})();
logit("Mínimo de votos necessários para concorrer: " + v_min);
p = 0;
for (j = 0, len = a_vots.length; j < len; j++) {
chapa_votes = a_vots[j];
if (chapa_votes < v_min) {
logit("Chapa " + (p + 1) + " não obteve o mínimo de votos necessários e foi eliminada.");
v_inva += 1;
results[p] = 'X';
a_vots[p] = 0;
total = a_vots.reduce(function(t, s) {
return t + s;
});
}
p += 1;
}
logit("Total de chapas aptas: " + (v_insc - v_inva));
r_vags = Math.round(total / 10);
if (v_vags > r_vags) {
vags = r_vags;
logit("Total de vagas reajustado para " + r_vags + " devido à quantidade de votantes");
} else {
vags = v_vags;
}
logit("Total de votos válidos: " + total);
logit("Votos nas chapas: " + a_vots);
quoc = (total / vags).toFixed(1);
logit("Quociente eleitoral de " + quoc);
logit("Iniciando a apuração");
while (vags > 0) {
maxv = Math.max.apply(this, a_vots);
maxp = a_vots.indexOf(maxv);
a_vots[maxp] = maxv > quoc ? Number(Math.round((maxv - quoc) + 'e2') + 'e-2') : 0;
v_used = [maxp + 1];
results[maxp] = parseInt(results[maxp]) + 1;
while (Math.max.apply(this, a_vots) === maxv) {
m = a_vots.indexOf(maxv);
v_used.push(m + 1);
results[m] = parseInt(results[m]) + 1;
a_vots[m] = maxv > quoc ? maxv - quoc : 0;
}
if (v_used.length > vags) {
p_rs.append("<div class='item'><b class='ui red circular huge label'>!</b>Houve um problema durante a apuração, referente a empate entre chapas disputando número insuficiente de vagas. Tratar isoladamente este caso. Clique em detalhes.</div>");
logit("Houve empate entre as chapas " + v_used + " disputando " + vags + " vagas!", "!");
return;
} else {
if (v_used.length > 1) {
logit("empataram, cada um tem um representante.", v_used);
} else {
logit("Chapa " + v_used[0] + " garantiu uma vaga.", v_used[0]);
}
vags = vags - v_used.length;
logit("Votos restantes: " + a_vots);
}
}
for (k = l = 0, len1 = results.length; l < len1; k = ++l) {
v = results[k];
color = (function() {
switch (false) {
case v !== 0:
return "grey";
case v !== 'X':
return "yellow";
default:
return "blue";
}
})();
if (v === 'X') {
v = 0;
}
label = v > 1 ? "Delegados" : "Delegado";
p_rs.append("<div class='item'>Chapa <b class='ui circular label'>" + (k + 1) + "</b><div class='right floated content'><div class='ui " + color + " statistic'><div class='value'>" + v + "</div><div class='label'>" + label + "</div></div></div></div>");
}
return logit("Apuração concluída.");
});
};
$(document).ready(ready);
$(document).on('page:load', ready);
}).call(this);