Skip to content

Commit

Permalink
Separate anniversary trivia handling from event-only submissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed May 30, 2024
1 parent c5d3a10 commit 1d92b83
Show file tree
Hide file tree
Showing 23 changed files with 352 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class AnniversaryTriviaPlayerCollectionManager extends DatabaseCollection
return {
discordId: "",
pastAttempts: [],
pastEventAttempts: [],
};
}

Expand Down
126 changes: 96 additions & 30 deletions src/database/utils/aliceDb/AnniversaryTriviaPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { StringHelper } from "@alice-utils/helpers/StringHelper";
import { AnniversaryTriviaAttempt } from "@alice-structures/utils/AnniversaryTriviaAttempt";
import { AnniversaryTriviaManager } from "@alice-utils/managers/AnniversaryTriviaManager";
import { AnniversaryTriviaCurrentAttemptQuestion } from "@alice-structures/utils/AnniversaryTriviaCurrentAttemptQuestion";
import { AnniversaryReviewType } from "@alice-enums/utils/AnniversaryReviewType";
import { CacheManager } from "@alice-utils/managers/CacheManager";

/**
* Represents a player in the anniversary trivia game.
Expand All @@ -27,6 +29,7 @@ export class AnniversaryTriviaPlayer
readonly discordId: string;
currentAttempt?: AnniversaryTriviaCurrentAttemptQuestion[];
readonly pastAttempts: AnniversaryTriviaAttempt[];
readonly pastEventAttempts: AnniversaryTriviaAttempt[];

constructor(
data: DatabaseAnniversaryTriviaPlayer = DatabaseManager.aliceDb
Expand All @@ -36,6 +39,7 @@ export class AnniversaryTriviaPlayer

this.discordId = data.discordId;
this.pastAttempts = data.pastAttempts;
this.pastEventAttempts = data.pastEventAttempts;
this.currentAttempt = data.currentAttempt;
}

Expand Down Expand Up @@ -78,21 +82,28 @@ export class AnniversaryTriviaPlayer
* @param currentQuestion The current question.
* @param attemptIndex The index of the attempt to review.
* @param language The language to use for localization.
* @param type The type of review.
*/
toReviewMessage(
member: GuildMember,
currentQuestion: AnniversaryTriviaQuestion,
attemptIndex: number,
language: Language,
type: AnniversaryReviewType,
): BaseMessageOptions {
const localization = this.getLocalization(language);
const attempt = this.pastAttempts[attemptIndex - 1];
const attempt = (
type === AnniversaryReviewType.past
? this.pastAttempts
: this.pastEventAttempts
)[attemptIndex - 1];

return {
components: this.createButtons(
currentQuestion,
language,
attemptIndex,
type,
),
embeds: [
EmbedCreator.createNormalEmbed({
Expand All @@ -113,17 +124,39 @@ export class AnniversaryTriviaPlayer
};
}

/**
* Creates buttons for embed.
*
* @param question The current question.
* @param language The language to use for localization.
* @returns The buttons.
*/
private createButtons(
question: AnniversaryTriviaQuestion,
language: Language,
): ActionRowBuilder<ButtonBuilder>[];

/**
* Creates buttons for embed.
*
* @param question The current question.
* @param language The language to use for localization.
* @param attemptIndex The index of the attempt to review.
* @param type The type of review.
* @returns The buttons.
*/
private createButtons(
question: AnniversaryTriviaQuestion,
language: Language,
attemptIndex: number,
type: AnniversaryReviewType,
): ActionRowBuilder<ButtonBuilder>[];

private createButtons(
question: AnniversaryTriviaQuestion,
language: Language,
attemptIndex?: number,
type?: AnniversaryReviewType,
): ActionRowBuilder<ButtonBuilder>[] {
const rows: ActionRowBuilder<ButtonBuilder>[] = [];

Expand All @@ -132,23 +165,32 @@ export class AnniversaryTriviaPlayer

const userAnswer = (
attemptIndex
? this.pastAttempts[attemptIndex - 1].answers
? type === AnniversaryReviewType.event
? this.pastEventAttempts[attemptIndex - 1].answers
: this.pastAttempts[attemptIndex - 1].answers
: this.currentAttempt
)?.find((m) => m.id === question.id);

for (const answer of question.answers) {
if (attemptIndex) {
answerRow.addComponents(
new ButtonBuilder()
.setCustomId(answer)
.setDisabled(true)
.setLabel(answer)
.setStyle(
userAnswer?.answer === answer
? ButtonStyle.Success
: ButtonStyle.Primary,
),
);
if (attemptIndex && type) {
const button = new ButtonBuilder()
.setCustomId(answer)
.setDisabled(true)
.setLabel(answer);

if (userAnswer && userAnswer.answer === answer) {
if (userAnswer.answer === question.correctAnswer) {
button.setStyle(ButtonStyle.Success);
} else {
button.setStyle(ButtonStyle.Danger);
}
} else if (answer === question.correctAnswer) {
button.setStyle(ButtonStyle.Success);
} else {
button.setStyle(ButtonStyle.Primary);
}

answerRow.addComponents(button);
} else {
answerRow.addComponents(
new ButtonBuilder()
Expand All @@ -168,23 +210,47 @@ export class AnniversaryTriviaPlayer
let questionRow = new ActionRowBuilder<ButtonBuilder>();

for (let i = 0; i < 15; ++i) {
const userAnswer = this.currentAttempt?.find((v) => v.id === i + 1);
const button = new ButtonBuilder()
.setCustomId(
`anniversaryTriviaQuestion#${i + 1}${type ? `#${type}` : ""}${attemptIndex ? `#${attemptIndex}` : ""}`,
)
.setLabel((i + 1).toString())
.setDisabled(i === question.id - 1);

questionRow.addComponents(
new ButtonBuilder()
.setCustomId(
`anniversaryTriviaQuestion#${i + 1}${attemptIndex ? `#${attemptIndex}` : ""}`,
)
.setLabel((i + 1).toString())
.setStyle(
userAnswer?.flagged
? ButtonStyle.Danger
: userAnswer
? ButtonStyle.Success
: ButtonStyle.Secondary,
)
.setDisabled(i === question.id - 1),
);
if (attemptIndex && type) {
const question = CacheManager.anniversaryTriviaQuestions.get(
i + 1,
)!;

const userAnswer = (
type === AnniversaryReviewType.event
? this.pastEventAttempts[attemptIndex - 1].answers
: this.pastAttempts[attemptIndex - 1].answers
).find((v) => v.id === i + 1);

// Highlight correctly and wrongly answered questions - as well as those that are not answered.
button.setStyle(
userAnswer
? userAnswer.answer === question.correctAnswer
? ButtonStyle.Success
: ButtonStyle.Danger
: ButtonStyle.Secondary,
);
} else {
const userAnswer = this.currentAttempt?.find(
(v) => v.id === i + 1,
);

button.setStyle(
userAnswer?.flagged
? ButtonStyle.Danger
: userAnswer
? ButtonStyle.Success
: ButtonStyle.Secondary,
);
}

questionRow.addComponents(button);

if (questionRow.components.length === 5) {
rows.push(questionRow);
Expand Down
7 changes: 7 additions & 0 deletions src/enums/utils/AnniversaryReviewType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Types of reviews available for the anniversary trivia.
*/
export enum AnniversaryReviewType {
event = "event",
past = "past",
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DatabaseManager } from "@alice-database/DatabaseManager";
import { AnniversaryContinueAttemptLocalization } from "@alice-localization/interactions/buttons/Anniversary/anniversaryContinueAttempt/AnniversaryContinueAttemptLocalization";
import { ButtonCommand } from "@alice-structures/core/ButtonCommand";
import { MessageCreator } from "@alice-utils/creators/MessageCreator";
Expand All @@ -14,9 +15,12 @@ export const run: ButtonCommand["run"] = async (_, interaction) => {
CommandHelper.getLocale(interaction),
);

const player = CacheManager.anniversaryTriviaPlayers.get(
interaction.user.id,
);
await InteractionHelper.deferReply(interaction);

const player =
await DatabaseManager.aliceDb.collections.anniversaryTriviaPlayer.getFromId(
interaction.user.id,
);

if (!player?.currentAttempt) {
return InteractionHelper.reply(interaction, {
Expand Down
28 changes: 14 additions & 14 deletions src/interactions/buttons/Anniversary/anniversaryNewAttempt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,34 @@ export const run: ButtonCommand["run"] = async (_, interaction) => {
CommandHelper.getLocale(interaction),
);

await InteractionHelper.deferReply(interaction);

const player =
CacheManager.anniversaryTriviaPlayers.get(interaction.user.id) ??
(await DatabaseManager.aliceDb.collections.anniversaryTriviaPlayer.getFromId(
interaction.user.id,
{
projection: {
_id: 0,
currentAttempt: 1,
},
},
)) ??
new AnniversaryTriviaPlayer({
discordId: interaction.user.id,
pastEventAttempts: [],
pastAttempts: [],
});

if (player?.currentAttempt !== undefined) {
if (player.currentAttempt !== undefined) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("existingAttemptExists"),
),
});
}

if (player.pastAttempts.length === 2) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("noMoreAttempts"),
),
});
}

player.currentAttempt = [];

await InteractionHelper.deferReply(interaction);

await DatabaseManager.aliceDb.collections.anniversaryTriviaPlayer.updateOne(
{
discordId: interaction.user.id,
Expand All @@ -53,13 +54,12 @@ export const run: ButtonCommand["run"] = async (_, interaction) => {
},
$setOnInsert: {
pastAttempts: [],
pastEventAttempts: [],
},
},
{ upsert: true },
);

CacheManager.anniversaryTriviaPlayers.set(interaction.user.id, player);

const firstQuestion = CacheManager.anniversaryTriviaQuestions.first()!;

InteractionHelper.reply(
Expand Down
Loading

0 comments on commit 1d92b83

Please sign in to comment.