Files
HQNode/getWeightedAnswers.js
2018-06-27 16:12:05 -04:00

43 lines
1.2 KiB
JavaScript

import { players } from './config.json';
// import Account from './classes/Account';
const weight1 = 40;
const weight2 = -5;
const weight3 = 20;
const weights = [weight1, weight2, weight3];
const getAnswerId = (index) => {
// convert answer index (0,1,2) to answerId to pass into sendAnswer below.
switch (index) {
case 0: return 'AnswerId1';
case 1: return 'AnswerId2';
case 2: return 'AnswerId3';
}
};
const totalWeight = weights
.filter(weight => weight > 0)
.reduce((accumulator, currentValue) => accumulator + currentValue);
const normalized = weights.map(weight => {
const zeroNegatives = weight < 0 ? 0 : weight;
return Math.round((zeroNegatives / totalWeight) * players.length);
});
console.log('weights', weights);
console.log('total weight', totalWeight);
console.log('total players', players.length);
console.log('normalized', normalized);
console.log();
let a = 1;
normalized.forEach((playersAssignedToAnswer, index) => {
for (let i = 0; i < playersAssignedToAnswer; i++) {
// Commented line below is what you'd actually use.
// players[i].sendAnswer(getAnswerId(index));
const player = players[i];
console.log(`Player ${a} sent answer ${index + 1}: ${getAnswerId(index)}!`);
a++;
}
});