PixelPiggy/CS_float
0
1const Bot = require('./bot'),2 utils = require('./utils'),3 EventEmitter = require('events').EventEmitter,4 errors = require('../errors');5 6class BotController extends EventEmitter {7 constructor() {8 super();9 10 this.readyEvent = false;11 this.bots = [];12 }13 14 addBot(loginData, settings) {15 let bot = new Bot(settings);16 bot.logIn(loginData.user, loginData.pass, loginData.auth);17 18 bot.on('ready', () => {19 if (!this.readyEvent && this.hasBotOnline()) {20 this.readyEvent = true;21 this.emit('ready');22 }23 });24 25 bot.on('unready', () => {26 if (this.readyEvent && this.hasBotOnline() === false) {27 this.readyEvent = false;28 this.emit('unready');29 }30 });31 32 this.bots.push(bot);33 }34 35 getFreeBot() {36 // Shuffle array to evenly distribute requests37 for (let bot of utils.shuffleArray(this.bots)) {38 if (!bot.busy && bot.ready) return bot;39 }40 41 return false;42 }43 44 hasBotOnline() {45 for (let bot of this.bots) {46 if (bot.ready) return true;47 }48 49 return false;50 }51 52 getReadyAmount() {53 let amount = 0;54 for (const bot of this.bots) {55 if (bot.ready) {56 amount++;57 }58 }59 return amount;60 }61 62 lookupFloat(data) {63 let freeBot = this.getFreeBot();64 65 if (freeBot) return freeBot.sendFloatRequest(data);66 else return Promise.reject(errors.NoBotsAvailable);67 }68}69 70module.exports = BotController;71 