functional queue
This commit is contained in:
110
src/queue.ts
110
src/queue.ts
@@ -1,4 +1,5 @@
|
||||
import { CommandInteraction, GuildMember } from "discord.js";
|
||||
import { shuffle } from "./util";
|
||||
|
||||
type queueInfo = {
|
||||
players: GuildMember[],
|
||||
@@ -23,17 +24,21 @@ export async function createQueue(interaction: CommandInteraction) {
|
||||
let channelId = interaction.channelId;
|
||||
|
||||
if (QUEUE.has(channelId)) {
|
||||
await interaction.reply('There is already an active queue in this channel, type `/join` to join');
|
||||
await interaction.reply('There is already an active queue in this channel, type `/join` to join'); //and you are already in it
|
||||
return;
|
||||
}
|
||||
|
||||
let teamsize = interaction.options.getInteger('teamsize', true);
|
||||
|
||||
QUEUE.set(channelId, {
|
||||
players: [],
|
||||
players: [
|
||||
member
|
||||
],
|
||||
initiator: member,
|
||||
teamsize: interaction.options.getInteger('teamsize', true)
|
||||
teamsize: teamsize
|
||||
});
|
||||
|
||||
await interaction.reply('Queue has been created, type `/join` to join');
|
||||
await interaction.reply(`Queue for teams of ${teamsize} has been created, and you have joined`);
|
||||
|
||||
}
|
||||
|
||||
@@ -57,6 +62,14 @@ export async function joinQueue(interaction: CommandInteraction) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (info.players.map(m=>m.id).includes(member.id)) {
|
||||
await interaction.reply('You are already in the active queue');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(JSON.stringify(member.id))
|
||||
console.log(JSON.stringify(info.players.map(m=>m.id)))
|
||||
|
||||
info.players.push(member);
|
||||
|
||||
QUEUE.set(channelId, info);
|
||||
@@ -65,6 +78,93 @@ export async function joinQueue(interaction: CommandInteraction) {
|
||||
|
||||
}
|
||||
|
||||
export async function leaveQueue(interaction: CommandInteraction) {
|
||||
|
||||
console.log('COMMAND leaveQueue')
|
||||
|
||||
let member = interaction.member;
|
||||
|
||||
if (!(member instanceof GuildMember)) {
|
||||
await interaction.reply('Unable to retrieve guild member information, please try again');
|
||||
return;
|
||||
}
|
||||
|
||||
let channelId = interaction.channelId;
|
||||
|
||||
let info = QUEUE.get(channelId);
|
||||
|
||||
if (!info) {
|
||||
await interaction.reply('There is not an active queue in this channel, type `/queue` to create one');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!info.players.map(m=>m.id).includes(member.id)) {
|
||||
await interaction.reply('You aren\'t in the active queue');
|
||||
return;
|
||||
}
|
||||
|
||||
info.players.splice(info.players.indexOf(member), 1);
|
||||
|
||||
QUEUE.set(channelId, info);
|
||||
|
||||
await interaction.reply('Left the queue');
|
||||
|
||||
}
|
||||
|
||||
export async function readyQueue(interaction: CommandInteraction) {
|
||||
|
||||
console.log('COMMAND readyQueue');
|
||||
|
||||
let member = interaction.member;
|
||||
|
||||
if (!(member instanceof GuildMember)) {
|
||||
await interaction.reply('Unable to retrieve guild member information, please try again');
|
||||
return;
|
||||
}
|
||||
|
||||
let channelId = interaction.channelId;
|
||||
|
||||
let info = QUEUE.get(channelId);
|
||||
|
||||
if (!info) {
|
||||
await interaction.reply('There is no active queue in this channel, type `/queue` to create one'); //and you are already in it
|
||||
return;
|
||||
}
|
||||
|
||||
let {initiator} = info;
|
||||
|
||||
if (member.id !== initiator.id) {
|
||||
await interaction.reply('Only the queue initiator can ready the queue');
|
||||
return;
|
||||
}
|
||||
|
||||
QUEUE.delete(channelId);
|
||||
|
||||
if (info.players.filter(m => m.id !== initiator.id).length === 0) {
|
||||
await interaction.reply('Nobody signed up for the queue, the queue has been reset');
|
||||
return;
|
||||
}
|
||||
|
||||
let playerlist: GuildMember[] = shuffle(info.players),
|
||||
teams: GuildMember[][] = [];
|
||||
|
||||
for (let i = 0; i < playerlist.length; i+= info.teamsize)
|
||||
teams.push(playerlist.slice(i, i+info.teamsize));
|
||||
|
||||
let teamsStr: string[] = [];
|
||||
|
||||
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```');
|
||||
|
||||
}
|
||||
|
||||
export async function queueInfo(interaction: CommandInteraction) {
|
||||
|
||||
console.log('COMMAND queueInfo')
|
||||
@@ -81,6 +181,6 @@ export async function queueInfo(interaction: CommandInteraction) {
|
||||
players: ${info.players.map(p => p.user.tag).join('\n ')}
|
||||
initiator: ${info.initiator.user.tag}
|
||||
teamsize: ${info.teamsize}
|
||||
`+'```')
|
||||
`+'```');
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user