-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathban-vote-cache.js
112 lines (103 loc) · 3.18 KB
/
ban-vote-cache.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
const db = require('./database');
const RankMetadata = require('./rank-definitions');
const UserCache = require('./user-cache');
const voteCache = {};
async function DeleteVotesForDefendant(defendantId) {
if (defendantId in voteCache) {
delete voteCache[defendantId];
}
await db.Query('DELETE FROM ban_votes WHERE defendant_id = ?', [defendantId]);
}
function CountTotalVotesForDefendant(defendantId) {
const votes = voteCache[defendantId] || {};
return Object.keys(votes).length;
}
function GetSortedVotesForDefendant(defendantId) {
const w = {
0: [],
1: [],
2: [],
};
const votes = voteCache[defendantId] || {};
for (const voterId in votes) {
const vote = votes[voterId];
const voter = UserCache.GetCachedUserByCommissarId(voterId);
const rankData = RankMetadata[voter.rank];
const weight = rankData.collectiveVoteWeight / rankData.count;
const color = (vote === 1) ? rankData.color : rankData.secondaryColor;
w[vote].push({ color, weight });
}
const compareWeight = (a, b) => (b.weight - a.weight);
w[0].sort(compareWeight);
w[1].sort(compareWeight);
w[2].sort(compareWeight);
return w;
}
function CountWeightedVotesForDefendant(defendantId) {
const w = {
0: 0,
1: 0,
2: 0,
};
const votes = voteCache[defendantId] || {};
for (const voterId in votes) {
const vote = votes[voterId];
const voter = UserCache.GetCachedUserByCommissarId(voterId);
const rankData = RankMetadata[voter.rank];
const individualVoteWeight = rankData.collectiveVoteWeight / rankData.count;
w[vote] += individualVoteWeight;
}
return w;
}
async function ExpungeVotesWithNoOngoingTrial() {
for (const defendantId in voteCache) {
const cu = UserCache.GetCachedUserByCommissarId(defendantId);
if (cu) {
if (!cu.ban_vote_start_time) {
await DeleteVotesForDefendant(defendantId);
// Bail after updating one defendant to avoid race conditions.
return;
}
}
}
}
async function LoadVotesFromDatabase() {
const votes = await db.Query('SELECT * FROM ban_votes');
for (const vote of votes) {
if (!(vote.defendant_id in voteCache)) {
voteCache[vote.defendant_id] = {};
}
voteCache[vote.defendant_id][vote.voter_id] = vote.vote;
}
}
async function RecordVoteIfChanged(defendantId, voterId, vote) {
const cu = UserCache.GetCachedUserByCommissarId(defendantId);
if (!cu) {
// Don't record votes for non-existent users or bots and the like.
return;
}
if (!cu.ban_vote_start_time) {
// Don't record votes unless a trial is under way.
return;
}
if (!(defendantId in voteCache)) {
voteCache[defendantId] = {};
}
const cachedVote = voteCache[defendantId][voterId];
if (vote === cachedVote) {
return;
}
voteCache[defendantId][voterId] = vote;
const sql = 'INSERT INTO ban_votes (defendant_id, voter_id, vote) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE vote = VALUES(vote)';
const values = [defendantId, voterId, vote];
await db.Query(sql, values);
}
module.exports = {
CountTotalVotesForDefendant,
CountWeightedVotesForDefendant,
DeleteVotesForDefendant,
ExpungeVotesWithNoOngoingTrial,
GetSortedVotesForDefendant,
LoadVotesFromDatabase,
RecordVoteIfChanged,
};