Refactor betting logic and add error handling in Table class; implement async turn advancement

This commit is contained in:
2025-12-20 23:10:03 +01:00
parent 32db9278e4
commit fcbe008a5c
2 changed files with 20 additions and 7 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
.env
node_modules
dist
.DS_Store
.env.local

View File

@@ -158,7 +158,7 @@ class Table {
}; };
if (this.currentSeatIndex === index) { if (this.currentSeatIndex === index) {
this.advanceTurn(); await this.advanceTurn();
} }
if (this.seats.every((seat) => !seat.userId)) { if (this.seats.every((seat) => !seat.userId)) {
@@ -169,7 +169,7 @@ class Table {
} }
async placeBet(userId, amount) { 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.'); throw new Error('A kor mar fut, varj a kovetkezo korig.');
} }
@@ -182,6 +182,11 @@ class Table {
throw new Error('Nem ulsz az asztalnal.'); 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 balanceRows = await query('SELECT balance FROM users WHERE id = ?', [userId]);
const balance = balanceRows[0]?.balance ?? 0; const balance = balanceRows[0]?.balance ?? 0;
if (balance < amount) { if (balance < amount) {
@@ -192,7 +197,6 @@ class Table {
const updatedBalanceRows = await query('SELECT balance FROM users WHERE id = ?', [userId]); const updatedBalanceRows = await query('SELECT balance FROM users WHERE id = ?', [userId]);
const updatedBalance = updatedBalanceRows[0]?.balance ?? 0; const updatedBalance = updatedBalanceRows[0]?.balance ?? 0;
const seat = this.seats[index];
seat.bet = amount; seat.bet = amount;
seat.ready = false; seat.ready = false;
seat.status = 'bet'; seat.status = 'bet';
@@ -218,6 +222,10 @@ class Table {
throw new Error('Eloszor tegyel tetet.'); throw new Error('Eloszor tegyel tetet.');
} }
if (this.phase !== 'betting') {
throw new Error('A kor mar elindult.');
}
seat.ready = true; seat.ready = true;
this.broadcastState(); this.broadcastState();
this.scheduleRound(); this.scheduleRound();
@@ -298,14 +306,14 @@ class Table {
const value = handValue(seat.hand).total; const value = handValue(seat.hand).total;
if (value > 21) { if (value > 21) {
seat.status = 'bust'; seat.status = 'bust';
this.advanceTurn(); await this.advanceTurn();
} else if (value === 21) { } else if (value === 21) {
seat.status = 'stood'; seat.status = 'stood';
this.advanceTurn(); await this.advanceTurn();
} }
} else if (action === 'stand') { } else if (action === 'stand') {
seat.status = 'stood'; seat.status = 'stood';
this.advanceTurn(); await this.advanceTurn();
} else if (action === 'double') { } else if (action === 'double') {
if (seat.hand.length !== 2) { if (seat.hand.length !== 2) {
throw new Error('Duplazni csak az elso ket lap utan lehet.'); throw new Error('Duplazni csak az elso ket lap utan lehet.');
@@ -323,7 +331,7 @@ class Table {
seat.bet += seat.bet; seat.bet += seat.bet;
seat.hand.push(draw(this.deck)); seat.hand.push(draw(this.deck));
seat.status = 'stood'; seat.status = 'stood';
this.advanceTurn(); await this.advanceTurn();
this.broadcast({ type: 'balance', balance: updatedBalance }, userId); this.broadcast({ type: 'balance', balance: updatedBalance }, userId);
} else { } else {
throw new Error('Ismeretlen akcio.'); throw new Error('Ismeretlen akcio.');