From 0e55449e7c13118492eaa4c9bfa12a3b62e011d3 Mon Sep 17 00:00:00 2001 From: dartz Date: Sat, 1 Jun 2024 14:58:40 -0400 Subject: [PATCH] Add bias option to simulation --- src/commands.ts | 8 ++++++++ src/index.ts | 4 ++-- src/predictor.ts | 9 +++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index 340e1d2..3ca0c35 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -1,7 +1,15 @@ +import {ApplicationCommandOptionType} from "discord.js"; const commands = [ { name: 'simulate', description: "Simulate the 2024 United States Presidential Election", + options: [ + { + name: 'bias', + description: 'Adds a bias to a certain candidate. Use a positive number for Biden bias, negative for Trump bias', + type: ApplicationCommandOptionType.Number, + } + ], } ]; diff --git a/src/index.ts b/src/index.ts index 67f9e10..e5efba0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,9 +23,9 @@ if (!config.token) { if (i instanceof CommandInteraction) { switch (i.commandName) { case "simulate": - var result = await MakePrediction(); + var result = await MakePrediction(Number(i.options.get('bias')?.value)); var embed = new EmbedBuilder() - .setTitle("2024 United States Presidential Election") + .setTitle("2024 United States Presidential Election Simulator") .setDescription(`The CalubViem Press has called the 2024 United States Presidential Election for ${result.winner}!`) .addFields( {name: `${result.winner === result.gopCandidate ? ":white_check_mark:" : ""} ${result.gopCandidate}`, value: `${result.gopVotes} Electoral Votes`, inline: true}, diff --git a/src/predictor.ts b/src/predictor.ts index 119fedb..7998469 100644 --- a/src/predictor.ts +++ b/src/predictor.ts @@ -9,8 +9,9 @@ const GOP_CANDIDATE = "Donald J. Trump"; const DEM_CANDIDATE = "Joseph R. Biden Jr."; const BASE_SVG = await fs.readFile("assets/ElectoralCollege2024.svg", "utf-8"); -export function MakePrediction() : Promise { +export function MakePrediction(bias: number) : Promise { return new Promise(async res => { + if (Number.isNaN(bias)) bias = 0; const window = createSVGWindow(); registerWindow(window, window.document); var election = {} as any; @@ -19,17 +20,17 @@ export function MakePrediction() : Promise { var draw = SVG(window.document.documentElement); draw.svg(BASE_SVG); Object.keys(ELECTORAL_COLLEGE).forEach(state => { - if ((GOP_WIN_ODDS as any)[state] >= Math.random()) { + if ((GOP_WIN_ODDS as any)[state] >= bias + Math.random()) { election[state] = "R"; gopVotes += (ELECTORAL_COLLEGE as any)[state]; // @ts-ignore - draw.find(`#${state}`).fill("#F07763"); + draw.find(`#${state}`).fill("#BF1D29"); } else { election[state] = "D"; demVotes += (ELECTORAL_COLLEGE as any)[state]; // @ts-ignore - draw.find(`#${state}`).fill("#698DC5"); + draw.find(`#${state}`).fill("#1C408C"); } }); var s = sharp(Buffer.from(draw.svg()));