diff --git a/bot.js b/bot.js index c60e93c..6674d1a 100644 --- a/bot.js +++ b/bot.js @@ -646,7 +646,7 @@ bot.action('my_enterprises', async (ctx) => { if (warehouse) { enterpriseButtons.push({ text: `🔄 ID: ${id}`, callback_data: `transfer_from_${id}` }); } - + enterpriseButtons.push({ text: `🚫 ID: ${id}`, callback_data: `sell_enterprise_${id}` }); buttons.push(enterpriseButtons); } message += `\n\n📈 - Улучшить\n🛻 - Купить грузовик [~${utils.spaces(metallPrice.price * 700)} руб.]\n🚛 - Улучшить грузовик\n💰 - Продать ресурсы с предприятия\n🔄 - Перевезти все на склад` @@ -715,7 +715,7 @@ bot.action(/build_(wood|coal|oil|metall|gold|diamond)/, async (ctx) => { // Проверка, есть ли достаточно денег у пользователя if (user.money < price) { - return await ctx.telegram.answerCbQuery(ctx.callbackQuery.id, `У вас недостаточно средств для постройки предприятия. Необходимо ${price} руб.`, { show_alert: true }) + return await ctx.telegram.answerCbQuery(ctx.callbackQuery.id, `У вас недостаточно средств для постройки предприятия. Необходимо ${utils.spaces(price)} руб.`, { show_alert: true }) } // Строим предприятие @@ -735,6 +735,77 @@ bot.action(/build_(wood|coal|oil|metall|gold|diamond)/, async (ctx) => { return await ctx.editMessageText(`Предприятие ${enterprise.name} (ID: ${enterprise.id}) построено!`) }) +// Запрашиваем подтверждение на продажу предприятия +bot.action(/sell_enterprise_(\d+)/, async (ctx) => { + try { + const enterpriseId = parseInt(ctx.match[1], 10); + const enterprise = await EnterpriseModel.findByPk(enterpriseId); + if (!enterprise) { + return await ctx.reply('Предприятие не найдено.'); + } + + const user = await UserModel.findByPk(ctx.from.id); + if (enterprise.playerId !== user.telegram_id) { + return await ctx.reply('Это не ваше предприятие.'); + } + // Получаем информацию о наличии грузовика у предприятия и его уровень + const truck = await TruckModel.findOne({ where: { enterpriseId: enterprise.id } }); + const truckLevel = truck ? truck.level : 0; + const metallPrice = await ResourcePriceModel.findOne({ where: { resource: 'metall' } }); + const truckPrice = metallPrice.price * 700 * truckLevel; + // Получаем текущую стоимость предприятия + const resourcePrice = await ResourcePriceModel.findOne({ + where: { resource: enterprise.resourceType } + }); + const price = Math.round(resourcePrice.price * 1.5 * 5 * 24 * 7 * enterprise.level + truckPrice * 0.5); + return await ctx.reply(`Вы уверены, что хотите продать предприятие ${enterprise.name} за ${utils.spaces(price)} руб?`, Markup.inlineKeyboard([ + [{ text: '✅ Да', callback_data: `confirm_enterprise_${enterpriseId}` }], + [{ text: '❌ Нет', callback_data: 'enterprise_menu' }] + ]).resize()); + } catch (error) { + console.error('Ошибка при запросе на продажу предприятия:', error); + return await ctx.reply('Произошла ошибка при запросе на продажу предприятия. Попробуйте позже.'); + } +}); + +// Продажа предприятия +bot.action(/confirm_enterprise_(\d+)/, async (ctx) => { + try { + const enterpriseId = parseInt(ctx.match[1], 10); + const enterprise = await EnterpriseModel.findByPk(enterpriseId); + if (!enterprise) { + return await ctx.reply('Предприятие не найдено.'); + } + + const user = await UserModel.findByPk(ctx.from.id); + if (enterprise.playerId !== user.telegram_id) { + return await ctx.reply('Это не ваше предприятие.'); + } + // Получаем информацию о наличии грузовика у предприятия и его уровень + const truck = await TruckModel.findOne({ where: { enterpriseId: enterprise.id } }); + const truckLevel = truck ? truck.level : 1; + const metallPrice = await ResourcePriceModel.findOne({ where: { resource: 'metall' } }); + const truckPrice = metallPrice.price * 700 * truckLevel; + // Получаем текущую стоимость предприятия + const resourcePrice = await ResourcePriceModel.findOne({ + where: { resource: enterprise.resourceType } + }); + console.log() + const price = Math.round(resourcePrice.price * 1.5 * 5 * 24 * 7 * enterprise.level + truckPrice * 0.5); + + // Продаем предприятие + await enterprise.destroy(); + // Удаляем грузовик + await TruckModel.destroy({ where: { enterpriseId: enterprise.id } }); + await user.update({ money: user.money + price }); + + return await ctx.reply(`Предприятие ${enterprise.name} продано за ${utils.spaces(price)} руб.`); + } catch (error) { + console.error('Ошибка при продаже предприятия:', error); + return await ctx.reply('Произошла ошибка при продаже предприятия. Попробуйте позже.'); + } +}); + // Функция для преобразования типа ресурса в читаемое название function getReadableType(type) { const names = { diff --git a/json/logs.json b/json/logs.json index cc83e11..4d4c2e8 100644 --- a/json/logs.json +++ b/json/logs.json @@ -33278,3 +33278,223 @@ } } >>>>>>> Stashed changes +{ + "timestamp": "2025-01-18T22:42:44.340Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 90 + } +} +{ + "timestamp": "2025-01-18T22:42:47.966Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 80 + } +} +{ + "timestamp": "2025-01-18T22:42:51.498Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 70 + } +} +{ + "timestamp": "2025-01-18T22:42:54.918Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 60 + } +} +{ + "timestamp": "2025-01-18T22:43:02.439Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 50 + } +} +{ + "timestamp": "2025-01-18T22:43:05.177Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 40 + } +} +{ + "timestamp": "2025-01-18T22:43:11.658Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 30 + } +} +{ + "timestamp": "2025-01-18T22:43:12.630Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Сумка)", + "extraData": { + "baseChance": 20, + "chance": 38, + "randomRoll": 5, + "moneyIn": 23491 + } +} +{ + "timestamp": "2025-01-18T22:43:15.138Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 20 + } +} +{ + "timestamp": "2025-01-18T22:43:21.650Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 10 + } +} +{ + "timestamp": "2025-01-18T22:43:22.289Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Сумка)", + "extraData": { + "baseChance": 20, + "chance": 38, + "randomRoll": 10, + "moneyIn": 4773 + } +} +{ + "timestamp": "2025-01-18T22:43:24.577Z", + "user": { + "id": 805814188, + "username": "Mana_Terroristo", + "first_name": "Егор", + "last_name": "" + }, + "chat": { + "id": 805814188, + "type": "private" + }, + "action": "Карманная кража (Start)", + "extraData": { + "cdBefore": 1697887440, + "stamina": 0 + } +} diff --git a/models/truck.model.js b/models/truck.model.js index fbb7b49..99454c8 100644 --- a/models/truck.model.js +++ b/models/truck.model.js @@ -3,6 +3,7 @@ const {DataTypes} = require('sequelize'); const Truck = sequelize.define('truck', { enterpriseId: DataTypes.INTEGER, + level: DataTypes.INTEGER, // Уровень транспорта capacity: DataTypes.INTEGER, // Сколько может перевезти за час efficiency: DataTypes.INTEGER // Частота транспортировки (количество операций в час) })