Skip to content

Commit

Permalink
Merge branch 'main' into DVG
Browse files Browse the repository at this point in the history
  • Loading branch information
N0ise9 committed Aug 29, 2024
2 parents 3585796 + c353a1b commit 9276875
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/controllers/EasterEggs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export async function normCommand(chatChannel: TextChannel, message: Message, op
chatHist.push({ content: message.content, role: "user" });
const completion = await openai.chat.completions.create({
messages: chatHist,
model: "gpt-3.5-turbo",
model: "gpt-4o-mini",
});

console.info(completion.usage);
Expand Down
64 changes: 57 additions & 7 deletions src/controllers/Interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export async function postCurrentQueue(queueChannel: TextChannel): Promise<Messa
return await queueChannel.send(MessageBuilder.queueMessage(ballchasers));
}

export let twos = false;

export async function handleInteraction(
buttonInteraction: ButtonInteraction,
NormClient: Client<boolean>
Expand Down Expand Up @@ -65,6 +67,7 @@ export async function handleInteraction(
msg.delete(),
await message.edit(MessageBuilder.fullQueueMessage(ballchasers)),
QueueRepository.resetCaptainsRandomVoters(),
QueueRepository.resetTwosVoters(),
]);
const diff = new Date().getTime() - time;
console.info(
Expand Down Expand Up @@ -116,7 +119,7 @@ export async function handleInteraction(
case ButtonCustomID.CreateRandomTeam: {
const playerInQueue = await QueueRepository.getBallChaserInQueue(buttonInteraction.user.id);
if (!playerInQueue) return;
await captainsRandomVote(buttonInteraction, message);
await captainsRandomVote(buttonInteraction, message, twos);
const diff = new Date().getTime() - time;
console.info(
`${month + 1}/${day}/${year} - ${hour}:${min}:${sec}:::${mil} | Random: ${
Expand All @@ -129,7 +132,7 @@ export async function handleInteraction(
case ButtonCustomID.ChooseTeam: {
const playerInQueue = await QueueRepository.getBallChaserInQueue(buttonInteraction.user.id);
if (!playerInQueue) return;
await captainsRandomVote(buttonInteraction, message);
await captainsRandomVote(buttonInteraction, message, twos);
const diff = new Date().getTime() - time;
console.info(
`${month + 1}/${day}/${year} - ${hour}:${min}:${sec}:::${mil} | Captains: ${
Expand Down Expand Up @@ -171,21 +174,67 @@ export async function handleInteraction(
);
break;
}

case ButtonCustomID.Twos: {
const playerInQueue = await QueueRepository.getBallChaserInQueue(buttonInteraction.user.id);
if (!playerInQueue) return;
await twosVoting(buttonInteraction, message);
const diff = new Date().getTime() - time;
console.info(
`${month + 1}/${day}/${year} - ${hour}:${min}:${sec}:::${mil} | 2v2 Vote: ${
buttonInteraction.user.username
} - ${diff}ms`
);
}
}
}

async function twosVoting(buttonInteraction: ButtonInteraction, message: Message) {
const ballChasers = await QueueRepository.getAllBallChasersInQueue();
const vote = await QueueRepository.count2v2Votes(buttonInteraction);

if (vote.twos == 4) {
twos = true;
const list = ballChasers.map((ballChaser) => {
return `<@${ballChaser.id}> `;
});

const msg = await message.channel.send({ content: list.toString() });
msg;

Promise.all([
msg.delete(),
await message.edit(MessageBuilder.fullQueueMessage(ballChasers)),
QueueRepository.resetCaptainsRandomVoters(),
QueueRepository.resetTwosVoters(),
]);
} else {
const players = await QueueRepository.getTwosVoters();
const twosVotes = vote.twos;
const voterList: PlayerInQueue[] = [];

for (const key of players.keys()) {
const player = await QueueRepository.getBallChaserInQueue(key);
if (player) {
voterList.push(player);
}
}
Promise.all([await message.edit(MessageBuilder.vote2v2sMessage(ballChasers, twosVotes, voterList, players))]);
}
}

async function captainsRandomVote(buttonInteraction: ButtonInteraction, message: Message) {
async function captainsRandomVote(buttonInteraction: ButtonInteraction, message: Message, twos: boolean) {
const playerInQueue = await QueueRepository.isPlayerInQueue(buttonInteraction.user.id);
const queue = await QueueRepository.getAllBallChasersInQueue();
if (!playerInQueue || queue.length != 6) return;
if (!playerInQueue || (queue.length != 6 && !twos)) return;
const ballChasers = await QueueRepository.getAllBallChasersInQueue();

const vote = await QueueRepository.countCaptainsRandomVote(buttonInteraction);

if (vote.captains == 4) {
if ((!twos && vote.captains == 4) || (twos && vote.captains == 3)) {
const players = await setCaptains(ballChasers);
await message.edit(MessageBuilder.captainChooseMessage(true, players));
} else if (vote.random == 4) {
await message.edit(MessageBuilder.captainChooseMessage(true, players, twos));
} else if ((!twos && vote.random == 4) || (twos && vote.random == 3)) {
const currentMatch = await createRandomMatch();
const emptyQueue: PlayerInQueue[] = [];

Expand All @@ -198,6 +247,7 @@ async function captainsRandomVote(buttonInteraction: ButtonInteraction, message:
]);

QueueRepository.resetCaptainsRandomVoters();
QueueRepository.resetTwosVoters();
} else if (vote.captains > 4 || vote.random > 4) return;
else {
const players = await QueueRepository.getCaptainsRandomVoters();
Expand Down
14 changes: 13 additions & 1 deletion src/controllers/MenuInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { bluePlayerChosen, orangePlayerChosen } from "../services/TeamAssignment
import { Team } from "../types/common";
import { getEnvVariable } from "../utils";
import MessageBuilder, { MenuCustomID } from "../utils/MessageHelper/MessageBuilder";
import { twos } from "./Interactions";

export async function handleMenuInteraction(menuInteraction: StringSelectMenuInteraction): Promise<void> {
const message = menuInteraction.message;
Expand All @@ -20,7 +21,18 @@ export async function handleMenuInteraction(menuInteraction: StringSelectMenuInt
if (isCaptain || isDev) {
const playersLeft = await bluePlayerChosen(menuInteraction.values[0]);

await message.edit(MessageBuilder.captainChooseMessage(false, playersLeft));
if (twos) {
const emptyQueue: PlayerInQueue[] = [];
const newActiveMatch = await createMatchFromChosenTeams();
Promise.all([
await message.channel.send(await MessageBuilder.activeMatchMessage(newActiveMatch)),
await message.edit(MessageBuilder.queueMessage(emptyQueue)),
]);

QueueRepository.resetCaptainsRandomVoters();
} else {
await message.edit(MessageBuilder.captainChooseMessage(false, playersLeft, twos));
}
break;
}

Expand Down
28 changes: 28 additions & 0 deletions src/repositories/QueueRepository/QueueRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ interface CaptainsRandomVotes {
random: number;
}

const twosMap = new Map<string, string>();
interface TwosVotes {
twos: number;
}

export class QueueRepository {
#Queue: Prisma.QueueDelegate<Prisma.RejectPerOperation>;
#BallChasers: Prisma.BallChaserDelegate<Prisma.RejectPerOperation>;
Expand All @@ -34,6 +39,27 @@ export class QueueRepository {
};
}

async count2v2Votes(buttonInteraction: ButtonInteraction): Promise<TwosVotes> {
twosMap.set(buttonInteraction.user.id, buttonInteraction.customId);
let twosCounter = 0;
for (const value of twosMap.values()) {
if (value == ButtonCustomID.Twos) {
twosCounter += 1;
}
}
return {
twos: twosCounter,
};
}

async getTwosVoters(): Promise<Map<string, string>> {
return twosMap;
}

async resetTwosVoters(): Promise<void> {
twosMap.clear();
}

async countCaptainsRandomVote(buttonInteraction: ButtonInteraction): Promise<CaptainsRandomVotes> {
playersMap.set(buttonInteraction.user.id, buttonInteraction.customId);
let captainsCounter = 0;
Expand Down Expand Up @@ -113,6 +139,8 @@ export class QueueRepository {
// console.error(err);
// }
});
this.resetCaptainsRandomVoters();
this.resetTwosVoters();
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/services/MatchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import QueueRepository from "../repositories/QueueRepository";
import { NewActiveMatchInput, PlayerInActiveMatch } from "../repositories/ActiveMatchRepository/types";
import { Team } from "../types/common";
import { calculateMMR, calculateProbability, calculateProbabilityDecimal } from "./MatchReportService";
import { twos } from "../controllers/Interactions";

interface ActiveMatchTeamDetails {
mmrStake: number;
Expand Down Expand Up @@ -79,7 +80,11 @@ export async function createMatchFromChosenTeams(): Promise<ActiveMatchCreated>
if (p.team !== null) {
createdTeams.push({ id: p.id, team: p.team });
} else {
createdTeams.push({ id: p.id, team: Team.Blue });
if (twos) {
createdTeams.push({ id: p.id, team: Team.Orange });
} else {
createdTeams.push({ id: p.id, team: Team.Blue });
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/utils/MessageHelper/CustomButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const enum ButtonCustomID {
ReportBlue = "reportBlue",
ReportOrange = "reportOrange",
BrokenQueue = "brokenQueue",
Twos = "twosQueue",
}

interface CustomButtonOptions extends Partial<Omit<ButtonBuilder, "customId">> {
Expand All @@ -37,6 +38,8 @@ export default class CustomButton extends ButtonBuilder {
return new ButtonBuilder().setCustomId("breakMatch").setLabel("DEV: Break Match").setStyle(ButtonStyle.Danger);
case ButtonCustomID.BrokenQueue:
return new ButtonBuilder().setCustomId("brokenQueue").setLabel("Broken Queue").setStyle(ButtonStyle.Danger);
case ButtonCustomID.Twos:
return new ButtonBuilder().setCustomId("twosQueue").setLabel("2v2").setStyle(ButtonStyle.Primary);
case ButtonCustomID.ReportBlue:
return new ButtonBuilder()
.setCustomId("reportBlue")
Expand Down
61 changes: 2 additions & 59 deletions src/utils/MessageHelper/EmbedBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { EmbedBuilder as MessageEmbed, EmbedData } from "discord.js";
import { ActiveMatchCreated } from "../../services/MatchService";
import { Team } from "../../types/common";
import EventRepository from "../../repositories/EventRepository/EventRepository";
import { ColorCodes } from "../utils";

export class BaseEmbed extends MessageEmbed {
Expand All @@ -27,63 +25,8 @@ export default class EmbedBuilder {
return new BaseEmbed({ color: ColorCodes.Green, description, title: "Queue is Full" });
}

static async activeMatchEmbed({ blue, orange }: ActiveMatchCreated): Promise<BaseEmbed> {
const blueTeam: Array<string> = blue.players.map((player) => "<@" + player.id + ">");
const orangeTeam: Array<string> = orange.players.map((player) => "<@" + player.id + ">");
const activeMatchEmbed = new BaseEmbed({
color: ColorCodes.DarkRed,
fields: [
{ name: "🔷 Blue Team 🔷", value: blueTeam.join("\n") },
{ name: "🔶 Orange Team 🔶", value: orangeTeam.join("\n") },
],
title: "Teams are set!",
});

let probability;
let winner;
if (blue.winProbability > orange.winProbability) {
probability = blue.winProbability;
winner = "Blue Team is";
} else if (blue.winProbability < orange.winProbability) {
probability = orange.winProbability;
winner = "Orange Team is";
} else {
probability = "50";
winner = "Both teams are";
}

const event = await EventRepository.getCurrentEvent();
const blueMMR = blue.mmrStake * event.mmrMult;
const orangeMMR = orange.mmrStake * event.mmrMult;

activeMatchEmbed.addFields({
name: "MMR Stake & Probability Rating:\n",
value:
"🔷 Blue Team: \u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0\u00A0**(+" +
blueMMR.toString() +
")**\u00A0\u00A0**(-" +
orange.mmrStake.toString() +
")** 🔷\n🔶 Orange Team:\u00A0\u00A0**(+" +
orangeMMR.toString() +
")**\u00A0\u00A0**(-" +
blue.mmrStake.toString() +
")** 🔶\n" +
winner +
" predicted to have a **" +
probability +
"%** chance of winning.",
});

if (event.mmrMult > 1) {
activeMatchEmbed.addFields({
name: "X" + event.mmrMult.toString() + " MMR Event!",
value: "Winnings are multiplied by **" + event.mmrMult.toString() + "** for this match!",
});
}

activeMatchEmbed.addFields({ name: "Reporting", value: "Use the buttons to report which team won the match." });

return activeMatchEmbed;
static activeMatchEmbed(): BaseEmbed {
return new BaseEmbed({ color: ColorCodes.DarkRed, title: "Teams are set!" });
}

static voteForCaptainsOrRandomEmbed(title: string, description: string): BaseEmbed {
Expand Down
Loading

0 comments on commit 9276875

Please sign in to comment.