Skip to content

Commit

Permalink
check queue position directly on CartQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
idinium96 committed Aug 6, 2020
1 parent 00d97c8 commit 279095f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 89 deletions.
54 changes: 54 additions & 0 deletions src/classes/CartQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import SteamID from 'steamid';

import Bot from './Bot';
import Cart from './Cart';
import DiscordWebhook from './DiscordWebhook';
import MyHandler from './MyHandler';

import log from '../lib/logger';

Expand All @@ -10,12 +12,17 @@ export = CartQueue;
class CartQueue {
private readonly bot: Bot;

readonly discord: DiscordWebhook;

private carts: Cart[] = [];

private busy = false;

private queuePositionCheck;

constructor(bot: Bot) {
this.bot = bot;
this.discord = new DiscordWebhook(bot);
}

enqueue(cart: Cart): number {
Expand All @@ -40,9 +47,56 @@ class CartQueue {
this.handleQueue();
});

clearTimeout(this.queuePositionCheck);
this.queueCheck();

return position;
}

private queueCheck(): void {
log.debug(`Checking queue position in 5 minutes...`);
this.queuePositionCheck = setTimeout(() => {
const position = this.carts.length;
log.debug(`Current queue position: ${position + 1}`);
if (position >= 2) {
if (
process.env.DISABLE_DISCORD_WEBHOOK_SOMETHING_WRONG_ALERT === 'false' &&
process.env.DISCORD_WEBHOOK_SOMETHING_WRONG_ALERT_URL
) {
const time = (this.bot.handler as MyHandler).timeWithEmoji();
this.discord.sendQueueAlert(position + 1, time.time);
this.bot.botManager
.restartProcess()
.then(restarting => {
if (!restarting) {
this.discord.sendQueueAlertFailedPM2(time.time);
}
})
.catch(err => {
log.warn('Error occurred while trying to restart: ', err);
this.discord.sendQueueAlertFailedError(err.message, time.time);
});
} else {
this.bot.messageAdmins(`⚠️ [Queue alert] Current position: ${position + 1}`, []);
this.bot.botManager
.restartProcess()
.then(restarting => {
if (!restarting) {
this.bot.messageAdmins(
'❌ Automatic restart on queue problem failed because are not running the bot with PM2! See the documentation: https://github.com/idinium96/tf2autobot/wiki/e.-Running-with-PM2',
[]
);
}
})
.catch(err => {
log.warn('Error occurred while trying to restart: ', err);
this.bot.messageAdmins(`❌ An error occurred while trying to restart: ${err.message}`, []);
});
}
}
}, 5 * 60 * 1000);
}

dequeue(steamID: SteamID | string): boolean {
log.debug('Dequeueing cart');
const position = this.getPosition(steamID);
Expand Down
89 changes: 0 additions & 89 deletions src/classes/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,51 +842,6 @@ export = class Commands {
(currentPosition !== 1 ? 'are' : 'is') +
` ${currentPosition} infront of you.`
);
clearTimeout(this.queuePositionCheck);
log.debug(`Checking queue position in 3 minutes...`);
this.queuePositionCheck = setTimeout(() => {
// Check position after 3 minutes
log.debug(`Current queue position: ${position}`);
if (this.cartQueue.getPosition(cart.partner) >= 1) {
if (
process.env.DISABLE_DISCORD_WEBHOOK_SOMETHING_WRONG_ALERT === 'false' &&
process.env.DISCORD_WEBHOOK_SOMETHING_WRONG_ALERT_URL
) {
const time = (this.bot.handler as MyHandler).timeWithEmoji();
this.discord.sendQueueAlert(position + 1, time.time);
this.bot.botManager
.restartProcess()
.then(restarting => {
if (!restarting) {
this.discord.sendQueueAlertFailedPM2(time.time);
}
})
.catch(err => {
log.warn('Error occurred while trying to restart: ', err);
this.discord.sendQueueAlertFailedError(err.message, time.time);
});
} else {
this.bot.messageAdmins(`⚠️ [Queue alert] Current position: ${position + 1}`, []);
this.bot.botManager
.restartProcess()
.then(restarting => {
if (!restarting) {
this.bot.messageAdmins(
'❌ Automatic restart on queue problem failed because are not running the bot with PM2! See the documentation: https://github.com/idinium96/tf2autobot/wiki/e.-Running-with-PM2',
[]
);
}
})
.catch(err => {
log.warn('Error occurred while trying to restart: ', err);
this.bot.messageAdmins(
`❌ An error occurred while trying to restart: ${err.message}`,
[]
);
});
}
}
}, 3 * 60 * 1000);
}
return;
}
Expand All @@ -900,50 +855,6 @@ export = class Commands {
(position !== 1 ? 'are' : 'is') +
` ${position} infront of you.`
);
clearTimeout(this.queuePositionCheck);
log.debug(`Checking queue position in 3 minutes...`);
this.queuePositionCheck = setTimeout(() => {
// Check position after 3 minutes
if (this.cartQueue.enqueue(cart) >= 2) {
if (
process.env.DISABLE_DISCORD_WEBHOOK_SOMETHING_WRONG_ALERT === 'false' &&
process.env.DISCORD_WEBHOOK_SOMETHING_WRONG_ALERT_URL
) {
const time = (this.bot.handler as MyHandler).timeWithEmoji();
this.discord.sendQueueAlert(position + 1, time.time);
this.bot.botManager
.restartProcess()
.then(restarting => {
if (!restarting) {
this.discord.sendQueueAlertFailedPM2(time.time);
}
})
.catch(err => {
log.warn('Error occurred while trying to restart: ', err);
this.discord.sendQueueAlertFailedError(err.message, time.time);
});
} else {
this.bot.messageAdmins(`⚠️ [Queue alert] Current position: ${position + 1}`, []);
this.bot.botManager
.restartProcess()
.then(restarting => {
if (!restarting) {
this.bot.messageAdmins(
'❌ Automatic restart on queue problem failed because are not running the bot with PM2! See the documentation: https://github.com/idinium96/tf2autobot/wiki/e.-Running-with-PM2',
[]
);
}
})
.catch(err => {
log.warn('Error occurred while trying to restart: ', err);
this.bot.messageAdmins(
`❌ An error occurred while trying to restart: ${err.message}`,
[]
);
});
}
}
}, 3 * 60 * 1000);
}
}

Expand Down

0 comments on commit 279095f

Please sign in to comment.