Add weighted answers calculation
This commit is contained in:
42
getWeightedAnswers.js
Normal file
42
getWeightedAnswers.js
Normal file
@@ -0,0 +1,42 @@
|
||||
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++;
|
||||
}
|
||||
});
|
||||
@@ -9,7 +9,8 @@
|
||||
"create": "babel-node createAccount.js --presets env,es2016,stage-2",
|
||||
"summary": "babel-node accountSummary.js --presets env,es2016,stage-2",
|
||||
"avatar": "babel-node changeAvatar.js --presets env,es2016,stage-2",
|
||||
"cashout": "babel-node cashout.js --presets env,es2016,stage-2"
|
||||
"cashout": "babel-node cashout.js --presets env,es2016,stage-2",
|
||||
"answers": "babel-node getWeightedAnswers.js --presets env,es2016,stage-2"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
|
||||
Reference in New Issue
Block a user