Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create checkskills command to diagnose the oatcakes problem #220

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions packages/oaf/src/commands/clan/checkskills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";

import { prisma } from "../../clients/database.js";
import {
getParticipationFromCurrentRaid,
mergeParticipation,
parseOldLogs,
} from "./skills.js";

export const data = new SlashCommandBuilder()
.setName("checkskills")
.setDescription("Check what dread skills someone is owed")
.addStringOption((option) =>
option
.setName("who")
.setDescription("The player id of the user in question")
.setRequired(true)
.setMaxLength(30),
);

export async function execute(interaction: ChatInputCommandInteraction) {
const playerId = Number(interaction.options.getString("who", true));
await interaction.deferReply();

const player = await prisma.player.findFirst({ where: { playerId } });

if (!player) {
return void (await interaction.editReply(
`Unable to find player with id ${playerId}`,
));
}

const lines: string[] = [];

try {
if (player.doneWithSkills) lines.push("Player is done with skills");
await parseOldLogs();

const { kills, skills } = mergeParticipation(
new Map([[playerId, player]]),
await getParticipationFromCurrentRaid(),
).get(playerId)!;

lines.push(`Across all registered raids, player has done ${kills} kills`);
lines.push(
`Across all registered raids, player has gained ${skills} skills`,
);
lines.push(
`Doing the math, they are owed ${Math.floor((kills + 450) / 900) - skills} skills`,
);
} catch (e) {
return void (await interaction.editReply(`Uh oh: ${e}`));
}

return void (await interaction.editReply(lines.join("\n")));
}
6 changes: 3 additions & 3 deletions packages/oaf/src/commands/clan/skills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function addParticipation(
});
}

function mergeParticipation(a: Participation, b: Participation) {
export function mergeParticipation(a: Participation, b: Participation) {
const merged = new Map(a);

for (const [playerId, data] of b) {
Expand All @@ -52,7 +52,7 @@ function mergeParticipation(a: Participation, b: Participation) {
return merged;
}

async function getParticipationFromCurrentRaid() {
export async function getParticipationFromCurrentRaid() {
const raidLogs = await Promise.all(
DREAD_CLANS.map((clan) => getRaidLog(clan.id)),
);
Expand Down Expand Up @@ -86,7 +86,7 @@ export function getParticipationFromRaidLog(raidLog: string) {
return participation;
}

async function parseOldLogs() {
export async function parseOldLogs() {
const parsedRaids = (
await prisma.raid.findMany({ select: { id: true } })
).map(({ id }) => id);
Expand Down