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

Show all rpg bounty cooldowns for any of the 3 buttons pressed #915

Merged
merged 2 commits into from
Dec 23, 2024
Merged
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
77 changes: 29 additions & 48 deletions src/discord/commands/guild/d.rpg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1359,9 +1359,8 @@ function getLastMonday(d:Date) {
export async function rpgBounties(
interaction: MessageComponentInteraction | ChatInputCommandInteraction,
command: 'quest' | 'dungeon' | 'raid' | null,
):Promise<InteractionEditReplyOptions | InteractionUpdateOptions> {
): Promise<InteractionEditReplyOptions | InteractionUpdateOptions> {
// Check if the user has a persona

const userData = await db.users.upsert({
where: {
discord_id: interaction.user.id,
Expand Down Expand Up @@ -1445,54 +1444,40 @@ export async function rpgBounties(
},
};

const allResetTimes: { [key: string]: Date } = {
quest: new Date(new Date().setHours(new Date().getHours() + 1, 0, 0, 0)),
dungeon: new Date(new Date(new Date().setDate(new Date().getDate() + 1)).setHours(0, 0, 0, 0)),
raid: new Date(new Date(getLastMonday(new Date()).getTime() + 7 * 24 * 60 * 60 * 1000).setHours(0, 0, 0, 0)),
};

// If the command is not null, we need to check the respective reset time
if (command !== null) {
const dbKey = `last_${command}`;
const lastBounties = personaData[dbKey as 'last_quest' | 'last_dungeon' | 'last_raid'] as Date;
// log.debug(F, `lastBounties: ${lastBounties}`);

let resetTime = {} as Date;
let timeout = false;
if (command === 'quest') {
const currentHour = new Date().getHours();
// log.debug(F, `currentHour: ${currentHour}`);

resetTime = new Date(new Date().setHours(currentHour + 1, 0, 0, 0));
if (lastBounties) {
// Check if the user has already completed the bounty type today, hourly, or weekly
const currentDate = new Date();

if (lastBounties) {
const lastBountiesHour = lastBounties.getHours();
// log.debug(F, `lastBountiesHour: ${lastBountiesHour}`);
if (lastBountiesHour === currentHour) {
timeout = true;
}
}
} else if (command === 'dungeon') {
const currentDay = new Date().getDate();
// log.debug(F, `currentDay: ${currentDay}`);
resetTime = new Date(new Date(new Date().setDate(currentDay + 1)).setHours(0, 0, 0, 0));

if (lastBounties) {
const lastBountiesDay = lastBounties.getDate();
// log.debug(F, `lastBountiesDay: ${lastBountiesDay}`);

// log.debug(F, `personaData1: ${JSON.stringify(personaData, null, 2)}`);
// if (lastBounties && (lastBounties.getTime() + interval > new Date().getTime())) {
if (lastBountiesDay === currentDay) {
timeout = true;
}
}
} else if (command === 'raid') {
const lastMonday = getLastMonday(new Date());
// log.debug(F, `lastMonday: ${lastMonday}`);
resetTime = new Date(new Date(lastMonday.getTime() + 7 * 24 * 60 * 60 * 1000).setHours(0, 0, 0, 0));
const timeComparison = {
quest: () => lastBounties.getHours() === currentDate.getHours(),
dungeon: () => lastBounties.getDate() === currentDate.getDate(),
raid: () => lastBounties.getTime() > getLastMonday(currentDate).getTime(),
};

// Check if the last bounties was done after the last monday
if (lastBounties && lastBounties.getTime() > lastMonday.getTime()) {
if (timeComparison[command] && timeComparison[command]()) {
timeout = true;
}
}

// log.debug(F, `resetTime: ${resetTime}`);
// log.debug(F, `timeout: ${timeout}`);
// Including all reset times in the response
const resetTimesMessage = stripIndents`
**Reset Times:**
- Quest: ${time(allResetTimes.quest, 'R')}
- Dungeon: ${time(allResetTimes.dungeon, 'R')}
- Raid: ${time(allResetTimes.raid, 'R')}
`;

if (timeout) {
return {
Expand All @@ -1501,42 +1486,38 @@ export async function rpgBounties(
.setFooter({ text: `${(interaction.member as GuildMember).displayName}'s TripSit RPG (BETA)`, iconURL: (interaction.member as GuildMember).displayAvatarURL() })
.setTitle(contracts[command].fail.title)
.setDescription(stripIndents`${contracts[command].fail.description}
You can try again ${time(resetTime, 'R')}
${resetTimesMessage}
${emojiGet('buttonBetSmall')} **Wallet:** ${personaData.tokens}`)
.setColor(contracts[command].fail.color)],
components: [rowBounties],
};
}

// Process tokens and other logic here...
let tokens = 10;
if (command === 'dungeon') { tokens = 50; } else if (command === 'raid') { tokens = 100; }

let tokenMultiplier = inventoryData
.filter(item => item.effect === 'tokenMultiplier')
.reduce((acc, item) => acc + parseFloat(item.effect_value), 1);
// log.debug(F, `tokenMultiplier (before donor): ${tokenMultiplier}`);

// CHeck if the user who started this interaction has the patreon or booster roles
// Check for roles and adjust multiplier
const member = await interaction.guild?.members.fetch(interaction.user.id);
if (member?.roles.cache.has(env.ROLE_BOOSTER) || member?.roles.cache.has(env.ROLE_PATRON)) {
tokenMultiplier += 0.1;
}

// Round token multiplier to 1 decimal place
tokenMultiplier = Math.round(tokenMultiplier * 10) / 10;
// log.debug(F, `tokenMultiplier: ${tokenMultiplier}`);

tokens *= tokenMultiplier;

if (env.NODE_ENV === 'development') { tokens *= 10; }

tokens = Math.round(tokens);

// Award the user tokens
// Award tokens to the user
personaData.tokens += tokens;
personaData[dbKey as 'last_quest' | 'last_dungeon' | 'last_raid'] = new Date();

// log.debug(F, `personaData2: ${JSON.stringify(personaData, null, 2)}`);
await db.personas.upsert({
where: {
id: personaData.id,
Expand All @@ -1551,7 +1532,7 @@ export async function rpgBounties(
.setFooter({ text: `${(interaction.member as GuildMember).displayName}'s TripSit RPG (BETA)`, iconURL: (interaction.member as GuildMember).displayAvatarURL() })
.setTitle(contracts[command].success.title)
.setDescription(stripIndents`${contracts[command].success.description.replace('{tokens}', tokens.toString())}
You can try again ${time(resetTime, 'R')}.
${resetTimesMessage}
${emojiGet('buttonBetSmall')} **Wallet:** ${personaData.tokens}`)
.setColor(contracts[command].success.color)],
components: [rowBounties],
Expand Down
Loading