Skip to content

tilttek/card-deck

✅ published

Card decks for online play: shuffled from a verifiable seed, drawn, dealt and discarded, with each player sent only the cards they may see.

tilttek/card-deck keeps the cards of a game on the server. A deck is an ordered list of cards; every card lies in one zone — the draw pile, a player's hand, the discard pile, the table, or a pile the game names — face up or face down. A card has an identity, separate from what it shows: one face or several, and a back. Every gesture at the table is a recorded event — shuffle, draw from the top, the bottom or at random, deal face down or face up, discard, flip, turn to another face, move, collect a zone into another — and replaying the journal of a game from its seed gives the same state, card for card.

The shuffles and the random draws come from a seed of tilttek/dice: its commitment is published when the game is opened, the players add a seed of their own, and the seed is revealed when the game is finished, so anyone can check that the deck was not arranged. A player is sent a view of the game, never the game: their own hand, the cards face up, the number of cards of every zone — and never the order of the draw pile, a hidden card or the seed.

It is for an application that plays card games online — a card game, the initiative cards of a role-playing game, the games of a game night — and must not trust the browser with what a player may not see. It is a plain PHP library whose one dependency is tilttek/dice, for the seed and its commitment.

Status

PartState
Card, Face, Deck, StandardDeck: cards of one face or several and a back, decks of up to 1 024 cards, the 52-card deck and its jokersImplemented and tested
ZoneId: the draw pile, the discard pile, the table, hands and named pilesImplemented and tested
Gesture\*: shuffle, draw, deal, discard, flip, turn a face, move, collect, and their codecImplemented and tested
DeckState: the state of a game, its gestures, its replay and its fingerprintImplemented and tested
Shuffler: the Fisher-Yates shuffle on the randomizer of a seedImplemented and tested
View\*: what a player or a spectator may seeImplemented and tested
CardTable, GameRecord, Reveal: open, start, play against a version, view, finish and verify a gameImplemented and tested
Contract\GameStore, Store\InMemoryGameStore: the store port, and the store in memory for testsImplemented and tested
A store on Doctrine DBAL, a Symfony bundle, a Mercure push of the viewsNot started: adapters of the application, or of a future bundle

Documentation

DocumentWhat it covers
InstallationRequirements, installing and wiring.
Cards and zonesCards, faces, decks, zones, and an empty zone.
GesturesEvery gesture, what it does and when it is refused; replaying a journal.
FairnessThe seed, its commitment, the shuffle, the random draws, and verifying a finished game.
Player viewsWhat a player sees, what they never see, and how to send it.
Games and storesCardTable, versions, the store port and writing a store.
Extension pointsWhat an application gives the library.
ExceptionsCardDeckException and its exceptions, with their messages.
TestingThe library's tests, its fuzz target and its benchmarks.
ReferenceEvery public type, with its role.

Quick start

use Random\Engine\Secure;
use Random\Randomizer;
use Tilttek\CardDeck\CardTable;
use Tilttek\CardDeck\GameId;
use Tilttek\CardDeck\Gesture\Deal;
use Tilttek\CardDeck\Gesture\Discard;
use Tilttek\CardDeck\PlayerId;
use Tilttek\CardDeck\StandardDeck;
use Tilttek\Dice\Fairness\Seed;

$table = new CardTable($store); // your Contract\GameStore
$game = new GameId('g-7f3a');
$alice = new PlayerId('u-101');
$bob = new PlayerId('u-102');

$commitment = $table->open($game, StandardDeck::create(jokers: 2), Seed::generate(new Randomizer(new Secure())));
// Publish $commitment->hex, then let the players choose their seed.
$table->start($game, $clientSeed);

$table->play($game, 1, new Deal([$alice, $bob], 7));
$view = $table->view($game, $alice);           // send $view->toArray() to Alice, and nothing else

$table->play($game, $view->version, new Discard($cardAliceChose));
$reveal = $table->finish($game);               // publish $reveal->toArray(): anyone can verify the game

Requirements

PHP 8.5 or later, and tilttek/dice 0.3.

Contributing

Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.

License

MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Tilttek project. See LICENSE.