Skip to content

Commit

Permalink
Merge pull request #102 from j3camero/presidential-election-reform
Browse files Browse the repository at this point in the history
Bunch of changes made on a branch
  • Loading branch information
j3camero authored Sep 23, 2024
2 parents aeedb41 + 30a9f03 commit 4a643b6
Show file tree
Hide file tree
Showing 19 changed files with 1,537 additions and 634 deletions.
43 changes: 38 additions & 5 deletions ban-vote-cache.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const db = require('./database');
const RankMetadata = require('./rank-definitions');
const UserCache = require('./user-cache');

const voteCache = {};
Expand All @@ -10,18 +11,48 @@ async function DeleteVotesForDefendant(defendantId) {
await db.Query('DELETE FROM ban_votes WHERE defendant_id = ?', [defendantId]);
}

function CountVotesForDefendant(defendantId) {
const totals = {
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];
totals[vote]++;
const voter = UserCache.GetCachedUserByCommissarId(voterId);
const rankData = RankMetadata[voter.rank];
const individualVoteWeight = rankData.collectiveVoteWeight / rankData.count;
w[vote] += individualVoteWeight;
}
return totals;
return w;
}

async function ExpungeVotesWithNoOngoingTrial() {
Expand Down Expand Up @@ -71,9 +102,11 @@ async function RecordVoteIfChanged(defendantId, voterId, vote) {
}

module.exports = {
CountVotesForDefendant,
CountTotalVotesForDefendant,
CountWeightedVotesForDefendant,
DeleteVotesForDefendant,
ExpungeVotesWithNoOngoingTrial,
GetSortedVotesForDefendant,
LoadVotesFromDatabase,
RecordVoteIfChanged,
};
Loading

0 comments on commit 4a643b6

Please sign in to comment.