Add bias option to simulation
This commit is contained in:
parent
a1dfffe237
commit
0e55449e7c
3 changed files with 15 additions and 6 deletions
|
@ -1,7 +1,15 @@
|
||||||
|
import {ApplicationCommandOptionType} from "discord.js";
|
||||||
const commands = [
|
const commands = [
|
||||||
{
|
{
|
||||||
name: 'simulate',
|
name: 'simulate',
|
||||||
description: "Simulate the 2024 United States Presidential Election",
|
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,
|
||||||
|
}
|
||||||
|
],
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
@ -23,9 +23,9 @@ if (!config.token) {
|
||||||
if (i instanceof CommandInteraction) {
|
if (i instanceof CommandInteraction) {
|
||||||
switch (i.commandName) {
|
switch (i.commandName) {
|
||||||
case "simulate":
|
case "simulate":
|
||||||
var result = await MakePrediction();
|
var result = await MakePrediction(Number(i.options.get('bias')?.value));
|
||||||
var embed = new EmbedBuilder()
|
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}!`)
|
.setDescription(`The CalubViem Press has called the 2024 United States Presidential Election for ${result.winner}!`)
|
||||||
.addFields(
|
.addFields(
|
||||||
{name: `${result.winner === result.gopCandidate ? ":white_check_mark:" : ""} ${result.gopCandidate}`, value: `${result.gopVotes} Electoral Votes`, inline: true},
|
{name: `${result.winner === result.gopCandidate ? ":white_check_mark:" : ""} ${result.gopCandidate}`, value: `${result.gopVotes} Electoral Votes`, inline: true},
|
||||||
|
|
|
@ -9,8 +9,9 @@ const GOP_CANDIDATE = "Donald J. Trump";
|
||||||
const DEM_CANDIDATE = "Joseph R. Biden Jr.";
|
const DEM_CANDIDATE = "Joseph R. Biden Jr.";
|
||||||
const BASE_SVG = await fs.readFile("assets/ElectoralCollege2024.svg", "utf-8");
|
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 => {
|
return new Promise(async res => {
|
||||||
|
if (Number.isNaN(bias)) bias = 0;
|
||||||
const window = createSVGWindow();
|
const window = createSVGWindow();
|
||||||
registerWindow(window, window.document);
|
registerWindow(window, window.document);
|
||||||
var election = {} as any;
|
var election = {} as any;
|
||||||
|
@ -19,17 +20,17 @@ export function MakePrediction() : Promise<Prediction> {
|
||||||
var draw = SVG(window.document.documentElement);
|
var draw = SVG(window.document.documentElement);
|
||||||
draw.svg(BASE_SVG);
|
draw.svg(BASE_SVG);
|
||||||
Object.keys(ELECTORAL_COLLEGE).forEach(state => {
|
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";
|
election[state] = "R";
|
||||||
gopVotes += (ELECTORAL_COLLEGE as any)[state];
|
gopVotes += (ELECTORAL_COLLEGE as any)[state];
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
draw.find(`#${state}`).fill("#F07763");
|
draw.find(`#${state}`).fill("#BF1D29");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
election[state] = "D";
|
election[state] = "D";
|
||||||
demVotes += (ELECTORAL_COLLEGE as any)[state];
|
demVotes += (ELECTORAL_COLLEGE as any)[state];
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
draw.find(`#${state}`).fill("#698DC5");
|
draw.find(`#${state}`).fill("#1C408C");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
var s = sharp(Buffer.from(draw.svg()));
|
var s = sharp(Buffer.from(draw.svg()));
|
||||||
|
|
Loading…
Reference in a new issue