From fcbe008a5cfaea6520ead7101335cac216c60f0d Mon Sep 17 00:00:00 2001 From: b3ni15 Date: Sat, 20 Dec 2025 23:10:03 +0100 Subject: [PATCH] Refactor betting logic and add error handling in Table class; implement async turn advancement --- .gitignore | 5 +++++ src/game/tables.js | 22 +++++++++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5acce99 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.env +node_modules +dist +.DS_Store +.env.local \ No newline at end of file diff --git a/src/game/tables.js b/src/game/tables.js index 14848b2..49496a5 100644 --- a/src/game/tables.js +++ b/src/game/tables.js @@ -158,7 +158,7 @@ class Table { }; if (this.currentSeatIndex === index) { - this.advanceTurn(); + await this.advanceTurn(); } if (this.seats.every((seat) => !seat.userId)) { @@ -169,7 +169,7 @@ class Table { } async placeBet(userId, amount) { - if (this.phase === 'playing') { + if (!['waiting', 'betting'].includes(this.phase)) { throw new Error('A kor mar fut, varj a kovetkezo korig.'); } @@ -182,6 +182,11 @@ class Table { throw new Error('Nem ulsz az asztalnal.'); } + const seat = this.seats[index]; + if (seat.bet > 0) { + throw new Error('Mar van teted erre a korre.'); + } + const balanceRows = await query('SELECT balance FROM users WHERE id = ?', [userId]); const balance = balanceRows[0]?.balance ?? 0; if (balance < amount) { @@ -192,7 +197,6 @@ class Table { const updatedBalanceRows = await query('SELECT balance FROM users WHERE id = ?', [userId]); const updatedBalance = updatedBalanceRows[0]?.balance ?? 0; - const seat = this.seats[index]; seat.bet = amount; seat.ready = false; seat.status = 'bet'; @@ -218,6 +222,10 @@ class Table { throw new Error('Eloszor tegyel tetet.'); } + if (this.phase !== 'betting') { + throw new Error('A kor mar elindult.'); + } + seat.ready = true; this.broadcastState(); this.scheduleRound(); @@ -298,14 +306,14 @@ class Table { const value = handValue(seat.hand).total; if (value > 21) { seat.status = 'bust'; - this.advanceTurn(); + await this.advanceTurn(); } else if (value === 21) { seat.status = 'stood'; - this.advanceTurn(); + await this.advanceTurn(); } } else if (action === 'stand') { seat.status = 'stood'; - this.advanceTurn(); + await this.advanceTurn(); } else if (action === 'double') { if (seat.hand.length !== 2) { throw new Error('Duplazni csak az elso ket lap utan lehet.'); @@ -323,7 +331,7 @@ class Table { seat.bet += seat.bet; seat.hand.push(draw(this.deck)); seat.status = 'stood'; - this.advanceTurn(); + await this.advanceTurn(); this.broadcast({ type: 'balance', balance: updatedBalance }, userId); } else { throw new Error('Ismeretlen akcio.');