ensl_gathers/lib/gather/helper.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

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