Walkthrough - Sprint 15: The Global Market & Gacha System
We have successfully designed, built, integrated, and verified the complete feature set for Sprint 15 (The Global Market & Gacha System) within thepcmtg-core monorepo. This implementation guarantees full compliance with the macroeconomic rules, zero-trust transactional mechanics, and Cloud Run scaling constraints defined in the PCMTG Project Bible.
🧱 Architectural System Sequence Flow
The following sequence diagram illustrates the Zero-Trust execution path of a player’s transaction under horizontal container scaling. Memorystore (Redis) gates ingress rate limits, and Postgres row-level locks protect balance ledger mutability.🚀 Key Achievements
1. Zero-Trust Transaction Ledger (Atomic Economy)
- File: market.js
- Implementation: To eliminate race conditions and double-spending, all market actions are handled inside secure
prisma.$transactionblocks.- An exclusive, pessimistic row lock is obtained on the player’s wallet using raw SQL
FOR UPDATEprior to checking or validating any fund balances: - The transaction block atomically deducts native funds, inserts a record into the
OwnedCardtable, logs the action into the ledger (TransactionLog), and commits or rolls back as a single atomic unit.
- An exclusive, pessimistic row lock is obtained on the player’s wallet using raw SQL
2. Identity & “Based Pill” Defection Protection
- File: market.js
- Implementation: Rather than dynamically deriving a user’s alignment from coordinate standings (which would break defection mechanics), we query the explicitly declared
quadrantstring field directly fromPlayerAccount.- Currencies are securely mapped directly to native currencies as per Project Bible §3.2.5:
AUTH_RIGHTdinarsAUTH_LEFTlaborLIB_LEFTpronounsLIB_RIGHTmonke
- Currencies are securely mapped directly to native currencies as per Project Bible §3.2.5:
3. Asymmetric Cross-Quadrant Gacha Mechanics
- File: market.js
- Implementation: Refactored
POST /market/buy-packto accept{ targetQuadrant }in the payload.- Pricing Model: Native synthesis packs cost 20 units of native currency, while defector synthesis (cross-quadrant) packs cost 50 units representing the ideological defection tax.
- Gacha Candidates & Cryptographic Randomness: Uses the cryptographically secure server-side Node.js
crypto.randomIntmodule for rolling rarity categories and selecting candidate cards to prevent client-side prediction:COMMON: 60%UNCOMMON: 25%RARE: 10%EPIC: 4%LEGENDARY: 1%
- Queries are strictly filtered by
cardType = 'MEME'andaffinity = targetQuadrantwith automatic fallback mapping if specific rarities are temporarily unavailable.
4. Serverless-Safe Sliding-Window Rate Limiter
- File: rateLimiter.js
- Implementation: Implemented an active sliding-window rate limiter using a Google Cloud Memorystore (Redis) instance (via
ioredis).- Utilizes atomic Redis pipeline transactions (
MULTI/EXEC) with sorted sets (ZSET) to log and clear timestamps per player id: - Strictly limits all
/market/*endpoints to 5 requests per 10 seconds per player. - Designed with defensive fail-open parameters to preserve game playability if Redis encounters transient cloud network outages.
- Utilizes atomic Redis pipeline transactions (
5. Premium Glassmorphic Frontend Page
- File: page.tsx
- Implementation: Structured a responsive layout styled using pure CSS 3D perspectives, custom booster-pack flip animations, and glassmorphic micro-layouts.
- Interactive State Flows: Toggles tabs seamlessly between
Spot Market(individual items/policies) andMeme Bazaar(booster synthesis packs). - Active State Syncing: Syncs with the global layout
<WalletOverlay />by triggering silent UI balance updates immediately on successful transaction receipts. - Overlay Animations: Provides immersive, high-fidelity card-opening animations: shaking booster packs, tearing wrappers, realistic card-flip perspective rotations, and rarity-colored radial glow drops (silver for
COMMONup to celestial golden fire animations forLEGENDARY).
- Interactive State Flows: Toggles tabs seamlessly between
🧪 Verification Results
We executed the end-to-end integration test suite in test-market.js utilizing an in-memoryioredis mocking interceptor to validate functionality against the live Neon Postgres database.
Test Execution Log Output
🔒 Security & Defensive Guardrails Met
- Race-Condition Elimination: Exclusive locked updates protect the balance check and card-minting sequences.
- Duplicate Modifier Seal: A single player cannot buy multiples of the same unique passive item or policy card.
- Sybil & DDoS Mitigation: The horizontal Memorystore sliding window instantly isolates and blocks brute-force purchase attempts.
- Cryptographic Anti-Tampering: Card rarity outcomes are computed strictly backend-side using random numbers derived from node’s native secure entropy generator.