Skip to content

Commit

Permalink
πŸ”₯ Streak feature #3 from Pexilo/streak-feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Pexilo authored Jun 24, 2023
2 parents 8901677 + e639b5c commit 98f80ed
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div align="center">
<img src="https://github.com/Pexilo/Whos-That/assets/67436391/fa031cc9-d9f5-4538-8a49-64df39791eca" align="center">
<a href="https://github.com/Pexilo/Whos-That/releases" target="_blank">
<img alt="Version" src="https://img.shields.io/badge/version-1.0.1-yellow.svg?cacheSeconds=2592000&style=for-the-badge" />
<img alt="Version" src="https://img.shields.io/badge/version-1.1.0-yellow.svg?cacheSeconds=2592000&style=for-the-badge" />
</a>
<a href="https://github.com/Pexilo/Whos-That/blob/main/LICENSE" target="_blank">
<img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg?style=for-the-badge" />
Expand Down
4 changes: 3 additions & 1 deletion src/interactions/selectmenus/whosthat.select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FetchUser,
UpdateUser,
} from "@utils/shortcuts";
import UserStreak from "@utils/streak-checker";
import { Guild, StringSelectMenuInteraction, TextChannel } from "discord.js";
import type { ShewenyClient } from "sheweny";
import { SelectMenu } from "sheweny";
Expand Down Expand Up @@ -69,9 +70,10 @@ export class WhosThatSelect extends SelectMenu {
whosThatResponded: whosthatRes,
});
let gameScore = 1; // 1 participation point
const userStreak = UserStreak(whosthatRes);

if (authorId === values[0]) {
gameScore += 1; // 1 point for correct answer
gameScore += 1 * userStreak; // 1 point per correct answer * streak bonus max 3
const totalPoints = await UpdateScore(
user.id,
userData,
Expand Down
2 changes: 1 addition & 1 deletion src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"fetchSourceErr": "❌ An error occured. Could not fetch sourceChannel. Please try again later.",
"fetchMsgErr": "❌ An error occured. Could not fetch the message. Please try again later.",
"alreadyResponded": "`🚫 You already responded to this message.\nThe response was <@${authorId}>!\n\n> Original message: ${message.url}`",
"rightAnswerRes": "`βœ… You got it right! The response was <@${authorId}>!\n\n> Original message: ${message.url}\n\n> \\`+1\\` participation point\n> \\`+1\\` right answer point\n> Total: \\`${totalPoints}\\` points`",
"rightAnswerRes": "`βœ… You got it right! The response was <@${authorId}>!\n\n> Original message: ${message.url}\n\n> \\`+${gameScore - 1}\\` right answer point(s) ${userStreak > 0 ? 'πŸ”₯'.repeat(userStreak):''}\n> \\`+1\\` participation point\n> Total: \\`${totalPoints}\\` points`",
"wrongAnswerRes": "`❌ You got it wrong! The response was <@${authorId}>!\n\n> Original message: ${message.url}\n\n> \\`+1\\` participation point\n> Total: \\`${totalPoints}\\` points`"
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"fetchSourceErr": "❌ Une erreur s'est produite. Impossible de récupérer sourceChannel. Veuillez réessayer ultérieurement.",
"fetchMsgErr": "❌ Une erreur s'est produite. Impossible de récupérer le message. Veuillez réessayer ultérieurement.",
"alreadyResponded": "`🚫 Vous avez déjà répondu à ce message.\nLa réponse était <@${authorId}> !\n\n> Message d'origine : ${message.url}`",
"rightAnswerRes": "`βœ… Vous avez trouvΓ© la bonne rΓ©ponse ! La rΓ©ponse Γ©tait <@${authorId}> !\n\n> Message d'origine : ${message.url}\n\n> \\`+1\\` point de participation\n> \\`+1\\` point de bonne rΓ©ponse\n> Total : \\`${totalPoints}\\` points`",
"rightAnswerRes": "`βœ… Vous avez trouvΓ© la bonne rΓ©ponse ! La rΓ©ponse Γ©tait <@${authorId}> !\n\n> Message d'origine : ${message.url}\n\n> \\`+${gameScore - 1}\\` point(s) de bonne rΓ©ponse ${userStreak > 0 ? 'πŸ”₯'.repeat(userStreak):''}\n> \\`+1\\` point de participation\n> Total : \\`${totalPoints}\\` points`",
"wrongAnswerRes": "`❌ Vous avez donné une mauvaise réponse ! La réponse était <@${authorId}> !\n\n> Message d'origine : ${message.url}\n\n> \\`+1\\` point de participation\n> Total : \\`${totalPoints}\\` points`"
}
},
Expand All @@ -62,7 +62,7 @@
"noUsersErrFR": "❌ Les utilisateurs n'ont pas encore joué à **Who's That**.",
"fetchUserErr": "`❌ Une erreur s'est produite lors de la récupération de l'utilisateur. Veuillez vérifier votre configuration avec </setup:${config.default.SLASH_COMMANDS_IDS.setup}> et réessayer ultérieurement.`",
"st": "er",
"nd": "nd",
"nd": "Γ¨me",
"rd": "Γ¨me",
"th": "Γ¨me",
"point": "point",
Expand Down
10 changes: 10 additions & 0 deletions src/models/ILeaderboardUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { GuildMember } from "discord.js";

export default interface ILeaderboardUser {
index: number;
position: number;
user: GuildMember | undefined;
points: number;
streak: number;
whosThatResponded: { guildId: string; messageId: string; correct: boolean }[];
}
32 changes: 16 additions & 16 deletions src/utils/generate-leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ILeaderboardUser from "@models/ILeaderboardUser";
import IUser from "@models/IUser";
import LanguageManager from "@utils/language-manager";
import {
Expand All @@ -7,17 +8,10 @@ import {
ButtonStyle,
CommandInteraction,
ComponentType,
GuildMember,
} from "discord.js";
import { Embed, FetchAndGetLang } from "./shortcuts";
import UserStreak from "./streak-checker";

type LeaderboardUser = {
index: number;
position: number;
user: GuildMember | undefined;
points: number;
whosThatResponded: { guildId: string; messageId: string; correct: boolean }[];
};
type PointsArray = {
guildId: string;
score: number;
Expand Down Expand Up @@ -65,6 +59,11 @@ object with additional properties. */
const previousPosition = index > 0 ? (await acc)[index - 1].position : -1;
const currentUser = user;

const whosThatGuild = currentUser.whosThatResponded.filter(
(whosThatResponded: { guildId: string }) =>
whosThatResponded.guildId === guild!.id
);

const userObject = {
index: index,
position:
Expand All @@ -81,6 +80,7 @@ object with additional properties. */
points: user.points.find(
(point: PointsArray) => point.guildId === guild!.id
)!.score,
streak: UserStreak(whosThatGuild),
whosThatResponded: user.whosThatResponded.filter(
(whosThatResponded: { messageId: string }) =>
whosThatResponded.messageId === messageId
Expand All @@ -92,8 +92,8 @@ object with additional properties. */
Promise.resolve([])
);

const user: LeaderboardUser = users.find(
(user: LeaderboardUser) => user.user?.id === interaction.user.id
const user: ILeaderboardUser = users.find(
(user: ILeaderboardUser) => user.user?.id === interaction.user.id
);

if (!user)
Expand All @@ -102,7 +102,7 @@ object with additional properties. */
});

const userIndex = users.findIndex(
(user: LeaderboardUser) => user.user?.id === interaction.user.id
(user: ILeaderboardUser) => user.user?.id === interaction.user.id
);

//User rank
Expand Down Expand Up @@ -140,7 +140,7 @@ object with additional properties. */
await UpdateInteraction(users, interaction, currentPage);

async function UpdateInteraction(
users: LeaderboardUser[],
users: ILeaderboardUser[],
interaction: CommandInteraction | ButtonInteraction,
currentPage: number
) {
Expand All @@ -167,14 +167,14 @@ object with additional properties. */
const leaderboardDescription = users
.slice((currentPage - 1) * pageSize, currentPage * pageSize)
.map(
(user: LeaderboardUser) =>
(user: ILeaderboardUser) =>
`${
user.position + 1 < 4
? rankEmoji[user.position]
: `β €${user.position + 1}.`
} ${user.user?.user} β€” \`${user.points}\` ${
generateLeaderboard.points
} ${
} ${user.user?.user} β€” ${
user.streak > 1 ? "πŸ”₯".repeat(user.streak) + " " : ""
} \`${user.points}\` ${generateLeaderboard.points} ${
user.whosThatResponded.length > 0
? user.whosThatResponded[0].correct
? "βœ…"
Expand Down
16 changes: 16 additions & 0 deletions src/utils/streak-checker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ILeaderboardUser from "@models/ILeaderboardUser";

export default function UserStreak(
whosThatGuild: ILeaderboardUser["whosThatResponded"]
): number {
const last3Whosthat = whosThatGuild.slice(-3);
let streak = 0;
for (const entry of last3Whosthat) {
if (entry.correct) {
streak++;
} else {
streak = 0;
}
}
return streak;
}

0 comments on commit 98f80ed

Please sign in to comment.