games: the poker table others can walk up to, and the one that empties when they leave
Phase C's handler cutover: hold'em now runs on the shared-table runtime instead of the solo game_live_hands blob. Solo is just a table nobody else has joined. - holdem implements tableGame (name/timeout/stacks). timeout auto-checks-or-folds a walked-away seat and marks it away; the audit is per-hand, with each pot's rake on the winner's row alone so HouseTake cannot 4x itself. - New endpoints: sit opens a table (or joins an open bot seat), leave gets you up (LeaveTable + CloseTable behind the last human), plus tables (lobby), stream (SSE), chat and say. The move path loads the player's table, applies at their seat, commits under the version guard, and fans an SSE nudge. - Engine grows Vacate/Occupy (a human leaving/joining between hands) and TableSeats (a named human + bots). The view carries your_seat, since a shared table has no seat-zero-is-you convention. - Storage grows OpenSoloTable (stake+claim+create+seat in one tx), PlayerSeat, and the abandoned-table reaper (AbandonedTables/ReapTable) — the seated-player counterpart to the session reaper, since a walked-away stack is inside a blob the session reaper cannot see. upsertSeat preserves last_seen so an auto-fold never refreshes an away player's clock. Not deployed, and the felt is not rewired yet: the frontend still assumes seat zero is you, so this is browser-unverified. Solo sit/deal/play/leave and two-human join/leave/reaper are covered by tests; the whole suite is green. Claude-Session: https://claude.ai/code/session_013M5nD7PgUboJXoDcYHzpuJ
This commit is contained in:
@@ -140,19 +140,108 @@ func OpenTable(t Table, seats []Seat) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// OpenSoloTable opens a table with the player already sitting at it — the "solo
|
||||
// is just a table nobody else has joined yet" path. It is SitDown and OpenTable
|
||||
// fused into one transaction: stake the buy-in, claim the occupancy row, create
|
||||
// the table, and seat everyone (the human, and the bots filling the rest of the
|
||||
// ring). Any step failing rolls the buy-in back with it, so a crash never leaves
|
||||
// a player charged for a felt that does not exist.
|
||||
//
|
||||
// The occupancy claim is the same primary key that stops a second solo hand, so a
|
||||
// player already at a table (or in another game) is refused here with
|
||||
// ErrHandInProgress and their buy-in returned untouched.
|
||||
func OpenSoloTable(t Table, seats []Seat, buyIn int64) error {
|
||||
if buyIn <= 0 {
|
||||
return ErrBadAmount
|
||||
}
|
||||
// The human seat is the one row that is not a bot; its player claims the table.
|
||||
var user, name string
|
||||
for _, s := range seats {
|
||||
if !s.Bot() {
|
||||
user, name = s.MatrixUser, s.Name
|
||||
break
|
||||
}
|
||||
}
|
||||
if user == "" {
|
||||
return ErrBadAmount // a solo table with no human is a bug, not a table
|
||||
}
|
||||
now := nowUnix()
|
||||
|
||||
tx, err := Get().Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("games: begin open solo: %w", err)
|
||||
}
|
||||
defer tx.Rollback() //nolint:errcheck // no-op once committed
|
||||
|
||||
res, err := tx.Exec(
|
||||
`UPDATE game_chips SET chips = chips - ?, last_played = ?, updated_at = ?
|
||||
WHERE matrix_user = ? AND chips >= ?`,
|
||||
buyIn, now, now, user, buyIn,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("games: stake solo buy-in: %w", err)
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return ErrInsufficientChips
|
||||
}
|
||||
|
||||
res, err = tx.Exec(
|
||||
`INSERT INTO game_live_hands (matrix_user, game, state, seed1, seed2, table_id, updated_at)
|
||||
VALUES (?, ?, '', 0, 0, ?, ?)
|
||||
ON CONFLICT(matrix_user) DO NOTHING`,
|
||||
user, t.Game, t.ID, now,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("games: claim solo seat: %w", err)
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return ErrHandInProgress
|
||||
}
|
||||
_ = name
|
||||
|
||||
if _, err := tx.Exec(
|
||||
`INSERT INTO game_tables (id, game, tier, state, seed1, seed2, phase, hand_no, version, deadline, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?)`,
|
||||
t.ID, t.Game, t.Tier, string(t.State), int64(t.Seed1), int64(t.Seed2),
|
||||
t.Phase, t.HandNo, t.Deadline, now, now,
|
||||
); err != nil {
|
||||
return fmt.Errorf("games: open solo table: %w", err)
|
||||
}
|
||||
for _, sc := range seats {
|
||||
if err := upsertSeat(tx, t.ID, sc, now); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("games: commit open solo: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// upsertSeat writes a seat row inside an open transaction, bots included.
|
||||
//
|
||||
// last_seen is the caller's if they set one, and only falls back to now when they
|
||||
// did not. That distinction is load-bearing: the turn clock rewrites a seat to
|
||||
// mark it away, and it must carry the seat's *existing* last_seen through
|
||||
// unchanged — otherwise every auto-fold refreshes the away player's clock, and
|
||||
// the abandoned-table reaper (which keys on how long ago a human last acted for
|
||||
// themselves) could never fire.
|
||||
func upsertSeat(tx *sql.Tx, tableID string, s Seat, now int64) error {
|
||||
var user any
|
||||
if s.MatrixUser != "" {
|
||||
user = s.MatrixUser
|
||||
}
|
||||
seen := s.LastSeen
|
||||
if seen == 0 {
|
||||
seen = now
|
||||
}
|
||||
if _, err := tx.Exec(
|
||||
`INSERT INTO game_seats (table_id, seat, matrix_user, name, staked, away, last_seen)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(table_id, seat) DO UPDATE SET
|
||||
matrix_user = excluded.matrix_user, name = excluded.name,
|
||||
staked = excluded.staked, away = excluded.away, last_seen = excluded.last_seen`,
|
||||
tableID, s.Seat, user, s.Name, s.Staked, boolInt(s.Away), now,
|
||||
tableID, s.Seat, user, s.Name, s.Staked, boolInt(s.Away), seen,
|
||||
); err != nil {
|
||||
return fmt.Errorf("games: seat: %w", err)
|
||||
}
|
||||
@@ -498,6 +587,22 @@ func LeaveTable(l Leave) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// PlayerSeat reports the table and chair a player is sitting at. It reads the
|
||||
// seat row, which sit and leave keep in lockstep with the occupancy claim in one
|
||||
// transaction, so a row here means a live-hand row there and vice versa.
|
||||
func PlayerSeat(user string) (tableID string, seat int, err error) {
|
||||
err = Get().QueryRow(
|
||||
`SELECT table_id, seat FROM game_seats WHERE matrix_user = ?`, user,
|
||||
).Scan(&tableID, &seat)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return "", 0, ErrNoLiveHand
|
||||
}
|
||||
if err != nil {
|
||||
return "", 0, fmt.Errorf("games: player seat: %w", err)
|
||||
}
|
||||
return tableID, seat, nil
|
||||
}
|
||||
|
||||
// TableOf reports which table a player is sitting at, if any. Read off the
|
||||
// occupancy claim, so it agrees with the cash-out check by construction.
|
||||
func TableOf(user string) (string, error) {
|
||||
@@ -548,6 +653,124 @@ func CloseTable(id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// AbandonedTables lists tables everyone walked away from: every human seat is
|
||||
// away, and the most recent one acted for themselves longer ago than the cutoff.
|
||||
//
|
||||
// It is the seated-player half of the reaper. The session reaper cashes out loose
|
||||
// chips on a game_chips stack; it cannot see a player whose chips are inside a
|
||||
// table blob, and those are exactly the chips a walked-away poker player has. So
|
||||
// this finds the tables where nobody is coming back and hands them to ReapTable.
|
||||
//
|
||||
// A table with a live hand is never abandoned in this sense — the turn clock is
|
||||
// still folding it forward — so only tables parked between hands qualify. Like
|
||||
// DueTables it closes its rows before returning, because the caller is about to
|
||||
// take a lock and open a transaction against the one connection.
|
||||
func AbandonedTables(cutoff int64) ([]TableRef, error) {
|
||||
rows, err := Get().Query(
|
||||
`SELECT t.id, t.version FROM game_tables t
|
||||
WHERE t.phase = 'handover'
|
||||
AND EXISTS (SELECT 1 FROM game_seats s
|
||||
WHERE s.table_id = t.id AND s.matrix_user IS NOT NULL)
|
||||
AND NOT EXISTS (SELECT 1 FROM game_seats s
|
||||
WHERE s.table_id = t.id AND s.matrix_user IS NOT NULL
|
||||
AND (s.away = 0 OR s.last_seen >= ?))`,
|
||||
cutoff,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("games: abandoned tables: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var out []TableRef
|
||||
for rows.Next() {
|
||||
var r TableRef
|
||||
if err := rows.Scan(&r.ID, &r.Version); err != nil {
|
||||
return nil, fmt.Errorf("games: scan abandoned table: %w", err)
|
||||
}
|
||||
out = append(out, r)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// Reap is one abandoned table being cashed out and closed. Stacks is what each
|
||||
// human seat has in front of it, read from the engine blob by the caller — the
|
||||
// only game-specific fact the reaper needs, since the chips-home number lives
|
||||
// inside a state only the engine can decode.
|
||||
type Reap struct {
|
||||
TableID string
|
||||
Version int64
|
||||
// Humans is the seats to cash out, each paired with the stack going home. A
|
||||
// seat's Amount may be zero (they busted and never got up), which still has to
|
||||
// close their occupancy row so they can play again.
|
||||
Humans []ReapSeat
|
||||
}
|
||||
|
||||
// ReapSeat is one human being sent home from an abandoned table.
|
||||
type ReapSeat struct {
|
||||
Seat int
|
||||
MatrixUser string
|
||||
Amount int64
|
||||
}
|
||||
|
||||
// ReapTable cashes out every human at an abandoned table and deletes it, in one
|
||||
// transaction, conditional on the version so it cannot race a player who came
|
||||
// back to the felt in the same instant.
|
||||
//
|
||||
// It is LeaveTable and CloseTable fused: award each stack, release each occupancy
|
||||
// claim, then drop the seats, chat and table. The version guard is what makes it
|
||||
// safe against a returning player — if their sit or move bumped the version
|
||||
// between the scan and here, every row matches zero and the whole thing rolls
|
||||
// back, leaving the table exactly as the returning player left it.
|
||||
func ReapTable(r Reap) error {
|
||||
now := nowUnix()
|
||||
|
||||
tx, err := Get().Begin()
|
||||
if err != nil {
|
||||
return fmt.Errorf("games: begin reap: %w", err)
|
||||
}
|
||||
defer tx.Rollback() //nolint:errcheck // no-op once committed
|
||||
|
||||
// Bump the version first, and refuse if it moved. Nothing below is conditional,
|
||||
// so this one check has to stand for the whole reap.
|
||||
res, err := tx.Exec(
|
||||
`UPDATE game_tables SET version = version + 1, updated_at = ? WHERE id = ? AND version = ?`,
|
||||
now, r.TableID, r.Version,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("games: reap bump version: %w", err)
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
return ErrStaleTable
|
||||
}
|
||||
|
||||
for _, h := range r.Humans {
|
||||
if h.Amount > 0 {
|
||||
if err := award(tx, h.MatrixUser, h.Amount, now); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := tx.Exec(
|
||||
`DELETE FROM game_live_hands WHERE matrix_user = ? AND table_id = ?`,
|
||||
h.MatrixUser, r.TableID,
|
||||
); err != nil {
|
||||
return fmt.Errorf("games: reap release claim: %w", err)
|
||||
}
|
||||
}
|
||||
for _, q := range []string{
|
||||
`DELETE FROM game_seats WHERE table_id = ?`,
|
||||
`DELETE FROM game_chat WHERE table_id = ?`,
|
||||
`DELETE FROM game_tables WHERE id = ?`,
|
||||
} {
|
||||
if _, err := tx.Exec(q, r.TableID); err != nil {
|
||||
return fmt.Errorf("games: reap close: %w", err)
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return fmt.Errorf("games: commit reap: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ---- the turn clock --------------------------------------------------------
|
||||
|
||||
// TableRef is a table the clock has found expired: which one, and at what version
|
||||
|
||||
Reference in New Issue
Block a user