A multiplayer racing game with real-time leaderboards, built with Unity and Nakama.
Apex-Racer/
├── backend/ # Nakama game server
│ ├── nakama/ # Nakama configuration and modules
│ │ ├── modules/ # Lua runtime modules
│ │ └── migrations/ # Database schema
│ └── tests/ # Backend test scripts
└── client-unity/ # Unity game client
├── Assets/ # Unity project assets
└── ProjectSettings/ # Unity configuration
- Cycle-Based Leaderboards - 5-minute racing cycles with automatic resets
- Authentication System - JWT-based auth with email/password and device ID
- Reward Distribution - Automatic rewards for top players each cycle
- Anti-Cheat System - Time-bound validation with admin review tools
- Idempotent Operations - Safe retry logic for all submissions
- Data Persistence - PostgreSQL storage via Nakama Storage API
- Nakama Integration - Full C# SDK integration
- Retry Logic - Exponential backoff for network operations
- Authentication UI - Email/password and device authentication
- Leaderboard Display - Real-time leaderboard updates
- Reward History - View past rewards and rankings
- Docker and Docker Compose
- Unity 2021.3+ (for client development)
-
Navigate to the backend directory:
cd backend/nakama -
Start the services:
docker-compose up --build
-
Access the Nakama Console:
- URL: http://localhost:7351
- Default credentials: admin/password
-
API Endpoints:
- HTTP API: http://localhost:7350
- WebSocket: ws://localhost:7351
-
Open the project in Unity:
cd client-unity # Open in Unity Editor
-
Open the main scene in
Assets/Scenes/ -
Press Play to run in the editor
- README.md - Comprehensive backend documentation
- SECURITY.md - Security configuration and best practices
- AUTH.md - Authentication system details
- ANTI_CHEAT.md - Anti-cheat implementation
- AUTOMATIC_RESET.md - Leaderboard reset system
- SCALABILITY.md - Scaling strategies
- DATABASE.md - Database schema
- STORAGE_QUERIES.md - Query examples
- QUICKSTART_UI.md - UI quick start guide
- UI_SETUP_GUIDE.md - Detailed UI setup
Run the backend tests:
cd backend/tests
# Basic leaderboard test
./test-leaderboard-simple.sh
# Automatic reset test
./test-automatic-reset.sh
# Anti-cheat test
./test-anti-cheat.sh
# Authentication test
./test-auth.shThis project uses default credentials for local development. These are intentionally simple to make local testing easy.
IMPORTANT: Never use these default credentials in production!
See SECURITY.md for:
- Production security configuration
- Generating secure credentials
- Environment variable setup
- Best practices
- Backend: Nakama (Lua runtime) + PostgreSQL
- Client: Unity (C#) + Nakama SDK
- Infrastructure: Docker + Docker Compose
- Scheduler: Custom cron service for automatic resets
- Lua Runtime - Simpler than Go plugins, no compilation needed
- External Scheduler - More reliable than internal timers
- Client-Side Idempotency Keys - Prevents duplicate submissions
- Cycle-Based Natural Keys - Automatic idempotency without coordination
- Non-Blocking Anti-Cheat - Flags suspicious activity without blocking users
- Submission latency: ~100ms p99
- Leaderboard query: ~50ms p99
- Reset duration: ~5s (1K users)
- Supports: 1K-10K active users (current setup)
For scaling beyond 10K users, see SCALABILITY.md.
curl -X POST "http://localhost:7350/v2/rpc/submit_race_time?http_key=defaultkey" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '"{\"race_time\":45.123,\"idempotency_key\":\"unique-uuid-here\"}"'curl -X POST "http://localhost:7350/v2/rpc/race_reward_history?http_key=defaultkey" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '""'curl -X GET "http://localhost:7350/v2/leaderboard/race_times?http_key=defaultkey"- Lua modules are in
backend/nakama/modules/ - Changes are hot-reloaded on file save
- Check logs:
docker logs nakama-go-server
- Scripts are in
client-unity/Assets/Scripts/ - Main connection manager:
NakamaConnectionManager.cs - Authentication:
NakamaEmailAuth.cs
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
Apex-Racer - Racing event simulator using Unity and Nakama Copyright (C) 2024 Ramin Shahab
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
See LICENSE for the full license text.
For issues or questions:
- Check the documentation in the
backend/folder - Review the test scripts for usage examples
- Check Nakama logs for debugging
- Mobile build support
- Additional race tracks
- Ghost replays
- Friend challenges
- Advanced anti-cheat metrics
- Global leaderboards (cross-region)