Skip to content

Commit

Permalink
Still a work in progress
Browse files Browse the repository at this point in the history
Rewriting the dotahandler module.
  • Loading branch information
MeLlamoPablo committed Jan 7, 2017
1 parent 5d1aa84 commit 227c59a
Show file tree
Hide file tree
Showing 6 changed files with 545 additions and 392 deletions.
95 changes: 54 additions & 41 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
"use strict";

const fs = require('fs')
, crypto = require('crypto')
, Clapp = require('./modules/clapp-discord')
, SummaryHandler = require('./modules/summaryhandler')
, db = require('./modules/dbhandler')
, cfg = require('../config.js')
, loadSteamBot = require('./modules/steambotshandler/loadbot')
, moment = require('moment')
, pkg = require('../package.json')
, Discord = require('discord.js')
, Dota2 = require('dota2')
, DotaHandler = require('./modules/dotahandler')
, ELobbyStatus = require('./structures/enums/ELobbyStatus')
, Steam = require('steam')
, steamVerf = require('steam-verificator')
, bot = new Discord.Client()
const fs = require('fs')
, crypto = require('crypto')
, Clapp = require('./modules/clapp-discord')
, SummaryHandler = require('./modules/summaryhandler')
, db = require('./modules/dbhandler')
, cfg = require('../config.js')
, loadSteamBot = require('./modules/steambotshandler/loadbot')
, moment = require('moment')
, pkg = require('../package.json')
, Discord = require('discord.js')
, Dota2 = require('dota2')
, DotaBotsHandler = require('./modules/dotahandler/DotaBotsHandler')
, DotaHandler = require('./modules/dotahandler')
, ELobbyStatus = require('./structures/enums/ELobbyStatus')
, Steam = require('steam')
, steamVerf = require('steam-verificator')
, bot = new Discord.Client()
;

let masterChannel, summaryHandler, botAdmins, blacklist = [], verificator, dotaHandler,
bannedCommands = [],
steamClient = new Steam.SteamClient(),
steamUser = new Steam.SteamUser(steamClient),
steamFriends = new Steam.SteamFriends(steamClient),
dotaClient = new Dota2.Dota2Client(steamClient, false, false);
steamBots = [];

if (!cfg.quick_inhouse.enabled) {
bannedCommands.push("quick-inhouse.js");
Expand Down Expand Up @@ -133,26 +131,8 @@ bot.on('message', msg => {
}
});

steamClient.on("error", console.error);

// If a sentry file is generated, update it.
steamUser.on('updateMachineAuth', function(sentry, callback) {
let hashedSentry = crypto.createHash('sha1').update(sentry.bytes).digest();
db.config.steam.saveSentryFile(hashedSentry).then(() => {
console.log("[STEAM] New sentry file saved to the database.");
callback({ sha_file: hashedSentry});
}).catch(err => {
console.log("[STEAM] Couldn't save the sentry file to the database:");
consle.error(err);

console.log("[STEAM] Saving it to the local machine instead");
fs.writeFileSync('sentry', hashedSentry);
callback({ sha_file: hashedSentry});
});
});

// Startup tasks
console.log("[INFO] Loading ScheduleBot... Please wait");
console.log(`[INFO] Loading ScheduleBot v${pkg.version}-dota... Please wait.`);

let startupPromises = [];

Expand Down Expand Up @@ -202,8 +182,18 @@ startupPromises.push(

let steamStartupPromises = [];

// Load all Steam bots
// Load all verificator ignored users
steamStartupPromises.push(new Promise((fulfill2, reject2) => {
db.users.getAllLinked()
.then(users => users.map(e => e.steam_id))
.then(fulfill2)
.catch(reject2);
}));

// Load all bots
steamStartupPromises.push(new Promise((fulfill2, reject2) => {

// Load all Steam bots
let loadBotsPromises = [];

for (let i = 0; i < bots.length; i++) {
Expand All @@ -212,9 +202,32 @@ startupPromises.push(
}

Promise.all(loadBotsPromises).then(fulfill2).catch(reject2);

}));

Promise.all(steamStartupPromises)/*.then(() => process.exit())*/.catch(reject);

Promise.all(steamStartupPromises).then(values => {

let ignoredUsers = values[0];
steamBots = values[1];

// Create the verificator based on the first bot.
verificator = new steamVerf.Verificator({
trigger: steamVerf.Trigger.FriendRequest,
triggerOptions: {
secondService: "Discord's " + cfg.name,
ignoredUsers: ignoredUsers
},
steamClient: steamBots[0].steam.client,
steamUser: steamBots[0].steam.user,
steamFriends: steamBots[0].steam.friends
});

dotaHandler = new DotaHandler(new DotaBotsHandler(steamBots));

fulfill();

}).catch(reject);

} else {
return reject(
Expand Down
71 changes: 71 additions & 0 deletions lib/modules/dotahandler/DotaBotsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const DotaHandler = require("./index");

// TODO remove this and move it to index.js

/**
* @class DotaBotsHandler
*
* This class manages dota bots, and interacts with the DotaHandler.
*
* @property {DotaClientX[]} clients
*/
class DotaBotsHandler {

/**
* @param {SteamBot[]} bots
*/
constructor(bots) {
this.clients = bots.map(e => e.dota.client);

// Convert the client object from Dota2Client to DotaClientX and subscribe to the
// practiceLobbyUpdate event
this.clients.forEach(client => {
client.currentLobby = {};

client.currentLobby.name = null;
client.currentLobby.password = null;
client.currentLobby.chatChannel = null;
client.currentLobby.starting = false;
client.currentLobby.enoughPeople = false;

client.on("practiceLobbyUpdate", lobby => {
this.currentLobby.chatChannel = "Lobby_" + lobby.lobby_id;

let people = lobby.members.filter(e => {
// e.team: 0 - Radiant
// 1 - Dire
// 2 - Casters |
// 3 - Coaches |-> Filter out these
// 4 - Unassigned |
return e.team === 0 || e.team === 1
}).length;

if (!client.currentLobby.starting) {
// TODO this.sendMessageToLobby(DotaHandler.generateStatusMessage(people));
}

client.currentLobby.enoughPeople = (people >= 10);
if (client.currentLobby.enoughPeople && !client.currentLobby.starting) {
// TODO this.start();
}
});
});
}

/**
* Returns any bot that is not in a lobby, or null if all bots are full.
*
* @returns {DotaClientX|null}
*/
getAvailableBot() {
this.clients.forEach(client => {
if (!client.currentLobby) {
return client;
}
});

return null;
}
}

module.exports = DotaBotsHandler;
Loading

0 comments on commit 227c59a

Please sign in to comment.