Skip to main content

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 the pcmtg-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.$transaction blocks.
    • An exclusive, pessimistic row lock is obtained on the player’s wallet using raw SQL FOR UPDATE prior to checking or validating any fund balances:
    • The transaction block atomically deducts native funds, inserts a record into the OwnedCard table, logs the action into the ledger (TransactionLog), and commits or rolls back as a single atomic unit.

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 quadrant string field directly from PlayerAccount.
    • Currencies are securely mapped directly to native currencies as per Project Bible Β§3.2.5:
      • AUTH_RIGHT β†’\rightarrow dinars
      • AUTH_LEFT β†’\rightarrow labor
      • LIB_LEFT β†’\rightarrow pronouns
      • LIB_RIGHT β†’\rightarrow monke

3. Asymmetric Cross-Quadrant Gacha Mechanics

  • File: market.js
  • Implementation: Refactored POST /market/buy-pack to 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.randomInt module 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' and affinity = targetQuadrant with 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.

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) and Meme 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 COMMON up to celestial golden fire animations for LEGENDARY).

πŸ§ͺ Verification Results

We executed the end-to-end integration test suite in test-market.js utilizing an in-memory ioredis mocking interceptor to validate functionality against the live Neon Postgres database.

Test Execution Log Output


πŸ”’ Security & Defensive Guardrails Met

  1. Race-Condition Elimination: Exclusive locked updates protect the balance check and card-minting sequences.
  2. Duplicate Modifier Seal: A single player cannot buy multiples of the same unique passive item or policy card.
  3. Sybil & DDoS Mitigation: The horizontal Memorystore sliding window instantly isolates and blocks brute-force purchase attempts.
  4. Cryptographic Anti-Tampering: Card rarity outcomes are computed strictly backend-side using random numbers derived from node’s native secure entropy generator.