- What is CodeBattle Arena?
- How Does It Work? (Simple Explanation)
- Project Structure (Like a Building Blueprint)
- The Frontend (What You See and Touch)
- The Backend (The Brain Behind Everything)
- How Users Move Around (The Journey)
- Special Features Explained
- How Everything Connects (The Magic)
- Database Structure (Where Information Lives)
- Security & Authentication (Keeping Users Safe)
- Development Setup (How to Build It)
- Troubleshooting Guide
Imagine if video games and programming had a baby - that's CodeBattle Arena! It's a website where people learn to code by solving puzzles and can even battle other programmers in real-time, like a coding video game.
- Practice Mode: Solo coding challenges (like single-player games)
- Arena Mode: Real-time 1v1 coding battles (like multiplayer games)
- AI Assistant: A smart robot helper that gives coding tips
- Progress Tracking: See how you improve over time
- Leaderboards: Rankings to see who's the best coder
Think of CodeBattle Arena like a digital coding school with these rooms:
- Location:
server/folder - What it does: This is like the school's main office that:
- Checks if students are allowed in (authentication)
- Stores all the homework problems (database)
- Runs students' code safely (code execution)
- Manages real-time battles (WebSocket connections)
- Provides AI help (artificial intelligence)
- Location:
client/folder - What it does: These are the rooms students see:
- Landing Page: The entrance hall
- Dashboard: Student's personal room with stats
- Practice Rooms: Where students solve problems alone
- Arena: The battle arena for competitions
- Leaderboard: The hall of fame
- HTTP Requests: Like passing notes between rooms
- WebSockets: Like having a live phone call during battles
- Database: Like a giant filing cabinet storing everything
CodeBattle-Arena/
โโโ ๐ server/ # The Brain (Backend)
โ โโโ index.js # Main control center
โ โโโ database.js # User data manager
โ โโโ problemDatabase.js # Problem storage system
โ โโโ arenaDatabase.js # Battle data manager
โ โโโ arenaSocket.js # Real-time battle controller
โ โโโ codeRun.js # Code execution engine
โ โโโ aiAssistance.js # AI helper brain
โ โโโ userStatsService.js # Progress tracker
โ โโโ seedDatabase.js # Sample data creator
โ
โโโ ๐ client/ # What Users See (Frontend)
โ โโโ ๐ public/ # Pages anyone can see
โ โ โโโ LandingPage/ # Welcome page
โ โ โโโ Accounts/ # Sign in/up pages
โ โ
โ โโโ ๐ private/ # Pages only for logged-in users
โ โโโ HomePage/ # Personal dashboard
โ โโโ CoderPage/ # Main coding environment
โ โโโ Arena/ # Battle arena
โ โโโ Easy/ # Beginner problems
โ โโโ Intermediate/ # Medium problems
โ โโโ Advanced/ # Hard problems
โ โโโ Real-World/ # Project-based challenges
โ โโโ Leaderboard/ # Rankings page
โ โโโ common/ # Shared tools
โ
โโโ ๐ Configuration Files
โโโ package.json # Project settings
โโโ README.md # Project overview
โโโ .env # Secret keys (not in repo)
- Purpose: First page visitors see - like a store window
- Files:
index.html: The page structurestyle.css: How it looks (colors, layout)script.js: Interactive features (button clicks, animations)
- Features:
- Introduces the platform
- Sign up/login buttons
- FAQ section
- Smooth scrolling animations
- signin.html: Login page
- signup.html: Registration page
- style.css: Shared styling for both pages
- Uses: Clerk authentication service (like a security guard)
- Purpose: User's personal control center
- Files:
codigo.html: The main dashboard layoutcodigo.css: All the visual styling (over 1000 lines!)script.js: Dynamic features (updating stats, navigation)
Dashboard Features:
<!-- Welcome Section -->
<div class="welcome-txt">
<h1>๐ป CodiGo Dashboard</h1>
<h2>Welcome, [Username]</h2>
</div>
<!-- Stats Cards -->
<div class="dashboard-container">
<div class="dashboard-column">
<!-- Quick Stats -->
<div class="dashboard-card welcome-card">
- Current Rank
- Arena Wins
- Streak Days
- Next Contest
</div>
<!-- Problems Solved -->
<div class="dashboard-card problems-card">
- Total solved
- Easy/Medium/Hard breakdown
- Visual progress indicators
</div>
</div>
<!-- Quick Actions (2x2 grid) -->
<div class="quick-actions-grid">
- Practice Coding
- Arena Duels
- Join Contest
- View Leaderboard
</div>
<!-- Activity Graph -->
<div class="activity-card">
- Coding activity over time
- Streak visualization
- Performance metrics
</div>
</div>- Purpose: Where users actually write and test code
- Files:
coder.html: The coding interfacecoder.css: Editor stylingcoder.js: Code editor logic (892 lines of functionality!)
Key Components:
- ACE Code Editor: Professional code editor (like VS Code in browser)
- Language Support: JavaScript, Python, C++, Java
- AI Assistant: Smart helper for beginners (only on Easy problems)
- Test Runner: Automatic code testing
- Problem Sidebar: Browse and select challenges
// AI Assistant Integration (only for Easy problems)
if (currentProblem && currentProblem.difficulty === 'easy') {
aiAssistanceInstance = new AIAssistanceManager(editor, currentLanguage);
aiAssistanceInstance.setProblem(currentProblem);
console.log('โ
AI Assistance enabled for beginner level problem');
}- Purpose: Real-time 1v1 coding competitions
- Architecture: Modular design with 7 specialized modules
Arena Modules:
// Core Arena Components
โโโ ArenaCore.js # Main controller (initialization, coordination)
โโโ ArenaAuth.js # User authentication for arena
โโโ ArenaSocketManager.js # Real-time communication with server
โโโ ArenaUIManager.js # User interface management
โโโ ArenaMatchManager.js # Match logic and progression
โโโ ArenaCodeManager.js # Code editor for arena battles
โโโ ArenaEffects.js # Visual effects and animationsHow Arena Battles Work:
- Queue System: Players join a difficulty-based queue
- Matchmaking: Server pairs players of similar skill
- Ready Phase: Both players confirm they're ready
- Battle: 5 coding problems, timed progression
- Scoring: 10 points per test case + 5 bonus for completion
- Results: Winner determined, stats updated
- Easy: 5-minute problems for beginners (with AI help)
- Intermediate: 8-minute problems for developing skills
- Advanced: 15-minute problems for experts
- Real-World: Project-based challenges (games, web apps, AI/ML)
- auth.js: Authentication helper (111 lines)
- aiAssistance.js: AI system (1541 lines!)
- problemLoader.js: Problem management
- problemStyles.css: Consistent styling across problem pages
The entire visual design is built on a CSS variable system:
:root {
--bg-dark: #0d1117; /* Dark background */
--bg-light: #f5f5f5; /* Light background */
--nav-bg: #191923; /* Navigation background */
--card-bg: #21262d; /* Card backgrounds */
--text-light: #e0e0e0; /* Light text */
--text-dark: #333; /* Dark text */
--accent-green: #00ff00; /* Primary accent */
--accent-blue: #539bf5; /* Secondary accent */
/* ... more color definitions */
}Responsive Design:
- Desktop: Multi-column layouts
- Tablet: Responsive grids
- Mobile: Single-column stacking
This is the central command center (254 lines) that:
// Essential Services
import { MongooseConnect, UserDBHandler } from "./database.js";
import { UserStatsService } from "./userStatsService.js";
import { CodeRunner } from "./codeRun.js";
import { ProblemDBHandler } from "./problemDatabase.js";
import { ArenaDBHandler } from "./arenaDatabase.js";
import { ArenaSocketHandler } from "./arenaSocket.js";
import { AIAssistanceService } from "./aiAssistance.js";What the server does:
- Authentication: Checks if users are allowed in
- File Serving: Delivers web pages to browsers
- API Endpoints: Handles requests for data
- Real-time Communication: Manages arena battles
- Code Execution: Runs user code safely
- AI Integration: Provides intelligent assistance
User Schema (like a form for each user):
userSchema = {
userID: String, // Unique identifier
name: String, // Display name
rank: Number, // Skill ranking
contests_count: Number, // Arena battles fought
streak_count: Number, // Days coding consecutively
problemsSolved: Number, // Total problems completed
easyCount: Number, // Easy problems solved
mediumCount: Number, // Medium problems solved
hardCount: Number, // Hard problems solved
realWorldCount: Number, // Project challenges completed
lastSolvedDate: Date, // When they last coded
admin: Boolean // Special permissions
}Real-Time Battle Management (1077 lines of battle logic!):
// Queue System
this.activeMatches = new Map(); // Current battles
this.playerTimers = new Map(); // Individual player timers
this.playerProgress = new Map(); // Each player's question progress
this.playerReadyState = new Map(); // Who's ready to start
// Battle Flow
1. Player joins queue โ 'arena:join-queue'
2. Server finds match โ handleMatchCreated()
3. Both players ready โ 'arena:ready'
4. Battle starts โ startMatchForPlayers()
5. Players submit โ 'arena:submit'
6. Results calculated โ updatePlayerStats()Arena Database Schema:
// Queue entries (waiting players)
queueSchema = {
userId: String,
username: String,
difficulty: String,
joinedAt: Date
}
// Active matches
arenaMatchSchema = {
matchId: String,
players: [{ userId, username, score, currentQuestion }],
difficulty: String,
status: 'waiting' | 'in_progress' | 'completed',
questions: [Array of problem IDs],
startedAt: Date,
completedAt: Date
}
// Player statistics
arenaPlayerStatsSchema = {
userId: String,
totalMatches: Number,
wins: Number,
losses: Number,
winRate: Number,
avgScore: Number,
bestStreak: Number,
currentStreak: Number,
rank: Number
}Google Gemini AI Integration (554 lines of intelligence):
class AIAssistanceService {
constructor() {
this.genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
this.model = this.genAI.getGenerativeModel({ model: 'gemini-2.0-flash' });
}
async analyzeCodeLine(code, language, problem, currentLine) {
// Intelligent code analysis
// Returns: suggestions, errors, warnings
}
async performRealTimeAnalysis(code, currentLine, language, problem) {
// Real-time help as user types
}
async analyzeTestCaseFailure(code, language, problem, testResults) {
// Explains why tests failed
}
}AI Features:
- Line-by-line analysis: Smart suggestions as you code
- Error explanation: Explains what went wrong
- Test case failure analysis: Helps debug failed tests
- Context-aware: Understands the specific problem
- Beginner-friendly: Only activates for Easy problems
Secure Code Running (73 lines of power):
class CodeRunner {
async endpoint(req, res) {
// Uses Piston API for secure execution
// Supports 40+ programming languages
// 10-second timeout for safety
// Returns output, errors, execution time
}
}How it works:
- User writes code in editor
- Frontend sends code to
/api/run/:language - Server sends code to Piston API (secure sandbox)
- Piston runs code safely in isolated environment
- Results sent back to user
Supported Languages: JavaScript, Python, C++, Java, C, Rust, Go, PHP, Ruby, and 30+ more!
Comprehensive Progress Management:
class UserStatsService {
// Track user progress across all activities
async updateOnProblemSolved(userId, difficulty, category) {
// Update problem counts
// Calculate streak
// Update rankings
// Broadcast real-time updates
}
async getLeaderboard(type = 'overall', limit = 100) {
// Returns top performers
// Supports: overall, arena, practice
}
async calculateRankPosition(userId) {
// Where user ranks globally
}
}Problem Database Schema (592 lines of challenge management):
problemSchema = {
problemId: String, // Unique identifier
title: String, // Problem name
description: String, // What to solve
difficulty: 'easy' | 'medium' | 'hard' | 'real-world',
category: String, // Arrays, Algorithms, etc.
tags: [String], // Keywords
examples: [{ // Sample inputs/outputs
input: String,
output: String,
explanation: String
}],
testCases: [{ // Hidden test cases
input: String,
expectedOutput: String,
isHidden: Boolean
}],
hints: [String], // Help for struggling users
timeLimit: Number, // Milliseconds
memoryLimit: Number, // MB
solvedCount: Number, // How many solved it
attemptCount: Number // Total attempts
}graph TD
A[๐ Landing Page] --> B{User Status}
B -->|New User| C[๐ Sign Up]
B -->|Returning| D[๐ Sign In]
C --> E[โ
Authentication via Clerk]
D --> E
E --> F[๐ Dashboard]
F --> G[๐ Practice Mode]
F --> H[โ๏ธ Arena Mode]
F --> I[๐ Real-World Projects]
F --> J[๐ Leaderboard]
G --> G1[๐ข Easy Problems]
G --> G2[๐ก Medium Problems]
G --> G3[๐ด Hard Problems]
H --> H1[๐ฏ Select Difficulty]
H1 --> H2[โณ Join Queue]
H2 --> H3[๐ค Match Found]
H3 --> H4[โก Ready Phase]
H4 --> H5[๐ฅ Live Battle]
H5 --> H6[๐ Results & Stats]
Sidebar Navigation (in all private pages):
// Navigation links with onclick handlers
showSection('home') โ Dashboard
showSection('practice') โ Problem selection
'../Arena/arena.html' โ Arena battles
showSection('help') โ AI assistance
showSection('contact') โ Contact formDynamic Section Switching:
function showSection(sectionName) {
// Hide all sections
document.querySelectorAll('.section').forEach(section => {
section.classList.remove('active');
});
// Show selected section
document.getElementById(sectionName).classList.add('active');
// Update navigation highlighting
updateNavigation(sectionName);
}When it activates: Only for Easy difficulty problems (beginner-friendly)
What it does:
- Real-time Analysis: Watches your code as you type
- Error Detection: Spots mistakes before you even run code
- Smart Suggestions: Offers improvements
- Test Failure Explanation: Explains why tests didn't pass
- Context Awareness: Understands the specific problem you're solving
Visual Indicators (VS Code-style):
// AI status indicator in UI
<div class="ai-status-indicator">
<span class="ai-status-icon">๐ค</span>
<span class="ai-status-text">AI Assistant Active</span>
<button class="ai-status-toggle">๐๏ธ</button>
</div>How it works behind the scenes:
// Frontend sends code for analysis
fetch('/api/ai/real-time-analysis', {
method: 'POST',
body: JSON.stringify({
code: userCode,
currentLine: lineNumber,
language: selectedLanguage,
problem: currentProblem
})
});
// AI responds with suggestions
{
"suggestions": [...],
"errors": [...],
"warnings": [...]
}Matchmaking Process:
- Queue Join: Player selects difficulty and joins queue
- Skill Matching: Server finds opponent of similar level
- Match Creation: Both players notified of match
- Ready Check: Both must confirm ready to start
- Battle Begin: 5 problems, independent timers
- Live Updates: Real-time progress tracking
- Results: Winner determined, stats updated
Independent Player Progression:
// Each player has their own:
this.playerTimers = new Map(); // Individual timers
this.playerProgress = new Map(); // Current question number
this.playerScores = new Map(); // Running score totals
// Players can be on different questions!
Player A: Question 3, 47 points
Player B: Question 2, 28 pointsScoring System:
- 10 points per test case passed
- 5 bonus points for complete solution
- Typical problem: 5-6 test cases = 55-65 points max
- Match total: 5 problems = ~275-325 points possible
Multi-Layered Stats System:
// Basic User Stats (database.js)
{
rank: 1250, // Skill ranking points
contests_count: 15, // Arena battles fought
streak_count: 7, // Days coding consecutively
problemsSolved: 48 // Total problems completed
}
// Detailed Stats (userStatsService.js)
{
easyCount: 25, // Easy problems solved
mediumCount: 18, // Medium problems solved
hardCount: 5, // Hard problems solved
realWorldCount: 3, // Project challenges
rankPosition: 127, // Global leaderboard position
lastSolvedDate: "2024-01-15" // Last activity
}
// Arena-Specific Stats (arenaDatabase.js)
{
totalMatches: 15, // Arena battles
wins: 9, // Victories
losses: 6, // Defeats
winRate: 60.0, // Win percentage
avgScore: 187.5, // Average match score
bestStreak: 4, // Best win streak
currentStreak: 2, // Current win streak
arenaRank: 45 // Arena-specific ranking
}Real-Time Updates: Stats update instantly across all pages when users complete activities.
Multiple Leaderboard Types:
- Overall Leaderboard: Based on total problems solved + rank points
- Arena Leaderboard: Based on arena wins and win rate
- Category Leaderboards: Per difficulty level
- Real-Time Updates: Automatically refreshes during competitions
Project Categories:
- ๐ฎ Games: Build actual games (Snake, Tetris, etc.)
- ๐ Web Applications: Create functional websites
- ๐ค AI/ML Projects: Machine learning challenges
- ๐ง Algorithms: Complex algorithmic challenges
- ๐ฑ IoT Projects: Internet of Things simulations
Extended Time Limits: These challenges can take hours or days to complete.
HTTP Requests (Normal web communication):
// Get user data
GET /api/userdata
โ Returns: { name, rank, contests_count, streak_count, ... }
// Submit a solution
POST /api/problems/:problemId/submit
โ Body: { code, language }
โ Returns: { success, testResults, score }
// Run code
POST /api/run/:language
โ Body: { code, input }
โ Returns: { output, error, executionTime }
// AI assistance
POST /api/ai/analyze-code
โ Body: { code, language, problem, currentLine }
โ Returns: { suggestions, errors, warnings }WebSocket Communication (Real-time for Arena):
// Join arena queue
socket.emit('arena:join-queue', { userId, username, difficulty });
โ Server response: 'arena:queue-joined' or 'arena:match-found'
// Battle events
socket.emit('arena:submit', { matchId, code, questionId });
โ Server broadcasts: 'arena:player-submitted' to opponent
// Real-time updates
socket.on('arena:opponent-progress', (data) => {
// Show opponent's progress in real-time
});Example 1: User Solves a Problem
sequenceDiagram
participant U as User
participant F as Frontend
participant S as Server
participant DB as Database
participant AI as AI Service
U->>F: Writes code & clicks submit
F->>S: POST /api/problems/123/submit
S->>AI: Analyze code (if Easy problem)
AI-->>S: Return suggestions
S->>S: Run code against test cases
S->>DB: Update user stats if passed
S->>F: Return results + AI feedback
F->>U: Show success + updated stats
Example 2: Arena Battle
sequenceDiagram
participant P1 as Player 1
participant P2 as Player 2
participant S as Server
participant DB as Database
P1->>S: Join queue (Easy)
P2->>S: Join queue (Easy)
S->>DB: Create match record
S->>P1: Match found!
S->>P2: Match found!
P1->>S: Ready to start
P2->>S: Ready to start
S->>P1: Battle begins (5 problems)
S->>P2: Battle begins (5 problems)
loop For each submission
P1->>S: Submit solution
S->>DB: Update scores
S->>P2: Opponent progress update
end
S->>P1: Final results
S->>P2: Final results
S->>DB: Update arena statistics
Users Collection
โโโ Basic Info (name, rank, etc.)
โโโ Problem Stats (easy/medium/hard counts)
โโโ Arena Stats (wins, losses, streaks)
Problems Collection
โโโ Problem Details (title, description)
โโโ Test Cases (input/output pairs)
โโโ Metadata (difficulty, category, tags)
Submissions Collection
โโโ Code + Results
โโโ Links to Users + Problems
โโโ Performance Metrics
Arena Matches
โโโ Match Details (players, difficulty)
โโโ Question Progression
โโโ Real-time Scores
User Problem Solved
โโโ First-time Completions
โโโ Links Users โ Problems
โโโ Prevents Duplicate Counting
Users Collection (UserDBHandler.userSchema):
{
_id: ObjectId("..."),
userID: "user_2ABC123XYZ", // Clerk user ID
name: "CodeWarrior123", // Display name
rank: 1250, // Skill points
contests_count: 15, // Arena battles
streak_count: 7, // Daily coding streak
admin: false, // Admin privileges
lastSolvedDate: ISODate("..."), // Last activity
problemsSolved: 48, // Total solved
easyCount: 25, // Easy problems
mediumCount: 18, // Medium problems
hardCount: 5, // Hard problems
realWorldCount: 3, // Project challenges
createdAt: ISODate("..."), // Account creation
updatedAt: ISODate("...") // Last modification
}Problems Collection (problemSchema):
{
_id: ObjectId("..."),
problemId: "array-sum-easy-001", // Unique identifier
title: "Array Sum Challenge", // Display name
description: "Calculate the sum...", // Problem description
difficulty: "easy", // easy|medium|hard|real-world
category: "Arrays", // Arrays|Algorithms|Math|etc.
tags: ["arrays", "loops", "basic"], // Search keywords
constraints: "1 <= n <= 1000", // Input limitations
examples: [{ // Sample cases
input: "[1, 2, 3, 4, 5]",
output: "15",
explanation: "1+2+3+4+5 = 15"
}],
testCases: [{ // Hidden test cases
input: "[1, 2, 3]",
expectedOutput: "6",
isHidden: false
}, {
input: "[10, -5, 3, 0]",
expectedOutput: "8",
isHidden: true // Hidden from users
}],
hints: [ // Progressive hints
"Try using a loop",
"Initialize a variable to 0",
"Add each element to your sum"
],
timeLimit: 5000, // 5 seconds
memoryLimit: 256, // 256 MB
solvedCount: 127, // How many solved it
attemptCount: 203, // Total attempts
createdAt: ISODate("..."),
updatedAt: ISODate("...")
}Submissions Collection (submissionSchema):
{
_id: ObjectId("..."),
submissionId: "sub_20240115_001", // Unique submission ID
userId: "user_2ABC123XYZ", // Who submitted
problemId: "array-sum-easy-001", // Which problem
code: "function sum(arr) { ... }", // User's code
language: "javascript", // Programming language
status: "accepted", // Result status
executionTime: 45, // Milliseconds
memoryUsed: 12.5, // MB used
testCasesPassed: 6, // How many passed
totalTestCases: 6, // Total test cases
submittedAt: ISODate("...") // When submitted
}
// Status values:
// "accepted" - All tests passed
// "wrong_answer" - Some tests failed
// "time_limit_exceeded" - Too slow
// "runtime_error" - Code crashed
// "compile_error" - Syntax errorsArena Matches Collection (arenaMatchSchema):
{
_id: ObjectId("..."),
matchId: "match_20240115_001", // Unique match ID
players: [{ // Array of players
userId: "user_2ABC123XYZ",
username: "CodeWarrior123",
score: 187, // Current score
currentQuestion: 3, // Which problem they're on
questionsCompleted: 2, // How many finished
isReady: true, // Ready to start
joinedAt: ISODate("...") // When they joined
}, {
userId: "user_2DEF456ABC",
username: "PythonMaster",
score: 165,
currentQuestion: 3,
questionsCompleted: 2,
isReady: true,
joinedAt: ISODate("...")
}],
difficulty: "medium", // Match difficulty
status: "in_progress", // Match state
questions: [ // Problems for this match
"array-reverse-med-005",
"string-search-med-012",
"tree-traversal-med-008",
"sort-algorithm-med-015",
"graph-path-med-003"
],
startedAt: ISODate("..."), // Battle start time
completedAt: null, // When finished (null if ongoing)
duration: 1847000, // Match length in ms
winner: null, // Winner's userId (null if ongoing)
createdAt: ISODate("...")
}
// Status values:
// "waiting" - Waiting for players to join
// "ready" - Both players ready, about to start
// "in_progress" - Battle ongoing
// "completed" - Battle finished
// "abandoned" - One player leftArena Player Stats (arenaPlayerStatsSchema):
{
_id: ObjectId("..."),
userId: "user_2ABC123XYZ", // Player identifier
totalMatches: 15, // Arena battles fought
wins: 9, // Victories
losses: 6, // Defeats
draws: 0, // Tied matches
winRate: 60.0, // Win percentage
avgScore: 187.5, // Average match score
bestScore: 295, // Highest single match
worstScore: 95, // Lowest single match
bestStreak: 4, // Best consecutive wins
currentStreak: 2, // Current win streak
totalScore: 2812, // Lifetime arena points
rank: 1450, // Arena ranking points
lastMatchAt: ISODate("..."), // Last battle
createdAt: ISODate("..."),
updatedAt: ISODate("...")
}User Problem Solved (Prevents duplicate counting):
{
_id: ObjectId("..."),
userId: "user_2ABC123XYZ", // Who solved it
problemId: "array-sum-easy-001", // Which problem
difficulty: "easy", // Problem difficulty
category: "Arrays", // Problem category
firstSolvedAt: ISODate("..."), // When first solved
totalAttempts: 3, // How many tries it took
bestSubmissionId: "sub_20240115_003" // Best submission
}
// Compound index: { userId: 1, problemId: 1 } unique
// This ensures each user can only be counted once per problem// User leaderboard performance
UserSchema.index({ rank: -1, problemsSolved: -1, streak_count: -1 });
// User lookups
UserSchema.index({ userID: 1 });
// Arena matchmaking
ArenaMatchSchema.index({ status: 1, difficulty: 1, createdAt: 1 });
// Problem searches
ProblemSchema.index({ difficulty: 1, category: 1, tags: 1 });
// Submission history
SubmissionSchema.index({ userId: 1, submittedAt: -1 });What is Clerk?: A third-party service that handles all the complicated security stuff so developers don't have to worry about it.
How it works:
// 1. Frontend initialization
await Clerk.load();
// 2. Check if user is signed in
if (Clerk.user) {
// User is authenticated
const userID = Clerk.user.id;
const userName = Clerk.user.firstName;
}
// 3. Sign in process
await Clerk.signIn.create({
identifier: "user@example.com",
password: "userpassword"
});Security Features:
- Encrypted passwords: Never stored in plain text
- Session management: Automatic login/logout handling
- Multi-factor authentication: Optional extra security
- Rate limiting: Prevents brute force attacks
- HTTPS encryption: All data transmitted securely
Protected Routes (requires authentication):
// Backend route protection
app.use('/private',
clerk.requireAuth(), // Must be signed in
uDBHandler.middleware_userAuth(), // Create user record if needed
express.static('client/private') // Serve private files
);
// API endpoint protection
app.get('/api/userdata',
clerk.requireAuth(), // Authentication required
uDBHandler.endpoint_userData() // Handle request
);Public Routes (no authentication needed):
// Anyone can access these
app.use('/public', express.static('client/public'));
app.get('/api/problems', problemDBHandler.endpoint_getProblems());
app.get('/api/health', healthCheckHandler);app.use(cors({
origin: process.env.FRONTEND_URL || '*', // Allowed origins
credentials: true // Allow cookies
}));Piston API Sandboxing:
- Isolated environment: User code runs in a secure sandbox
- Time limits: 10-second maximum execution
- Memory limits: Prevents resource abuse
- No file system access: Can't read/write files
- No network access: Can't make external requests
- Language restrictions: Only approved languages
// Secure execution request
const runReq = await fetch("https://emkc.org/api/v2/piston/execute", {
method: "POST",
body: JSON.stringify({
language: req.params.lang, // Validated language
version: langInfo.version, // Specific version
files: [{ content: req.body.code }], // User code
stdin: req.body.input || "", // Input data
run_timeout: 10000 // 10-second limit
})
});Server-side validation:
// Example: Code submission validation
if (!req.params.lang) {
res.status(400).json({ error: "Missing required :lang param" });
return;
}
if (!req.body.code) {
res.status(400).json({ error: "Missing code in request body" });
return;
}
// Validate language is supported
const supportedLanguages = ['javascript', 'python', 'c++', 'java'];
if (!supportedLanguages.includes(req.params.lang)) {
res.status(400).json({ error: "Unsupported language" });
return;
}# .env file (not included in repository)
MONGO_DB_URL=mongodb+srv://username:password@cluster.mongodb.net/
CLERK_PUBLISHABLE_KEY=pk_test_...
CLERK_SECRET_KEY=sk_test_...
GEMINI_API_KEY=AIzaSy...
FRONTEND_URL=http://localhost:8080
AI_ASSISTANCE_ENABLED=true
PORT=8080Security practices:
- Never commit
.envfiles to version control - Use different keys for development/production
- Rotate keys regularly
- Limit API key permissions
# Required software
Node.js (v16 or higher) # JavaScript runtime
MongoDB (local or Atlas) # Database
Git # Version control
Code editor (VS Code) # Development environment# 1. Clone the repository
git clone https://github.com/vikashgupta16/CodeBattle-Arena.git
cd CodeBattle-Arena
# 2. Install dependencies
npm install
# 3. Create environment file
cp .env.example .env
# 4. Edit .env with your credentials
MONGO_DB_URL=your_mongodb_connection_string
CLERK_PUBLISHABLE_KEY=your_clerk_public_key
CLERK_SECRET_KEY=your_clerk_secret_key
GEMINI_API_KEY=your_google_ai_key
# 5. Seed the database with sample problems
npm run seed
# 6. Start development server
npm run dev{
"scripts": {
"start": "node server/index.js", // Production server
"dev": "nodemon server/index.js", // Development (auto-restart)
"test": "echo \"No tests specified\"", // Future: test suite
"seed": "node server/seedDatabase.js", // Populate database
"lint": "eslint server/ client/", // Code quality check
"build": "echo \"Build verification\"", // Future: build process
"health": "curl http://localhost:8080/health" // Health check
}
}Production Dependencies:
{
"@clerk/express": "^1.3.59", // Authentication service
"@google/generative-ai": "^0.2.1", // AI assistance (Gemini)
"cookie-session": "^2.1.0", // Session management
"cors": "^2.8.5", // Cross-origin requests
"dotenv": "^16.4.7", // Environment variables
"express": "^5.1.0", // Web server framework
"mongoose": "^8.13.1", // MongoDB object modeling
"node-fetch": "^3.3.2", // HTTP requests
"socket.io": "^4.8.1" // Real-time communication
}Development Dependencies:
{
"nodemon": "^3.0.0", // Auto-restart server
"eslint": "^8.57.0" // Code quality checker
}1. MongoDB Atlas (Database):
# 1. Go to https://mongodb.com/atlas
# 2. Create free cluster
# 3. Create database user
# 4. Get connection string
# 5. Add to .env as MONGO_DB_URL2. Clerk Authentication:
# 1. Go to https://clerk.com
# 2. Create new application
# 3. Get publishable and secret keys
# 4. Add to .env as CLERK_PUBLISHABLE_KEY and CLERK_SECRET_KEY
# 5. Configure sign-in/sign-up URLs3. Google AI (Gemini):
# 1. Go to https://ai.google.dev
# 2. Create API key
# 3. Add to .env as GEMINI_API_KEY
# 4. Enable Gemini 2.0 Flash model# Daily development routine
# 1. Pull latest changes
git pull origin main
# 2. Install any new dependencies
npm install
# 3. Start development server
npm run dev
# 4. Open browser to http://localhost:8080
# 5. Make changes to code
# 6. Test changes (server auto-restarts)
# 7. Check for code quality issues
npm run lint
# 8. Commit changes
git add .
git commit -m "Add new feature"
git push origin feature-branchWhy these technologies?
- Express.js: Fast, minimal web framework for Node.js
- MongoDB: Flexible NoSQL database, perfect for JSON data
- Socket.IO: Reliable real-time communication for arena battles
- Clerk: Professional authentication without security headaches
- ACE Editor: Powerful code editor that works in browsers
- Piston API: Secure code execution without server security risks
- Google Gemini: Advanced AI for intelligent code assistance
Design Patterns Used:
- MVC Pattern: Models (database), Views (frontend), Controllers (API endpoints)
- Modular Architecture: Each feature in separate files/modules
- Service Layer: Business logic separated from routes
- Repository Pattern: Database operations abstracted into handlers
Error: Cannot connect to MongoDB
# Solution 1: Check MongoDB connection
# Verify MONGO_DB_URL in .env file
# Test connection: mongodb+srv://user:pass@cluster.mongodb.net/dbname
# Solution 2: Check MongoDB Atlas IP whitelist
# Add your IP address to Atlas security settings
# Or use 0.0.0.0/0 for development (not production!)Error: Port 8080 already in use
# Solution: Kill process using port or change port
lsof -ti:8080 | xargs kill -9
# OR change PORT in .env file
PORT=3000Error: Clerk configuration error
# Solution: Verify Clerk keys in .env
# Check publishable key starts with pk_test_ or pk_live_
# Check secret key starts with sk_test_ or sk_live_
# Ensure keys match your Clerk applicationError: User redirected to sign-in repeatedly
# Solution: Check Clerk domain configuration
# Verify CLERK_SIGN_IN_URL and CLERK_SIGN_UP_URL
# Ensure URLs match your deployment domainError: AI analysis failed
# Solution 1: Check Gemini API key
# Verify GEMINI_API_KEY in .env
# Test key at https://ai.google.dev
# Solution 2: Check AI_ASSISTANCE_ENABLED
AI_ASSISTANCE_ENABLED=true
# Solution 3: Check if problem is Easy difficulty
# AI only works on Easy problems for beginnersError: Socket connection failed
# Solution 1: Check Socket.IO connection
# Open browser console, look for Socket.IO errors
# Verify FRONTEND_URL in .env matches your domain
# Solution 2: Check CORS configuration
# Ensure CORS allows your frontend domain
# Add specific domain instead of '*' for productionError: Match not found
# Solution: Clear stale matches
# Restart server to clear in-memory match data
# Check MongoDB for stuck matches in 'waiting' statusError: Unsupported language
# Solution: Check Piston API status
curl https://emkc.org/api/v2/piston/runtimes
# Verify language name matches Piston format:
# 'javascript' not 'js'
# 'python' not 'py'
# 'c++' not 'cpp'Error: Code execution timeout
# Solution: Check code for infinite loops
# Verify input format matches expected format
# Add console.log() to debug code executionError: Dashboard not loading stats
# Solution 1: Check browser console for API errors
# Look for 401 (unauthorized) or 500 (server error)
# Solution 2: Clear browser cache and localStorage
localStorage.clear();
location.reload();
# Solution 3: Check network tab for failed requests
# Verify API endpoints are responding correctlyError: Quick Actions stacking vertically
# Solution: Verify HTML structure has .quick-actions-grid wrapper
<div class="quick-actions-grid">
<div class="action-item">...</div>
<div class="action-item">...</div>
<div class="action-item">...</div>
<div class="action-item">...</div>
</div>Enable verbose logging:
// Add to server/index.js
if (process.env.NODE_ENV === 'development') {
app.use((req, res, next) => {
console.log(`${req.method} ${req.path}`, req.body);
next();
});
}Frontend debugging:
// Add to any JavaScript file
window.DEBUG = true;
function debugLog(message, data) {
if (window.DEBUG) {
console.log(`[DEBUG] ${message}`, data);
}
}Server health check:
# Test all endpoints
curl http://localhost:8080/api/health
curl http://localhost:8080/api/problems
curl http://localhost:8080/api/leaderboardDatabase performance:
// Add to any database operation
const start = Date.now();
const result = await Collection.find(query);
console.log(`Query took ${Date.now() - start}ms`);Check logs first:
# Server logs
npm run dev # Watch console output
# Database logs
# Check MongoDB Atlas logs in dashboard
# Frontend logs
# Open browser Developer Tools โ ConsoleCommon log patterns:
โ
Good: "Successfully connected to MongoDB!"
โ Bad: "MongooseError: connection timed out"
โ
Good: "๐ฏ [Arena] Player ready for match"
โ Bad: "โ [Arena] Match not found"
โ
Good: "๐ค AI Assistance enabled"
โ Bad: "AI analyze-code error: API key invalid"CodeBattle Arena is a sophisticated yet approachable platform that combines:
- ๐ฎ Gamification: Making coding fun through competitions and progress tracking
- ๐ค AI Integration: Smart assistance for beginners without creating dependency
- โ๏ธ Real-time Battles: Competitive programming with live multiplayer features
- ๐ Progressive Learning: Structured difficulty levels from beginner to expert
- ๐ Community Features: Leaderboards and social coding challenges
The architecture is designed to be:
- Scalable: Can handle thousands of concurrent users
- Secure: Multiple layers of protection for user data and code execution
- Maintainable: Modular design makes adding features straightforward
- Educational: Helps users learn programming in an engaging environment
Whether you're a 10-year-old just starting to code or an experienced developer looking to contribute, this documentation should give you a complete understanding of how every piece of CodeBattle Arena works together to create an engaging coding education platform.
๐ Quick Links:
- GitHub Repository: https://github.com/vikashgupta16/CodeBattle-Arena
- Live Demo: [Add your deployment URL]
- Documentation: This file!
- Issues & Bugs: GitHub Issues page
- Contributing: See CONTRIBUTING.md
๐ฅ Credits:
- Authors: Rouvik Maji & Archisman Pal
- License: GPL-3.0-or-later
- Built with: โค๏ธ and lots of โ