Skip to content

Commit

Permalink
Progress on Dota lobbies
Browse files Browse the repository at this point in the history
Now the bot is able to create lobbies and start with force-lobby-start
  • Loading branch information
MeLlamoPablo committed Dec 7, 2016
1 parent cad0207 commit 19d7d64
Show file tree
Hide file tree
Showing 13 changed files with 600 additions and 54 deletions.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = {
// --server flag to the add-inhouse command.
// Go to that command's file (Or type -schedulebot add-ihouse --help)
// to see possible values.
defaultServer: "Europe"
defaultServer: "Luxembourg"
}
};

Expand Down
14 changes: 14 additions & 0 deletions lib/commands/admin/force-lobby-start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict";

const Clapp = require('../../modules/clapp-discord/index');

module.exports = new Clapp.Command({
name: "force-lobby-start",
desc: "Forces the current lobby to start, even if there aren't enough players.",
fn: (argv, context) => {
return new Promise((fulfill, reject) => {
context.dotaHandler.forceStart().catch(reject);
fulfill("Forced current lobby start");
});
}
});
23 changes: 23 additions & 0 deletions lib/commands/admin/get-lobby.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

const Clapp = require('../../modules/clapp-discord/index');

module.exports = new Clapp.Command({
name: "get-lobby",
desc: "Gets the current lobby name and password sent to you on a DM",
fn: (argv, context) => {
return new Promise((fulfill, reject) => {
let lobby = context.dotaHandler.currentLobbyName;
let pass = context.dotaHandler.currentLobbyPassword;

context.msg.author.sendMessage(
"Hello! Here's the information you requested:\n\n" +

"- **Current lobby name**: `" + lobby + "`\n" +
"- **Current lobby password**: `" + pass + "`"
).then(() => {
fulfill("The information you requested was sent to you in a DM.");
}).catch(reject);
});
}
});
35 changes: 29 additions & 6 deletions lib/commands/general/add-inhouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Clapp = require('../../modules/clapp-discord/index')
, cfg = require('../../../config')
, db = require('../../modules/dbhandler').events;
, db = require('../../modules/dbhandler');

module.exports = new Clapp.Command({
name: "add-inhouse",
Expand All @@ -13,15 +13,38 @@ module.exports = new Clapp.Command({
"\nWhen the event time comes, every attendant will be invited to the event.",
fn: (argv, context) => {
return new Promise((fulfill, reject) => {
db.get(argv.args.id).then(event => {
db.events.get(argv.args.id).then(event => {
if (event !== null) {
let inhouseProps = {};

inhouseProps.gameMode = argv.flags.gamemode.toLowerCase().replace(" ", "");
inhouseProps.server = argv.flags.server.toLowerCase().replace(" ", "");

db.addInhouse(event, inhouseProps).then(() => {
fulfill("The inhouse has been added to the event `#" + argv.args.id + "`");
db.events.addInhouse(event, inhouseProps).then(() => {

// Kick every user that hasn't linked their Steam from the event
db.confirms.getByEvent(event).then(confirms => {
let people = confirms.map(e => { return e.user });

for (let i = 0; i < people.length; i++) {
db.users.getByDiscord(people[i]).then(user => {
if (user === null || user.steam_id === null) {
db.confirms.deleteByUserAndEvent(
people[i], event.id
).then(() => {
context.summaryHandler.updateSummary(event)
.catch(reject);
}).catch(reject);
}
}).catch(reject);
}
}).catch(reject);
context.summaryHandler.updateSummary(event).catch(reject);

fulfill("The inhouse has been added to the event `#"
+ argv.args.id + "`\n" +
"If there were any people who confirmed their attendance, but didn't" +
" have their Steam account linked, they have been removed.");
}).catch(reject);
} else {
fulfill("The event `#" + argv.args.id + "` doesn't exist.");
Expand Down Expand Up @@ -61,7 +84,7 @@ module.exports = new Clapp.Command({
desc: "The inhouse server. Possible values are:\n" +
"- US West\n" +
"- US East\n" +
"- Europe\n" +
"- Luxembourg\n" +
"- Australia\n" +
"- Stockholm\n\n" +
"" +
Expand All @@ -78,7 +101,7 @@ module.exports = new Clapp.Command({
switch (value) {
case "uswest":
case "useast":
case "europe":
case "luxembourg":
case "australia":
case "stockholm":
return true;
Expand Down
65 changes: 48 additions & 17 deletions lib/commands/general/confirm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,62 @@ module.exports = new Clapp.Command({
"`yes` or `no`"); return;
}

db.events.get(argv.args.id).then(event => {
let isSteamLinkedPromise = new Promise((fulfill, reject) => {
db.users.getByDiscord(context.msg.author.id).then(user => {
fulfill(user !== null && user.steam_id !== null);
});
});
let getEventPromise = db.events.get(argv.args.id);

Promise.all([isSteamLinkedPromise, getEventPromise]).then(values => {
let isSteamLinked = values[0];
let event = values[1];

if (event !== null) {
db.confirms.getByEvent(event).then(confirms => {
let attendanceConfirms = confirms !== null ? confirms.filter(el => {
return el.attends;
}) : [];

if (attendanceConfirms.length < event.limit) {
db.confirms.add(
event,
context.msg.author,
argv.args.attendance === "yes"
).then(() => {
cb("Your attendance status was updated.");
context.summaryHandler.updateSummary(event)
.catch(console.error);
}).catch(reject);
let getInhousePropsPromise = db.events.getInhouse(event);
let getConfirmsPromise = db.confirms.getByEvent(event);

Promise.all([getInhousePropsPromise, getConfirmsPromise]).then(values2 => {
let inhouseProps = values2[0];
let confirms = values2[1];

if (!(inhouseProps !== null && !isSteamLinked)) {
let attendanceConfirms = confirms !== null ? confirms.filter(el => {
return el.attends;
}) : [];

if (attendanceConfirms.length < event.limit) {
db.confirms.add(
event,
context.msg.author,
argv.args.attendance === "yes"
).then(() => {
// TODO change to fulfill in master as well
fulfill("Your attendance status was updated.");
context.summaryHandler.updateSummary(event)
.catch(console.error);
}).catch(reject);
} else {
fulfill("Sorry, the event is full.");
}
} else {
fulfill("Sorry, the event is full.");
fulfill("Error: this event is an inhouse, but you haven't linked" +
" your Steam account.\n" +
"Use the `link-steam` command to link it, then try to confirm" +
" again.");
}
}).catch(reject);

db.confirms.getByEvent(event).then(confirms => {

}).catch(reject);
} else {
fulfill("Event #`" + argv.args.id + "` does not exist");
}
}).catch(reject);
db.events.get(argv.args.id).then(event => {

}).catch(reject);
});
},
args: [
Expand Down
36 changes: 36 additions & 0 deletions lib/commands/general/resend-invite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";

const Clapp = require('../../modules/clapp-discord/index')
, cfg = require('../../../config')
, db = require('../../modules/dbhandler');

module.exports = new Clapp.Command({
name: "resend-invite",
desc: "Resends an invite to the current lobby. You need to have confirmed attendance for" +
" this to work.",
fn: (argv, context) => {
return new Promise((fulfill, reject) => {
context.dotaHandler.currentLobbyEvent.getConfirms().then(people => {
let confirmed = false;

for (let i = 0; i < people.confirmed.length; i++) {
let discordID = people.confirmed[i];

if (context.msg.author.id === discordID) {
confirmed = true;
}
}

if (confirmed) {
context.dotaHandler.invite(context.msg.author.id).then(() => {
fulfill("The invite has been resent. Accept it inside Dota 2.");
}).catch(reject);
} else {
fulfill("Error: you haven't confirmed attendance to the event `#" +
context.dotaHandler.currentLobbyEvent.id + "`.\n" +
"You need to do so before requesting to be invited to the lobby");
}
}).catch(reject);
});
}
});
55 changes: 47 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ const fs = require('fs')
, SummaryHandler = require('./modules/summaryhandler')
, db = require('./modules/dbhandler')
, cfg = require('../config.js')
, moment = require('moment')
, pkg = require('../package.json')
, Discord = require('discord.js')
, Dota2 = require('dota2')
, DotaHandler = require('./modules/dotahandler')
, Steam = require('steam')
, steamVerf = require('steam-verificator')
, bot = new Discord.Client();

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

let generalApp = new Clapp.App({
name: cfg.name,
Expand Down Expand Up @@ -79,7 +83,8 @@ bot.on('message', msg => {
summaryHandler: summaryHandler,
steam: {
verificator: verificator
}
},
dotaHandler: dotaHandler
});
} else {
msg.reply('\n' + "Sorry, you are blacklisted and can't use any commands.")
Expand All @@ -94,7 +99,8 @@ bot.on('message', msg => {
msg: msg,
summaryHandler: summaryHandler,
botAdmins: botAdmins,
blacklist: blacklist
blacklist: blacklist,
dotaHandler: dotaHandler
});
} else {
msg.reply('\n' + "Yo, this is top secret! You need to be a bot admin to access" +
Expand Down Expand Up @@ -130,7 +136,7 @@ steamUser.on('updateMachineAuth', function(sentry, callback) {
});

// Startup tasks
console.log("Loading ScheduleBot...");
console.log("[INFO] Loading ScheduleBot... Please wait");

let startupPromises = [];

Expand Down Expand Up @@ -258,7 +264,14 @@ startupPromises.push(
steamFriends: steamFriends
});

fulfill();
dotaClient.on("ready", () => {
console.log("[DOTA] Running!");
dotaHandler = new DotaHandler(dotaClient);

fulfill();
});

dotaClient.launch();
} else {
let err = new Error("[STEAM] Login failed");
err.steamResponse = response;
Expand All @@ -282,10 +295,35 @@ Promise.all(startupPromises).then(values => {
// Execute the update function now and every update_interval milliseconds
(function update(){
// Update all active events' summaries
db.events.getAllActive().then(
db.events.getAll().then(
events => {
for (let i = 0; i < events.length; i++) {
summaryHandler.updateSummary(events[i]).catch(console.error);

if (events[i].time.diff(moment()) < 0) {
let getInhousePromise = db.events.getInhouse(events[i]);
let getLobbyEndedPromise = events[i].getLobbyEnded();

Promise.all([
getInhousePromise,
getLobbyEndedPromise
]).then(values => {
let inhouseProps = values[0];
let lobbyEnded = values[1];

if (
inhouseProps !== null &&
dotaClient.Lobby === null &&
!lobbyEnded
) {
// If the event has an inhouse, is already happening, the
// dota client is not currently in a lobby, and this event's
// lobby was never created then create it.
inhouseProps.event = events[i];
dotaHandler.createLobby(inhouseProps).catch(console.error);
}
}).catch(console.error);
}
}
}
).catch(console.error);
Expand All @@ -294,6 +332,7 @@ Promise.all(startupPromises).then(values => {
})();

console.log("[DISCORD] Running!");
});
console.log("[INFO] ScheduleBot finished loading.")
}).catch(console.error);

}).catch(console.error);
Loading

0 comments on commit 19d7d64

Please sign in to comment.