Add optional card mode: server deals cards and settles showdowns automatically
- New "カードを配る" switch on the create-table screen (default off); the lobby shows "カードあり" for tables that use it - Card mode (server/cards.js restored): crypto-shuffled deck kept server-side, hole cards sent only to their owner until showdown/all-in reveal, board dealt per street with burns, all-in runout on a timer (frozen while paused) - Showdown is evaluated and every pot (incl. side pots, split pots) is paid out automatically; host winner selection is only used when card mode is off - Play UI: board, hole cards, own cards in the action bar, hand names, winning cards highlighted; layout tuned for 7+ seats and short screens - Playing-card styles use .pcard so they don't clash with the existing .card panel - Tests for hand evaluation, card privacy, runout, side pots, ties and random play Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6d33440048
commit
66907345ff
13 files changed
+653
-42
No files matched your search
@@ -0,0 +1,96 @@
|
||||
'use strict';
|
||||
|
||||
const crypto = require('crypto');
|
||||
|
||||
// カードは "As"(スペードのA), "Td"(ダイヤの10)のような2文字
|
||||
const RANKS = '23456789TJQKA';
|
||||
const SUITS = 'shdc';
|
||||
|
||||
// 暗号論的乱数で Fisher-Yates シャッフル(山札はサーバの外に出さない)
|
||||
function newDeck() {
|
||||
const deck = [];
|
||||
for (const r of RANKS) for (const s of SUITS) deck.push(r + s);
|
||||
for (let i = deck.length - 1; i > 0; i--) {
|
||||
const j = crypto.randomInt(i + 1);
|
||||
[deck[i], deck[j]] = [deck[j], deck[i]];
|
||||
}
|
||||
return deck;
|
||||
}
|
||||
|
||||
const rankValue = (card) => RANKS.indexOf(card[0]) + 2;
|
||||
|
||||
// 5枚の役の強さ。配列を先頭から比較して大きいほうが強い
|
||||
// [役の種類(0〜8), 比較用の数字...]
|
||||
function score5(cards) {
|
||||
const vals = cards.map(rankValue).sort((a, b) => b - a);
|
||||
const flush = cards.every((c) => c[1] === cards[0][1]);
|
||||
|
||||
let straightHigh = 0;
|
||||
if (new Set(vals).size === 5) {
|
||||
if (vals[0] - vals[4] === 4) straightHigh = vals[0];
|
||||
else if (vals[0] === 14 && vals[1] === 5) straightHigh = 5; // A-2-3-4-5
|
||||
}
|
||||
if (straightHigh && flush) return [8, straightHigh];
|
||||
|
||||
// 枚数の多い順、同じ枚数なら数字の大きい順
|
||||
const counts = new Map();
|
||||
for (const v of vals) counts.set(v, (counts.get(v) || 0) + 1);
|
||||
const groups = [...counts].sort((a, b) => b[1] - a[1] || b[0] - a[0]);
|
||||
const byGroup = groups.map((g) => g[0]);
|
||||
|
||||
if (groups[0][1] === 4) return [7, ...byGroup];
|
||||
if (groups[0][1] === 3 && groups[1][1] === 2) return [6, ...byGroup];
|
||||
if (flush) return [5, ...vals];
|
||||
if (straightHigh) return [4, straightHigh];
|
||||
if (groups[0][1] === 3) return [3, ...byGroup];
|
||||
if (groups[0][1] === 2 && groups[1][1] === 2) return [2, ...byGroup];
|
||||
if (groups[0][1] === 2) return [1, ...byGroup];
|
||||
return [0, ...vals];
|
||||
}
|
||||
|
||||
function compareScore(a, b) {
|
||||
for (let i = 0; i < Math.max(a.length, b.length); i++) {
|
||||
const d = (a[i] || 0) - (b[i] || 0);
|
||||
if (d) return d;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const HAND_NAMES = [
|
||||
'ハイカード',
|
||||
'ワンペア',
|
||||
'ツーペア',
|
||||
'スリーカード',
|
||||
'ストレート',
|
||||
'フラッシュ',
|
||||
'フルハウス',
|
||||
'フォーカード',
|
||||
'ストレートフラッシュ',
|
||||
];
|
||||
|
||||
function handName(score) {
|
||||
if (score[0] === 8 && score[1] === 14) return 'ロイヤルフラッシュ';
|
||||
return HAND_NAMES[score[0]];
|
||||
}
|
||||
|
||||
// 5〜7枚から最も強い5枚を選ぶ
|
||||
function bestHand(cards) {
|
||||
let best = null;
|
||||
const n = cards.length;
|
||||
const pick = (start, chosen) => {
|
||||
if (chosen.length === 5) {
|
||||
const score = score5(chosen);
|
||||
if (!best || compareScore(score, best.score) > 0) best = { score, cards: chosen.slice() };
|
||||
return;
|
||||
}
|
||||
for (let i = start; i <= n - (5 - chosen.length); i++) {
|
||||
chosen.push(cards[i]);
|
||||
pick(i + 1, chosen);
|
||||
chosen.pop();
|
||||
}
|
||||
};
|
||||
pick(0, []);
|
||||
return best && { score: best.score, cards: best.cards, name: handName(best.score) };
|
||||
}
|
||||
|
||||
module.exports = { newDeck, score5, compareScore, bestHand, handName };
|
||||
+4
-1
@@ -61,8 +61,11 @@ module.exports = {
|
||||
// 誰も接続していないテーブルを削除するまでの時間
|
||||
EMPTY_TABLE_TTL_MS: 30 * 60 * 1000,
|
||||
|
||||
// ハンド終了後、次のハンドまで結果を表示する時間
|
||||
// オールインで残りの場札をめくる間隔(カード機能あり)
|
||||
RUNOUT_STEP_MS: 1500,
|
||||
// ハンド終了後、次のハンドまで結果を表示する時間(カードありは手札を見比べられるよう長め)
|
||||
SHOWDOWN_MS: 4000,
|
||||
CARD_SHOWDOWN_MS: 6000,
|
||||
FOLD_WIN_MS: 2500,
|
||||
// 参加者が2人揃ってから次のハンドを始めるまで
|
||||
NEXT_HAND_DELAY_MS: 2000,
|
||||
|
||||
+98
-25
@@ -1,19 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
// 1ハンド分のチップの動き(ブラインド・ベッティング・ポット分配)。
|
||||
// カードは実物で配るので扱わない。ショーダウンでは勝者をプレイヤーが選ぶ。
|
||||
const { newDeck, bestHand, compareScore } = require('./cards');
|
||||
|
||||
// 1ハンド分の進行(ブラインド・ベッティング・ポット分配)。
|
||||
// cards = false: カードは実物で配るので扱わない。ショーダウンでは勝者をホストが選ぶ。
|
||||
// cards = true : サーバが配り、ショーダウンで役を判定して自動で分配する。
|
||||
// seats は Table.seats への参照で、スタックはここで直接増減する。
|
||||
// 参加者の席はハンド終了まで空席にならない(退席者は Table 側で予約したまま)。
|
||||
const STREETS = ['PREFLOP', 'FLOP', 'TURN', 'RIVER'];
|
||||
|
||||
class Hand {
|
||||
constructor({ number, seats, order, button, sb, bb }) {
|
||||
constructor({ number, seats, order, button, sb, bb, cards = false }) {
|
||||
this.number = number;
|
||||
this.seats = seats;
|
||||
this.order = order; // 参加者の席番号。ボタンの次から時計回り
|
||||
this.button = button;
|
||||
this.sb = sb;
|
||||
this.bb = bb;
|
||||
this.cards = cards;
|
||||
|
||||
// 山札はサーバの外に出さない
|
||||
this.deck = cards ? newDeck() : null;
|
||||
this.board = [];
|
||||
this.revealed = false; // ショーダウン・オールイン時に残った人の手札を公開
|
||||
this.runout = false; // オールインでこれ以上ベットが無く、残りの場札を順にめくっている
|
||||
|
||||
this.street = 'PREFLOP'; // PREFLOP / FLOP / TURN / RIVER / SHOWDOWN
|
||||
this.pot = 0; // 前のストリートまでに集めた額
|
||||
@@ -29,6 +39,8 @@ class Hand {
|
||||
this.players.set(i, {
|
||||
seat: i,
|
||||
token: seats[i].token,
|
||||
cards: [],
|
||||
best: null, // ショーダウンでの最強の5枚
|
||||
bet: 0, // このストリートで出した額
|
||||
committed: 0, // このハンドで出した合計
|
||||
folded: false,
|
||||
@@ -38,6 +50,7 @@ class Hand {
|
||||
won: 0,
|
||||
});
|
||||
}
|
||||
if (cards) for (let r = 0; r < 2; r++) for (const i of order) this.players.get(i).cards.push(this.deck.pop());
|
||||
|
||||
// ヘッズアップはボタンが SB
|
||||
const headsUp = order.length === 2;
|
||||
@@ -53,8 +66,9 @@ class Hand {
|
||||
return this.order.map((i) => this.players.get(i));
|
||||
}
|
||||
|
||||
// 勝者の選択待ち(カードありのときは自動で判定するので来ない)
|
||||
get awaitingWinners() {
|
||||
return this.street === 'SHOWDOWN' && !this.finished;
|
||||
return !this.cards && this.street === 'SHOWDOWN' && !this.finished;
|
||||
}
|
||||
|
||||
live() {
|
||||
@@ -212,15 +226,41 @@ class Hand {
|
||||
this.currentBet = 0;
|
||||
this.minRaise = this.bb;
|
||||
|
||||
// リバーが終わった、または全員オールインでこれ以上ベットが無い → ショーダウン
|
||||
if (this.street === 'RIVER' || this.live().filter((p) => !p.allIn).length < 2) {
|
||||
this.street = 'SHOWDOWN';
|
||||
return;
|
||||
const noMoreBetting = this.live().filter((p) => !p.allIn).length < 2;
|
||||
if (!this.cards) {
|
||||
// リバーが終わった、または全員オールインでこれ以上ベットが無い → ショーダウン
|
||||
if (this.street === 'RIVER' || noMoreBetting) {
|
||||
this.street = 'SHOWDOWN';
|
||||
return;
|
||||
}
|
||||
this.street = STREETS[STREETS.indexOf(this.street) + 1];
|
||||
} else {
|
||||
if (this.street === 'RIVER') return this.finishShowdown();
|
||||
if (noMoreBetting) {
|
||||
// 手札を公開して、残りの場札は Table のタイマーで順にめくる(runoutStep)
|
||||
this.revealed = true;
|
||||
this.runout = true;
|
||||
return;
|
||||
}
|
||||
this.dealStreet();
|
||||
}
|
||||
this.street = STREETS[STREETS.indexOf(this.street) + 1];
|
||||
this.setNextActor(this.button);
|
||||
}
|
||||
|
||||
dealStreet() {
|
||||
this.deck.pop(); // バーン
|
||||
if (this.street === 'PREFLOP') this.board.push(this.deck.pop(), this.deck.pop(), this.deck.pop());
|
||||
else this.board.push(this.deck.pop());
|
||||
this.street = STREETS[STREETS.indexOf(this.street) + 1];
|
||||
}
|
||||
|
||||
// オールイン後に場札を1ストリート分めくる。リバーまでめくり終えていたらショーダウン
|
||||
runoutStep() {
|
||||
if (!this.runout || this.finished) return;
|
||||
if (this.street === 'RIVER') this.finishShowdown();
|
||||
else this.dealStreet();
|
||||
}
|
||||
|
||||
// メインポット・サイドポット(同じ参加者のポットはまとめる)
|
||||
buildPots() {
|
||||
const all = this.list;
|
||||
@@ -258,38 +298,71 @@ class Hand {
|
||||
if (new Set(w).size !== w.length || !w.every((s) => pots[i].eligible.includes(s))) return '勝者の指定が不正です';
|
||||
}
|
||||
|
||||
const results = [];
|
||||
pots.forEach((pot, i) => {
|
||||
// 割り切れない端数はボタンの左から順に
|
||||
const ws = this.order.filter((s) => winners[i].includes(s));
|
||||
const share = Math.floor(pot.amount / ws.length);
|
||||
let rest = pot.amount - share * ws.length;
|
||||
for (const s of ws) {
|
||||
const amount = share + (rest > 0 ? 1 : 0);
|
||||
if (rest > 0) rest--;
|
||||
this.players.get(s).won += amount;
|
||||
this.seats[s].stack += amount;
|
||||
}
|
||||
results.push({ amount: pot.amount, winners: ws });
|
||||
});
|
||||
const results = pots.map((pot, i) => ({ amount: pot.amount, winners: this.payout(pot.amount, winners[i]) }));
|
||||
this.end(true, results);
|
||||
return null;
|
||||
}
|
||||
|
||||
// カードありのショーダウン: ポットごとに役を比べて分配する
|
||||
finishShowdown() {
|
||||
this.revealed = true;
|
||||
for (const p of this.live()) p.best = bestHand(p.cards.concat(this.board));
|
||||
const results = this.buildPots().map((pot) => {
|
||||
const contenders = pot.eligible.map((s) => this.players.get(s));
|
||||
let top = null;
|
||||
for (const c of contenders) if (!top || compareScore(c.best.score, top) > 0) top = c.best.score;
|
||||
const best = contenders.filter((c) => compareScore(c.best.score, top) === 0);
|
||||
return {
|
||||
amount: pot.amount,
|
||||
winners: this.payout(pot.amount, best.map((c) => c.seat)),
|
||||
handName: best[0].best.name,
|
||||
bestCards: best[0].best.cards,
|
||||
};
|
||||
});
|
||||
this.end(true, results);
|
||||
}
|
||||
|
||||
// amount を seats で山分けする。割り切れない端数はボタンの左から順に。分配した順の席番号を返す
|
||||
payout(amount, seats) {
|
||||
const ws = this.order.filter((s) => seats.includes(s));
|
||||
const share = Math.floor(amount / ws.length);
|
||||
let rest = amount - share * ws.length;
|
||||
for (const s of ws) {
|
||||
const a = share + (rest > 0 ? 1 : 0);
|
||||
if (rest > 0) rest--;
|
||||
this.players.get(s).won += a;
|
||||
this.seats[s].stack += a;
|
||||
}
|
||||
return ws;
|
||||
}
|
||||
|
||||
end(showdown, results) {
|
||||
this.finished = true;
|
||||
this.runout = false;
|
||||
this.showdown = showdown;
|
||||
this.toAct = null;
|
||||
this.results = results;
|
||||
}
|
||||
|
||||
view() {
|
||||
// 閲覧者に見せてよい情報だけを返す(他人の手札は公開時のみ)
|
||||
view(viewerSeat, viewerToken) {
|
||||
const players = {};
|
||||
for (const p of this.list) {
|
||||
players[p.seat] = { bet: p.bet, folded: p.folded, allIn: p.allIn, last: p.last, won: p.won };
|
||||
const v = { bet: p.bet, folded: p.folded, allIn: p.allIn, last: p.last, won: p.won };
|
||||
if (this.cards) {
|
||||
const mine = p.seat === viewerSeat && p.token === viewerToken;
|
||||
const shown = mine || (this.revealed && !p.folded);
|
||||
v.cards = shown ? p.cards : null;
|
||||
v.handName =
|
||||
shown && !p.folded && this.board.length >= 3 ? (p.best || bestHand(p.cards.concat(this.board))).name : null;
|
||||
}
|
||||
players[p.seat] = v;
|
||||
}
|
||||
return {
|
||||
number: this.number,
|
||||
cards: this.cards,
|
||||
board: this.board,
|
||||
runout: this.runout,
|
||||
street: this.street,
|
||||
pot: this.pot,
|
||||
potTotal: this.pot + this.list.reduce((sum, p) => sum + p.bet, 0),
|
||||
|
||||
+3
-1
@@ -114,6 +114,7 @@ app.post('/api/tables', (req, res) => {
|
||||
const bigBlind = b.bigBlind;
|
||||
const blindTimerEnabled = b.blindTimerEnabled === true;
|
||||
const blindIntervalMin = b.blindIntervalMin;
|
||||
const cardsEnabled = b.cardsEnabled === true;
|
||||
const hostToken = cleanToken(b.token);
|
||||
|
||||
if (tables.tables.size >= config.MAX_TABLES) return res.status(429).json({ error: 'テーブル数が上限に達しています' });
|
||||
@@ -132,6 +133,7 @@ app.post('/api/tables', (req, res) => {
|
||||
bigBlind,
|
||||
blindTimerEnabled,
|
||||
blindIntervalMin: blindTimerEnabled ? blindIntervalMin : 10,
|
||||
cardsEnabled,
|
||||
hostToken,
|
||||
});
|
||||
broadcastLobby();
|
||||
@@ -276,7 +278,7 @@ io.on('connection', (socket) => {
|
||||
});
|
||||
});
|
||||
|
||||
// ブラインドタイマー・持ち時間・次のハンドなど時間で進む処理
|
||||
// ブラインドタイマー・オールイン後の場札・次のハンドなど時間で進む処理
|
||||
setInterval(() => {
|
||||
const now = Date.now();
|
||||
for (const table of tables.tables.values()) {
|
||||
|
||||
+32
-6
@@ -14,6 +14,8 @@ class Table {
|
||||
this.baseBigBlind = opts.bigBlind;
|
||||
this.timerEnabled = opts.blindTimerEnabled;
|
||||
this.intervalMin = opts.blindIntervalMin;
|
||||
// カード機能: サーバがカードを配り、ショーダウンの勝敗も自動で判定する(オフなら実物のカードで遊ぶ)
|
||||
this.cardsEnabled = opts.cardsEnabled === true;
|
||||
this.hostToken = opts.hostToken || null;
|
||||
this.maxSeats = config.MAX_SEATS;
|
||||
|
||||
@@ -28,6 +30,7 @@ class Table {
|
||||
this.handNumber = 0;
|
||||
this.buttonSeat = null;
|
||||
this.nextHandAt = null;
|
||||
this.runoutAt = null; // オールイン後、次の場札をめくる時刻
|
||||
this.levelEndsAt = null;
|
||||
this.levelRemainingMs = this.intervalMs;
|
||||
|
||||
@@ -139,7 +142,16 @@ class Table {
|
||||
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 });
|
||||
this.runoutAt = null;
|
||||
this.hand = new Hand({
|
||||
number: this.handNumber,
|
||||
seats: this.seats,
|
||||
order,
|
||||
button: this.buttonSeat,
|
||||
sb,
|
||||
bb,
|
||||
cards: this.cardsEnabled,
|
||||
});
|
||||
}
|
||||
|
||||
// ---- プレイヤー操作(エラー時はメッセージを返す) ----
|
||||
@@ -202,8 +214,9 @@ class Table {
|
||||
if (!this.isHost(token)) return 'ホストのみ操作できます';
|
||||
if (!this.started) return 'まだ開始していません';
|
||||
if (this.paused) {
|
||||
// 止めていた時間だけ次のハンドの開始を後ろにずらす
|
||||
// 止めていた時間だけ次のハンドの開始・場札をめくる時刻を後ろにずらす
|
||||
if (this.nextHandAt != null) this.nextHandAt += now - this.pausedAt;
|
||||
if (this.runoutAt != null) this.runoutAt += now - this.pausedAt;
|
||||
if (this.timerEnabled) this.levelEndsAt = now + this.levelRemainingMs;
|
||||
this.paused = false;
|
||||
this.pausedAt = null;
|
||||
@@ -218,7 +231,7 @@ class Table {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 時間で進む処理(ブラインドタイマー・次のハンドの開始)
|
||||
// 時間で進む処理(ブラインドタイマー・オールイン後の場札・次のハンドの開始)
|
||||
// 状態が変わったら true
|
||||
tick(now) {
|
||||
let changed = false;
|
||||
@@ -229,9 +242,20 @@ class Table {
|
||||
}
|
||||
if (this.paused) return changed;
|
||||
|
||||
const h = this.hand;
|
||||
if (h && h.runout) {
|
||||
if (this.runoutAt == null) this.runoutAt = now + config.RUNOUT_STEP_MS;
|
||||
else if (now >= this.runoutAt) {
|
||||
h.runoutStep();
|
||||
this.runoutAt = h.runout ? now + config.RUNOUT_STEP_MS : null;
|
||||
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);
|
||||
const showdownMs = this.cardsEnabled ? config.CARD_SHOWDOWN_MS : config.SHOWDOWN_MS;
|
||||
this.nextHandAt = now + (this.hand.showdown ? showdownMs : config.FOLD_WIN_MS);
|
||||
} else if (now >= this.nextHandAt) {
|
||||
if (this.canDeal()) this.startNewHand();
|
||||
else {
|
||||
@@ -267,18 +291,19 @@ class Table {
|
||||
paused: this.paused,
|
||||
timerEnabled: this.timerEnabled,
|
||||
intervalMin: this.intervalMin,
|
||||
cardsEnabled: this.cardsEnabled,
|
||||
createdAt: this.createdAt,
|
||||
};
|
||||
}
|
||||
|
||||
// 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 mySeat = this.seatOf(token);
|
||||
const h = this.hand;
|
||||
const hv = h ? h.view() : null;
|
||||
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);
|
||||
|
||||
@@ -287,6 +312,7 @@ class Table {
|
||||
name: this.name,
|
||||
startingStack: this.startingStack,
|
||||
maxSeats: this.maxSeats,
|
||||
cardsEnabled: this.cardsEnabled,
|
||||
level: this.level,
|
||||
smallBlind: sb,
|
||||
bigBlind: bb,
|
||||
|
||||
Reference in new issue
Block a user