functional queue
This commit is contained in:
12
src/api.ts
12
src/api.ts
@@ -7,9 +7,11 @@ import http from 'https';
|
||||
//while names cant have spaces, the name slot in ogtitle could be shown as "No Player Name" which has spaces
|
||||
const uniteApiRegex = {
|
||||
//$1 = name, $2 = id
|
||||
ogtitle: /Unite API - ([\w\d ]+) \((.*)\)/,
|
||||
ogtitle: /unite api - ([\w\d ]+) \((.*)\)/i,
|
||||
//$1 = level, $2 = rank, $3 = elo, $4 = battles, $5 = wins, $6 = win rate
|
||||
ogdescription: /Pokémon Unite : Lv.(\d+) (\w+) \((\d+)\)\n Battles : (\d+)\n Wins : (\d+)\n Win Rate : (\d+)%/,
|
||||
ogdescription: /pokémon unite : lv\.(\d+) (\w+) \((\d+)\)\n battles : (\d+)\n Wins : (\d+)\n win rate : (\d+)%/i,
|
||||
//for non-masters (no elo)
|
||||
//ogdescription: /pokémon unite : lv\.(\d+) (\w+): class (\d+) *\n battles : (\d+) *\n wins : (\d+) *\n win rate : (\d+)%/i,
|
||||
}
|
||||
|
||||
type uniteApiData = {
|
||||
@@ -151,13 +153,13 @@ export async function getPlayer(name: string): Promise<uniteApiData|null> {
|
||||
|
||||
export async function getPlayerInteraction(interaction: CommandInteraction): Promise<void> {
|
||||
let username = interaction.options.getString('username', true);
|
||||
interaction.deferReply();
|
||||
await interaction.deferReply();
|
||||
|
||||
let data = await getPlayer(username);
|
||||
if (data === null)
|
||||
await interaction.reply('Unable to find user');
|
||||
await interaction.editReply('Unable to find user');
|
||||
else
|
||||
await interaction.reply('```\n'+JSON.stringify(data, null, 2)+'\n```');
|
||||
await interaction.editReply('```\n'+JSON.stringify(data, null, 2)+'\n```');
|
||||
}
|
||||
|
||||
//await getPlayer('IanWhysp')
|
||||
@@ -4,7 +4,7 @@ import { Routes } from 'discord-api-types/v9';
|
||||
const commands = [
|
||||
{
|
||||
name: 'queue',
|
||||
description: 'creates a queue',
|
||||
description: 'create a queue',
|
||||
options: [
|
||||
{
|
||||
type: 4, //INTEGER
|
||||
@@ -16,24 +16,28 @@ const commands = [
|
||||
},
|
||||
{
|
||||
name: 'join',
|
||||
description: 'joins the active queue'
|
||||
description: 'join the active queue'
|
||||
},
|
||||
{
|
||||
name: 'leave',
|
||||
description: 'leave the active queue'
|
||||
},
|
||||
{
|
||||
name: 'ready',
|
||||
description: 'readys the queue and displays team info'
|
||||
description: 'ready the queue and display team info'
|
||||
},
|
||||
{
|
||||
name: 'queueinfo',
|
||||
description: 'gets info of the current queue'
|
||||
description: 'get info of the current queue'
|
||||
},
|
||||
{
|
||||
name: 'elo',
|
||||
description: 'displays your elo information',
|
||||
description: 'display elo information',
|
||||
options: [
|
||||
{
|
||||
type: 3, //STRING
|
||||
name: 'username',
|
||||
description: 'your in game name',
|
||||
description: 'in game name',
|
||||
required: true
|
||||
}
|
||||
]
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Client, Intents } from 'discord.js';
|
||||
import * as fs from 'fs';
|
||||
import { getPlayerInteraction } from './api';
|
||||
import { registerCommands } from './discord';
|
||||
import { createQueue, joinQueue, queueInfo } from './queue';
|
||||
import { createQueue, joinQueue, leaveQueue, queueInfo, readyQueue } from './queue';
|
||||
const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS] });
|
||||
|
||||
console.log(new Date().toISOString()+'\n\n');
|
||||
@@ -28,8 +28,10 @@ CLIENT.on('interactionCreate', async interaction => {
|
||||
await createQueue(interaction);
|
||||
else if (interaction.commandName === 'join')
|
||||
await joinQueue(interaction);
|
||||
//else if (interaction.commandName === 'ready')
|
||||
// await readyQueue(interaction);
|
||||
else if (interaction.commandName === 'leave')
|
||||
await leaveQueue(interaction);
|
||||
else if (interaction.commandName === 'ready')
|
||||
await readyQueue(interaction);
|
||||
else if (interaction.commandName === 'queueinfo')
|
||||
await queueInfo(interaction);
|
||||
else if (interaction.commandName === 'elo')
|
||||
|
||||
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}
|
||||
`+'```')
|
||||
`+'```');
|
||||
|
||||
}
|
||||
24
src/util.ts
Normal file
24
src/util.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
/**
|
||||
* shuffles an array
|
||||
* https://stackoverflow.com/a/2450976/2856416
|
||||
* @param array an array
|
||||
* @returns an array but shuffled
|
||||
*/
|
||||
export function shuffle(array: any[]) {
|
||||
let currentIndex = array.length, randomIndex;
|
||||
|
||||
// While there remain elements to shuffle...
|
||||
while (currentIndex != 0) {
|
||||
|
||||
// Pick a remaining element...
|
||||
randomIndex = Math.floor(Math.random() * currentIndex);
|
||||
currentIndex--;
|
||||
|
||||
// And swap it with the current element.
|
||||
[array[currentIndex], array[randomIndex]] = [
|
||||
array[randomIndex], array[currentIndex]];
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
Reference in New Issue
Block a user