Add bias option to simulation

This commit is contained in:
dartz 2024-06-01 14:58:40 -04:00
parent a1dfffe237
commit 0e55449e7c
3 changed files with 15 additions and 6 deletions

View file

@ -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,
}
],
}
];

View file

@ -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},

View file

@ -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<Prediction> {
export function MakePrediction(bias: number) : Promise<Prediction> {
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<Prediction> {
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()));