Skip to main content

Project Code Battlegrounds 1 5

Owls are cool

Code Battlegrounds

Documentation Website Link

Keywords

  • Section 1
  • Multiplayer
  • Game
  • Pair-programming
  • Real-time

Project Abstract

This repository contains a multiplayer web game that is meant to teach collaborative programming concepts. It utilizes pair-programming, where each user must complete their part to ensure the solution meets all requirements. The coder writes the code to solve the prompt. The quality assurance user cannot edit the code. They can write test cases, and run them. They can discuss potential approaches or failing tests and how to fix them with the coder.

High Level Requirement

From a user's perspective, the application must provide an intuitive way to complete their tasks. It needs to support low latency communication between clients. It also must support fast and secure untrusted code execution for user submissions and test cases. The scoring system must be robust and fair, prioritizing efficient code over fast submissions.

Conceptual Design

Tech Stack:

  • Bun
  • Node.js
  • Next.js
  • TypeScript
  • Socket.io
  • Socket.io Redis Adapter
  • JavaScript
  • Playwright
  • Jest
  • BetterAuth
  • Prisma ORM
  • PostgreSQL
  • Redis

The frontend includes Bun as a runtime, which launches a Node.js websocket server using Next.js routing.

The backend is designed to be stateless and inherently scalable. Redis is used for game state, such as timers, code, etc. PostgreSQL is used for persistence data such as match results through Prisma ORM.

Background

Previous similar projects include leetcode.com and hackerrank.com, known for their programming challenges. They do not offer any form of collaboration, which fails to realistically simulate a developer's life.

Pair-programming has been shown to be "faster than solo programming when programming task complexity is low and yields code solutions of higher quality when task complexity is high." (See here).

We hope that necessitating pair-programming will encourage better testing, documentation, and communication among those learning to write code.

As far as we can tell, this is the first platform of its type.

Required Resources

To use the web application, a computer with an active internet connection will be required.

Development

To develop, you will need a computer with Git, Node, Bun, and Docker Compose.

Environment Setup

  1. Clone the repository.
  2. Create a .env file and populate it with the following (filling the tokens as needed, they shouldn't matter too much for local development):
    # config
    PORT=3000
    NODE_ENV=development

    # better auth
    BETTER_AUTH_SECRET=SOME_SECRET_TOKEN
    BETTER_AUTH_URL=http://localhost:3000

    # postgres
    POSTGRES_USER=appuser
    POSTGRES_PASSWORD=SOME_SECRET_TOKEN
    POSTGRES_DB=appdb
    POSTGRES_HOST=localhost
    POSTGRES_PORT=5432

    # redis
    REDIS_HOST=localhost
    REDIS_PORT=6379

    # executor image
    EXECUTOR_PORT=6969

    # for prisma
    DATABASE_URL=postgresql://appuser:SOME_SECRET_TOKEN@localhost:5432/appdb
    Remember to source as needed!
  3. Run bun install to install the dependencies.
  4. Run bunx --bun prisma generate to generate the Prisma client and database migrations.
  5. Run docker compose -f ./dev-docker-compose.yml up -d to bring up the containers (you may need to run as root).
  6. Run bunx --bun prisma migrate dev to bring the database up to schema.
  7. Run bunx --bun prisma db seed to add mock data to the dev database.
  8. Run bun dev to launch the development server and navigate to localhost:3000 to view the page.
  9. When done, docker compose -f ./dev-docker-compose.yml down will bring the containers down.

Helpful Common Commands and Tricks

  • docker compose -f ./dev-docker-compose.yml down -v will stop the containers and purge the volumes. Good if you really mess something up (you WILL lose data!).
  • bunx --bun prisma migrate reset will drop the DB (you WILL lose data!).
  • If your database is stuck out of sync and you can't apply migrations, run rm -rf ./prisma/migrations/** to remove all pending migrations. Then recreate the migration with bunx prisma migrate dev --name dev and apply it.
  • To see how the websockets are working, try opening up the game page in an incognito tab to be registered as a different client.

Testing

We use two testing libraries: Jest and Playwright. Jest is used for individual API tests while Playwright is used for end-to-end flow tests.

To run the Playwright tests, ensure you have the application running and run bunx playwright test --workers=1. You may need to have Chromium headless installed.

For the Jest tests, run bunx jest tests/api/ --forceExit.

Collaborators

Julia FasickJesse HerreraKyle FauntroyElan ReizasSamir BuchMichael ZachSaad Chaudry

Contributors

Made with contrib.rocks.