Permalink
Browse files

Various bug fixes.

  • Loading branch information...
roncli committed Sep 10, 2018
1 parent a07cee4 commit 2e6fc59369d080e303b01b2c69bf618f5e18d14f
Showing with 94 additions and 25 deletions.
  1. +51 −13 commands.js
  2. +2 −2 database.js
  3. +18 −3 discord.js
  4. +15 −6 event.js
  5. +8 −1 log.js
@@ -1,5 +1,4 @@
const Db = require("./database"),
Event = require("./event"),
Exception = require("./exception"),
pjson = require("./package.json"),
@@ -10,7 +9,7 @@ const Db = require("./database"),
reportParse = /^(-?[0-9]+) (-?[0-9]+)$/,
twoIdParse = /^<@!?([0-9]+)> <@!?([0-9]+)>$/;
let Discord;
let Discord, Event;
// ### #
// # # #
@@ -36,6 +35,10 @@ class Commands {
if (!Discord) {
Discord = require("./discord");
}
if (!Event) {
Event = require("./event");
}
}
// # # ## # #
@@ -269,7 +272,7 @@ class Commands {
return true;
}
if (!Event.isJoinable()) {
if (!Event.isJoinable) {
await Discord.queue(`You have successfully set one of your home maps to \`${message}\`. Your maps for the season are now setup. You can use \`!resethome\` at any point prior to playing a match to reset your home maps.`, user);
return true;
}
@@ -375,7 +378,7 @@ class Commands {
const homes = {};
homeList.forEach((row) => {
const name = Discord.getGuildUser(row.DiscordID);
const name = Discord.getGuildUser(row.DiscordID) || `<@${row.DiscordID}>`;
if (!homes[name]) {
homes[name] = [];
@@ -722,6 +725,7 @@ class Commands {
Event.openEvent();
// await Discord.queue("Hey @everyone, a new tournament has been created. If you'd like to play be sure you have set your home maps for the season by using the `!home` command, setting one map at a time, for example, `!home Logic x2`. Then `!join` the tournament!");
await Discord.queue("Hey everyone, a new tournament has been created. If you'd like to play be sure you have set your home maps for the season by using the `!home` command, setting one map at a time, for example, `!home Logic x2`. Then `!join` the tournament!");
return true;
@@ -874,10 +878,15 @@ class Commands {
throw new Error("User has not joined the event.");
}
const removedUser = Discord.getGuildUser(matches[1]);
if (player.withdrawn) {
await Discord.queue(`Sorry, ${user}, but that player has already withdrawn from this event. You can use \`!addplayer\` to add them instead.`, channel);
throw new Error("User has already withdrew.");
}
Event.removePlayer(matches[1]);
const removedUser = Discord.getGuildUser(matches[1]);
await Discord.queue(`You have successfully removed ${removedUser ? removedUser.displayName : message} from the event.`, channel);
if (removedUser) {
await Discord.queue(`${Discord.getGuildUser(user).displayName} has removed you from the event.`, removedUser);
@@ -922,7 +931,7 @@ class Commands {
let matches;
try {
matches = Event.generateRound();
matches = await Event.generateRound();
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a problem matching players up for the next round.`, channel);
throw err;
@@ -1020,6 +1029,30 @@ class Commands {
throw new Error("Users were not mentioned.");
}
const player1 = Event.getPlayer(matches[1]);
if (!player1) {
await Discord.queue(`Sorry, ${user}, but <@${matches[1]}> has not joined the event.`, channel);
throw new Error("Player 1 hasn't joined the event.");
}
if (player1.withdrawn) {
await Discord.queue(`Sorry, ${user}, but <@${matches[1]}> has withdrawn from the event.`, channel);
throw new Error("Player 1 has withdrawn from the event.");
}
const player2 = Event.getPlayer(matches[2]);
if (!player2) {
await Discord.queue(`Sorry, ${user}, but <@${matches[2]}> has not joined the event.`, channel);
throw new Error("Player 2 hasn't joined the event.");
}
if (player2.withdrawn) {
await Discord.queue(`Sorry, ${user}, but <@${matches[2]}> has withdrawn from the event.`, channel);
throw new Error("Player 2 has withdrawn from the event.");
}
try {
await Event.createMatch(matches[1], matches[2]);
} catch (err) {
@@ -1125,19 +1158,24 @@ class Commands {
throw new Error("Users were not mentioned.");
}
const score1 = +matches[1],
score2 = +matches[2];
const match = Event.getCurrentMatch(matches[1]);
if (!match || match.players.indexOf(matches[1]) === -1 || match.players.indexOf(matches[2]) === -1) {
await Discord.queue(`Sorry, ${user}, but I cannot find a match between those two players.`, channel);
throw new Error("No current match between players.");
}
const score1 = +matches[3],
score2 = +matches[4];
if (Event.isJoinable && (score1 < 20 || score1 === 20 && score1 - score2 < 2 || score1 > 20 && score1 - score2 !== 2)) {
await Discord.queue(`Sorry, ${user}, but that is an invalid score. Games must be played to 20, and you must win by 2 points.`, channel);
throw new Error("Invalid score.");
}
const match = Event.getCurrentMatch(matches[1]);
if (!match || match.players.indexOf(matches[1]) === -1 || match.players.indexOf(matches[2]) === -1) {
await Discord.queue(`Sorry, ${user}, but I cannot find a match between those two players.`, channel);
throw new Error("No current match between players.");
if (!Event.isJoinable && score1 <= score2) {
await Discord.queue(`Sorry, ${user}, but that is an invalid score. The first player must be the winner.`, channel);
throw new Error("Invalid score.");
}
if (!match.homeSelected) {
@@ -76,7 +76,7 @@ class Database {
* @returns {Promise<{DiscordID: string, Home: string}[]>} An array of levels containing all of the home levels for every player by their Discord ID.
*/
static async getHomeList() {
const data = await Db.query("SELECT DiscordID, Home FROM tblHome");
const data = await db.query("SELECT DiscordID, Home FROM tblHome");
return data && data.recordsets && data.recordsets[0];
}
@@ -109,7 +109,7 @@ class Database {
* @returns {Promise<{PlayerID: number, Name: string, DiscordID: string, Rating: number, RatingDeviation: number, Volatility: number}[]>} An array of players containing their database ID, name, Discord ID, and rating details.
*/
static async getPlayers() {
const data = await Db.query("SELECT PlayerID, Name, DiscordID, Rating, RatingDeviation, Volatility from tblPlayer");
const data = await db.query("SELECT PlayerID, Name, DiscordID, Rating, RatingDeviation, Volatility from tblPlayer");
return data && data.recordsets && data.recordsets[0];
}
@@ -5,7 +5,7 @@ const DiscordJs = require("discord.js"),
settings = require("./settings"),
discord = new DiscordJs.Client(settings.discord),
messageParse = /^!([^ ]+)(?: +(.+[^ ]))? *$/,
messageParse = /^!([^ ]+)(?: +(.*[^ ]))? *$/,
noPermissions = {
CREATE_INSTANT_INVITE: false,
ADD_REACTIONS: false,
@@ -134,8 +134,10 @@ class Discord {
obsGuild = discord.guilds.find((g) => g.name === "The Observatory");
generalChannel = obsGuild.channels.find((c) => c.name === "general");
resultsChannel = obsGuild.channels.find((c) => c.name === "match-results");
// generalChannel = obsGuild.channels.find((c) => c.name === "general");
// resultsChannel = obsGuild.channels.find((c) => c.name === "match-results");
generalChannel = obsGuild.channels.find((c) => c.name === "temp");
resultsChannel = obsGuild.channels.find((c) => c.name === "temp");
eventRole = obsGuild.roles.find((r) => r.name === "In Current Event");
seasonRole = obsGuild.roles.find((r) => r.name === "Season 11 Participant");
@@ -241,6 +243,19 @@ class Discord {
channel = generalChannel;
}
const msg = {
embed: {
description: message,
timestamp: new Date(),
color: 0x263686,
footer: {icon_url: Discord.icon, text: "DescentBot"}
}
};
if (JSON.stringify(msg).length > 1024) {
return channel.send(message);
}
return channel.send(
"",
{
@@ -401,7 +401,7 @@ class Event {
standings.forEach((player) => {
player.score = player.wins * 3 + player.defeated.reduce((accumulator, currentValue) => accumulator + standings[currentValue].wins);
});
console.log(standings);
return standings.sort((a, b) => b.score + b.wins / 100 - b.losses / 10000 - (a.score + a.wins / 100 - a.losses / 10000));
}
@@ -456,6 +456,11 @@ class Event {
* @returns {boolean} Whether matching players was successful for this iteration.
*/
static matchPlayers(eventPlayers, potentialMatches) {
// If there's only one player, we can't match anyone.
if (eventPlayers.length <= 1) {
return false;
}
const remainingPlayers = eventPlayers.filter((p) => potentialMatches.filter((m) => m.indexOf(p.id) !== -1).length === 0),
firstPlayer = remainingPlayers[0],
@@ -508,19 +513,19 @@ class Event {
// ###
/**
* Generates the matches for the next round.
* @returns {object[]} The potential matches for the round.
* @returns {Promise<object[]>} The potential matches for the round.
*/
static generateRound() {
static async generateRound() {
try {
const ratedPlayers = Db.getPlayers();
const ratedPlayers = await Db.getPlayers();
const potentialMatches = [];
if (!Event.matchPlayers(
Object.getOwnPropertyNames(players).filter((id) => !players[id].withdrawn).map((id) => ({
players.filter((player) => !player.withdrawn).map((id) => ({
id,
eventPlayer: players[id],
ratedPlayer: ratedPlayers.find((p) => p.DiscordID === id) || {
Name: Discord.getGuildUser(id).displayName,
Name: Discord.getGuildUser(id) ? Discord.getGuildUser(id).displayName : `<@${id}>`,
DiscordID: id,
Rating: 1500,
RatingDeviation: 200,
@@ -671,6 +676,10 @@ class Event {
ratedPlayers.forEach(async (player) => {
await Db.updatePlayerRating(player.Name, player.DiscordID, player.Rating, player.RatingDeviation, player.Volatility, player.PlayerID);
});
running = false;
matches.splice(0, matches.length);
players.splice(0, players.length);
}
}
9 log.js
@@ -117,9 +117,16 @@ class Log {
}
if (log.obj) {
const msg = util.inspect(log.obj);
if (msg.length > 1024) {
Discord.queue(msg, log.type === "exception" ? errorChannel : logChannel);
return;
}
message.embed.fields.push({
name: "Message",
value: util.inspect(log.obj)
value: msg
});
}

0 comments on commit 2e6fc59

Please sign in to comment.