29 lines
1.7 KiB
JavaScript
29 lines
1.7 KiB
JavaScript
const {
|
||
UserModel,
|
||
WorldModel,
|
||
BusinessModel
|
||
} = require('/workspace/degradin/Dev/Telegram/CampFire Play/config')
|
||
|
||
module.exports = async (ctx) => {
|
||
ctx.args = ctx.update.message.text.split(' ')
|
||
let user = await UserModel.findByPk(ctx.from.id)
|
||
if (user.business.id == 0) return await ctx.reply(`🚫 Вы не владеете организацией.`)
|
||
let world = await WorldModel.findByPk(1)
|
||
let business = await BusinessModel.findOne({
|
||
where: {
|
||
owner: ctx.from.id.toString()
|
||
}
|
||
})
|
||
if (business === null) return await ctx.reply(`🚫 У вас нет организации.`)
|
||
if (!ctx.args[1]) return ctx.reply(`🚫 Не указан аргумент.`)
|
||
if (!Number(ctx.args[1])) return ctx.reply(`ℹ️ Количество должно быть числом.`)
|
||
if (ctx.args[1] < 1) return ctx.reply(`ℹ️ Минимальное количество для покупки - 1`)
|
||
if (ctx.args[1]*world.matPrice > business.balance) return ctx.reply(`🚫 Недостаточно средств на балансе организации.`)
|
||
if (business.materials >= 700) return ctx.reply(`🚫 Склады полны.`)
|
||
if (business.materials + Number(ctx.args[1]) > 700) return ctx.reply(`🚫 Нет столько места на складах.\n Можно закупить еще ${700 - business.materials}`)
|
||
let count = Number(ctx.args[1])
|
||
business.balance -= world.matPrice*count
|
||
business.materials += count
|
||
business.save()
|
||
return await ctx.reply(`➕ Закуплено ${ctx.args[1]} материалов для организации на сумму ₽${world.matPrice*count}`)
|
||
} |