Skip to content

Commit

Permalink
feat(*): added autoremove quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
aronbergman committed Jun 2, 2024
1 parent a83794e commit 4cc091b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
23 changes: 23 additions & 0 deletions bot/commands/admin/resetQuiz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { INITIAL_SESSION } from '../../constants/index.js'

export const resetQuiz = bot => {
bot.onText(/\/rq/, async msg => {
if (msg?.chat?.id == process.env.NOTIF_GROUP) {
const { id: chatId } = msg.chat
const msgId = msg.message_id
const { id } = msg.from
const options = {
parse_mode: 'HTML',
reply_to_message_id: msgId
}

try {
лучше созданять дату и время, когда у человека будет доступна следубщая игра
позже при открытии этим пользователем викторины я должен буду проверить эту дату
если эта дата будет
} catch (error) {
await bot.sendMessage(chatId, `${error.message}`, options)
}
}
})
}
17 changes: 12 additions & 5 deletions bot/commands/keyboard/quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import { getStringOrDist } from '../../utils/quiz/getStringOrDist.js'
import dotenv from 'dotenv'
import { ct } from '../../utils/createTranslate.js'
import { createNewQuizKeyboard } from '../../utils/quiz/createNewQuizKeyboard.js'
import { Sequelize } from 'sequelize'

dotenv.config()

const miniGames = ['🏀', '🏀', '🏀', '⚽', '⚽', '⚽', '🎳', '🎲', '🎯']

const getRebootDateForUser = (count) => {
if (count !== 0) return null;
return Sequelize.literal('CURRENT_TIMESTAMP')
}

export const keyboardQuiz = async (bot, msg, isDescription) => {
const t = await ct(msg)
let accountMessage
Expand All @@ -30,10 +36,9 @@ export const keyboardQuiz = async (bot, msg, isDescription) => {
where: {
chat_id: chatId
}
}).then(res => {
const keyboard = createNewQuizKeyboard(res, msgId, t)
}).then(async res => {
const keyboard = await createNewQuizKeyboard(res, msgId, t, chatId)
const timeout = setTimeout(async () => {
// TODO: Сделать подсчет колличества бесплатных запросов в сутки на бесплатном режиме
await bot.deleteMessage(chatId, accountMessage.message_id)
accountMessage = await bot.sendMessage(
chatId,
Expand Down Expand Up @@ -101,7 +106,8 @@ export const keyboardQuiz = async (bot, msg, isDescription) => {

await db.subscriber.update(
{
quiz_token_available: available
quiz_token_available: available,
quiz_reboot_date: getRebootDateForUser(available)
},
{ where: { chat_id: chatId } }
).then(res => {
Expand Down Expand Up @@ -202,7 +208,8 @@ export const keyboardQuiz = async (bot, msg, isDescription) => {

await db.subscriber.update(
{
quiz_subs_available: available
quiz_subs_available: available,
quiz_reboot_date: getRebootDateForUser(available)
},
{ where: { chat_id: chatId } }
).then(res => {
Expand Down
5 changes: 5 additions & 0 deletions bot/db/models/settings.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export default (sequelize, DataTypes) => {
type: DataTypes.DOUBLE,
required: true
},
days_freez: {
type: DataTypes.DOUBLE,
defaultValue: 7,
required: true
},
})

return SettingsSchema
Expand Down
3 changes: 3 additions & 0 deletions bot/db/models/subscriber.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export default (sequelize, DataTypes) => {
type: DataTypes.DOUBLE,
defaultValue: 0
},
quiz_reboot_date: {
type: DataTypes.DATE,
},
MI_count: {
type: DataTypes.DOUBLE,
defaultValue: 0
Expand Down
45 changes: 37 additions & 8 deletions bot/utils/quiz/createNewQuizKeyboard.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,53 @@
export const createNewQuizKeyboard = (res, chatId, t) => {
import { db } from '../../db/index.js'

export const createNewQuizKeyboard = async (res, msgId, t, chatId) => {
const keyboard = []
const keyboard2 = []
// проверить, если с даты блокировки прошло ровно 7 дней - обнулить
const dateFreezeQuiz = res.dataValues?.quiz_reboot_date
const today = new Date()
const daysHavePassed = (today - dateFreezeQuiz) / 1000 / 60 / 60 / 24
const VALUES = 5;
const { dataValues } = await db.settings.findOne({ where: { user_id: 0 } })

if ((res?.dataValues?.quiz_subs_available === 0 && res.dataValues?.quiz_token_available === 0)
&& (dataValues['days_freez'] - daysHavePassed) <= 0) {
db.subscriber.findOne(
{ where: { chat_id: chatId } }
).then(() => {

if (res?.dataValues?.quiz_subs_available === 0 && res.dataValues?.quiz_token_available === 0) {
db.subscriber.update(
{
quiz_reboot_date: null,
quiz_subs_available: VALUES,
quiz_token_available: VALUES
},
{ where: { chat_id: chatId } }
).then(() => {
keyboard.push({
text: `${t('btn_win_subs')} (${VALUES})`,
callback_data: `WIN_SUBS_${msgId}`
})
keyboard2.push({
text: `${t('btn_win_tokens')} (${VALUES})`,
callback_data: `WIN_REQ_${msgId}`
})
})
})
} else if ((dataValues['days_freez'] - daysHavePassed) >= 0) {
keyboard.push({ text: t('new_quiz_after_week'), callback_data: `NO_ATTEMPTS` })
} else {
if (res?.dataValues?.quiz_subs_available !== 0)
keyboard.push({
text: `${t('btn_win_subs')} (${res.dataValues.quiz_subs_available})`,
callback_data: `WIN_SUBS_${chatId}`
callback_data: `WIN_SUBS_${msgId}`
})
if (res?.dataValues?.quiz_token_available !== 0)
keyboard2.push({
text: `${t('btn_win_tokens')} (${res.dataValues.quiz_token_available})`,
callback_data: `WIN_REQ_${chatId}`
callback_data: `WIN_REQ_${msgId}`
})
}

return [
keyboard,
keyboard2
]
return [keyboard, keyboard2]
}

0 comments on commit 4cc091b

Please sign in to comment.