Permalink
Browse files

Start working on openfinals command

  • Loading branch information...
roncli committed Oct 22, 2018
1 parent 2452060 commit 8a866d3ff7ae9ea00857a971a544f30c1cbc653a
Showing with 215 additions and 45 deletions.
  1. +34 −22 commands.js
  2. +82 −4 database.js
  3. +99 −19 event.js
@@ -143,7 +143,7 @@ class Commands {
throw new Error("No event currently running.");
}
if (!Event.isJoinable) {
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can join.`, channel);
throw new Error("Not a joinable event.");
}
@@ -214,7 +214,7 @@ class Commands {
throw new Error("No event currently running.");
}
if (!Event.isJoinable) {
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can withdraw from.`, channel);
throw new Error("Not a withdrawable event.");
}
@@ -288,7 +288,7 @@ class Commands {
return true;
}
if (!Event.isJoinable) {
if (!Event.isRunning || Event.isFinals) {
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);
await Discord.queue(`${user} has set their home levels, please check them against the ban list.`, Discord.alertsChannel);
return true;
@@ -478,10 +478,10 @@ class Commands {
const player = Event.getPlayer(user.id);
if (!player) {
if (Event.isJoinable) {
await Discord.queue(`Sorry, ${user}, but you first need to \`!join\` the tournament before toggling your ability to host games.`, channel);
} else {
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but you are not entered into this tournament.`, channel);
} else {
await Discord.queue(`Sorry, ${user}, but you first need to \`!join\` the tournament before toggling your ability to host games.`, channel);
}
throw new Error("Player hasn't joined tournament.");
}
@@ -562,7 +562,7 @@ class Commands {
throw new Error("No event currently running.");
}
if (!Event.isJoinable) {
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can report games in.`, channel);
throw new Error("Event does not allow reporting.");
}
@@ -633,7 +633,7 @@ class Commands {
throw new Error("No event currently running.");
}
if (!Event.isJoinable) {
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can report games in.`, channel);
throw new Error("Event does not allow reporting.");
}
@@ -739,8 +739,20 @@ class Commands {
const {1: season, 2: eventName, 3: time} = openEventParse.exec(message);
let date;
try {
date = new Date(`${new Date().toDateString()} ${time}`);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but that is an invalid time.`);
throw new Error("Invalid time.");
}
if (date < new Date()) {
date.setDate(date.getDate() + 1);
}
try {
await Event.openEvent(+season, eventName, time);
await Event.openEvent(+season, eventName, date);
} catch (err) {
await Discord.queue(`Sorry, ${user}, but there was a problem matching opening a new event.`, channel);
throw err;
@@ -779,8 +791,8 @@ class Commands {
throw new Error("Event is not currently running.");
}
if (!Event.isJoinable) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can generate rounds for. Did you mean to use the \`!creatematch\` command?`, channel);
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can generate rounds for.`, channel);
throw new Error("Event is not of the right type.");
}
@@ -1013,6 +1025,11 @@ class Commands {
throw new Error("Event is not currently running.");
}
if (Event.isFinals) {
await Discord.queue(`Sorry, ${user}, but this is not an event you can report scores for. Did you mean \`!reportgame\` instead?`, channel);
throw new Error("No current match between players.");
}
const matches = forceReportParse.exec(message);
if (!matches) {
@@ -1027,24 +1044,19 @@ class Commands {
throw new Error("No current match between players.");
}
if (!match.homeSelected) {
await Discord.queue(`Sorry, ${user}, but no home map has been set for this match.`, channel);
throw new Error("Current match has no home map set.");
}
const score1 = +matches[3],
score2 = +matches[4];
if (Event.isJoinable && (score1 < 20 || score1 === 20 && score1 - score2 < 2 || score1 > 20 && score1 - score2 !== 2)) {
if (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.");
}
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) {
await Discord.queue(`Sorry, ${user}, but no home map has been set for this match.`, channel);
throw new Error("Current match has no home map set.");
}
Event.confirmResult(match, matches[1], [score1, score2]);
return true;
@@ -85,18 +85,18 @@ 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 {boolean} finals Whether the event is a Finals Tournament.
* @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) {
static async backup(matches, players, finals, 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) => {
value: JSON.stringify({matches, players, finals, round}, (key, value) => {
if (["channel", "voice", "results"].indexOf(key) !== -1) {
return value.id;
}
@@ -172,13 +172,91 @@ class Database {
// ### #
/**
* Gets the current backup.
* @returns {Promise<{matches: object[], players: object[], joinable: boolean, round: number}>} A promise that resolves with the current backup.
* @returns {Promise<{matches: object[], players: object[], finals: 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;
}
// # ## ## # # #
// # # # # # # #
// ### ## ### # ## ### ### ## ### # ### ### ### ### ## ### ### ###
// # # # ## # # # ## # # ## # # # # # # # # # # # # # # # # # ##
// ## ## # # # ## # ## ## # # # # # # # # ## # # # # # # # ## ##
// # ## ## ## ## # # ### ## # # ## ## # # # # ### ### # # # ###
// ### ###
/**
* Gets a season's standings.
* @param {number} season The season to get standings for.
* @returns {Promise<{discordId: string, score: number}[]>} A promise that resolves with a season's standings.
*/
static async getSeasonStandings(season) {
const data = await db.query(`
DECLARE @results TABLE (
WinnerPlayerID INT NOT NULL,
LoserPlayerID INT NOT NULL,
EventID INT NOT NULL
)
DECLARE @standings TABLE (
PlayerID INT NOT NULL,
Score INT NOT NULL
)
INSERT INTO @results
SELECT
(SELECT TOP 1 PlayerID FROM tblScore WHERE MatchID = m.MatchID ORDER BY Score DESC),
(SELECT TOP 1 PlayerID FROM tblScore WHERE MatchID = m.MatchID ORDER BY Score),
(SELECT EventID FROM tblMatch WHERE MatchID = m.MatchID)
FROM tblMatch m
INNER JOIN tblEvent e ON m.EventID = e.EventID
WHERE e.Season = @season
AND e.Event LIKE '%Qualifier%'
INSERT INTO @standings
SELECT
a.WinnerPlayerID,
SUM(a.Score)
FROM (
SELECT
r.WinnerPlayerID,
3 + (SELECT COUNT(r2.WinnerPlayerID) Won FROM @results r2 WHERE r2.WinnerPlayerID = r.LoserPlayerId AND r2.EventID = r.EventID) Score
FROM @results r
) a
GROUP BY a.WinnerPlayerID
INSERT INTO @standings
SELECT LoserPlayerID, 0
FROM @results
WHERE LoserPlayerID NOT IN (SELECT PlayerID FROM @standings)
SELECT p.DiscordID, s.Score
FROM @standings s
INNER JOIN tblPlayer p ON s.PlayerID = p.PlayerID
ORDER BY s.Score DESC, p.Rating DESC
`, {season: {type: Db.INT, value: season}});
return data && data.recordsets && data.recordsets[0] && data.recordsets[0].map((row) => ({discordId: row.DiscordID, score: row.Score})) || [];
}
// # #### # ## # #### ##
// # # # # # # # # #
// ### ## ### ### # # ## ### ### # ## # # ### ### ### ## ### # ## ### ### ## ###
// # # # ## # # # # # ## # # # # # # # # # # # # # # # # # # ## # # ## # # # #
// ## ## # # # # ## # # # # # # # # # # # # # # # # # # ## # ## ## # # # #
// # ## ## #### # ## # # ## ## ## ### # # ## # ## # ## ## # # ### ## # #
// ###
/**
* Gets the number of events for a season.
* @param {number} season The season to get the event count for.
* @returns {Promise<number>} A promise that resolves with the number of events in a season.
*/
static async getEventCountForSeason(season) {
const data = await db.query("SELECT COUNT(EventID) Events FROM tblEvent WHERE Season = @season", {season: {type: Db.INT, value: season}});
return data && data.recordsets && data.recordsets[0] && data.recordsets[0][0] && data.recordsets[0][0].Events || 0;
}
// # # # ## # #### ### # # ### #
// # # # # # # # # # # # #
// ### ## ### #### ## # # ## # ## # # ### ### ### ## ### # # ## ### ## ## ### ### # ###
Oops, something went wrong.

0 comments on commit 8a866d3

Please sign in to comment.