fix crash when all candidates end up with <=0 odds

This commit is contained in:
Elijah R 2024-06-04 01:11:06 -04:00
parent ccc2b6e205
commit 20292a7160

View file

@ -21,6 +21,9 @@ export function MakePrediction(election : Election) : Promise<Prediction> {
}
for (const state of Object.keys(election.states)) {
if (Object.keys(election.states[state].odds).every(p => election.states[state].odds[p] <= 0))
for (const candidate of election.candidates)
election.states[state].odds[candidate.party] = 1;
var winner = await weightedRand(election.states[state].odds);
pred.candidates.find((c : any) => c.party === winner).votes += election.states[state].electoralVotes;
// @ts-ignore
@ -62,6 +65,7 @@ export function betterRandom(min : number, max : number) : Promise<number> {
export async function weightedRand(spec : { [key : string] : number }) : Promise<string> {
var i, j, table = [];
for (i in spec) {
if (spec[i] <= 0) continue;
for (j = 0; j < spec[i] * 10; j++) {
table.push(i);
}