CampFirePlay/commands/organization/payday.js
2024-12-21 09:34:53 +03:00

70 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const {
Telegraf
} = require('telegraf')
const bot = new Telegraf(process.env.BOT_TOKEN)
const {
UserModel,
WorldModel,
BusinessModel
} = require('/dev/Telegram/CampFireGameBot/config')
const {
spaces,
giveExp,
rand
} = require('/dev/Telegram/CampFireGameBot/utils')
module.exports = async (ctx) => {
let user = null
let business = await BusinessModel.findOne({
where: {
owner: ctx.from.id.toString()
}
})
let world = await WorldModel.findByPk(1)
if (business === null) return await ctx.reply(`У вас нет организации.`)
if (business.checks < 12) return await ctx.reply(`Недостаточно отработок для формирования выплаты.`)
let percentSum = 0
let text = ``
let percentErrorText = ``
let profit = 0
let piece = 0
let moneyList = rand(1000, 3000)
moneyList = moneyList*business.users.length
for (i = 0; i < business.users.length; i++) { // Summary percent
user = await UserModel.findByPk(business.users[i])
percentSum += Number(user.business.percent)
percentErrorText += ` ${user.username} ${user.business.percent}% [/percent ${user.username} [проценты]]\n`
}
if (percentSum > 100) return await ctx.reply(`⚠️ Общий процент всех сотрудников превышает 100%\n Остаток процентов перенесется в баланс организации.\n\n${percentErrorText}\n`)
for (x = 0; x < business.users.length; x++) { // Общая внесенная сумма всеми участниками
user = await UserModel.findByPk(business.users[x])
profit += user.business.checks * moneyList * user.level
text += ` ${user.username} отработал ${user.business.checks} раз.\n`
user.business = {
id: user.business.id,
checks: 0,
percent: user.business.percent
}
user.save()
}
text += ` 💹 Наработка за текущий период: ${profit}\n`
let profitCheck = profit
for (n = 0; n < business.users.length; n++) { // Процент выплаты участникам
user = await UserModel.findByPk(business.users[n])
piece = Math.round(profit / 100 * user.business.percent)
profitCheck -= piece
user.money += piece
text += ` ${user.username} получает ₽${piece}\n`
await bot.telegram.sendMessage(user.telegram_id, ` Директор организации сформировал выплаты.\n На ваш баланс поступило: ₽${spaces(piece)}`)
giveExp(user, business.checks)
user.save()
}
if (profitCheck > 0) {
business.balance += profitCheck
text += ` На баланс бизнеса поступило ₽${profitCheck}`
}
business.checks = 0
business.save()
console.log(`Payday: ${business.name}\n${text}`)
return await ctx.reply(text)
}