Skip to content

Commit

Permalink
Merge pull request #35 from N3aar/fix/jumblegame-handler
Browse files Browse the repository at this point in the history
fix: jumble game handler errors
  • Loading branch information
Psykka authored Jun 23, 2024
2 parents 4387865 + 4551691 commit 09168e6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/commands/lastfm/jumble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class JumbleCommand extends Command {
const totalArtists =
await this.container.lastFmAPI.getTotalArtists(username);
const totalPages = Math.ceil((totalArtists ?? 100) / 100);
const twentyPercent = Math.floor(totalPages / 2);
const twentyPercent = Math.floor(totalPages * 0.3);

const page = getRandomNumber(1, Math.max(totalPages - twentyPercent, 1));
const artists = await this.container.lastFmAPI.getTopArtists(
Expand Down
93 changes: 56 additions & 37 deletions src/shared/handlers/JumbleGameHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,8 @@ export default class JumbleGameHandler {
time: 25_000,
});

collector.on(
"collect",
async (interaction) =>
await this.processInteraction(interaction, channelId),
collector.on("collect", async (interaction) =>
this.processInteraction(interaction, channelId),
);

collector.on("end", async (collected) => await this.finishGame(channelId));
Expand Down Expand Up @@ -285,17 +283,35 @@ export default class JumbleGameHandler {
}
}

private async addPoints(userId: string, points: number) {
await container.db.jumble.update({
where: {
userId: userId,
},
data: {
points: {
increment: points,
private async addPoints(userId: string, points: number): Promise<boolean> {
try {
const user = await container.db.user.findUnique({
where: {
discordId: userId,
},
},
});
});

if (!user) return false;

await container.db.jumble.upsert({
where: {
userId: user.id,
},
update: {
points: {
increment: points,
},
},
create: {
userId: user.id,
points: points,
},
});

return true;
} catch (error) {
return false;
}
}

private async setBestTime(userId: string, time: number) {
Expand All @@ -318,7 +334,7 @@ export default class JumbleGameHandler {

gameContext.additionalHints++;

const hints = gameContext.hints.slice(0, hintsCount).join("\n");
const hints = gameContext.hints.slice(0, hintsCount + 1).join("\n");
const addHints = gameContext.additionalHints;

const extraHints = addHints > 1 ? "dicas extras" : "dica extra";
Expand All @@ -335,13 +351,13 @@ export default class JumbleGameHandler {
const newMessage = {
embeds: [newEmbed],
components: [
hintsCount + 1 > maxHints
hintsCount + 1 >= maxHints
? this.createComponents(true)
: (component as ActionRow<ButtonComponent>),
],
};

await gameContext?.message?.edit(newMessage);
gameContext?.message?.edit(newMessage);
}

private async reshuffle(gameContext: JumbleGameContext) {
Expand All @@ -354,7 +370,7 @@ export default class JumbleGameHandler {
const reshuffled = shuffleString(artistName);
const newEmbed = embed.setDescription(`\`${reshuffled}\``);

await message.edit({
message.edit({
embeds: [newEmbed],
});
}
Expand Down Expand Up @@ -385,7 +401,7 @@ export default class JumbleGameHandler {
return;
}

await interaction.deferUpdate();
interaction.deferUpdate();
gameContext.status = JumbleStatus.GIVEUP;
collector.stop();
}
Expand All @@ -397,14 +413,18 @@ export default class JumbleGameHandler {
) {
const gameContext = this.getJumbleGameContext(channelId);
const message = gameContext?.message;
const collector = gameContext?.collector;

if (!gameContext || !message) return;
if (!gameContext || !message || !collector) return;

const artist = gameContext.artistName;
const passed = Date.now() - gameContext.started;
const seconds = Number.parseFloat((passed / 1000).toFixed(1));
const description = `**${winnerName}** acertou! A resposta foi \`${artist}\``;

gameContext.status = JumbleStatus.WINNER;
collector.stop();

const userData = await container.db.user.findUnique({
where: { discordId: winnerId },
select: {
Expand All @@ -415,27 +435,26 @@ export default class JumbleGameHandler {

if (!userData || !userData.Jumble) return;

await this.addPoints(userData.id, 1);

const bestTime = userData.Jumble.bestTime;
const added = await this.addPoints(userData.id, 1);
if (added) {
const bestTime = userData.Jumble.bestTime;

if (bestTime <= 0 || seconds < bestTime) {
await this.setBestTime(userData.id, seconds);
if (bestTime <= 0 || seconds < bestTime) {
await this.setBestTime(userData.id, seconds);
}
}

gameContext.status = JumbleStatus.WINNER;

const embed = new EmbedBuilder()
.setDescription(description)
.setFooter({ text: `Respondido em ${seconds}s` })
.setColor(embedColors.success);

await message.channel.send({
message.channel.send({
embeds: [embed],
});
}

private async processInteraction(
private processInteraction(
interaction: ButtonInteraction,
channelId: string,
) {
Expand All @@ -446,27 +465,27 @@ export default class JumbleGameHandler {

switch (customId) {
case ButtonCustomIds.ADD_HINT:
await interaction.deferUpdate();
await this.addHint(gameContext);
interaction.deferUpdate();
this.addHint(gameContext);
break;

case ButtonCustomIds.RESHUFFLE:
await interaction.deferUpdate();
await this.reshuffle(gameContext);
interaction.deferUpdate();
this.reshuffle(gameContext);
break;

case ButtonCustomIds.GIVE_UP:
await this.giveUp(gameContext, interaction);
this.giveUp(gameContext, interaction);
break;
}
}

private async finishGame(channelId: string) {
const gameContext = this.getJumbleGameContext(channelId);
if (!gameContext) return;

this.deleteJamGame(channelId);

if (!gameContext) return;

const message = gameContext.message;
const embed = gameContext.embed;
const guild = message?.guild;
Expand All @@ -491,7 +510,7 @@ export default class JumbleGameHandler {
if (isWinner) embed.spliceFields(1, 1);
else embed.spliceFields(1, 1, newField);

await message.edit({
message.edit({
embeds: [embed],
components: [],
});
Expand Down

0 comments on commit 09168e6

Please sign in to comment.