Permalink
Browse files

Bug fixes, this is now stable to use.

  • Loading branch information...
roncli committed Oct 4, 2018
1 parent 021b245 commit a88b239b273de4ee2c996f3c8b834254ba1b047d
Showing with 374 additions and 88 deletions.
  1. +59 −24 commands.js
  2. +86 −21 database.js
  3. +22 −6 discord.js
  4. +207 −37 event.js
@@ -39,6 +39,8 @@ class Commands {
if (!Event) {
Event = require("./event");
}
Event.onLoad();
}
// # # ## # #
@@ -167,7 +169,7 @@ class Commands {
}
if (player) {
delete player.withdrawn;
Event.rejoinPlayer(player, homes);
} else {
Event.addPlayer(user.id, homes);
}
@@ -220,7 +222,12 @@ class Commands {
throw new Error("Player has already withdrew.");
}
Event.removePlayer(user.id);
try {
await Event.removePlayer(user.id);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a Discord error removing a pilot from the tournament.", err);
}
await Discord.queue("You have been successfully withdrawn from the event. If you wish to return before the end of the event, you may use the `!join` command once again.", user);
await Discord.queue(`${Discord.getGuildUser(user).displayName} has withdrawn from the tournament.`);
@@ -636,15 +643,7 @@ class Commands {
throw new Error("Player tried to confirm their own report.");
}
match.winner = match.reported.winner;
match.score = match.reported.score;
delete match.reported;
await Discord.queue(`This match has been reported as a win for ${Discord.getGuildUser(user).displayName} by the score of ${match.score[0]} to ${match.score[1]}. If this is in error, please contact an admin. You may add a comment to this match using \`!comment <your comment>\` any time before your next match. This channel and the voice channel will close in 2 minutes.`, match.channel);
setTimeout(() => {
Event.postResult(match);
}, 120000);
Event.confirmResult(match, match.reported.winner, match.reported.score);
return true;
}
@@ -724,8 +723,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!");
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;
}
@@ -882,7 +880,12 @@ class Commands {
throw new Error("User has already withdrew.");
}
Event.removePlayer(matches[1]);
try {
await Event.removePlayer(matches[1]);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a server error. roncli will be notified about this.`, channel);
throw new Exception("There was a Discord error removing a pilot from the tournament.", err);
}
const removedUser = Discord.getGuildUser(matches[1]);
@@ -1064,7 +1067,7 @@ class Commands {
throw err;
}
await Discord.queue(`Additional match:\n**${player1.displayName}** vs **${player2.displayName}**`);
await Discord.queue(`Additional match:\n**${Discord.getGuildUser(player1.id).displayName}** vs **${Discord.getGuildUser(player2.id).displayName}**`);
return true;
}
@@ -1189,15 +1192,7 @@ class Commands {
throw new Error("Current match has no home map set.");
}
match.winner = matches[1];
match.score = [score1, score2];
delete match.reported;
await Discord.queue(`This match has been reported as a win for ${Discord.getGuildUser(match.winner).displayName} by the score of ${match.score[0]} to ${match.score[1]}. If this is in error, please contact an admin. You may add a comment to this match using \`!comment <your comment>\` any time before your next match. This channel and the voice channel will close in 2 minutes.`, match.channel);
setTimeout(() => {
Event.postResult(match);
}, 120000);
Event.confirmResult(match, matches[1], [score1, score2]);
return true;
}
@@ -1255,6 +1250,46 @@ class Commands {
return true;
}
// # #
// # #
// ### ### ## # # # # ###
// # # # # # ## # # # #
// # # # ## # # # # # # #
// ### # # ## # # ### ###
// #
/**
* Backs up the event.
* @param {User} user The user initiating the command.
* @param {string} message The text of the command.
* @param {object} channel The channel the command was sent on.
* @returns {Promise<bool>} A promise that resolves with whether the command completed successfully.
*/
async backup(user, message, channel) {
const commands = this;
Commands.adminCheck(commands, user);
if (message) {
return false;
}
if (!Event.isRunning) {
await Discord.queue(`Sorry, ${user}, but there is no event currently running.`, channel);
throw new Error("Event is not currently running.");
}
try {
await Event.backup();
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there is was an error backing up the event.`, channel);
throw new Exception("There was an error while ending the event.", err);
}
await Discord.queue("The event has been backed up.", channel);
return true;
}
}
module.exports = Commands;
@@ -20,10 +20,10 @@ class Database {
// # ## # # # # # # # # # # ##
// # # ### ### # # ## # # ##
/**
* Adds a home level for a player.
* Adds a home map for a player.
* @param {string} discordId The player's Discord ID.
* @param {string} home The home level to add.
* @returns {Promise} A promise that resolves when the home level has been added.
* @param {string} home The home map to add.
* @returns {Promise} A promise that resolves when the home map has been added.
*/
static async addHome(discordId, home) {
await db.query("INSERT INTO tblHome (DiscordID, Home) VALUES (@discordId, @home)", {
@@ -32,21 +32,85 @@ class Database {
});
}
// # #
// # #
// ### ### ## # # # # ###
// # # # # # ## # # # #
// # # # ## # # # # # # #
// ### # # ## # # ### ###
// #
/**
* Backs up the event data to the database.
* @param {object[]} matches The matches.
* @param {object[]} players The players.
* @param {boolean} joinable Whether the event is joinable.
* @param {number} round The current round number.
* @returns {Promise} A promise that resolves when the backup is complete.
*/
static async backup(matches, players, joinable, round) {
await db.query(`
DELETE FROM tblBackup
INSERT INTO tblBackup (Code) VALUES (@code)
`, {
code: {
type: Db.TEXT,
value: JSON.stringify({matches, players, joinable, round}, (key, value) => {
if (["channel", "voice", "results"].indexOf(key) !== -1) {
return value.id;
}
return value;
})
}
});
}
// ## ### #
// # # # #
// ## # ## ### ### ### ### ## # # # # ###
// # # # ## # # # # # # # # # ## # # # #
// # # ## # ## # # # # ## # # # # # # #
// ## ### ## # # # ### # # ## # # ### ###
// #
/**
* Clears the current backup.
* @returns {Promise} A promise that resolves when the backup is complete.
*/
static async clearBackup() {
await db.query("DELETE FROM tblBackup");
}
// # ## # # # #### ### # # ### #
// # # # # # # # # # # #
// ### ## # ## ### ## #### ## # # ## ### ### ## ### # # ## ### ## ## ### ### # ###
// # # # ## # # ## # # ## # # # # #### # ## ## # # # # # # # # ## # # # # # # # # # #
// # # ## # ## # ## # # # # # # ## ## # # # # # # # ## # # # # # # # # #
// ### ## ### ## ## ## # # ## # # ## ### # ## # ### ### ### ## ## # ### ### ###
/**
* Deletes a player's home levels from their Discord ID.
* Deletes a player's home maps from their Discord ID.
* @param {string} discordId The player's Discord ID.
* @returns {Promise} A promise that resolves when the home levels have been deleted.
* @returns {Promise} A promise that resolves when the home maps have been deleted.
*/
static async deleteHomesForDiscordId(discordId) {
await db.query("DELETE FROM tblHome WHERE DiscordID = @discordId", {discordId: {type: Db.VARCHAR(50), value: discordId}});
}
// # ### #
// # # # #
// ### ## ### ### ### ## # # # # ###
// # # # ## # # # # # # ## # # # #
// ## ## # # # # ## # # # # # # #
// # ## ## ### # # ## # # ### ###
// ### #
/**
* Gets the current backup.
* @returns {Promise<{matches: object[], players: object[], joinable: boolean, round: number}>} A promise that resolves with the current backup.
*/
static async getBackup() {
const data = await db.query("SELECT Code FROM tblBackup");
return data && data.recordsets && data.recordsets[0] && data.recordsets[0][0] && data.recordsets[0][0].Code && JSON.parse(data.recordsets[0][0].Code) || void 0;
}
// # # # ## # #### ### # # ### #
// # # # # # # # # # # # #
// ### ## ### #### ## # # ## # ## # # ### ### ### ## ### # # ## ### ## ## ### ### # ###
@@ -55,9 +119,9 @@ class Database {
// # ## ## # # ## # # ## ## ## ### # # ## # ## # ### ### ### ## ## # ### ### ###
// ###
/**
* Gets the number of home levels for a player from their Discord ID.
* Gets the number of home maps for a player from their Discord ID.
* @param {string} discordId The player's DiscordID.
* @returns {Promise<number>} The number of home levels the player has set.
* @returns {Promise<number>} The number of home maps the player has set.
*/
static async getHomeCountForDiscordId(discordId) {
const data = await db.query("SELECT COUNT(Home) Homes FROM tblHome WHERE DiscordID = @discordId", {discordId: {type: Db.VARCHAR(50), value: discordId}});
@@ -72,8 +136,8 @@ class Database {
// # ## ## # # ## # # ## #### ### ### ##
// ###
/**
* Gets the home level list for all players.
* @returns {Promise<{DiscordID: string, Home: string}[]>} An array of levels containing all of the home levels for every player by their Discord ID.
* Gets the home map list for all players.
* @returns {Promise<{DiscordID: string, Home: string}[]>} An array of maps containing all of the home maps for every player by their Discord ID.
*/
static async getHomeList() {
const data = await db.query("SELECT DiscordID, Home FROM tblHome");
@@ -90,7 +154,7 @@ class Database {
/**
* Gets the homes for a player from their Discord ID.
* @param {string} discordId The player's Discord ID.
* @returns {Promise<string[]>} The player's home levels.
* @returns {Promise<string[]>} The player's home maps.
*/
static async getHomesForDiscordId(discordId) {
const data = await db.query("SELECT Home FROM tblHome WHERE DiscordID = @discordId", {discordId: {type: Db.VARCHAR(50), value: discordId}});
@@ -121,9 +185,9 @@ class Database {
// # ## ## # # ## ### ## ## ## ## # # ## ### ### # ## # ### ### ### ## ## # ### ### ###
// ###
/**
* Retrieves a player's home level reset status from their Discord ID.
* Retrieves a player's home map reset status from their Discord ID.
* @param {string} discordId The player's Discord ID.
* @returns {Promise<{hasHomes: boolean, locked: boolean}>} An object that contains the player's home level reset status. hasHomes returns whether the player has home levels defined, and locked returns whether the player's home levels are locked.
* @returns {Promise<{hasHomes: boolean, locked: boolean}>} An object that contains the player's home map reset status. hasHomes returns whether the player has home maps defined, and locked returns whether the player's home maps are locked.
*/
static async getResetStatusForDiscordId(discordId) {
const data = await db.query("SELECT TOP 1 Locked FROM tblHome WHERE DiscordID = @discordId ORDER BY Locked DESC", {discordId: {type: Db.VARCHAR(50), value: discordId}});
@@ -133,18 +197,19 @@ class Database {
};
}
// ## # # # # ## #### ### # # ### #
// # # # # # # # # # # # #
// # ## ## # # #### ## # # ## # ## # # ## # ### ### ## ### # # ## ### ## ## ### ### # ### ###
// # # # # ## # # # # #### # ## # # ## # # # ## # ## # # # # # # # # ## # # # # # # # # # # ##
// # # # # # # # # # # # # ## # ## # # ## # ## # # # # # # # ## # # # # # # # # # ##
// ### ## ## # # # # ## # # ## #### ## # ## ### ### # ## # ### ### ### ## ## # ### ### ### ###
// ## # # # # # #### ### # # ### #
// # # # # #### # # # # # #
// # ## ## # # #### ## # # ## #### ### ### ### ### ## ### # # ## ### ## ## ### ### # ### ###
// # # # # ## # # # # #### # ## # # # # # # ## # # # # # # # # ## # # # # # # # # # # ##
// # # # # # # # # # # # # ## # # # ## # # ## # # # # # # # ## # # # # # # # # # ##
// ### ## ## # # # # ## # # ## # # # # ### ### # ## # ### ### ### ## ## # ### ### ### ###
// #
/**
* Locks players' home levels from their Discord IDs.
* Locks players' home maps from their Discord IDs.
* @param {string[]} discordIds An array of the players' Discord IDs.
* @return {Promise} A promise that resolves when the players' home levels have been locked.
* @return {Promise} A promise that resolves when the players' home maps have been locked.
*/
static async lockHomeLevelsForDiscordIds(discordIds) {
static async lockHomeMapsForDiscordIds(discordIds) {
const players = discordIds.map((discordId, index) => ({index: `player${index}`, atIndex: `@player${index}`, discordId}));
await db.query(`UPDATE tblHome SET Locked = 1 WHERE DiscordID IN (${players.map((p) => p.atIndex).join(", ")})`, players.reduce((accumulator, player) => {
@@ -173,23 +173,23 @@ class Discord {
* @returns {void}
*/
static startup() {
Discord.commands = new Commands();
discord.addListener("ready", () => {
Log.log("Connected to 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 === "temp");
resultsChannel = obsGuild.channels.find((c) => c.name === "temp");
generalChannel = obsGuild.channels.find((c) => c.name === "general");
resultsChannel = obsGuild.channels.find((c) => c.name === "match-results");
eventRole = obsGuild.roles.find((r) => r.name === "In Current Event");
seasonRole = obsGuild.roles.find((r) => r.name === "Season 11 Participant");
pilotsChatCategory = obsGuild.channels.find((c) => c.name === "Pilots Chat");
pilotsVoiceChatCategory = obsGuild.channels.find((c) => c.name === "Pilots Voice Chat");
if (!Discord.commands) {
Discord.commands = new Commands();
}
});
discord.on("disconnect", (ev) => {
@@ -426,6 +426,22 @@ class Discord {
return obsGuild.channels.find((c) => c.name === name);
}
// # # # ## # ## ### ### #
// # # # # # # # # # # #
// # ## ### ### # ### ### ### ### ## # ### # # # ###
// ### # # # # # # # # # # # # # # # ## # # # # # # # #
// # # # # # # # # # # # ## # # # # ## # # # # # # # #
// # ### # # ### ## # # # # # # # # ## ### ### # ### ###
// #
/**
* Finds a Discord channel by its ID.
* @param {string} id The ID of the channel.
* @returns {Channel} The Discord channel.
*/
static findChannelById(id) {
return obsGuild.channels.find((c) => c.id === id);
}
// # # # ### ## ### # #
// # # # # # # # # ## #
// # ## ### ### # # ## # ## ### # # ## # ### # # ##
Oops, something went wrong.

0 comments on commit a88b239

Please sign in to comment.