Jocs web
42 Secret Horse Race
Competitive web game of horse race type for 42 Barcelona: data pooling, ranking, metrics and OAuth.
Context
Competitive web game of horse race type designed for the 42 Barcelona community. It uses the 42 OAuth API for authentication and access to user data, with a real-time ranking system and periodic data pooling.
What it does
- 42 OAuth Authentication: secure login with the official 42 School API
- Data pooling: periodic collection of user data from the 42 API
- Ranking system: real-time leaderboard with metrics and statistics
- Web interface: interactive dashboard using Jinja2, HTMX for partial updates without reload
- REST API: endpoints to access ranking data and metrics programmatically
- Scheduler: periodic tasks to update data and recalculate rankings
Architecture
FastAPI backend with PostgreSQL via SQLAlchemy, Jinja2 templates with HTMX for interactivity, and Alpine.js for lightweight client-side logic. Deployed with Docker.
Main challenges
- 42 OAuth: authentication flow with expiring tokens, refresh tokens and rate limits from the API
- Efficient pooling: fetching data from many users without saturating the 42 API
- Real-time ranking: recalculating positions when changes occur efficiently
- Concurrency: multiple users accessing the ranking simultaneously
Solutions
- Complete implementation of the 42 OAuth flow with token management and refresh
- Pooling system with configurable intervals and retry-based error handling
- Ranking calculated with optimized PostgreSQL queries
- Async throughout the data pipeline to maximize throughput
Lessons learned
- OAuth 2.0 has many nuances when dealing with APIs that have strict rate limits
- HTMX provides fast interactivity without the complexity of an SPA framework
- PostgreSQL is superior to SQLite for rankings with complex queries and concurrency