Game & Simulation
Private

Okey & 101 Multiplayer Game Engine: .NET 9 SignalR Backend

C#SQL.NET 9.0ASP.NET CoreSignalREntity Framework CorePostgreSQLConcurrent CollectionsJWTNginx

A highly concurrent, authoritative multiplayer game engine built under a commercial contract for a Unity-based Okey/101 board game, featuring real-time state synchronization.

Tech Stack

C#SQL.NET 9.0ASP.NET CoreSignalREntity Framework CorePostgreSQLConcurrent CollectionsJWTNginx

System Metrics

Lock-free, thread-safe in-memory state resolution via `ConcurrentDictionary` for high-frequency actions (tile draw/discard) bypassing standard DB latency
Aggressive utilization of `AsNoTracking()` in Entity Framework Core for read-only queries, completely eliminating Change Tracker memory allocation overhead
Optimized Kestrel web server configuration tailored for Nginx reverse proxying, strictly handling `X-Forwarded-For` headers for network edge security

Why Did I Build This?

"Developed as a commercial freelance contract, this project required a low-latency infrastructure for turn-based multiplayer board games where traditional HTTP request-response cycles fall short. To prevent client-side (Unity) manipulation and cheating, I needed to build a strictly authoritative server where all game logic, tile distribution, and win validations execute back-end. Modularity was crucial to support various game modes (Classic, 101, Bowl) without degrading into monolithic spaghetti code."

Architecture & Decisions

The core runs on .NET 9.0, utilizing SignalR Hubs (`SocialHub`, `TableHub`) for full-duplex real-time communication. To completely bypass database I/O bottlenecks during high-frequency gameplay events, active game states (`GameMatchState`) are managed in RAM using a thread-safe `ConcurrentDictionary`. Diverse game rules are strictly decoupled via the Strategy Pattern implementing the `IGameRuleHandler` interface. Persistence (inventory, transactional ledgers, chat) is handled by PostgreSQL via Entity Framework Core, while heavy aggregate tasks like calculating leaderboards are offloaded to an asynchronous `PeriodicTimer`-based `BackgroundService` worker.

Key Features

  • 01.Production-ready economy and progression system enforcing transactional integrity across inventory, XP, and ledgers
  • 02.Isolated matchmaking tables and clan/private chat rooms orchestrated via the SignalR Group API
  • 03.Strategy Pattern implementation dynamically injecting diverse game rule constraints (Okey 101, Classic, 1v1) at runtime via DI
  • 04.Server-side signaling relay infrastructure to support WebRTC-based in-game voice chat
  • 05.Asynchronous background workers periodically computing and caching daily, weekly, and monthly leaderboards
The most critical architectural decision in this commercial delivery was the strict decoupling of in-memory State Management from database Persistence. During active gameplay, tile pointers and turn sequences are resolved in memory in sub-milliseconds. Upon game completion (`ClaimWin`), the engine executes an atomic `db.Database.BeginTransactionAsync()` block to distribute XP, rewards, and bowl splits to PostgreSQL. This guarantees absolute ACID compliance, preventing any virtual currency duplication or ledger inconsistencies even during catastrophic server crashes.