-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #440 from webkom/normal-elections
Backend changes to support normal elections
- Loading branch information
Showing
46 changed files
with
971 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ node_modules | |
*.map | ||
dist | ||
public | ||
app/stv/stv.js | ||
app/algorithms/*.js |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import isEmpty = require('lodash/isEmpty'); | ||
import { Status, Vote, Alternative, ElectionResult, Count } from './types'; | ||
|
||
// This is a TypeScript file in a JavaScript project so it must be complied | ||
// If you make changes to this file it must be recomplied using `tsc` in | ||
// order for the changes to be reflected in the rest of the program. | ||
// | ||
// app/models/election .elect() is the only file that uses this function | ||
// and importes it from normal.js, which is the compiled result of this file. | ||
// | ||
// | ||
|
||
/** | ||
* @param votes - All votes for the election | ||
* @param useStrict - Sets the threshold to 67% | ||
* | ||
* @return The amount votes needed to win | ||
*/ | ||
const winningThreshold = (votes: Vote[], useStrict: boolean): number => { | ||
if (useStrict) { | ||
return Math.floor((2 * votes.length) / 3) + 1; | ||
} | ||
return Math.floor(votes.length / 2 + 1); | ||
}; | ||
|
||
/** | ||
* Will calculate the election result using Single Transferable Vote | ||
* @param votes - All votes for the election | ||
* @param useStrict - This election will require a qualified majority. Default false | ||
* | ||
* @return The full election result | ||
*/ | ||
const calculateWinnerUsingNormal = ( | ||
inputVotes: any, | ||
inputAlternatives: any, | ||
seats = 1, | ||
useStrict = false | ||
): ElectionResult => { | ||
// Stringify and clean the votes | ||
const votes: Vote[] = inputVotes.map((vote: any) => ({ | ||
_id: String(vote._id), | ||
priorities: vote.priorities.map((vote: any) => ({ | ||
_id: String(vote._id), | ||
description: vote.description, | ||
election: String(vote._id), | ||
})), | ||
hash: vote.hash, | ||
})); | ||
|
||
// Stringify and clean the alternatives | ||
let alternatives: Alternative[] = inputAlternatives.map( | ||
(alternative: any) => ({ | ||
_id: String(alternative._id), | ||
description: alternative.description, | ||
election: String(alternative._id), | ||
}) | ||
); | ||
|
||
// Reduce votes to the distinct counts for each alternative | ||
const count: Count = votes.reduce( | ||
(reduced: Count, vote: Vote) => { | ||
if (isEmpty(vote.priorities)) { | ||
reduced['blank'] += 1; | ||
} else { | ||
reduced[vote.priorities[0].description] = | ||
(reduced[vote.priorities[0].description] || 0) + 1; | ||
} | ||
|
||
return reduced; | ||
}, | ||
{ blank: 0 } | ||
); | ||
|
||
// Calculate threshold and see if an alternative can be | ||
const thr = winningThreshold(votes, useStrict); | ||
|
||
// Winner key | ||
const maxKey: string = Object.keys(count).reduce((a, b) => | ||
count[a] > count[b] ? a : b | ||
); | ||
|
||
// Check if we can call the vote Resolved based on | ||
const status = count[maxKey] >= thr ? Status.resolved : Status.unresolved; | ||
|
||
// Create winner alternative from maxKey | ||
const winner = | ||
count[maxKey] >= thr | ||
? { | ||
...alternatives.find((a) => a.description === maxKey), | ||
count: count[maxKey], | ||
} | ||
: undefined; | ||
|
||
return { | ||
result: { | ||
status, | ||
winners: winner ? [winner] : undefined, | ||
}, | ||
thr, | ||
seats, | ||
voteCount: inputVotes.length, | ||
blankVoteCount: count['blank'], | ||
useStrict, | ||
log: count, | ||
}; | ||
}; | ||
|
||
export default calculateWinnerUsingNormal; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.