Skip to content

A scalable 1v1 coding e-sports platform demonstrating Motia's unified event-driven runtime.

License

Notifications You must be signed in to change notification settings

sambhandavale/code-battle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

23 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

logo CodeBattle

Real-Time 1v1 Competitive Coding Platform for Esports & Offline Events

CodeBattle transforms competitive programming from a solitary grind into a live, high-stakes esports experience. Think online chess platforms like Chess.com, but for competitive codingโ€”designed for real-time 1v1 duels, college fests, hackathons, and coding tournaments.

Instead of asynchronous problem-solving, CodeBattle enables synchronous competitive duels where speed, accuracy, and strategy determine the winner in real time. Perfect for both online competitions and offline college coding events.


๐ŸŽฏ Why CodeBattle?

The Gap in Competitive Programming

Most coding platforms (LeetCode, HackerRank) are built for learning, not competing:

  • Static, asynchronous problem-solving
  • No real-time competition pressure
  • Lack of spectator engagement
  • Not designed for organized events or tournaments

The CodeBattle Difference

We're bringing the adrenaline of esports to competitive programming:

โœ… Live 1v1 Battles โ€” Real-time synchronized duels with server-authoritative timing
โœ… Offline Event Ready โ€” Ideal for college fests, hackathons, and coding competitions
โœ… Flexible Time Control โ€” Users select match duration (perfect for event planning)
โœ… Multi-Language Support โ€” Code in Python, JavaScript, C++, Java, Go, Rust, and more
โœ… Live Spectator Mode โ€” Audiences watch real-time gameplay
โœ… AI-Powered Post-Match Analysis โ€” Get instant feedback on your code logic and optimizations
โœ… Easy Room Sharing โ€” Create a room, share the code, challenge a friend or stranger


๐Ÿ† Use Cases

๐Ÿ’ป Online 1v1 Duels

Challenge friends or strangers to real-time coding battles. Set your own time limit and compete live.

๐ŸŽ“ College Coding Fests

Run live coding tournaments at your college events. Stream matches on a projector, engage the audience, crown a championโ€”all on one platform.

๐Ÿ… Organized Tournaments

Coming soon: Round-robin tournament brackets for larger competitive events. Automatically match players and track rankings.

๐ŸŽช Offline Events with Online Competition

Host a physical coding event while using CodeBattle's online platform to broadcast and manage competitions in real time.


โœจ Core Features

๐ŸŽฎ Gameplay

Feature Details
Room-Based Matches One player creates a room, shares the code, opponent joinsโ€”instant 1v1
Custom Time Control Set match duration to fit your event (5 min, 15 min, 30 min, custom)
Multi-Language Arena Support for Python, JavaScript, C++, Java, Go, Rust, and more
Live Code Execution Secure, sandboxed execution via Piston (Docker-based)
Real-Time Verdicts Instant feedback on code submissions (Accepted/Rejected/TLE)

๐Ÿ‘ฅ Spectator & Social

Feature Details
Live Spectator Mode Watch 1v1 matches in real-time via WebSocket streams
Synchronized Viewing All spectators see the exact same stateโ€”perfect for projector displays
Match History Browse past duels and learn from top players

๐Ÿค– AI Coaching

Feature Details
Post-Match Analysis AI coach (Gemini 2.5) reviews your solution
Logic Breakdown Understand why your solution works (or doesn't)
Optimization Tips Time complexity analysis (O(N) vs O(Nยฒ)), edge cases, improvements
Learning Mode Not just a verdictโ€”a coaching experience

๐Ÿ—๏ธ Architecture Overview

CodeBattle uses Motia Steps to orchestrate APIs, background jobs, AI agents, and real-time streamsโ€”all within a single unified runtime.

graph TD
    %% Nodes
    User([Client / Frontend])

    subgraph "Motia Unified Runtime"
        direction TB
        API[MatchAPI Step]
        Engine{GameEngine Step}
        Runner[CodeRunner Step]
        Referee[AiReferee Step]
        Stream[MatchStream]
    end

    subgraph "External Services"
        Piston[Piston Engine]
        AI[Gemini 2.5]
    end

    %% Main Input Flow
    User ==>|HTTP POST| API
    API -->|Event: player.joined| Engine

    %% Worker Flows
    Engine -->|Event: run.code| Runner
    Runner <-->|Docker Exec| Piston
    Runner -->|Event: code.processed| Engine

    %% AI Logic
    Engine -->|Event: analyze.code| Referee
    Referee -->|API Call| AI

    %% Output Stream
    Engine -->|State Update| Stream
    Stream -.->|Real-time WebSocket| User
Loading

๐Ÿ”„ Match Lifecycle

  1. Create a Room
    Player A initiates a match, sets time limit โ†’ MatchAPI persists state โ†’ waits for opponent

  2. Share & Join
    Player A shares room code โ†’ Player B joins โ†’ MongoDB atomic update ensures exactly 2 players โ†’ player.joined event fires

  3. Match Starts
    GameEngine validates both players ready โ†’ synchronized timer starts โ†’ problem loaded โ†’ match.started event

  4. Live Coding
    Both players write code in their language of choice โ†’ real-time sync to spectators โ†’ verdicts returned instantly

  5. Submission & Verdict
    Code received โ†’ CodeRunner executes in sandbox โ†’ test cases validated โ†’ instant feedback

  6. AI Analysis
    Post-match, AiReferee (Gemini 2.5) analyzes code asynchronously and streams coaching feedback


๐Ÿš€ Quick Start

Prerequisites

  • Node.js 18+
  • MongoDB (Local or Atlas)
  • Motia CLI

1๏ธโƒฃ Clone & Install

git clone https://github.com/sambhandavale/code-battle.git

# Backend
cd battle-engine
npm install

# Frontend
cd ../battle-frontier
npm install

2๏ธโƒฃ Environment Setup

Create a .env file in both directories.

battle-engine/.env

MONGO_URI=mongodb://localhost:27017/codebattle
GEMINI_API_KEY=your_gemini_key_here

battle-frontier/.env

ENV=dev
NEXT_PUBLIC_API_GATEWAY_URL=http://localhost:2000
NEXT_PUBLIC_WS_GATEWAY_URL=ws://localhost:2000

3๏ธโƒฃ Run the Project

Terminal 1 โ€” Backend

cd battle-engine
npm run dev
# Motia Dashboard: http://localhost:3000

Terminal 2 โ€” Frontend

cd battle-frontier
npm run dev
# App: http://localhost:3000 (or configured port)

๐Ÿ› ๏ธ Tech Stack

Layer Technology
Orchestration Motia (unified runtime)
Backend Runtime Node.js 18+ (TypeScript)
Database MongoDB
Code Execution Piston (Docker-based, sandboxed)
AI Coaching Google Gemini 2.5 Flash
Frontend Next.js, Tailwind CSS, Monaco Editor
Real-Time WebSocket streams via Motia

๐Ÿ—บ๏ธ Roadmap

Current Features โœ…

  • Live 1v1 real-time matches
  • Multi-language code execution
  • Custom time control
  • Live spectator mode
  • AI post-match analysis

Coming Soon ๐Ÿš€

  • Tournament Mode โ€” Round-robin bracket system for organized events
  • Leaderboards โ€” Global and event-specific rankings
  • Problem Sets โ€” Curated difficulty tiers
  • Team Battles โ€” 2v2 or team-based competitions
  • Event Management Dashboard โ€” Tools for organizing college fests and hackathons

๐Ÿ’ก Why Motia?

We built CodeBattle to showcase what Motia can do. This project demonstrates how a unified backend runtime collapses infrastructure complexity.

Before Motia (Traditional Stack)

Express (API) + Redis (Pub/Sub) + Celery (Workers) + Socket.io (WebSockets)
= 4 separate systems to manage

With Motia (Unified Runtime)

Motia Steps: API โ†’ GameEngine โ†’ CodeRunner โ†’ Stream
= 1 system, built-in event routing, automatic glue code

Key Benefits We Experienced

  • Zero-config event bus โ€” Steps communicate via simple emit() calls
  • Visual debugging โ€” Built-in dashboard shows match flow in real-time
  • Reduced complexity โ€” 3ร— less boilerplate, 2ร— faster development
  • Single deployment โ€” All components in one runtime

Learn more: Motia GitHub Repository


๐Ÿ“ Contributing

We welcome contributions! Whether you're fixing bugs, adding languages, or improving the AI coaching, PRs are always appreciated.

To Get Started

  1. Fork the repo
  2. Create a feature branch (git checkout -b feature/your-feature)
  3. Commit your changes (git commit -m 'Add feature')
  4. Push and open a PR

๐Ÿ“ง Feedback & Support

Have ideas for CodeBattle? Found a bug? Want to use it for your college event?

  • Open an issue on GitHub
  • Reach out with feedbackโ€”we'd love to hear how you use CodeBattle!

๐Ÿ™ Acknowledgments

Massive shout-out to the Motia team. This project would have required 3ร— the code and 2ร— the infrastructure complexity without their unified runtime.

Motia made it possible to build APIs, background workers, real-time streams, and AI agents using a single architectural primitive โ€” Steps.


One runtime. One primitive. Infinite possibilities.

Built for competitive coding. Designed for esports. Ready for your event. ๐Ÿš€

About

A scalable 1v1 coding e-sports platform demonstrating Motia's unified event-driven runtime.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages