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

View File

@@ -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.');