From 7ad89768e131e146eed7d54b010516b3cae2a39d Mon Sep 17 00:00:00 2001 From: dartz Date: Fri, 26 Jul 2024 01:51:59 -0400 Subject: [PATCH] Add Senate simulator --- assets/{SenateMap2024.svg => Senate2024.svg} | 337 +++---------------- src/VoteType.ts | 1 + src/election.ts | 3 +- src/elections/2024_senate.ts | 307 +++++++++++++++++ src/elections/elections.ts | 2 + src/index.ts | 2 +- src/predictor.ts | 7 + 7 files changed, 371 insertions(+), 288 deletions(-) rename assets/{SenateMap2024.svg => Senate2024.svg} (79%) create mode 100644 src/elections/2024_senate.ts diff --git a/assets/SenateMap2024.svg b/assets/Senate2024.svg similarity index 79% rename from assets/SenateMap2024.svg rename to assets/Senate2024.svg index 9e27265..90b10d3 100644 --- a/assets/SenateMap2024.svg +++ b/assets/Senate2024.svg @@ -4,7 +4,7 @@ height="593" version="1.1" id="svg2" - sodipodi:docname="SenateMap2024.svg" + sodipodi:docname="Senate2024.svg" inkscape:version="1.3.2 (091e20e, 2023-11-25, custom)" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" @@ -22,12 +22,12 @@ inkscape:pageopacity="0" inkscape:pagecheckerboard="1" inkscape:deskcolor="#505050" - inkscape:zoom="1.4142136" - inkscape:cx="461.74073" - inkscape:cy="331.63308" - inkscape:window-width="1920" - inkscape:window-height="1018" - inkscape:window-x="-8" + inkscape:zoom="1.569697" + inkscape:cx="625.91697" + inkscape:cy="365.67566" + inkscape:window-width="1858" + inkscape:window-height="1058" + inkscape:window-x="54" inkscape:window-y="-8" inkscape:window-maximized="1" inkscape:current-layer="svg2" @@ -40,7 +40,7 @@ image/svg+xml - 2024 US presidential election results + 2020 US presidential election results @@ -222,18 +222,9 @@ font-family="Helvetica Neue" font-weight="bold"> - + font-size="28px" + id="g1" + transform="translate(0.90094684,3.6037874)"> WA - + y="49.386745" + style="font-size:13.3333px;text-align:center;text-anchor:middle;fill:#ffffff">WA CA + y="279.08011" + style="font-size:13.3333px;text-align:center;text-anchor:middle;fill:#ffffff">CA NV + y="232.08958" + style="font-size:13.3333px;text-align:center;text-anchor:middle;fill:#ffffff">NV AZ + y="358.99054" + style="font-size:13.3333px;fill:#ffffff">AZ UT - + y="251.48579" + style="font-size:13.3333px;text-align:center;text-anchor:middle;fill:#ffffff">UT MT + y="85.347488" + style="font-size:13.3333px;text-align:center;text-anchor:middle;fill:#ffffff">MT WY - + style="font-size:13.3333px;text-align:center;text-anchor:middle;fill:#ffffff">WY TX - - NE - WI - - MO - - MS - - FL - - + x="763.88647" + y="510.85617" + style="font-size:13.3333px;text-align:center;text-anchor:middle;fill:#f8f8f8;fill-opacity:1">FL TN - OH + y="235.38675" + style="font-size:13.3333px;text-align:center;text-anchor:middle;fill:#ffffff;fill-opacity:1">OH PA + y="210.08958" + style="font-size:13.3333px;text-align:center;text-anchor:middle;fill:#ffffff">PA NY - ME - + id="tspan4" + x="893.28827" + y="89.306831" + style="text-align:center;text-anchor:middle;fill:#ffffff">ME IIS - VT @@ -802,7 +567,7 @@ y="327" style="font-size:16px"> + id="tspan100">DC 3 + sodipodi:nodetypes="cccccccccccccc" /> { return { name: `${(result.winner === c.name ? ":white_check_mark:" : "")} ${c.name} (${c.party})`, - value: `${election.voteType === VoteType.Electoral ? `${c.electoralVotes} electoral votes\n` : ""}${c.votes.toLocaleString()} votes (${((c.votes / result.totalVotes) * 100).toFixed(2)}%)`, + value: `${election.voteType === VoteType.Electoral ? `${c.electoralVotes} electoral votes\n` : ""}${election.voteType === VoteType.Senate ? `${c.seats} seats\n` : ""}${c.votes.toLocaleString()} votes (${((c.votes / result.totalVotes) * 100).toFixed(2)}%)`, inline: true } })) diff --git a/src/predictor.ts b/src/predictor.ts index 9d19be5..fce9491 100644 --- a/src/predictor.ts +++ b/src/predictor.ts @@ -19,6 +19,7 @@ export function MakePrediction(election : Election, overlay_image?: string, over party: candidate.party, votes: 0, electoralVotes: 0, + seats: candidate.startingSeats, }); } @@ -32,6 +33,9 @@ export function MakePrediction(election : Election, overlay_image?: string, over if (election.voteType === VoteType.Electoral) { pred.candidates.find((c : any) => c.party === winner).electoralVotes += election.states[state].electoralVotes; } + if (election.voteType === VoteType.Senate) { + pred.candidates.find((c : any) => c.party === winner).seats += 1; + } for (const candidate of election.candidates) { pred.candidates.find((c : any) => c.party === candidate.party).votes += votes[candidate.party]; } @@ -74,6 +78,8 @@ export function MakePrediction(election : Election, overlay_image?: string, over pred.winner = pred.candidates.sort((a : any, b : any) => b.electoralVotes - a.electoralVotes)[0].name; else if (election.voteType === VoteType.Popular) pred.winner = pred.candidates.sort((a : any, b : any) => b.votes - a.votes)[0].name; + else if (election.voteType === VoteType.Senate) + pred.winner = pred.candidates.sort((a : any, b : any) => b.seats - a.seats)[0].name; pred.totalVotes = pred.candidates.reduce((a : any, b : any) => a + b.votes, 0); pred.svg = draw.svg(); pred.png = png; @@ -87,6 +93,7 @@ export interface Prediction { name : string, party : string, electoralVotes : number, + seats : number, votes : number, }[], winner: string,