feat: daily/weekly/monthly rewards

This commit is contained in:
DaInfLoop 2024-06-22 19:45:37 +01:00
parent 5f3ff1c3bf
commit 4850b94bcd

165
index.js
View file

@ -24,6 +24,7 @@ async function initializeUser(slackUserId) {
if (a.length === 0) { if (a.length === 0) {
a = await sql`INSERT INTO users (slack_id) VALUES (${slackUserId}) RETURNING *;` a = await sql`INSERT INTO users (slack_id) VALUES (${slackUserId}) RETURNING *;`
await sql`INSERT INTO cooldowns (slack_id) VALUES (${slackUserId});`
} }
return a[0]; return a[0];
@ -982,6 +983,170 @@ app.command('/viewopponents', async (ctx) => {
} }
}); });
function getTimeDifference(date1, date2) {
// Ensure both dates are Date objects
const start = new Date(date1);
const end = new Date(date2);
// Calculate the difference in milliseconds
const diff = Math.abs(end - start);
// Calculate the difference in weeks, days, hours, minutes, and seconds
const weeks = Math.floor(diff / (1000 * 60 * 60 * 24 * 7));
const days = Math.floor(diff / (1000 * 60 * 60 * 24)) % 7;
const hours = Math.floor(diff / (1000 * 60 * 60)) % 24;
const minutes = Math.floor(diff / (1000 * 60)) % 60;
const seconds = Math.floor(diff / 1000) % 60;
// Determine which unit to use for the output
if (weeks > 0) {
return `${weeks} Week${weeks > 1 ? 's' : ''}`;
} else if (days > 0) {
return `${days} Day${days > 1 ? 's' : ''}`;
} else if (hours > 0) {
return `${hours} Hour${hours > 1 ? 's' : ''}`;
} else if (minutes > 0) {
return `${minutes} Minute${minutes > 1 ? 's' : ''}`;
} else {
return `${seconds} Second${seconds > 1 ? 's' : ''}`;
}
}
app.command('/b-daily', async (ctx) => {
await ctx.ack();
const [ cooldown ] = await sql`SELECT * FROM cooldowns WHERE slack_id = ${ctx.context.userId};`
const user = await initializeUser(ctx.context.userId)
const slackUser = (await ctx.client.users.info({ user: ctx.context.userId })).user.profile;
const now = new Date();
if (!cooldown.daily || (cooldown.daily.getTime() <= now.getTime())) {
const cshards = Math.floor(Math.random() * 10) + 1;
const spoints = Math.floor(Math.random() * 5) + 1;
ctx.respond({
response_type: 'ephemeral',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Battle Master:* Oh ${slackUser.display_name_normalized}, apologies for the wait. Here is your reward for today.\n\n> ${cshards} Creation Shards\n> ${spoints} Skill Points`
}
}
]
})
await sql`UPDATE users SET cshards = ${user.cshards + cshards}, spoints = ${user.spoints + spoints} WHERE slack_id = ${ctx.context.userId};`
await sql`UPDATE cooldowns SET daily = ${new Date(now.getTime() + 24 * 60 * 60 * 1000)};`
} else {
ctx.respond({
response_type: 'ephemeral',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Battle Master:* Apologies battler. Unfortunately you must wait *${getTimeDifference(now, cooldown.daily)}* before you can collect your next daily reward.`
}
}
]
})
}
})
app.command('/b-weekly', async (ctx) => {
await ctx.ack();
const [ cooldown ] = await sql`SELECT * FROM cooldowns WHERE slack_id = ${ctx.context.userId};`
const user = await initializeUser(ctx.context.userId)
const slackUser = (await ctx.client.users.info({ user: ctx.context.userId })).user.profile;
const now = new Date();
if (!cooldown.weekly || (cooldown.weekly.getTime() <= now.getTime())) {
const cshards = Math.floor(Math.random() * 20) + 1;
const dshards = Math.floor(Math.random() * 15) + 1;
const spoints = Math.floor(Math.random() * 10) + 1;
ctx.respond({
response_type: 'ephemeral',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Battle Master:* It's that time again ${slackUser.display_name_normalized}. Enjoy your weekly reward.\n\n> ${cshards} Creation Shards\n> ${dshards} Destruction Shards\n> ${spoints} Skill Points`
}
}
]
})
await sql`UPDATE users SET cshards = ${user.cshards + cshards}, dshards = ${user.dshards + dshards}, spoints = ${user.spoints + spoints} WHERE slack_id = ${ctx.context.userId};`
await sql`UPDATE cooldowns SET weekly = ${new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000)};`
} else {
ctx.respond({
response_type: 'ephemeral',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Battle Master:* Apologies battler. Unfortunately you must wait *${getTimeDifference(now, cooldown.weekly)}* before you can collect your next weekly reward.`
}
}
]
})
}
})
app.command('/b-monthly', async (ctx) => {
await ctx.ack();
const [ cooldown ] = await sql`SELECT * FROM cooldowns WHERE slack_id = ${ctx.context.userId};`
const user = await initializeUser(ctx.context.userId)
const slackUser = (await ctx.client.users.info({ user: ctx.context.userId })).user.profile;
const now = new Date();
if (!cooldown.monthly || (cooldown.monthly.getTime() <= now.getTime())) {
const cshards = Math.floor(Math.random() * 30) + 1;
const dshards = Math.floor(Math.random() * 25) + 1;
const spoints = Math.floor(Math.random() * 15) + 1;
ctx.respond({
response_type: 'ephemeral',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Battle Master:* Another month, another reward. You have my appreciation ${slackUser.display_name_normalized}.\n\n> ${cshards} Creation Shards\n> ${dshards} Destruction Shards\n> ${spoints} Skill Points`
}
}
]
})
await sql`UPDATE users SET cshards = ${user.cshards + cshards}, dshards = ${user.dshards + dshards}, spoints = ${user.spoints + spoints} WHERE slack_id = ${ctx.context.userId};`
await sql`UPDATE cooldowns SET monthly = ${new Date(now.getTime() + 30 * 7 * 24 * 60 * 60 * 1000)};`
} else {
ctx.respond({
response_type: 'ephemeral',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Battle Master:* Apologies battler. Unfortunately you must wait *${getTimeDifference(now, cooldown.monthly)}* before you can collect your next monthly reward.`
}
}
]
})
}
})
; (async () => { ; (async () => {
await app.start(process.env.PORT); await app.start(process.env.PORT);