133 lines
4.6 KiB
JavaScript
133 lines
4.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.queueInfo = exports.readyQueue = exports.leaveQueue = exports.joinQueue = exports.createQueue = exports.queueContains = void 0;
|
|
const discord_js_1 = require("discord.js");
|
|
const util_1 = require("./util");
|
|
//maps ChannelID to QueueInfo
|
|
const QUEUE = new Map();
|
|
/**
|
|
* get the GuildMember of an interaction
|
|
* @param interaction
|
|
* @throws string message if it cannot be read
|
|
* @returns member
|
|
*/
|
|
function getMember(interaction) {
|
|
let member = interaction.member;
|
|
if (!(member instanceof discord_js_1.GuildMember))
|
|
throw 'Unable to retrieve guild member information, please try again';
|
|
return member;
|
|
}
|
|
/**
|
|
* get the queueInfo of an interaction
|
|
* @param interaction
|
|
* @throws string message if it does not exist
|
|
* @returns queue info
|
|
*/
|
|
function getInfo(interaction) {
|
|
let info = QUEUE.get(interaction.channelId);
|
|
if (!info)
|
|
throw 'There is not an active queue in this channel, type `/queue` to create one';
|
|
return info;
|
|
}
|
|
/**
|
|
* checks if the interaction data is already in the queue
|
|
* @param interaction
|
|
* @returns boolean
|
|
*/
|
|
function queueContains(interaction) {
|
|
let member = getMember(interaction), info = getInfo(interaction);
|
|
if (info.players.map(m => m.id).includes(member.id))
|
|
return true;
|
|
return false;
|
|
}
|
|
exports.queueContains = queueContains;
|
|
/**
|
|
* creates a queue from an interaction
|
|
* @param interaction
|
|
* @throws string message if it cannot be created
|
|
*/
|
|
async function createQueue(interaction) {
|
|
let { channelId } = interaction, member = getMember(interaction), teamsize = interaction.options.getInteger('teamsize', true);
|
|
if (QUEUE.has(channelId))
|
|
throw 'There is already an active queue in this channel, ' + (queueContains(interaction) ? 'and you are already in it' : 'type `/join` to join'); //and you are already in it
|
|
QUEUE.set(channelId, {
|
|
players: [
|
|
member
|
|
],
|
|
initiator: member,
|
|
teamsize: teamsize
|
|
});
|
|
await interaction.reply(`Queue for teams of ${teamsize} has been created, and you have joined`);
|
|
}
|
|
exports.createQueue = createQueue;
|
|
/**
|
|
* joins a queue from an interaction
|
|
* @param interaction
|
|
* @throws string message if it cannot be joined
|
|
*/
|
|
async function joinQueue(interaction) {
|
|
let member = getMember(interaction), info = getInfo(interaction);
|
|
if (queueContains(interaction))
|
|
throw 'You are already in the active queue';
|
|
info.players.push(member);
|
|
QUEUE.set(interaction.channelId, info);
|
|
await interaction.reply('Joined the queue');
|
|
}
|
|
exports.joinQueue = joinQueue;
|
|
/**
|
|
* leaves a queue from an interaction
|
|
* @param interaction
|
|
* @throws string message if it cannot be left
|
|
*/
|
|
async function leaveQueue(interaction) {
|
|
let member = getMember(interaction), info = getInfo(interaction);
|
|
if (!queueContains(interaction))
|
|
throw 'You aren\'t in the active queue';
|
|
info.players.splice(info.players.indexOf(member), 1);
|
|
QUEUE.set(interaction.channelId, info);
|
|
await interaction.reply('Left the queue');
|
|
}
|
|
exports.leaveQueue = leaveQueue;
|
|
/**
|
|
* readys a queue from an interaction
|
|
* @param interaction
|
|
* @throws string message if it cannot be readied
|
|
*/
|
|
async function readyQueue(interaction) {
|
|
let member = getMember(interaction), info = getInfo(interaction), { initiator } = info;
|
|
if (member.id !== initiator.id)
|
|
throw 'Only the queue initiator can ready the queue';
|
|
//reset queue
|
|
QUEUE.delete(interaction.channelId);
|
|
if (info.players.filter(m => m.id !== initiator.id).length === 0)
|
|
throw 'Nobody signed up for the queue, the queue has been reset';
|
|
//team data
|
|
let playerlist = (0, util_1.shuffle)(info.players), teams = [];
|
|
//fill team data
|
|
for (let i = 0; i < playerlist.length; i += info.teamsize)
|
|
teams.push(playerlist.slice(i, i + info.teamsize));
|
|
//convert teams to strings
|
|
let teamsStr = [];
|
|
teams.forEach((team, i) => {
|
|
let str = [`Team ${i + 1}`];
|
|
team.forEach(m => str.push(` ${m.user.tag}`));
|
|
teamsStr.push(str.join('\n'));
|
|
});
|
|
await interaction.reply('```\n' + teamsStr.join('\n\n') + '\n```');
|
|
}
|
|
exports.readyQueue = readyQueue;
|
|
/**
|
|
* sends the queue information from an interaction
|
|
* @param interaction
|
|
* @throws string message if it cannot be read
|
|
*/
|
|
async function queueInfo(interaction) {
|
|
let info = getInfo(interaction);
|
|
await interaction.reply('```' + `
|
|
players: ${info.players.map(p => p.user.tag).join('\n ')}
|
|
initiator: ${info.initiator.user.tag}
|
|
teamsize: ${info.teamsize}
|
|
` + '```');
|
|
}
|
|
exports.queueInfo = queueInfo;
|