Game Module Guide
"Headless Game Backend" infrastructure for modern web-based games.
The Game Module (apps/game-starter) offers critical features for multiplayer games, such as leaderboards, real-time lobby management, and virtual economies. RapidCore acts as a powerful bridge between your game engine (Unity, Phaser, etc.) and the web world.
Game Engine Integration (Unity & Phaser)
RapidCore works as a Headless Game Backend for your game. You can communicate with the server from within your game using REST API or WebSockets.
API Requests
You can fetch a player's data from within the game like this:
// Unity (C#) Example
IEnumerator GetPlayerData(string playerId) {
using (UnityWebRequest webRequest = UnityWebRequest.Get("https://api.rapidcore.io/game/player/" + playerId)) {
yield return webRequest.SendWebRequest();
if (webRequest.result == UnityWebRequest.Result.Success) {
Debug.Log("Data Received: " + webRequest.downloadHandler.text);
}
}
}
Avoid making API requests in every frame within the game engine. Keep data in a local cache and only send it to the server at critical moments (Level end, purchase).
Real-time Lobby and Synchronization
We use Supabase Realtime channels for the multiplayer experience. This allows players to see each other's status instantly.
Lobby Configuration
Use the following structure in apps/game-starter/src/lib/lobby.ts to track lobby status:
const lobbyChannel = supabase.channel('lobby-01')
.on('presence', { event: 'sync' }, () => {
const state = lobbyChannel.presenceState();
console.log('Active Players:', state);
})
.subscribe();
Low-latency
To reduce packet size in WebSocket connections, consider sending data in Protocol Buffers format instead of JSON.
Virtual Economy and Inventory Control
A secure economy system is essential for the sustainability of your game.
- Secure Wallet: Player gold and gems are never updated without server-side validation.
- Purchase Flow: The process of adding packages purchased via Stripe to the inventory is shown in the diagram below:
graph TD
A[Player] -->|Stripe Checkout| B(Payment Confirmation);
B -->|Webhook| C{Server Verification};
C -->|Success| D[Add to Inventory];
C -->|Error| E[Log & Cancel];
D -->|Realtime| F[HUD Update];
Edge Function Security (Anti-Cheat)
To prevent cheating, scores and rewards should never be calculated on the client-side.
Why Edge Functions?
Vercel Edge Functions process requests at the point closest to the user, reducing latency while providing server-side security.
Score Verification Example
// apps/game-starter/api/verify-score.ts
export default async function handler(req: Request) {
const { score, hash } = await req.json();
const isValid = verifySignature(score, hash); // Verification with server key
if (!isValid) return new Response("Cheat detected!", { status: 403 });
await db.leaderboard.update(score);
return new Response("Score recorded.");
}
Game-Specific UI Components
You can create HUDs and leaderboards specific to your game using Tailwind v4.
- HUD: Components like health bars and ammo indicators are available in
packages/ui. - Inventory Slots: Use the
inventory-slotclass for modern glassmorphism slots:
<div class="inventory-slot hover:border-pink-500 transition-all">
<img src="/sword.png" alt="Sword" />
</div>
Keep HUD components in a fixed position with the highest z-index so they are always visible over the game scene.