When friends or couples play an icebreaker checklist together, traditional quiz apps force an ugly trade-off: either require everyone to sign up with an email address and dump raw answers into a user table, or rely on static screenshots that expose individual choices nobody wanted to broadcast.
While building the open-web card engine for PurityTestGame (disclosure: I built and maintain this project), we solved three fun systems problems in pure zero-dependency ES modules and Cloudflare Pages Functions:
-
Deterministic Stratified Card Dealing (
Mulberry32) so up to 10 phones deal the exact same shuffled hand from a tiny seed. -
16-Byte Binary Bit-Packing + CRC-8 Integrity (
22Base64url characters) for compact state transport. -
Zero-Spoiler Double-Blind Intersection (
Want_A AND Want_B) so unreciprocated choices never leave the server payload.
Here is the exact architecture and code you can reuse in any multiplayer trivia, poll, or compatibility web app.
1. Seeded Stratified Shuffling with Mulberry32
Math.random() cannot be seeded in JavaScript. If a host deals 10 questions out of a 100-item pool, we want two guarantees:
- Chapter balance: Draw 2 cards from each of 5 escalating chapters rather than 10 random easy items.
- Reproducibility: Anyone joining from the same room seed sees the exact same 10 prompts in the exact same order.
Mulberry32 gives uniform 32-bit pseudorandom numbers in 9 lines of code:
export function mulberry32(seedInt) {
let a = seedInt >>> 0;
return function next() {
a |= 0;
a = (a + 0x6d2b79f5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
export function hashSeedString(seedStr) {
let h = 2166136261;
for (let i = 0; i < seedStr.length; i++) {
h ^= seedStr.charCodeAt(i);
h = Math.imul(h, 16777619);
}
return h >>> 0;
}
Running a FisherâYates shuffle per 20-item bucket with mulberry32(hashSeedString(seed)) takes < 0.05 ms on mobile Safari and guarantees balanced hands every round.
2. Packing 100 Booleans + Metadata Into 22 URL-Safe Characters
Storing 100 answers as JSON ([true, false, ...]) takes over 600 bytes. Packing them at the bit level shrinks the entire stateâversion, quiz mode, age cohort, 100 answers, and a CRC-8 checksumâinto 16 raw bytes (22 Base64url characters):
| Byte Offset | Bits | Purpose |
|---|---|---|
Byte 0 |
8 |
Protocol version (4b) + Deck ID (4b) |
Byte 1 |
8 |
Age cohort curve (4b) + Edition flag (4b) |
Bytes 2..14 |
104 |
100 boolean answers + 4 zero pad bits |
Byte 15 |
8 |
CRC-8 (x^8 + x^2 + x + 1, poly 0x07) |
export function crc8(bytes, length = bytes.length) {
let crc = 0x00;
for (let i = 0; i < length; i++) {
crc ^= bytes[i];
for (let bit = 0; bit < 8; bit++) {
crc = (crc & 0x80) ? ((crc << 1) ^ 0x07) & 0xff : (crc << 1) & 0xff;
}
}
return crc;
}
If a chat app truncates the final character of a pasted token, crc8(buf, 15) !== buf[15] catches the corruption immediately instead of rendering garbage scores.
3. Stateless HttpOnly Guest Identity & Double-Blind Intersection
For our two-player mode (Couples Double-Blind Sync), both partners answer cards across three states: Not for Me, Done Together, or Want to Try.
The core privacy requirement is zero unilateral risk: if Partner A taps Want to Try on card #14 and Partner B taps Not for Me, Partner B must never learn that Partner A selected itânot even in browser DevTools network responses.
Instead of user accounts, we issue a zero-KV stateless HMAC-SHA256 HttpOnly cookie via WebCrypto (crypto.subtle.sign) to bind creator/partner browser sessions, and evaluate the intersection strictly inside the edge handler before serializing JSON:
function computeDoubleBlindReveal(hostChoices, partnerChoices, questionIds) {
const doneTogether = [];
const mutualWishes = [];
for (let i = 0; i < questionIds.length; i++) {
const a = hostChoices[i]; // 0 = pass, 1 = done, 2 = want_to_try
const b = partnerChoices[i];
if (a === 1 && b === 1) {
doneTogether.push(questionIds[i]);
} else if (a === 2 && b === 2) {
mutualWishes.push(questionIds[i]);
}
// Unmatched `2`s (1-sided wishes) are discarded on the edge and never returned!
}
return { doneTogether, mutualWishes };
}
We also pair each room with two O(1) KV keys (tbl:{code} and tbl-state:{code}) rather than KV.list() prefix scans, keeping p99 edge latency under 18 ms even when nine friends submit guesses in a group chat at the same second.
Key Takeaways & Trade-offs
-
Bit-packing beats JSON in chat links:
22Base64url characters survive SMS, Discord, and Instagram DM link wrappers without truncation, whileCRC-8catches accidental character loss before rendering invalid results. -
Intersection at the edge eliminates unilateral exposure: Evaluating
a === 2 && b === 2before serializing JSON guarantees one-sided selections never appear in client network payloads. - Deterministic PRNGs simplify multiplayer state: Sending a short seed instead of full question arrays keeps room payloads tiny across mobile connections.
Happy to answer any questions about Cloudflare KV concurrency patterns, WebCrypto stateless cookies, or zero-dependency QR matrix encoding in the comments!
Top comments (0)