
Trivia Battle AI
Daily Trivia · Brain Game
Trivia Battle AI is a four-option quiz game where an AI generates questions in real time. Across 12 categories, difficulty auto-adjusts to the player's accuracy, while a rotating daily challenge and streak rewards build a light brain-game habit. Built with SwiftUI and Observation, question generation goes through an AWS Lambda proxy rather than calling OpenAI from the device, keeping the API key behind Secrets Manager.
Quiz apps backed by a fixed question bank run dry and repeat quickly, losing the player's interest. By generating fresh questions with OpenAI (gpt-4o-mini) and adapting difficulty across a 1-10 range from the last 10 answers, the app stays endlessly novel while never being too easy or too hard.
App preview
What it does
AI Real-Time Question Generation
An AWS Lambda proxy calls OpenAI (gpt-4o-mini) to produce four-option questions and explanations matched to category, difficulty (1-10), and language, returned in strict JSON mode (response_format: json_object). The prompt maps difficulty into easy/medium/hard/very-hard labels, and the app over-fetches by three questions per request and caches the surplus for seamless play.
Dedup & Prefetch Caching
A QuestionGenerator actor caches unused questions under a "category_difficulty" key and passes already-served question IDs (up to 50) as an exclusion list in the prompt to prevent repeats. If every result is a duplicate, it automatically retries without exclusions as a fallback.
Adaptive Difficulty Engine
AdaptiveDifficultyEngine starts at 5.0 and adjusts the rating by +0.3 on a correct answer (with a +0.2 bonus at a 3+ streak) and -0.4 on a wrong one, clamped to the 1-10 range. A 10-answer sliding window tracks recent accuracy in parallel, keeping challenge at the right level.
Daily Challenge & Streaks
A date seed deterministically rotates the challenge category each day, and consecutive-day streaks grant a score multiplier (up to 2x) and bonus quizzes. A streak calendar view visualizes habit building to keep players coming back.
Pure-Function Scoring
QuizEngine is a side-effect-free static function: it sums a 100-point base with a streak bonus (streak x 10, capped at 50) and a remaining-time bonus (up to 50 points). Faster, more accurate answers earn more, and state is always returned as a fresh immutable QuizState.
AI Hints
A dedicated /quiz/hint Lambda route generates gpt-4o-mini hints with a system prompt engineered to give a 1-2 sentence clue without revealing the answer. It takes the question, options, and correct index and returns an educational hint in the user's language.
Global Leaderboard
An AWS DynamoDB-backed leaderboard Lambda stores score, accuracy, and streak, and serves rankings by today/week/all-time period filters plus a category filter. Scores are validated server-side to a 0-10,000 range and display names are truncated to 20 characters.
Premium Subscription & Hybrid Monetization
StoreKit 2 monthly/yearly subscriptions (with a free trial) unlock the daily quiz limit and four premium categories, while free users see a hybrid model combining Google Mobile Ads rewarded and interstitial ads. App Tracking Transparency (ATT) consent is handled before ad initialization.
How it's built
iOS
Concurrency
Monetization
AI
Backend
Testing & Build
An MVVM structure organized by feature folders paired with pure-function domain engines, with the iOS client separated from an AWS serverless backend. Game logic (scoring, difficulty) is isolated into side-effect-free static enums for deterministic testing, external I/O is handled by actor-based services, and secrets live behind a Lambda proxy.
- 1
QuizEngine and AdaptiveDifficultyEngine are pure-function enums over immutable state (state-in → new-state-out), making scoring and difficulty rules deterministically testable — covered by 15 unit tests
- 2
OpenAIService, QuestionGenerator, and LeaderboardService are isolated as actors, managing cache, dedup tracking, and network safely without data races
- 3
A serverless security design keeps the API key off the device, protected behind an AWS Lambda proxy plus Secrets Manager (the key is cached after cold start)
- 4
The quiz-generation Lambda and the leaderboard Lambda are split by responsibility, written in Node.js ESM (.mjs) on AWS SDK v3 to minimize cold start and dependencies
- 5
The leaderboard Lambda composes period (today/week/all-time) and category filters as DynamoDB FilterExpressions and validates score range (0-10,000) and display-name length server-side
- 6
High-cohesion modules split by feature (Home, Quiz, Leaderboard, Profile, Settings, Onboarding) with @Observable ViewModels and SwiftUI views, syncing the OpenAI prompt language to the device locale (en/ko/ja/zh)
- 7
An AQ design system (Colors, Typography, Spacing, Shadows, Animations, Components) is tokenized in a dedicated Core layer for consistent game UI
12 (8 free + 4 premium)
Quiz categories
4 (en/ko/ja/zh-Hans)
Languages
15
Domain engine unit tests
1–10
Difficulty rating range
2 (quiz + leaderboard)
AWS Lambda functions
76
Swift files



