CampFirePlay-ItemsEditor/app.js
2025-01-22 00:09:31 +03:00

84 lines
2.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

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.

require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const sequelize = require('./db');
const { CommunityItem } = require('./models/item');
const path = require('path');
const app = express();
// Функция старта
const start = async () => {
try {
await sequelize.authenticate();
console.log('Connection has been established successfully.');
await sequelize.sync(); // Создание таблиц в базе данных
} catch (error) {
console.error('Unable to connect to the database:', error);
}
};
start();
// Настройки
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
// Мидлвары
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
// Главная страница с формой
app.get('/add-item', (req, res) => {
res.render('addItem');
});
// API для добавления предметов в базу данных
app.post('/add-item', async (req, res) => {
const {
text_id,
name,
description,
price,
rarity,
type,
duration,
canBeEquipped,
img,
dropChance,
effectType,
effectAmount
} = req.body;
// Формируем массив эффектов
const effectData = effectType.map((type, index) => ({
type,
amount: parseInt(effectAmount[index]),
}));
try {
await CommunityItem.create({
text_id,
name,
description,
effectData, // Массив эффектов
price: parseInt(price),
rarity: parseInt(rarity),
type,
duration: duration ? parseInt(duration) : null,
canBeEquipped: canBeEquipped === 'on',
img,
dropChance: parseFloat(dropChance),
});
res.redirect('/add-item'); // Перенаправляем обратно на страницу добавления
} catch (error) {
console.error(error);
res.status(500).send('Ошибка при добавлении предмета');
}
});
// Запуск сервера
const PORT = process.env.PORT || 3002;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});