Tongits Pro started as a card tracker I used during real games. It grew into a full game: you can play offline against bots, or online in rooms with friends.
Two ways to play usually means two copies of the rules. Tongits has a lot of them: when you're allowed to sapaw (lay a card on someone else's meld), when you can call a draw, how wild cards count, how the pot is paid out. If the browser and the server each had their own version, they would drift, and an online move could be legal on your screen and illegal on the server.
So there is only one copy.
One class, no UI
All the rules live in GameEngine.js, a plain JavaScript class. It has no React and no DOM, just methods like drawCard, layDownMeld, discardCard, and layOffCard that change the game state and return false when a move isn't allowed.
Offline, the React app creates the engine directly in the browser, bots included:
const g = new GameEngine({ playerCount: n, botSkill: skill });
Online, the Node server imports the exact same file:
import { GameEngine } from "../frontend/src/engine/GameEngine.js";
The server is the referee
In an online room, the browser never sends a new game state. It sends what the player wants to do, and the server runs it through the engine:
export function applyPlayerAction(game, seat, body) {
const { action, cardIds, cardId, targetPlayerId, meldIndex } = body;
switch (action) {
case "DRAW":
return game.drawCard(seat);
case "MELD":
return game.layDownMeld(seat, cardIds || []);
case "DISCARD":
return game.discardCard(seat, cardId);
case "SAPAW":
return game.layOffCard(seat, cardId, targetPlayerId, meldIndex);
// ...chow, draw calls, challenges, folds
default:
return false;
}
}
If the engine returns false, the server answers with an error and nothing changes. Because it's the same code the offline game uses, "what the server allows" and "what the game allows" are the same thing by construction.
Keeping the table in order
After every accepted move, the server sends the new state to everyone in the room, and it numbers each broadcast. The browser only accepts a state whose number is higher than the last one it applied, so a duplicate or late message can't roll the table back to an earlier turn.
Testing without a browser
Since the engine has no UI, its tests run in plain Node with Vitest. They cover the rules that are easy to get wrong:
- the deck size for each wild-card mode
- how many cards are dealt in 2- and 3-player games
- a sapaw is rejected if the player hasn't opened yet
- a valid sapaw after opening extends the right meld
The catch
Sharing a file across the frontend and the server has one deployment gotcha. The server imports from the frontend/ folder, so the host has to check out the whole repository. Point the host at just the server/ folder and the import breaks, because the engine isn't there.
That's a small price for never having to ask which copy of the rules is right.