Add roundStartsAt property to Table class and update state management

This commit is contained in:
2025-12-21 02:54:09 +01:00
parent 97ed3c4d9e
commit 438bb4c8a3

View File

@@ -30,6 +30,7 @@ class Table {
this.turnTimeout = null; this.turnTimeout = null;
this.turnEndsAt = null; this.turnEndsAt = null;
this.turnCounter = 0; this.turnCounter = 0;
this.roundStartsAt = null;
this.clients = new Set(); this.clients = new Set();
} }
@@ -60,6 +61,7 @@ class Table {
maxBet: MAX_BET, maxBet: MAX_BET,
roundId: this.roundId, roundId: this.roundId,
turnEndsAt: this.turnEndsAt, turnEndsAt: this.turnEndsAt,
roundStartsAt: this.roundStartsAt,
dealerHand, dealerHand,
currentSeatIndex: this.currentSeatIndex, currentSeatIndex: this.currentSeatIndex,
seats: this.seats.map((seat, index) => ({ seats: this.seats.map((seat, index) => ({
@@ -271,10 +273,14 @@ class Table {
return; return;
} }
this.roundStartsAt = Date.now() + ROUND_START_DELAY_MS;
this.broadcastState();
this.roundTimeout = setTimeout(() => { this.roundTimeout = setTimeout(() => {
this.roundTimeout = null; this.roundTimeout = null;
const hasBets = this.seats.some((seat) => seat.bet > 0); const hasBets = this.seats.some((seat) => seat.bet > 0);
if (!hasBets) { if (!hasBets) {
this.roundStartsAt = null;
this.broadcastState();
return; return;
} }
void this.startRound(); void this.startRound();
@@ -287,6 +293,7 @@ class Table {
} }
this.clearTurnTimer(); this.clearTurnTimer();
this.roundStartsAt = null;
this.phase = 'playing'; this.phase = 'playing';
this.roundId += 1; this.roundId += 1;
this.deck = shuffle(createDeck()); this.deck = shuffle(createDeck());
@@ -466,6 +473,7 @@ class Table {
resetRound() { resetRound() {
this.clearTurnTimer(); this.clearTurnTimer();
this.roundStartsAt = null;
for (const seat of this.seats) { for (const seat of this.seats) {
if (!seat.userId) { if (!seat.userId) {
continue; continue;
@@ -484,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() { export function listTables() {
return tables.map((table) => table.snapshot()); return tables.map((table) => table.snapshot());