Files
1800queue/dist/queue.js
2022-01-31 13:18:58 -06:00

140 lines
5.3 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.queueInfo = exports.readyQueue = exports.leaveQueue = exports.joinQueue = exports.createQueue = 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;
}
/**
* creates a queue from an interaction
* @param interaction
* @throws string message if it cannot be created
*/
function createQueue(interaction) {
return __awaiter(this, void 0, void 0, function* () {
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, type `/join` to join'; //and you are already in it
QUEUE.set(channelId, {
players: [
member
],
initiator: member,
teamsize: teamsize
});
yield 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
*/
function joinQueue(interaction) {
return __awaiter(this, void 0, void 0, function* () {
let member = getMember(interaction), info = getInfo(interaction);
if (info.players.map(m => m.id).includes(member.id))
throw 'You are already in the active queue';
info.players.push(member);
QUEUE.set(interaction.channelId, info);
yield interaction.reply('Joined the queue');
});
}
exports.joinQueue = joinQueue;
/**
* leaves a queue from an interaction
* @param interaction
* @throws string message if it cannot be left
*/
function leaveQueue(interaction) {
return __awaiter(this, void 0, void 0, function* () {
let member = getMember(interaction), info = getInfo(interaction);
if (!info.players.map(m => m.id).includes(member.id))
throw 'You aren\'t in the active queue';
info.players.splice(info.players.indexOf(member), 1);
QUEUE.set(interaction.channelId, info);
yield interaction.reply('Left the queue');
});
}
exports.leaveQueue = leaveQueue;
/**
* readys a queue from an interaction
* @param interaction
* @throws string message if it cannot be readied
*/
function readyQueue(interaction) {
return __awaiter(this, void 0, void 0, function* () {
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'));
});
yield 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
*/
function queueInfo(interaction) {
return __awaiter(this, void 0, void 0, function* () {
let info = getInfo(interaction);
yield interaction.reply('```' + `
players: ${info.players.map(p => p.user.tag).join('\n ')}
initiator: ${info.initiator.user.tag}
teamsize: ${info.teamsize}
` + '```');
});
}
exports.queueInfo = queueInfo;