tinyfiles.tv
Moves: 0 Score: 0 Time: 00:00
🔄
Backend  ·  Proposed Architecture · Not connected

Solitaire Backend — Proposed Architecture

Right now this game is 100% local. The deck is shuffled in the browser, and the timer stops if you close the tab. This is the map to make it real: server-generated seeds so everyone gets the same "Daily Deal", server-authoritative move validation to prevent cheating, and secure leaderboards.

01 / Files to create
api/solitaire/new.php         POST /api/solitaire/new
api/solitaire/state.php       GET  /api/solitaire/state?game_id=...
api/solitaire/move.php        POST /api/solitaire/move
api/solitaire/undo.php        POST /api/solitaire/undo
api/solitaire/finish.php      POST /api/solitaire/finish
sql/solitaire_tables.sql

Keep the standard tinyfiles PHP pattern: /api/bootstrap.php, json_in() / json_out(), member sessions, and prepared PDO. The server must be authoritative over the shuffle.

02 / Database tables
-- sql/solitaire_tables.sql
solitaire_games
  id CHAR(36) PRIMARY KEY
  member VARCHAR(80) NULL              -- NULL = guest
  seed VARCHAR(32) NOT NULL            -- Server-generated seed
  draw_mode ENUM('1','3') NOT NULL DEFAULT '1'
  status ENUM('playing','won','abandoned') NOT NULL DEFAULT 'playing'
  moves INT NOT NULL DEFAULT 0
  score INT NOT NULL DEFAULT 0
  started_at DATETIME NOT NULL
  finished_at DATETIME NULL

solitaire_history
  id BIGINT AUTO_INCREMENT PRIMARY KEY
  game_id CHAR(36) NOT NULL
  action JSON NOT NULL                 -- { type: 'move', from: 'tableau-0', to: 'foundation-h', card: 'H1' }
  created_at DATETIME NOT NULL

solitaire_leaderboard
  id INT AUTO_INCREMENT PRIMARY KEY
  member VARCHAR(80) NOT NULL
  time_ms INT NOT NULL
  score INT NOT NULL
  moves INT NOT NULL
  won_at DATETIME NOT NULL
Store the seed, not the shuffled 52-card array. When a player makes a move, the server replays the moves against the deterministic shuffle based on the seed.
03 / API endpoints & request flow
POST /api/solitaire/new
{ "daily": 1, "draw_mode": 1 }
-> { "ok": true, "game_id": "uuid...", "seed": "d_2026_11_18", "state": { "stock": [...], "tableau": [...] } }

POST /api/solitaire/move
{ "game_id": "uuid...", "from": "tableau-2", "to": "foundation-h", "card_index": 4 }
- Server loads seed.
- Server replays history to reconstruct exact board state.
- Server validates if the move is legal (e.g., H1 to foundation-h, alternating colors on tableau).
- If valid: Appends to history, increments moves, returns { "ok": true }
- If invalid: Returns { "ok": false, "error": "illegal_move" }

POST /api/solitaire/undo
{ "game_id": "uuid..." }
- Pops the last action from history, returns new state. (Server must handle un-flipping revealed cards).

POST /api/solitaire/finish
{ "game_id": "uuid..." }
- Verifies all 4 foundations have 13 cards.
- Computes time and saves to leaderboard.
04 / Deterministic Shuffle (Server-side)
// Never use Math.random() on the server for games. 
// Use a seeded PRNG so players can share a "Daily Deal".

// PHP example using mt_srand:
function getShuffledDeck($seed) {
    $deck = [];
    $suits = ['h', 'd', 'c', 's'];
    for($s=0; $s<4; $s++){
        for($r=1; $r<=13; $r++){
            $deck[] = $suits[$s] . $r;
        }
    }
    
    mt_srand(crc32($seed));
    for($i = count($deck) - 1; $i > 0; $i--){
        $j = mt_rand(0, $i);
        $temp = $deck[$i];
        $deck[$i] = $deck[$j];
        $deck[$j] = $temp;
    }
    return $deck;
}

// Client side: The browser can use the seed to shuffle the deck identically 
// using a JS implementation of the same PRNG (or the server sends the initial array).

By making the seed visible (e.g., "Today's Game: 2026-11-18"), players can compete on the exact same layout without the server having to store thousands of unique game arrays.