Compare commits

...

17 Commits

Author SHA1 Message Date
438bb4c8a3 Add roundStartsAt property to Table class and update state management 2025-12-21 02:54:09 +01:00
97ed3c4d9e Add promo code functionality to deposits and enhance wallet routes 2025-12-21 00:41:39 +01:00
3861b4477d Update deposit intent to log stripeAmount and use it for payment creation 2025-12-21 00:32:36 +01:00
0fe2781a11 Add logging for Stripe deposit requests and round amount input 2025-12-21 00:29:33 +01:00
be7b0c48e3 Refactor MIN_DEPOSIT calculation to ensure it respects environment variable settings 2025-12-21 00:24:56 +01:00
3331c31d22 Update wallet route to use environment variables for deposit limits 2025-12-21 00:21:06 +01:00
20efdb5dc2 Add ensureStripeConfigured function and improve error handling in wallet routes 2025-12-21 00:16:56 +01:00
50a1631222 Ensure dotenv is imported for environment variable configuration 2025-12-21 00:10:42 +01:00
d67c8b5ac1 Add logging for database connection status and host 2025-12-21 00:09:28 +01:00
683eabecdd Remove redundant dotenv import from index.js 2025-12-21 00:08:42 +01:00
16cba64d03 Refactor code structure for improved readability and maintainability 2025-12-21 00:08:25 +01:00
89ef1d6e8b Add logging for SQL execution and database host in query function 2025-12-21 00:06:23 +01:00
927a1b75c6 Refactor dotenv configuration to load environment variables from multiple candidates; add error handling for missing DB_HOST 2025-12-21 00:04:02 +01:00
0aa2ae1637 Update dotenv configuration to specify environment file path 2025-12-21 00:01:41 +01:00
f638c4709b Refactor database connection logic; ensure DB is connected before starting the server 2025-12-21 00:00:46 +01:00
e97cede55c Improve error handling in Discord auth callback; return JSON error response in non-production environments 2025-12-20 23:48:34 +01:00
6e0a97df53 Add turn timer functionality to Table class; update environment configuration 2025-12-20 23:33:55 +01:00
9 changed files with 1324 additions and 16 deletions

View File

@@ -20,3 +20,6 @@ MIN_BET=10
MAX_BET=500
ROUND_START_DELAY_MS=3000
ROUND_RESET_DELAY_MS=5000
TURN_TIME_MS=15000
MIN_DEPOSIT=175
MAX_DEPOSIT=1000

View File

@@ -10,7 +10,7 @@
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"dotenv": "^16.6.1",
"express": "^4.19.2",
"helmet": "^7.1.0",
"jsonwebtoken": "^9.0.2",

1096
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -8,15 +8,42 @@ CREATE TABLE IF NOT EXISTS users (
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS promo_codes (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
code VARCHAR(32) NOT NULL UNIQUE,
bonus_type VARCHAR(16) NOT NULL DEFAULT 'fixed',
bonus_value INT NOT NULL DEFAULT 0,
max_uses INT NULL,
expires_at DATETIME NULL,
active TINYINT(1) NOT NULL DEFAULT 1,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS deposits (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
amount INT NOT NULL,
bonus_amount INT NOT NULL DEFAULT 0,
promo_code_id BIGINT UNSIGNED NULL,
stripe_payment_intent_id VARCHAR(128) NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'created',
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT fk_deposits_user_id FOREIGN KEY (user_id) REFERENCES users(id)
CONSTRAINT fk_deposits_user_id FOREIGN KEY (user_id) REFERENCES users(id),
CONSTRAINT fk_deposits_promo_code_id FOREIGN KEY (promo_code_id) REFERENCES promo_codes(id)
);
CREATE INDEX idx_deposits_stripe ON deposits (stripe_payment_intent_id);
CREATE TABLE IF NOT EXISTS promo_redemptions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT UNSIGNED NOT NULL,
promo_code_id BIGINT UNSIGNED NOT NULL,
deposit_id BIGINT UNSIGNED NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT fk_redemptions_user_id FOREIGN KEY (user_id) REFERENCES users(id),
CONSTRAINT fk_redemptions_promo_code_id FOREIGN KEY (promo_code_id) REFERENCES promo_codes(id),
CONSTRAINT fk_redemptions_deposit_id FOREIGN KEY (deposit_id) REFERENCES deposits(id),
UNIQUE KEY uniq_user_promo (user_id, promo_code_id)
);

View File

@@ -1,3 +1,4 @@
import 'dotenv/config';
import mysql from 'mysql2/promise';
const pool = mysql.createPool({
@@ -12,6 +13,9 @@ const pool = mysql.createPool({
});
export async function query(sql, params = []) {
console.log('Executing SQL:', sql, 'with params:', params);
console.log('Database Host:', process.env.DB_HOST);
const [rows] = await pool.execute(sql, params);
return rows;
}

View File

@@ -5,6 +5,7 @@ const MIN_BET = Number(process.env.MIN_BET || 10);
const MAX_BET = Number(process.env.MAX_BET || 100);
const ROUND_START_DELAY_MS = Number(process.env.ROUND_START_DELAY_MS || 3000);
const ROUND_RESET_DELAY_MS = Number(process.env.ROUND_RESET_DELAY_MS || 5000);
const TURN_TIME_MS = Number(process.env.TURN_TIME_MS || 15000);
class Table {
constructor(id, seatCount) {
@@ -26,6 +27,10 @@ class Table {
this.roundId = 0;
this.roundTimeout = null;
this.resetTimeout = null;
this.turnTimeout = null;
this.turnEndsAt = null;
this.turnCounter = 0;
this.roundStartsAt = null;
this.clients = new Set();
}
@@ -55,6 +60,8 @@ class Table {
minBet: MIN_BET,
maxBet: MAX_BET,
roundId: this.roundId,
turnEndsAt: this.turnEndsAt,
roundStartsAt: this.roundStartsAt,
dealerHand,
currentSeatIndex: this.currentSeatIndex,
seats: this.seats.map((seat, index) => ({
@@ -103,6 +110,36 @@ class Table {
}
}
clearTurnTimer() {
if (this.turnTimeout) {
clearTimeout(this.turnTimeout);
}
this.turnTimeout = null;
this.turnEndsAt = null;
}
startTurnTimer() {
this.clearTurnTimer();
if (this.currentSeatIndex === null) {
return;
}
this.turnEndsAt = Date.now() + TURN_TIME_MS;
const turnToken = ++this.turnCounter;
this.turnTimeout = setTimeout(() => {
if (this.turnCounter !== turnToken) {
return;
}
const seat = this.seats[this.currentSeatIndex];
if (!seat || seat.status !== 'playing') {
return;
}
seat.status = 'stood';
void this.advanceTurn();
}, TURN_TIME_MS);
}
findSeatIndexByUser(userId) {
return this.seats.findIndex((seat) => seat.userId === userId);
}
@@ -236,10 +273,14 @@ class Table {
return;
}
this.roundStartsAt = Date.now() + ROUND_START_DELAY_MS;
this.broadcastState();
this.roundTimeout = setTimeout(() => {
this.roundTimeout = null;
const hasBets = this.seats.some((seat) => seat.bet > 0);
if (!hasBets) {
this.roundStartsAt = null;
this.broadcastState();
return;
}
void this.startRound();
@@ -251,6 +292,8 @@ class Table {
return;
}
this.clearTurnTimer();
this.roundStartsAt = null;
this.phase = 'playing';
this.roundId += 1;
this.deck = shuffle(createDeck());
@@ -272,6 +315,7 @@ class Table {
return;
}
this.startTurnTimer();
this.broadcastState();
}
@@ -310,6 +354,8 @@ class Table {
} else if (value === 21) {
seat.status = 'stood';
await this.advanceTurn();
} else {
this.startTurnTimer();
}
} else if (action === 'stand') {
seat.status = 'stood';
@@ -343,15 +389,18 @@ class Table {
async advanceTurn() {
const nextIndex = this.nextPlayableSeatIndex(this.currentSeatIndex ?? -1);
if (nextIndex === null) {
this.clearTurnTimer();
await this.dealerTurn();
return;
}
this.currentSeatIndex = nextIndex;
this.startTurnTimer();
this.broadcastState();
}
async dealerTurn() {
this.clearTurnTimer();
this.phase = 'dealer';
this.currentSeatIndex = null;
@@ -423,6 +472,8 @@ class Table {
}
resetRound() {
this.clearTurnTimer();
this.roundStartsAt = null;
for (const seat of this.seats) {
if (!seat.userId) {
continue;
@@ -441,7 +492,7 @@ class Table {
}
}
const tables = Array.from({ length: 3 }, (_, index) => new Table(index + 1, 7));
const tables = Array.from({ length: 3 }, (_, index) => new Table(index + 1, 4));
export function listTables() {
return tables.map((table) => table.snapshot());

View File

@@ -8,8 +8,8 @@ import { WebSocketServer } from 'ws';
import authRoutes from './routes/auth.js';
import lobbyRoutes from './routes/lobby.js';
import walletRoutes from './routes/wallet.js';
import pool from './db.js';
import { setupWebSocket } from './ws.js';
dotenv.config();
const app = express();
@@ -31,7 +31,28 @@ const wss = new WebSocketServer({ server });
setupWebSocket(wss);
const PORT = Number(process.env.PORT || 4000);
server.listen(PORT, () => {
async function ensureDbConnected() {
try {
console.log('Ellenorizzuk az adatbazis kapcsolatot...');
console.log('HOST:', process.env.DB_HOST);
const connection = await pool.getConnection();
await connection.ping();
connection.release();
} catch (err) {
// eslint-disable-next-line no-console
console.error('DB kapcsolat hiba inditas kozben:', err);
process.exit(1);
}
}
async function start() {
await ensureDbConnected();
server.listen(PORT, () => {
// eslint-disable-next-line no-console
console.log(`Backend fut: http://localhost:${PORT}`);
});
});
}
start();

View File

@@ -73,6 +73,11 @@ router.get('/auth/discord/callback', async (req, res) => {
return res.json({ token });
} catch (err) {
console.error('Discord auth hiba:', err);
const message = err?.message || 'Hiba a bejelentkezes kozben.';
if (process.env.NODE_ENV !== 'production') {
return res.status(500).json({ error: message });
}
return res.status(500).send('Hiba a bejelentkezes kozben.');
}
});

View File

@@ -7,16 +7,91 @@ const router = Router();
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY || '', {
apiVersion: '2024-06-20'
});
const STRIPE_MIN_HUF = 175;
const MIN_DEPOSIT = Math.max(Number(process.env.MIN_DEPOSIT || STRIPE_MIN_HUF), STRIPE_MIN_HUF);
const MAX_DEPOSIT = Number(process.env.MAX_DEPOSIT || 1000);
function ensureStripeConfigured(res) {
if (!process.env.STRIPE_SECRET_KEY) {
res.status(500).json({ error: 'Stripe nincs beallitva.' });
return false;
}
return true;
}
async function getPromoCode(code) {
const rows = await query(
'SELECT id, code, bonus_type, bonus_value, max_uses, expires_at, active FROM promo_codes WHERE code = ? AND active = 1 AND (expires_at IS NULL OR expires_at > NOW())',
[code]
);
return rows[0];
}
function calcBonusAmount(amount, promo) {
if (!promo) {
return 0;
}
if (promo.bonus_type === 'percent') {
return Math.max(0, Math.floor(amount * Number(promo.bonus_value) / 100));
}
if (promo.bonus_type === 'fixed') {
return Math.max(0, Number(promo.bonus_value));
}
return 0;
}
router.post('/api/wallet/deposit-intent', authMiddleware, async (req, res) => {
try {
const amount = Number(req.body.amount);
if (!Number.isFinite(amount) || amount < 50 || amount > 100) {
return res.status(400).json({ error: 'A feltoltes 50 es 100 Ft kozott lehet.' });
if (!ensureStripeConfigured(res)) {
return;
}
const rawAmount = req.body.amount;
const amount = Math.round(Number(rawAmount));
const stripeAmount = Math.round(amount * 100);
if (process.env.NODE_ENV !== 'production') {
console.log('Stripe deposit request:', { rawAmount, amount, stripeAmount, min: MIN_DEPOSIT, max: MAX_DEPOSIT });
}
if (!Number.isFinite(amount) || amount < MIN_DEPOSIT || amount > MAX_DEPOSIT) {
return res.status(400).json({ error: `A feltoltes ${MIN_DEPOSIT} es ${MAX_DEPOSIT} Ft kozott lehet.` });
}
const promoInput = req.body.promoCode?.toString().trim().toUpperCase();
let promo = null;
let bonusAmount = 0;
let promoCodeId = null;
if (promoInput) {
promo = await getPromoCode(promoInput);
if (!promo) {
return res.status(400).json({ error: 'Ervenytelen promo kod.' });
}
const redeemed = await query(
'SELECT id FROM promo_redemptions WHERE user_id = ? AND promo_code_id = ? LIMIT 1',
[req.userId, promo.id]
);
if (redeemed.length > 0) {
return res.status(400).json({ error: 'A promo kod mar felhasznalva.' });
}
if (promo.max_uses) {
const usage = await query(
'SELECT COUNT(*) AS count FROM promo_redemptions WHERE promo_code_id = ?',
[promo.id]
);
if ((usage[0]?.count ?? 0) >= promo.max_uses) {
return res.status(400).json({ error: 'A promo kod mar elfogyott.' });
}
}
bonusAmount = calcBonusAmount(amount, promo);
promoCodeId = promo.id;
}
const paymentIntent = await stripe.paymentIntents.create({
amount,
amount: stripeAmount,
currency: 'huf',
automatic_payment_methods: { enabled: true },
metadata: {
@@ -25,25 +100,33 @@ router.post('/api/wallet/deposit-intent', authMiddleware, async (req, res) => {
});
await query(
'INSERT INTO deposits (user_id, amount, stripe_payment_intent_id, status) VALUES (?, ?, ?, ?)',
[req.userId, amount, paymentIntent.id, 'created']
'INSERT INTO deposits (user_id, amount, bonus_amount, promo_code_id, stripe_payment_intent_id, status) VALUES (?, ?, ?, ?, ?, ?)',
[req.userId, amount, bonusAmount, promoCodeId, paymentIntent.id, 'created']
);
return res.json({ clientSecret: paymentIntent.client_secret, paymentIntentId: paymentIntent.id });
} catch (err) {
console.error('Stripe fizetes hiba:', err);
if (process.env.NODE_ENV !== 'production') {
return res.status(500).json({ error: err.message || 'Nem sikerult letrehozni a fizetest.' });
}
return res.status(500).json({ error: 'Nem sikerult letrehozni a fizetest.' });
}
});
router.post('/api/wallet/confirm', authMiddleware, async (req, res) => {
try {
if (!ensureStripeConfigured(res)) {
return;
}
const paymentIntentId = req.body.paymentIntentId?.toString();
if (!paymentIntentId) {
return res.status(400).json({ error: 'Hianyzo paymentIntentId.' });
}
const rows = await query(
'SELECT id, status, amount FROM deposits WHERE user_id = ? AND stripe_payment_intent_id = ?',
'SELECT id, status, amount, bonus_amount, promo_code_id FROM deposits WHERE user_id = ? AND stripe_payment_intent_id = ?',
[req.userId, paymentIntentId]
);
const deposit = rows[0];
@@ -61,13 +144,31 @@ router.post('/api/wallet/confirm', authMiddleware, async (req, res) => {
'UPDATE deposits SET status = ? WHERE id = ?',
['succeeded', deposit.id]
);
await query('UPDATE users SET balance = balance + ? WHERE id = ?', [deposit.amount, req.userId]);
const totalCredit = Number(deposit.amount) + Number(deposit.bonus_amount || 0);
await query('UPDATE users SET balance = balance + ? WHERE id = ?', [totalCredit, req.userId]);
if (deposit.promo_code_id) {
const already = await query(
'SELECT id FROM promo_redemptions WHERE user_id = ? AND promo_code_id = ? LIMIT 1',
[req.userId, deposit.promo_code_id]
);
if (already.length === 0) {
await query(
'INSERT INTO promo_redemptions (user_id, promo_code_id, deposit_id) VALUES (?, ?, ?)',
[req.userId, deposit.promo_code_id, deposit.id]
);
}
}
}
const balanceRows = await query('SELECT balance FROM users WHERE id = ?', [req.userId]);
const balance = balanceRows[0]?.balance ?? 0;
return res.json({ status: 'succeeded', balance });
} catch (err) {
console.error('Stripe confirm hiba:', err);
if (process.env.NODE_ENV !== 'production') {
return res.status(500).json({ error: err.message || 'Nem sikerult a befizetes ellenorzese.' });
}
return res.status(500).json({ error: 'Nem sikerult a befizetes ellenorzese.' });
}
});