A TypeScript, Express, and Node.js project that allows developers to configure and run Codyfighters via the Codyfight API.
To get up and running quickly, follow these steps:
Ensure you have the correct versions of the required dependencies installed:
- Node.js:
>= 23.3.0
- NPM:
10.9.0
or later
git clone https://github.com/codyfight/codyfight-bots.git
cd codyfight-bots
The default settings should work out of the box, but you can adjust configurations in .env if needed.
Copy the .env.template
file to .env
:
cp .env.template .env
npm install
npm run build
npm run start:server
- The configuration page will be available at http://localhost:3000
- Use this page to configure and manage your bots.
npm run start:bot-runner
- This will start all bots that have been configured.
- Features
- Requirements
- Project Structure
- Setup & Configuration
- Scripts
- Development
- Production
- Extending the Database Layer
- Extending Strategies
- Contributing
- License
- Configuration UI: A user interface to add or remove bots from the database.
- Bot Runner: Runs configured bots that connect to the Codyfight API and perform actions in the game.
- Bot Strategies: Easily extend the move and cast strategies to customize bot behavior.
- Multiple Database Support: Uses SQLite by default; can switch to Postgres or any other DB by implementing an interface.
- TypeScript:
>= 5.0.0
- Express:
^4.18.2
- Node.js:
>= 23.3.0
- NPM:
10.9.1
or later
.
├── src/
│ ├── bots/
│ ├── client/
│ ├── game/
│ ├── server/
│ ├── utils/
│ └── ...
├── package.json
├── tsconfig.json
├── yarn.lock
└── ...
src/bots
: Contains core bot logic, including strategies and bot runner.src/server
: Houses the Express server and database setup (SQLite or Postgres).src/client
: Front-end/public assets for the configuration page.src/game
: Game-specific logic and data structures (e.g., map, state, pathfinding).src/utils
: Shared utility functions.
Create a .env
file at the root of your project and add the necessary variables. Use the template .env
file for reference.
# Environment
NODE_ENV=development
# SQLite (default)
DB_PATH=./src/db/bots.db
DB_DIALECT=sqlite
-
Create the Database:
Run the following scripts to initialize and seed an SQLite database:# Create tables node --loader ts-node/esm src/db/scripts/sqllite/create-db.ts # Seed database with some dummy records node --loader ts-node/esm src/db/scripts/sqllite/seed-db.ts
-
Confirm
.env
:
Make sure.env
contains:DB_DIALECT=sqlite DB_PATH=./src/server/db/bots.db
- Set Up Postgres:
Create a Postgres database (e.g.,codyfight_bots
) and a user (e.g.,admin
). - Configure
.env
:DB_DIALECT=postgres DATABASE_URL=postgresql://admin@localhost:1234/codyfight_bots
There are two ways to run the bots:
The simplest way to start and manage bots is by using npm scripts. This will launch a configuration page where you can add, remove, and manage bots.
npm run dev:server
- This starts an Express server locally.
- The config page will be available at
http://localhost:3000/
, where you can manage bots.
npm run dev:bot-runner
- This starts all bots that have been added via the config page.
For more granular control, you can use the provided API. This allows you to start, stop, and manage bots individually through HTTP requests.
npm run dev:server
- This launches the Express API on
http://localhost:3000
.
- You can interact with the API to create, update, delete, or start/stop individual bots.
- A Postman collection with full API details can be found in:
src/server/codyfight_bots.postman_collection.json
Command | Description |
---|---|
npm run dev:server |
Start the server with a configuration page (localhost:3000). |
npm run dev:bot-runner |
Start all configured bots. |
npm run start:server |
Start the compiled server (for production). |
npm run start:bot-runner |
Start all bots from the compiled build. |
npm run build |
Build the project for production. |
-
Install Dependencies
npm install
-
Database
- For SQLite: Initialize the database (see Database Setup).
- For Postgres: Configure
.env
and set up the database with necessary scripts.
-
Start in Development Mode
- Configuration Page:
npm run dev:config
- Bot Runner:
npm run dev:run-bots
- Configuration Page:
-
Build the Project
npm build
-
Start the Compiled Server
- Configuration Page:
npm run start:config
- Bot Runner:
npm run start:run-bots
- Configuration Page:
To add support for a new database type (e.g., MongoDB):
- See
src/db/repository/custom-c-bot-repository.ts
for an example implementation of a custom database repository.
You can extend or override the bot’s move and cast strategies by implementing custom logic:
- Cast Strategy: See
src/bots/strategies/cast/custom-cast-strategy.ts
for an example. - Move Strategy: See
src/bots/strategies/move/custom-move-strategy.ts
for an example.
- Fork this repository and clone your fork.
- Create a new branch for your feature or fix.
- Commit and push your changes.
- Open a Pull Request against the main repository. Please include clear descriptions and, if necessary, add or update tests.
This project is open-sourced under the MIT License. Contributions are welcome!