Skip to content

Latest commit

ย 

History

History
1502 lines (1243 loc) ยท 45.8 KB

File metadata and controls

1502 lines (1243 loc) ยท 45.8 KB

๐Ÿš€ CodeBattle Arena - Complete Documentation

The Ultimate Guide to Understanding Every Part of the Platform


๐Ÿ“– Table of Contents

  1. What is CodeBattle Arena?
  2. How Does It Work? (Simple Explanation)
  3. Project Structure (Like a Building Blueprint)
  4. The Frontend (What You See and Touch)
  5. The Backend (The Brain Behind Everything)
  6. How Users Move Around (The Journey)
  7. Special Features Explained
  8. How Everything Connects (The Magic)
  9. Database Structure (Where Information Lives)
  10. Security & Authentication (Keeping Users Safe)
  11. Development Setup (How to Build It)
  12. Troubleshooting Guide

๐ŸŽฏ What is CodeBattle Arena?

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.

๐ŸŒŸ Main Features:

  • 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

๐Ÿค” How Does It Work? (Simple Explanation)

Think of CodeBattle Arena like a digital coding school with these rooms:

๐Ÿ  The Main Building (Server)

  • 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)

๐ŸŽจ The Classrooms (Frontend)

  • 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

๐Ÿ”— The Communication System

  • HTTP Requests: Like passing notes between rooms
  • WebSockets: Like having a live phone call during battles
  • Database: Like a giant filing cabinet storing everything

๐Ÿ—๏ธ Project Structure (Like a Building Blueprint)

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)

๐ŸŽจ The Frontend (What You See and Touch)

๐ŸŒ Public Pages (Anyone can visit)

1. Landing Page (client/public/LandingPage/)

  • Purpose: First page visitors see - like a store window
  • Files:
    • index.html: The page structure
    • style.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

2. Account Pages (client/public/Accounts/)

  • signin.html: Login page
  • signup.html: Registration page
  • style.css: Shared styling for both pages
  • Uses: Clerk authentication service (like a security guard)

๐Ÿ” Private Pages (Only for logged-in users)

1. Dashboard/Homepage (client/private/HomePage/)

  • Purpose: User's personal control center
  • Files:
    • codigo.html: The main dashboard layout
    • codigo.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>

2. Coding Environment (client/private/CoderPage/)

  • Purpose: Where users actually write and test code
  • Files:
    • coder.html: The coding interface
    • coder.css: Editor styling
    • coder.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');
}

3. Arena Battle System (client/private/Arena/)

  • 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 animations

How Arena Battles Work:

  1. Queue System: Players join a difficulty-based queue
  2. Matchmaking: Server pairs players of similar skill
  3. Ready Phase: Both players confirm they're ready
  4. Battle: 5 coding problems, timed progression
  5. Scoring: 10 points per test case + 5 bonus for completion
  6. Results: Winner determined, stats updated

4. Difficulty-Based Practice (client/private/Easy/, Intermediate/, Advanced/)

  • 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)

5. Shared Components (client/private/common/)

  • auth.js: Authentication helper (111 lines)
  • aiAssistance.js: AI system (1541 lines!)
  • problemLoader.js: Problem management
  • problemStyles.css: Consistent styling across problem pages

๐ŸŽจ Styling System

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

๐Ÿง  The Backend (The Brain Behind Everything)

๐ŸŽฏ Main Server (server/index.js)

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:

  1. Authentication: Checks if users are allowed in
  2. File Serving: Delivers web pages to browsers
  3. API Endpoints: Handles requests for data
  4. Real-time Communication: Manages arena battles
  5. Code Execution: Runs user code safely
  6. AI Integration: Provides intelligent assistance

๐Ÿ—„๏ธ Database Management (server/database.js)

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
}

๐Ÿ† Arena System (server/arenaSocket.js & server/arenaDatabase.js)

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
}

๐Ÿค– AI Assistance System (server/aiAssistance.js)

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

โšก Code Execution Engine (server/codeRun.js)

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:

  1. User writes code in editor
  2. Frontend sends code to /api/run/:language
  3. Server sends code to Piston API (secure sandbox)
  4. Piston runs code safely in isolated environment
  5. Results sent back to user

Supported Languages: JavaScript, Python, C++, Java, C, Rust, Go, PHP, Ruby, and 30+ more!

๐Ÿ“Š Statistics & Progress Tracking (server/userStatsService.js)

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 Management (server/problemDatabase.js)

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
}

๐Ÿ—บ๏ธ How Users Move Around (The Journey)

๐Ÿ“ User Journey Map

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]
Loading

๐Ÿ”„ Navigation System

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 form

Dynamic 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);
}

โœจ Special Features Explained

๐Ÿค– AI Assistant (The Smart Helper)

When it activates: Only for Easy difficulty problems (beginner-friendly)

What it does:

  1. Real-time Analysis: Watches your code as you type
  2. Error Detection: Spots mistakes before you even run code
  3. Smart Suggestions: Offers improvements
  4. Test Failure Explanation: Explains why tests didn't pass
  5. 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": [...]
}

โš”๏ธ Arena Battle System (Real-Time PvP)

Matchmaking Process:

  1. Queue Join: Player selects difficulty and joins queue
  2. Skill Matching: Server finds opponent of similar level
  3. Match Creation: Both players notified of match
  4. Ready Check: Both must confirm ready to start
  5. Battle Begin: 5 problems, independent timers
  6. Live Updates: Real-time progress tracking
  7. 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 points

Scoring 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

๐Ÿ“Š Progress Tracking & Statistics

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.

๐Ÿ† Leaderboard System

Multiple Leaderboard Types:

  1. Overall Leaderboard: Based on total problems solved + rank points
  2. Arena Leaderboard: Based on arena wins and win rate
  3. Category Leaderboards: Per difficulty level
  4. Real-Time Updates: Automatically refreshes during competitions

๐ŸŒ Real-World Projects

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.


๐Ÿ”— How Everything Connects (The Magic)

๐ŸŒ Communication Protocols

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
});

๐Ÿ”„ Data Flow Examples

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
Loading

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
Loading

๐Ÿ“Š Database Relationships

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

๐Ÿ—ƒ๏ธ Database Structure (Where Information Lives)

๐Ÿข MongoDB Collections (Like Filing Cabinets)

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 errors

Arena 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 left

Arena 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

๐Ÿ“ˆ Database Indexes (For Fast Searches)

// 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 });

๐Ÿ” Security & Authentication (Keeping Users Safe)

๐Ÿ›ก๏ธ Clerk Authentication System

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

๐Ÿ”’ API Security

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);

๐ŸŒ CORS Security (Cross-Origin Protection)

app.use(cors({
    origin: process.env.FRONTEND_URL || '*',  // Allowed origins
    credentials: true                         // Allow cookies
}));

๐Ÿ” Code Execution Security

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
    })
});

๐Ÿ›ก๏ธ Input Validation & Sanitization

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;
}

๐Ÿ”ฅ Environment Variables (Secret Information)

# .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=8080

Security practices:

  • Never commit .env files to version control
  • Use different keys for development/production
  • Rotate keys regularly
  • Limit API key permissions

๐Ÿ› ๏ธ Development Setup (How to Build It)

๐Ÿ“‹ Prerequisites (What You Need First)

# Required software
Node.js (v16 or higher)     # JavaScript runtime
MongoDB (local or Atlas)    # Database
Git                         # Version control
Code editor (VS Code)       # Development environment

๐Ÿš€ Installation Steps

# 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

๐Ÿ”ง Package.json Scripts

{
    "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
    }
}

๐Ÿ“ฆ Dependencies Explained

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
}

๐ŸŒ External Services Setup

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_URL

2. 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 URLs

3. 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

๐Ÿ“ Development Workflow

# 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-branch

๐Ÿ—๏ธ Project Architecture Decisions

Why 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

๐Ÿšจ Troubleshooting Guide

๐Ÿ” Common Issues & Solutions

1. Server Won't Start

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=3000

2. Authentication Issues

Error: 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 application

Error: 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 domain

3. AI Assistant Not Working

Error: 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 beginners

4. Arena Battles Not Starting

Error: 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 production

Error: Match not found

# Solution: Clear stale matches
# Restart server to clear in-memory match data
# Check MongoDB for stuck matches in 'waiting' status

5. Code Execution Problems

Error: 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 execution

6. Frontend Issues

Error: 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 correctly

Error: 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>

๐Ÿ”ง Debug Mode Setup

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);
    }
}

๐Ÿ“Š Performance Monitoring

Server health check:

# Test all endpoints
curl http://localhost:8080/api/health
curl http://localhost:8080/api/problems
curl http://localhost:8080/api/leaderboard

Database performance:

// Add to any database operation
const start = Date.now();
const result = await Collection.find(query);
console.log(`Query took ${Date.now() - start}ms`);

๐Ÿ†˜ Getting Help

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 โ†’ Console

Common 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"

๐ŸŽ‰ Conclusion

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:

๐Ÿ‘ฅ Credits:

  • Authors: Rouvik Maji & Archisman Pal
  • License: GPL-3.0-or-later
  • Built with: โค๏ธ and lots of โ˜•