Skip to content

Commit

Permalink
More bug fixes and enhancements found in testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
roncli committed Jan 22, 2024
1 parent 3139307 commit 74c1d85
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 47 deletions.
2 changes: 0 additions & 2 deletions docker-compose.yml
Expand Up @@ -24,8 +24,6 @@ services:
build: ./node
depends_on:
- logging
- db
- redis
networks:
- sprint-racebot-network
logging:
Expand Down
4 changes: 2 additions & 2 deletions node/src/discord/commands/help.js
Expand Up @@ -39,7 +39,7 @@ class HelpCmd {
fields: [
{
name: "General Commands",
value: "`.help`/`.h` - This help text.\n`.race` - Start a new race.",
value: "`.help`/`.h` - This help text.\n`.race <settings>` - Start a new race. The settings for the race must be entered here. For instance, `.race F5 Null Hard Mild`",
inline: true
},
{
Expand All @@ -52,7 +52,7 @@ class HelpCmd {
},
{
name: "Race Room Commands - After the Race",
value: "`.notdone`/`.n` - If you did `.done` or `.forfeit`, this undoes that, continuing the race and re-entering you into it.\n`.rematch` - Starts a new race in the same race channel."
value: "`.notdone`/`.n` - If you did `.done` or `.forfeit`, this undoes that, continuing the race and re-entering you into it.\n`.rematch` - Start a new race in this channel with the same settings but on a different seed."
},
{
name: "Staff Commands",
Expand Down
22 changes: 13 additions & 9 deletions node/src/discord/commands/race.js
Expand Up @@ -27,23 +27,27 @@ class RaceCmd {
* Handles the command.
* @param {DiscordJs.GuildMember} member The member using the command.
* @param {DiscordJs.TextChannel} channel The channel the command is being used in.
* @param {string} parameters The parameters.
* @returns {Promise} A promise that returns when the command is done executing.
*/
static async handle(member, channel) {
if (channel.parent && channel.parent.id === Discord.raceCategory.id) {
return;
static async handle(member, channel, parameters) {
if (!parameters || parameters.trim() === "") {
parameters = "F5 Null Normal Mild";
}

// If there are already 20 race rooms, don't create another.
// If there are already 40 race rooms, don't create another.
if (Discord.raceCategory.children.cache.size >= 40) {
Discord.embedBuilder({
title: "Too Many Races",
description: "There are too many races going on at once. Please join one of them instead!"
});
await Discord.richQueue(
Discord.embedBuilder({
title: "Too Many Races",
description: "There are too many races going on at once. Please join one of them instead!"
}),
channel
);
return;
}

const race = await Race.createRace();
const race = await Race.createRace(parameters || "");

await Discord.richQueue(
Discord.embedBuilder({
Expand Down
4 changes: 3 additions & 1 deletion node/src/models/countdown.js
Expand Up @@ -31,15 +31,17 @@ class Countdown {

this.race.start = Date.now() + 10000;

setTimeout(async () => {
(async () => {
await Discord.richQueue(
Discord.embedBuilder({
title: "Race Starts in 10 Seconds",
description: "Everyone is ready! The race will start in 10 seconds!"
}),
this.race.channel
);
})();

setTimeout(async () => {
if (this.cancelled) {
return;
}
Expand Down
62 changes: 29 additions & 33 deletions node/src/models/race.js
Expand Up @@ -26,15 +26,18 @@ class Race {
// ## # ## # # ## ## # # # # ## ##
/**
* Creates a race.
* @param {string} parameters The race parameters.
* @returns {Promise<Race>} A promise that returns the created race.
*/
static async createRace() {
static async createRace(parameters) {
const channel = await Discord.createNumberedChannel("race"),
race = new Race(channel);

race.parameters = parameters;

await race.setup();

race.channel.setTopic("Type .help or .h for help. See the pinned post for current race status.", "Creating a race room.");
race.channel.setTopic(`${parameters ? `Race settings: ${parameters} | ` : ""}Type .help or .h for help | See the pinned post for current race status.`, "Creating a race room.");

Race.races.push(race);
return race;
Expand Down Expand Up @@ -138,6 +141,12 @@ class Race {
* @type {NodeJS.Timeout}
*/
this.autoCloseTimeout = null;

/**
* The race parameters.
* @type {string}
*/
this.parameters = "";
}

// ##
Expand All @@ -152,9 +161,14 @@ class Race {
* @returns {Promise} A promise that resolves when the race room is closed.
*/
async close(member) {
if (this.autoCloseTimeout) {
clearTimeout(this.autoCloseTimeout);
this.autoCloseTimeout = null;
}

// Record the race results, if there were any.
if (this.end > 0) {
await postResult();
await this.postResult();
}

// Remove this race from the races array.
Expand Down Expand Up @@ -190,17 +204,6 @@ class Race {
return void 0;
}

if (this.end > 0) {
await Discord.richQueue(
Discord.embedBuilder({
title: "Race Already Finished",
description: `Sorry, ${member}, but this race is already finished.`
}),
this.channel
);
return void 0;
}

const player = this.players.find((p) => p.discordId === member.id);
if (!player) {
await Discord.richQueue(
Expand All @@ -224,25 +227,18 @@ class Race {
return void 0;
}

if (player.forfeit) {
await Discord.richQueue(
Discord.embedBuilder({
title: "Already Forfeited",
description: `Sorry, ${member}, but you have already forfeited.`
}),
this.channel
);
return void 0;
}

player.finish = Date.now();

// Get the number of finished racers.
const finishedPlayers = this.players.filter((p) => p.finish > 0).sort((a, b) => a.finish - b.finish),
finished = finishedPlayers.length;

const ordinal = finished % 100 >= 11 && finished % 100 <= 13 ? "th" : finished % 10 === 1 ? "st" : finished % 10 === 2 ? "nd" : finished % 10 === 3 ? "rd" : "th";
await Discord.queue(`${member} has finished ${finished}${ordinal} with **${Race.formatTime(player.finish - this.start)}**.`, this.channel);
await Discord.queue(`${member} has ${player.forfeit ? "unforfeited, and " : ""}finished ${finished}${ordinal} with **${Race.formatTime(player.finish - this.start)}**.`, this.channel);

if (player.forfeit) {
player.forfeit = false;
}

// Get the number of racers still racing.
const racing = this.players.filter((p) => p.finish === 0 && !p.forfeit).length;
Expand Down Expand Up @@ -627,7 +623,7 @@ class Race {
async postResult() {
const message = Discord.embedBuilder({
title: "Completed Race",
description: `Race completed <t:${Math.floor(this.end / 1000)}:R>\nSeed: ${this.seed}`,
description: `Race completed <t:${Math.floor(this.end / 1000)}:R>\nSeed: ${this.seed}\nSettings: ${this.parameters}`,
fields: []
});

Expand Down Expand Up @@ -772,7 +768,7 @@ class Race {
this.pinnedPost = await Discord.richQueue(
Discord.embedBuilder({
title: "New Race",
description: `Seed: ${this.seed}`,
description: `Seed: ${this.seed}\nSettings: ${this.parameters}`,
fields: [
{
name: "Commands",
Expand Down Expand Up @@ -926,7 +922,7 @@ class Race {
// Build message.
const message = Discord.embedBuilder({
title: "New Race",
description: `Seed: ${this.seed}`,
description: `Seed: ${this.seed}\nSettings: ${this.parameters}`,
fields: []
});

Expand Down Expand Up @@ -961,7 +957,7 @@ class Race {
// Build message.
const message = Discord.embedBuilder({
title: "Race Starting",
description: `Seed: ${this.seed}\nStarting <t:${Math.floor(this.start / 1000)}:R>`,
description: `Seed: ${this.seed}\nSettings: ${this.parameters}\nStarting <t:${Math.floor(this.start / 1000)}:R>`,
fields: [
{
name: "Players",
Expand Down Expand Up @@ -992,7 +988,7 @@ class Race {

const message = Discord.embedBuilder({
title: "Race Started",
description: `Seed: ${this.seed}\nStarted <t:${Math.floor(this.start / 1000)}:R>`,
description: `Seed: ${this.seed}\nSettings: ${this.parameters}\nStarted <t:${Math.floor(this.start / 1000)}:R>`,
fields: []
});

Expand Down Expand Up @@ -1041,7 +1037,7 @@ class Race {

const message = Discord.embedBuilder({
title: "Race Complete",
description: `Seed: ${this.seed}\nStarted <t:${Math.floor(this.start / 1000)}:R>\nEnded <t:${Math.floor(this.end / 1000)}:R>`,
description: `Seed: ${this.seed}\nSettings: ${this.parameters}\nStarted <t:${Math.floor(this.start / 1000)}:R>\nEnded <t:${Math.floor(this.end / 1000)}:R>`,
fields: []
});

Expand All @@ -1063,7 +1059,7 @@ class Race {

message.addFields({
name: "Commands",
value: "`.rematch` - Start a new race in this channel.\n`.undone` or `.u` - Reenter the race if you accidentally completed or forfeited"
value: "`.rematch` - Start a new race in this channel with the same settings but on a different seed.\n`.undone` or `.u` - Reenter the race if you accidentally completed or forfeited"
});

await Discord.richEdit(this.pinnedPost, message);
Expand Down

0 comments on commit 74c1d85

Please sign in to comment.