-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new election
BudgetElection
and some minor changes in `MultiC…
…hoiceElection`
- Loading branch information
1 parent
e741d20
commit 138f5cd
Showing
7 changed files
with
342 additions
and
25 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
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,134 @@ | ||
import { MultiLanguage } from '../../util/lang'; | ||
import { IElectionParameters } from './election'; | ||
import { UnpublishedElection } from './unpublished'; | ||
import { ElectionMetadata, ElectionMetadataTemplate, ElectionResultsTypeNames } from '../metadata'; | ||
|
||
export interface IBudgetElectionParametersInfo extends IElectionParameters { | ||
minStep?: number; | ||
forceFullBudget?: boolean; | ||
} | ||
|
||
export interface IBudgetElectionParametersWithCensusWeight extends IBudgetElectionParametersInfo { | ||
useCensusWeightAsBudget: true; | ||
} | ||
|
||
export interface IBudgetElectionParametersWithBudget extends IBudgetElectionParametersInfo { | ||
useCensusWeightAsBudget: false; | ||
maxBudget: number; | ||
} | ||
|
||
export type IBudgetElectionParameters = IBudgetElectionParametersWithCensusWeight | IBudgetElectionParametersWithBudget; | ||
|
||
/** | ||
* Represents a budget election | ||
*/ | ||
export class BudgetElection extends UnpublishedElection { | ||
private _minStep: number; | ||
private _forceFullBudget: boolean; | ||
|
||
/** | ||
* Constructs a budget election | ||
* | ||
* @param params Budget election parameters | ||
*/ | ||
public constructor(params: IBudgetElectionParameters) { | ||
super(params); | ||
this.minStep = params.minStep ?? 1; | ||
this.forceFullBudget = params.forceFullBudget ?? false; | ||
this.useCensusWeightAsBudget = params.useCensusWeightAsBudget; | ||
if ('maxBudget' in params) { | ||
this.maxBudget = params.maxBudget; | ||
} | ||
} | ||
|
||
public static from(params: IBudgetElectionParameters) { | ||
return new BudgetElection(params); | ||
} | ||
|
||
public addQuestion( | ||
title: string | MultiLanguage<string>, | ||
description: string | MultiLanguage<string>, | ||
choices: Array<{ title: string } | { title: MultiLanguage<string> }> | ||
) { | ||
if (this.questions.length > 0) { | ||
throw new Error('This type of election can only have one question'); | ||
} | ||
|
||
return super.addQuestion( | ||
title, | ||
description, | ||
choices.map((choice, index) => ({ | ||
title: typeof choice.title === 'string' ? { default: choice.title } : choice.title, | ||
value: index, | ||
})) | ||
); | ||
} | ||
|
||
public generateVoteOptions(): object { | ||
const maxCount = this.questions[0].choices.length; | ||
const maxValue = 0; | ||
const maxVoteOverwrites = this.voteType.maxVoteOverwrites; | ||
const maxTotalCost = this.useCensusWeightAsBudget ? 0 : this.maxBudget; | ||
const costExponent = 1; | ||
|
||
return { maxCount, maxValue, maxVoteOverwrites, maxTotalCost, costExponent }; | ||
} | ||
|
||
public generateEnvelopeType(): object { | ||
const serial = false; // TODO | ||
const anonymous = this.electionType.anonymous; | ||
const encryptedVotes = this.electionType.secretUntilTheEnd; | ||
const uniqueValues = false; | ||
const costFromWeight = this.useCensusWeightAsBudget; | ||
|
||
return { serial, anonymous, encryptedVotes, uniqueValues, costFromWeight }; | ||
} | ||
|
||
public generateMetadata(): ElectionMetadata { | ||
const metadata = ElectionMetadataTemplate; | ||
|
||
metadata.type = { | ||
name: ElectionResultsTypeNames.BUDGET, | ||
properties: { | ||
useCensusWeightAsBudget: this.useCensusWeightAsBudget, | ||
maxBudget: this.useCensusWeightAsBudget ? null : this.maxBudget, | ||
forceFullBudget: this.forceFullBudget, | ||
minStep: this.minStep, | ||
}, | ||
}; | ||
|
||
return super.generateMetadata(metadata); | ||
} | ||
|
||
get minStep(): number { | ||
return this._minStep; | ||
} | ||
|
||
set minStep(value: number) { | ||
this._minStep = value; | ||
} | ||
|
||
get forceFullBudget(): boolean { | ||
return this._forceFullBudget; | ||
} | ||
|
||
set forceFullBudget(value: boolean) { | ||
this._forceFullBudget = value; | ||
} | ||
|
||
get useCensusWeightAsBudget(): boolean { | ||
return this.voteType.costFromWeight; | ||
} | ||
|
||
set useCensusWeightAsBudget(value: boolean) { | ||
this.voteType.costFromWeight = value; | ||
} | ||
|
||
get maxBudget(): number { | ||
return this.voteType.maxTotalCost; | ||
} | ||
|
||
set maxBudget(value: number) { | ||
this.voteType.maxTotalCost = value; | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.