Skip to content

Commit bde582f

Browse files
committed
more testing
1 parent 04151dd commit bde582f

File tree

5 files changed

+373
-525
lines changed

5 files changed

+373
-525
lines changed

apps/client/src/components/ResultsViewer.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function ResultBar(props: {
3838
}
3939

4040
// download text as file
41-
const handleDownload = (filename: string, filetype: string, text: string) => {
41+
const _handleDownload = (filename: string, filetype: string, text: string) => {
4242
const element = document.createElement("a");
4343
const file = new Blob([text], { type: filetype });
4444
element.href = URL.createObjectURL(file);
@@ -84,15 +84,6 @@ export function ResultsViewer({ results }: { results: ResultsView }) {
8484
...results.results.map((v) => v.votes),
8585
);
8686

87-
const handleRoundsDownload = () => {
88-
const roundsText = JSON.stringify(results.rounds, null, 2);
89-
handleDownload(
90-
"preferential-vote-rounds.json",
91-
"application/json",
92-
roundsText,
93-
);
94-
};
95-
9687
return (
9788
<div className="flex flex-col gap-4">
9889
{candidates.map((result) => (
@@ -109,9 +100,6 @@ export function ResultsViewer({ results }: { results: ResultsView }) {
109100
max={maxVote}
110101
grey={true}
111102
/>
112-
<button className="btn" onClick={handleRoundsDownload} type="button">
113-
Download Rounds Data
114-
</button>
115103
</div>
116104
);
117105
}

apps/server/src/live/question.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export interface SingleVoteResultsView {
6767
export interface PreferentialVoteResultsView {
6868
type: typeof preferentialVoteType;
6969
results: CandidateWithRank[];
70-
rounds: RoundRecord[];
70+
records: RoundRecord[]
7171
}
7272

7373
export type ResultsView = { abstained: number } & (

apps/server/src/room/interaction/db/questions.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { QuestionResponse, ResultsView } from "../../../live/question";
66
import type { VotingCandidate } from "../../../live/states";
77
import { UnreachableError } from "../../../unreachableError";
88
import type { CreateQuestionParams, QuestionFormatDetails } from "../../types";
9-
import { rankedElection } from "../preferentialVote";
9+
import { STVElection } from "../preferentialVote";
1010
import type { CloseQuestionDetails, DbQuestionData } from "./queries";
1111
import {
1212
dbCloseQuestion,
@@ -113,30 +113,28 @@ function mapDbQuestionData(question: DbQuestionData): RoomQuestion {
113113
},
114114
);
115115

116-
const candidateIds = question.candidates.map(
117-
(candidate) => candidate.id,
118-
);
116+
const election = new STVElection()
117+
118+
for (const prefs of votingPreferences) {
119+
election.addBallot(prefs);
120+
}
119121

120-
const { elected, rounds } = rankedElection(
121-
candidateIds,
122-
votingPreferences,
123-
question.maxElected,
124-
); // get the result in order of preference
122+
const { elected, records } = election.runElection(question.maxElected);
125123

126124
const results = elected.map((c, index) => ({
127-
id: c.id,
128-
// biome-ignore lint/style/noNonNullAssertion: Known keys
129-
name: question.candidates.find((candidate) => candidate.id === c.id)
130-
?.name!,
125+
id: c,
126+
name:
127+
question.candidates.find(
128+
(candidate) => candidate.id === c,
129+
)?.name || "Unknown Candidate",
131130
rank: index + 1,
132-
votes: candidateWithVotes.filter((vote) => vote.candidateId === c.id)
133-
.length,
131+
votes: records[records.length - 1]?.voteTotals[c] || 0,
134132
}));
135133

136134
return {
137135
type: "PreferentialVote",
138136
results,
139-
rounds,
137+
records,
140138
abstained: abstainCount,
141139
};
142140
}

0 commit comments

Comments
 (0)