Skip to content

Commit 27acc37

Browse files
authored
Merge pull request #36 from N3aar/fix/get-artist-info
fix: get artist information and give points when winning
2 parents 09168e6 + 6630b05 commit 27acc37

File tree

6 files changed

+69
-81
lines changed

6 files changed

+69
-81
lines changed

src/commands/lastfm/jumble.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { UserArtistData } from "@/shared/types/lastFmTypes.js";
22
import { embedColors } from "@/utils/contants.js";
3-
import { getRandomNumber } from "@/utils/random.js";
3+
import { getSkewedRandomInt } from "@/utils/random.js";
44
import { shuffleString } from "@/utils/stringFormat.js";
55
import { Command } from "@sapphire/framework";
66
import { EmbedBuilder, type GuildMember, type TextChannel } from "discord.js";
@@ -74,9 +74,9 @@ export class JumbleCommand extends Command {
7474
const totalArtists =
7575
await this.container.lastFmAPI.getTotalArtists(username);
7676
const totalPages = Math.ceil((totalArtists ?? 100) / 100);
77-
const twentyPercent = Math.floor(totalPages * 0.3);
77+
const reducedPages = Math.floor(totalPages * 0.3);
7878

79-
const page = getRandomNumber(1, Math.max(totalPages - twentyPercent, 1));
79+
const page = getSkewedRandomInt(1, reducedPages);
8080
const artists = await this.container.lastFmAPI.getTopArtists(
8181
username,
8282
100,
@@ -111,7 +111,7 @@ export class JumbleCommand extends Command {
111111

112112
if (!gameContext) {
113113
await interaction.reply({
114-
content: "Não foi encontrado informações sobre o artista!",
114+
content: "Não foi possível iniciar este Jumble, tente novamente!",
115115
ephemeral: false,
116116
fetchReply: false,
117117
});

src/events/announceScheduledEvent.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { birthdayVideos } from "@/utils/contants.js";
2-
import { getRandomNumber } from "@/utils/random.js";
2+
import { getRandomInt } from "@/utils/random.js";
33
import { container } from "@sapphire/pieces";
44
import type { MessageCreateOptions, TextChannel } from "discord.js";
55

@@ -89,8 +89,7 @@ export default async function announceScheduledEvent() {
8989
};
9090

9191
if (hasBirthdays) {
92-
const random =
93-
birthdayVideos[getRandomNumber(0, birthdayVideos.length - 1)];
92+
const random = birthdayVideos[getRandomInt(0, birthdayVideos.length - 1)];
9493

9594
message.files = [
9695
{

src/shared/handlers/JumbleGameHandler.ts

+50-68
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fail } from "node:assert";
12
import {
23
type TranslationKey,
34
embedColors,
@@ -47,13 +48,13 @@ type JumbleGameContext = {
4748
};
4849

4950
type ArtistInfo = {
50-
isGroup: boolean;
51-
type: TranslationKey | null;
52-
gender: TranslationKey | null;
53-
musicGenre: string | undefined;
54-
country: string | null;
55-
begin: string | null;
56-
end: string | null;
51+
isGroup?: boolean;
52+
musicGenre?: string;
53+
type?: TranslationKey | null;
54+
gender?: TranslationKey | null;
55+
country?: string | null;
56+
begin?: string | null;
57+
end?: string | null;
5758
};
5859

5960
enum ButtonCustomIds {
@@ -87,18 +88,8 @@ export default class JumbleGameHandler {
8788
name: string,
8889
mbid?: string | null,
8990
): Promise<JumbleGameContext | null> {
90-
const artistInfo = await this.getArtistInfo(name, mbid);
91-
if (!artistInfo) return null;
92-
93-
const hints = this.createHints(
94-
playcount,
95-
artistInfo.type,
96-
artistInfo.country,
97-
artistInfo.musicGenre,
98-
artistInfo.gender,
99-
artistInfo.begin,
100-
artistInfo.end,
101-
);
91+
const artistInfo: ArtistInfo = (await this.getArtistInfo(name, mbid)) ?? {};
92+
const hints = this.createHints(playcount, artistInfo);
10293

10394
const gameContext: JumbleGameContext = {
10495
hints,
@@ -203,15 +194,9 @@ export default class JumbleGameHandler {
203194
return collector;
204195
}
205196

206-
private createHints(
207-
playcount: number,
208-
type: TranslationKey | null,
209-
country: string | null,
210-
musicGenre: string | undefined,
211-
gender: TranslationKey | null,
212-
begin: string | null,
213-
end: string | null,
214-
): string[] {
197+
private createHints(playcount: number, artistInfo: ArtistInfo): string[] {
198+
const { type, country, musicGenre, gender, begin, end } = artistInfo;
199+
215200
const hints: string[] = [];
216201
const isGroup = type ? this.isGroupType(type) : false;
217202

@@ -222,8 +207,8 @@ export default class JumbleGameHandler {
222207
? convertToDiscordTimestamp(end, discordTimestampFormats.LONG_DATE)
223208
: null;
224209

225-
const arttype = translations[type ?? "group"];
226-
const artgender = translations[gender ?? "male"];
210+
const arttype = translations[type ?? "group"] ?? type;
211+
const artgender = translations[gender ?? "male"] ?? gender;
227212
const flag = `:flag_${country?.toLowerCase()}:`;
228213

229214
const conditionsAndHints: [boolean, string][] = [
@@ -283,35 +268,17 @@ export default class JumbleGameHandler {
283268
}
284269
}
285270

286-
private async addPoints(userId: string, points: number): Promise<boolean> {
287-
try {
288-
const user = await container.db.user.findUnique({
289-
where: {
290-
discordId: userId,
291-
},
292-
});
293-
294-
if (!user) return false;
295-
296-
await container.db.jumble.upsert({
297-
where: {
298-
userId: user.id,
299-
},
300-
update: {
301-
points: {
302-
increment: points,
303-
},
304-
},
305-
create: {
306-
userId: user.id,
307-
points: points,
271+
private async addPoints(userId: string, points: number) {
272+
await container.db.jumble.update({
273+
where: {
274+
userId: userId,
275+
},
276+
data: {
277+
points: {
278+
increment: points,
308279
},
309-
});
310-
311-
return true;
312-
} catch (error) {
313-
return false;
314-
}
280+
},
281+
});
315282
}
316283

317284
private async setBestTime(userId: string, time: number) {
@@ -433,25 +400,32 @@ export default class JumbleGameHandler {
433400
},
434401
});
435402

436-
if (!userData || !userData.Jumble) return;
403+
if (!userData || !userData.Jumble) {
404+
const embed = new EmbedBuilder()
405+
.setDescription(
406+
"Use o comando /lastfm para registrar sua conta antes de jogar.",
407+
)
408+
.setColor(embedColors.default);
437409

438-
const added = await this.addPoints(userData.id, 1);
439-
if (added) {
440-
const bestTime = userData.Jumble.bestTime;
410+
await message.channel.send({ embeds: [embed] });
441411

442-
if (bestTime <= 0 || seconds < bestTime) {
443-
await this.setBestTime(userData.id, seconds);
444-
}
412+
return;
413+
}
414+
415+
await this.addPoints(userData.id, 1);
416+
417+
const bestTime = userData.Jumble.bestTime;
418+
419+
if (bestTime <= 0 || seconds < bestTime) {
420+
await this.setBestTime(userData.id, seconds);
445421
}
446422

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

452-
message.channel.send({
453-
embeds: [embed],
454-
});
428+
message.channel.send({ embeds: [embed] });
455429
}
456430

457431
private processInteraction(
@@ -510,6 +484,14 @@ export default class JumbleGameHandler {
510484
if (isWinner) embed.spliceFields(1, 1);
511485
else embed.spliceFields(1, 1, newField);
512486

487+
if (!isWinner && !isGiveUp) {
488+
const failEmbed = new EmbedBuilder()
489+
.setDescription(`Ninguém adivinhou. A resposta era \`${artistName}\``)
490+
.setColor(embedColors.error);
491+
492+
message.reply({ embeds: [failEmbed] });
493+
}
494+
513495
message.edit({
514496
embeds: [embed],
515497
components: [],

src/shared/handlers/xpHandler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expValues } from "@/utils/contants.js";
2-
import { getRandomNumber } from "@/utils/random.js";
2+
import { getRandomInt } from "@/utils/random.js";
33
import { container } from "@sapphire/pieces";
44
import type { Guild, GuildMember, TextChannel } from "discord.js";
55

@@ -63,7 +63,7 @@ export default class ExpHandler {
6363

6464
if (!expStats?.canGetExp) return;
6565

66-
expStats.exp += getRandomNumber(expValues.min, expValues.max);
66+
expStats.exp += getRandomInt(expValues.min, expValues.max);
6767
expStats.canGetExp = false;
6868

6969
const newXp = expStats.exp;

src/utils/contants.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ export const translations = {
6363
group: "grupo",
6464
person: "pessoa",
6565
orchestra: "orquestra",
66-
choir: "Coro",
67-
character: "Personagem",
68-
other: "Outro",
66+
choir: "coro",
67+
character: "personagem",
68+
other: "outro",
6969
male: "masculino",
7070
female: "feminino",
7171
neither: "nenhum",
72+
not_applicable: "não aplicável",
7273
};
7374

7475
export const fieldTitles = {

src/utils/random.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
export function getRandomNumber(min: number, max: number) {
1+
export function getRandomInt(min: number, max: number) {
22
return Math.floor(Math.random() * (max - min + 1)) + min;
33
}
44

5+
export function getSkewedRandomInt(min: number, max: number) {
6+
const random = Math.random();
7+
const skewed = random ** 2;
8+
return Math.floor(skewed * (max - min + 1)) + min;
9+
}
10+
511
export function shuffle<T>(array: T[]): T[] {
612
const newArray = array.slice();
713

0 commit comments

Comments
 (0)