Implement Texas Hold'em play: dealing, betting, side pots, showdown

- Hand engine (server/hand.js): crypto-shuffled deck kept server-side,
  blinds (heads-up button = SB), min-raise rules incl. incomplete all-in,
  uncalled bet refunds, side pots, split pots, all-in runout
- Hand evaluator (server/cards.js) and node:test suite (npm test)
- Table flow: hands auto-continue after host START, 30s action clock
  (timeout = check/fold + sit out), sit out / sit in, rebuy, mid-hand leave
  folds and keeps the seat reserved until the hand ends, pause freezes all clocks
- Only each player's own hole cards are sent until a showdown/runout reveal;
  all actions validated server-side
- Play UI: board, hole cards, bets, action tags, turn timer, raise panel with
  presets, showdown results with winning cards highlighted, compact 7+ layout
- Remove the host crown and the temporary "next phase" control

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
agomizi1cmandClaude Opus 5.5 committed 2026-10-15 17:34:25 +09:00
1 parent b2c91062f1
commit 8de55f7ca5
12 files changed
+1648 -167

No files matched your search

+200 -77
View File
@@ -3,9 +3,7 @@
const crypto = require('crypto');
const config = require('./config');
const Blinds = require('../public/assets/blinds');
// 進行フェーズ。プレイ処理は未実装なので、ホストが手動で進めるスタブ。
const HAND_PHASES = ['PREFLOP', 'FLOP', 'TURN', 'RIVER', 'SHOWDOWN'];
const { Hand } = require('./hand');
class Table {
constructor(opts) {
@@ -19,14 +17,17 @@ class Table {
this.hostToken = opts.hostToken || null;
this.maxSeats = config.MAX_SEATS;
this.seats = new Array(this.maxSeats).fill(null); // { token, name, stack }
this.phase = 'WAITING';
// { token, name, stack, buyIns, sittingOut, left }
// left: ハンド中に退席した人。ハンドが終わるまで席を予約しておく
this.seats = new Array(this.maxSeats).fill(null);
this.hand = null;
this.started = false;
this.paused = false;
this.pausedAt = null;
this.level = 1;
this.handNumber = 0;
this.pot = 0;
this.buttonSeat = null;
this.nextHandAt = null;
this.levelEndsAt = null;
this.levelRemainingMs = this.intervalMs;
@@ -42,114 +43,165 @@ class Table {
return Blinds.blindsAt(this.baseSmallBlind, this.baseBigBlind, this.level);
}
get phase() {
if (!this.hand) return 'WAITING';
return this.hand.finished ? 'FINISHING' : this.hand.street;
}
get handActive() {
return Boolean(this.hand && !this.hand.finished);
}
get playerCount() {
return this.seats.filter(Boolean).length;
return this.seats.filter((s) => s && !s.left).length;
}
seatOf(token) {
return this.seats.findIndex((s) => s && s.token === token);
return token ? this.seats.findIndex((s) => s && s.token === token) : -1;
}
isHost(token) {
return Boolean(token) && token === this.hostToken;
}
// 次のハンドに参加できる席
eligibleSeats() {
return this.seats
.map((s, i) => (s && !s.left && !s.sittingOut && s.stack > 0 ? i : -1))
.filter((i) => i >= 0);
}
canDeal() {
return this.started && !this.paused && this.eligibleSeats().length >= 2;
}
// 着席した席番号を返す。満席なら -1(観戦)
join(token, name) {
const existing = this.seatOf(token);
if (existing >= 0) {
this.seats[existing].name = name;
const s = this.seats[existing];
s.name = name;
if (s.left) {
s.left = false; // ハンド中に退席して戻ってきた
if (this.seatOf(this.hostToken) < 0 || this.seats[this.seatOf(this.hostToken)].left) this.hostToken = token;
}
return existing;
}
const free = this.seats.findIndex((s) => !s);
if (free < 0) return -1;
this.seats[free] = { token, name, stack: this.startingStack };
this.seats[free] = { token, name, stack: this.startingStack, buyIns: 1, sittingOut: false, left: false };
// ホスト(通常は作成者)が着席していなければ、この人がホストになる
if (this.seatOf(this.hostToken) < 0) this.hostToken = token;
const host = this.seatOf(this.hostToken);
if (host < 0 || this.seats[host].left) this.hostToken = token;
return free;
}
leave(token) {
leave(token, now = Date.now()) {
const i = this.seatOf(token);
if (i < 0) return false;
this.seats[i] = null;
if (this.hostToken === token) {
const next = this.seats.find(Boolean);
this.hostToken = next ? next.token : null;
if (i < 0 || this.seats[i].left) return false;
if (this.hand && this.hand.players.has(i)) {
// 結果表示やポット計算のため、ハンドが終わるまで席は残す
this.seats[i].left = true;
this.hand.forceFold(i, now);
} else {
this.seats[i] = null;
}
if (this.playerCount < 2 && this.phase !== 'WAITING') {
this.phase = 'WAITING';
if (this.hostToken === token) {
const next = this.seats.find((s) => s && !s.left);
this.hostToken = next ? next.token : null;
}
return true;
}
// 次に埋まっている席(時計回り = 席番号の昇順)
nextOccupied(from) {
removeLeftSeats() {
this.seats = this.seats.map((s) => (s && s.left ? null : s));
}
nextOf(list, from) {
for (let k = 1; k <= this.maxSeats; k++) {
const i = (from + k) % this.maxSeats;
if (this.seats[i]) return i;
if (list.includes(i)) return i;
}
return null;
}
blindSeats() {
if (this.buttonSeat == null || this.phase === 'WAITING' || this.playerCount < 2) {
return { sbSeat: null, bbSeat: null };
}
let sbSeat;
if (this.playerCount === 2 && this.seats[this.buttonSeat]) {
sbSeat = this.buttonSeat; // ヘッズアップはボタンがSB
} else {
sbSeat = this.nextOccupied(this.buttonSeat);
}
return { sbSeat, bbSeat: this.nextOccupied(sbSeat) };
}
startNewHand() {
const occupied = this.seats.map((s, i) => (s ? i : -1)).filter((i) => i >= 0);
startNewHand(now) {
this.removeLeftSeats();
const eligible = this.eligibleSeats();
this.buttonSeat =
this.buttonSeat == null
? occupied[Math.floor(Math.random() * occupied.length)]
: this.nextOccupied(this.buttonSeat);
this.handNumber++;
this.pot = 0;
this.phase = 'PREFLOP';
}
// ---- ホスト操作(エラー時はメッセージを返す) ----
start(token) {
if (!this.isHost(token)) return 'ホストのみ開始できます';
if (this.phase !== 'WAITING') return 'すでに進行中です';
if (this.playerCount < 2) return '2人以上必要です';
if (!this.started) {
this.started = true;
if (this.timerEnabled) this.levelEndsAt = Date.now() + this.intervalMs;
? eligible[crypto.randomInt(eligible.length)]
: this.nextOf(eligible, this.buttonSeat);
const order = [];
for (let i = this.nextOf(eligible, this.buttonSeat); order.length < eligible.length; i = this.nextOf(eligible, i)) {
order.push(i);
}
this.startNewHand();
this.handNumber++;
const { sb, bb } = this.blinds;
this.nextHandAt = null;
this.hand = new Hand({ number: this.handNumber, seats: this.seats, order, button: this.buttonSeat, sb, bb, now });
}
// ---- プレイヤー操作(エラー時はメッセージを返す) ----
act(token, type, amount, now = Date.now()) {
const i = this.seatOf(token);
if (i < 0) return '着席していません';
if (!this.handActive) return 'ハンドが進行していません';
if (this.paused) return '一時停止中です';
return this.hand.act(i, type, amount, now);
}
setSittingOut(token, value, now = Date.now()) {
const i = this.seatOf(token);
if (i < 0) return '着席していません';
const s = this.seats[i];
s.sittingOut = value;
// 手番中に離席したら、その場でチェック/フォールド
if (value && this.handActive && this.hand.toAct === i && !this.paused) this.hand.autoAct(now);
return null;
}
// スタブ: フェーズを1つ進める(SHOWDOWN の次は新しいハンド)
nextPhase(token) {
if (!this.isHost(token)) return 'ホストのみ操作できます';
if (this.phase === 'WAITING') return 'まだ開始していません';
if (this.playerCount < 2) return '2人以上必要です';
const i = HAND_PHASES.indexOf(this.phase);
if (i === HAND_PHASES.length - 1) this.startNewHand();
else this.phase = HAND_PHASES[i + 1];
rebuy(token) {
const i = this.seatOf(token);
if (i < 0) return '着席していません';
const s = this.seats[i];
if (s.stack > 0) return 'チップが残っています';
if (this.handActive && this.hand.players.has(i) && !this.hand.players.get(i).folded) return 'ハンドの終了後にリバイできます';
s.stack = this.startingStack;
s.buyIns++;
s.sittingOut = false;
return null;
}
togglePause(token) {
// ---- ホスト操作 ----
start(token, now = Date.now()) {
if (!this.isHost(token)) return 'ホストのみ開始できます';
if (this.started) return 'すでに開始しています';
if (this.eligibleSeats().length < 2) return '2人以上必要です';
this.started = true;
if (this.timerEnabled) this.levelEndsAt = now + this.intervalMs;
this.startNewHand(now);
return null;
}
togglePause(token, now = Date.now()) {
if (!this.isHost(token)) return 'ホストのみ操作できます';
if (!this.started) return 'まだ開始していません';
const now = Date.now();
if (this.paused) {
this.paused = false;
// 止めていた時間だけ各期限を後ろにずらす
const shift = now - this.pausedAt;
const h = this.hand;
if (h && h.deadline != null) h.deadline += shift;
if (h && h.runoutAt != null) h.runoutAt += shift;
if (this.nextHandAt != null) this.nextHandAt += shift;
if (this.timerEnabled) this.levelEndsAt = now + this.levelRemainingMs;
this.paused = false;
this.pausedAt = null;
} else {
this.paused = true;
this.pausedAt = now;
if (this.timerEnabled) {
this.levelRemainingMs = Math.max(0, this.levelEndsAt - now);
this.levelEndsAt = null;
@@ -158,7 +210,8 @@ class Table {
return null;
}
// ブラインドタイマー。レベルが上がったら true
// 時間で進む処理(ブラインドタイマー・手番の時間切れ・オールイン時のめくり・次のハンド)
// 状態が変わったら true
tick(now) {
let changed = false;
while (this.levelEndsAt != null && now >= this.levelEndsAt) {
@@ -166,6 +219,41 @@ class Table {
this.levelEndsAt += this.intervalMs;
changed = true;
}
if (this.paused) return changed;
const h = this.hand;
if (h && !h.finished) {
if (h.toAct != null && now >= h.deadline) {
this.seats[h.toAct].sittingOut = true; // 時間切れの人は離席扱い(戻るボタンで復帰)
h.timeout(now);
changed = true;
}
if (h.runoutAt != null && now >= h.runoutAt) {
h.runoutStep(now);
changed = true;
}
}
if (this.hand && this.hand.finished) {
if (this.nextHandAt == null) {
this.nextHandAt = now + (this.hand.showdown ? config.SHOWDOWN_MS : config.FOLD_WIN_MS);
} else if (now >= this.nextHandAt) {
if (this.canDeal()) this.startNewHand(now);
else {
this.hand = null;
this.nextHandAt = null;
this.removeLeftSeats();
}
changed = true;
}
} else if (!this.hand) {
if (!this.canDeal()) this.nextHandAt = null;
else if (this.nextHandAt == null) this.nextHandAt = now + config.NEXT_HAND_DELAY_MS;
else if (now >= this.nextHandAt) {
this.startNewHand(now);
changed = true;
}
}
return changed;
}
@@ -188,11 +276,28 @@ class Table {
};
}
// token = 閲覧者のトークン(他人のトークンは送らない)
// token = 閲覧者のトークン(他人のトークンや手札は送らない)
publicState(token, connectedTokens) {
const now = Date.now();
const { sb, bb } = this.blinds;
const next = Blinds.blindsAt(this.baseSmallBlind, this.baseBigBlind, this.level + 1);
const host = this.seatOf(this.hostToken);
const mySeat = this.seatOf(token);
const h = this.hand;
const hv = h ? h.view(mySeat, token) : null;
const me = mySeat >= 0 ? this.seats[mySeat] : null;
const inLiveHand = Boolean(this.handActive && h.players.has(mySeat) && !h.players.get(mySeat).folded);
let turn = null;
if (this.handActive && h.toAct != null) {
const remainingMs = Math.max(0, h.deadline - (this.paused ? this.pausedAt : now));
turn = {
seat: h.toAct,
endsAt: this.paused ? null : h.deadline,
remainingMs,
totalMs: config.ACTION_TIME_MS,
};
}
return {
id: this.id,
name: this.name,
@@ -206,23 +311,41 @@ class Table {
enabled: this.timerEnabled,
intervalMin: this.intervalMin,
endsAt: this.levelEndsAt,
remainingMs: this.levelEndsAt != null ? this.levelEndsAt - Date.now() : this.levelRemainingMs,
remainingMs: this.levelEndsAt != null ? this.levelEndsAt - now : this.levelRemainingMs,
running: this.levelEndsAt != null,
},
phase: this.phase,
started: this.started,
paused: this.paused,
pot: this.pot,
handNumber: this.handNumber,
buttonSeat: this.phase === 'WAITING' ? null : this.buttonSeat,
...this.blindSeats(),
hostSeat: host >= 0 ? host : null,
hand: hv,
turn,
seats: this.seats.map((s, i) =>
s ? { seat: i, name: s.name, stack: s.stack, connected: connectedTokens.has(s.token) } : null
s && !s.left
? {
seat: i,
name: s.name,
stack: s.stack,
connected: connectedTokens.has(s.token),
sittingOut: s.sittingOut,
...(hv && hv.players[i] ? { inHand: true, ...hv.players[i] } : { inHand: false }),
}
: null
),
mySeat: this.seatOf(token),
mySeat: me && !me.left ? mySeat : -1,
me:
me && !me.left
? {
stack: me.stack,
net: me.stack + (inLiveHand ? h.players.get(mySeat).committed : 0) - me.buyIns * this.startingStack,
buyIns: me.buyIns,
sittingOut: me.sittingOut,
canRebuy: me.stack === 0 && !inLiveHand,
options: this.handActive && !this.paused ? h.options(mySeat) : null,
}
: null,
isHost: this.isHost(token),
serverNow: Date.now(),
serverNow: now,
};
}
}
@@ -268,4 +391,4 @@ class TableManager {
}
}
module.exports = { TableManager };
module.exports = { TableManager, Table };