ensl_gathers/lib/gather/helper.js
2015-08-18 10:56:35 +01:00

50 lines
No EOL
1.3 KiB
JavaScript

"use strict";
var User = require("../user/user");
var client = require("../ensl/client")();
var async = require("async");
var createTestUsers = (options, callback) => {
var gather = options.gather;
var getRandomUser = callback => {
var id = Math.floor(Math.random() * 5000) + 1;
client.getUserById({
id: id
}, (error, response, body) => {
if (response.statusCode !== 200) return getRandomUser(callback);
return callback(error, response, body);
});
};
var instructions = [];
for (var i = 0; i < 11; i++) {
instructions.push(callback => {
getRandomUser((error, response, body) => {
if (error) return callback(error);
if (gather.can("addGatherer")) {
gather.addGatherer(new User(body));
}
callback();
});
});
};
async.parallel(instructions, (error) => {
if (error) {
console.log("Error while adding gatherers", error);
} else {
console.log("Loaded gatherers");
gather.gatherers.forEach((gatherer, index, array) => {
var candidate = Math.floor(Math.random() * array.length);
array[index].leaderVote = array[candidate].id;
});
console.log("Assigned vote for each gatherer");
if (typeof callback === 'function') return callback(gather);
}
});
};
module.exports = {
createTestUsers: createTestUsers
};