squash everything since there was profanity in the commit log and maybe people won't like that
This commit is contained in:
commit
f0ca174ada
3
.env.sample
Normal file
3
.env.sample
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
NODE_ENV=development
|
||||||
|
BOT_TOKEN=
|
||||||
|
CHESSH_MOVE_PATH=http://127.0.0.1:3000/api/bots/games/:gameId/turn
|
38
.eslintrc
Normal file
38
.eslintrc
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
{
|
||||||
|
"root": true,
|
||||||
|
"env": {
|
||||||
|
"jest": true
|
||||||
|
},
|
||||||
|
"parser": "@typescript-eslint/parser",
|
||||||
|
"parserOptions": {
|
||||||
|
"project": [
|
||||||
|
"./tsconfig.json"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"extends": "airbnb-typescript/base",
|
||||||
|
"plugins": [
|
||||||
|
"import",
|
||||||
|
"@typescript-eslint"
|
||||||
|
],
|
||||||
|
"rules": {
|
||||||
|
"comma-dangle": 0,
|
||||||
|
"no-underscore-dangle": 0,
|
||||||
|
"no-param-reassign": 0,
|
||||||
|
"no-return-assign": 0,
|
||||||
|
"import/extensions": 0,
|
||||||
|
"@typescript-eslint/no-redeclare": 0,
|
||||||
|
"camelcase": "off",
|
||||||
|
"@typescript-eslint/camelcase": ["warn"]
|
||||||
|
},
|
||||||
|
"settings": {
|
||||||
|
"import/parsers": {
|
||||||
|
"@typescript-eslint/parser": [
|
||||||
|
".ts",
|
||||||
|
".tsx"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"import/resolver": {
|
||||||
|
"typescript": {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
76
.gitignore
vendored
Normal file
76
.gitignore
vendored
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
# Logs
|
||||||
|
logs
|
||||||
|
*.log
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
|
||||||
|
# Runtime data
|
||||||
|
pids
|
||||||
|
*.pid
|
||||||
|
*.seed
|
||||||
|
*.pid.lock
|
||||||
|
|
||||||
|
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||||
|
lib-cov
|
||||||
|
|
||||||
|
# Coverage directory used by tools like istanbul
|
||||||
|
coverage
|
||||||
|
|
||||||
|
# nyc test coverage
|
||||||
|
.nyc_output
|
||||||
|
|
||||||
|
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
|
||||||
|
.grunt
|
||||||
|
|
||||||
|
# Bower dependency directory (https://bower.io/)
|
||||||
|
bower_components
|
||||||
|
|
||||||
|
# node-waf configuration
|
||||||
|
.lock-wscript
|
||||||
|
|
||||||
|
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||||
|
build/Release
|
||||||
|
|
||||||
|
# Dependency directories
|
||||||
|
node_modules/
|
||||||
|
jspm_packages/
|
||||||
|
|
||||||
|
# TypeScript v1 declaration files
|
||||||
|
typings/
|
||||||
|
|
||||||
|
# Optional npm cache directory
|
||||||
|
.npm
|
||||||
|
|
||||||
|
# Optional eslint cache
|
||||||
|
.eslintcache
|
||||||
|
|
||||||
|
# Optional REPL history
|
||||||
|
.node_repl_history
|
||||||
|
|
||||||
|
# Output of 'npm pack'
|
||||||
|
*.tgz
|
||||||
|
|
||||||
|
# Yarn Integrity file
|
||||||
|
.yarn-integrity
|
||||||
|
|
||||||
|
# dotenv environment variables file
|
||||||
|
.env
|
||||||
|
|
||||||
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# next.js build output
|
||||||
|
.next
|
||||||
|
|
||||||
|
# nuxt.js build output
|
||||||
|
.nuxt
|
||||||
|
|
||||||
|
# vuepress build output
|
||||||
|
.vuepress/dist
|
||||||
|
|
||||||
|
# Serverless directories
|
||||||
|
.serverless
|
||||||
|
|
||||||
|
dist
|
||||||
|
.vercel
|
5
.prettierrc
Normal file
5
.prettierrc
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"tabWidth": 2,
|
||||||
|
"singleQuote": true,
|
||||||
|
"trailingComma": "all"
|
||||||
|
}
|
8
Dockerfile
Normal file
8
Dockerfile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
FROM node:alpine
|
||||||
|
WORKDIR /usr/chessh_bot
|
||||||
|
COPY package.json .
|
||||||
|
RUN npm install\
|
||||||
|
&& npm install typescript -g
|
||||||
|
COPY . .
|
||||||
|
RUN tsc
|
||||||
|
CMD ["node", "./dist/index.js"]
|
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
MIT License Copyright (c) 2020 CJ R.
|
||||||
|
|
||||||
|
Permission is hereby granted, free
|
||||||
|
of charge, to any person obtaining a copy of this software and associated
|
||||||
|
documentation files (the "Software"), to deal in the Software without
|
||||||
|
restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
publish, distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to the
|
||||||
|
following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice
|
||||||
|
(including the next paragraph) shall be included in all copies or substantial
|
||||||
|
portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||||
|
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
||||||
|
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||||
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
11
README.md
Normal file
11
README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# example chessh bot
|
||||||
|
|
||||||
|
this is a simple CheSSH bot written in TypeScript using js-chess-engine. it's not very good.
|
||||||
|
|
||||||
|
the bot "entrypoint" is at `/src/api/index.ts`.
|
||||||
|
|
||||||
|
## environment
|
||||||
|
```
|
||||||
|
BOT_TOKEN: the token generated by CheSSH for which the bot will be deployed
|
||||||
|
CHESSH_MOVE_PATH: the path to POST an attempted move in the form "<from square><to square>" according to the CheSSH contract to the game with id :gameId.
|
||||||
|
```
|
12
docker-compose.yml
Normal file
12
docker-compose.yml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
services:
|
||||||
|
server:
|
||||||
|
container_name: chesshbot
|
||||||
|
restart: always
|
||||||
|
env_file: .env
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
ports:
|
||||||
|
- 4200:4200
|
7188
package-lock.json
generated
Normal file
7188
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
50
package.json
Normal file
50
package.json
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
{
|
||||||
|
"name": "chessh-bot",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "A basic chessh bot example",
|
||||||
|
"main": "src/index.ts",
|
||||||
|
"scripts": {
|
||||||
|
"start": "ts-node src/index.ts",
|
||||||
|
"dev": "nodemon src/index.ts",
|
||||||
|
"build": "tsc",
|
||||||
|
"lint": "eslint --fix src",
|
||||||
|
"vercel-build": "echo hello"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "CJ R. <cj@null.computer> (https://w3cj.now.sh)",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/w3cj/express-api-starter.git"
|
||||||
|
},
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"axios": "^1.4.0",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"dotenv": "^16.0.3",
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"helmet": "^6.1.5",
|
||||||
|
"js-chess-engine": "^1.0.2",
|
||||||
|
"morgan": "^1.10.0",
|
||||||
|
"prettier": "^2.8.8"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/cors": "^2.8.13",
|
||||||
|
"@types/express": "^4.17.17",
|
||||||
|
"@types/jest": "^29.5.1",
|
||||||
|
"@types/morgan": "^1.9.4",
|
||||||
|
"@types/node": "^18.16.3",
|
||||||
|
"@types/supertest": "^2.0.12",
|
||||||
|
"@typescript-eslint/eslint-plugin": "^5.59.1",
|
||||||
|
"@typescript-eslint/parser": "^5.59.1",
|
||||||
|
"eslint": "^8.39.0",
|
||||||
|
"eslint-config-airbnb-typescript": "^17.0.0",
|
||||||
|
"eslint-import-resolver-typescript": "^3.5.5",
|
||||||
|
"eslint-plugin-import": "^2.27.5",
|
||||||
|
"jest": "^29.5.0",
|
||||||
|
"nodemon": "^2.0.22",
|
||||||
|
"supertest": "^6.3.3",
|
||||||
|
"ts-jest": "^29.1.0",
|
||||||
|
"ts-node": "^10.9.1",
|
||||||
|
"typescript": "^5.0.4"
|
||||||
|
}
|
||||||
|
}
|
18
src/api/Chessh.ts
Normal file
18
src/api/Chessh.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
export interface BotMoveRequest {
|
||||||
|
bot_id: number;
|
||||||
|
bot_name: string;
|
||||||
|
game_id: number;
|
||||||
|
fen: string;
|
||||||
|
turn: string;
|
||||||
|
bot_turn: boolean;
|
||||||
|
last_move: string;
|
||||||
|
status: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BotMoveAttempt {
|
||||||
|
attempted_move: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BotMoveResponse {
|
||||||
|
message: string;
|
||||||
|
}
|
57
src/api/index.ts
Normal file
57
src/api/index.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
import express from 'express';
|
||||||
|
import {
|
||||||
|
BotMoveRequest,
|
||||||
|
BotMoveAttempt,
|
||||||
|
BotMoveResponse,
|
||||||
|
} from './Chessh';
|
||||||
|
import axios from 'axios';
|
||||||
|
import { aiMove } from 'js-chess-engine';
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
const chesshMovePath = (gameId: number) =>
|
||||||
|
process.env.CHESSH_MOVE_PATH!.replace(
|
||||||
|
':gameId',
|
||||||
|
gameId.toString(),
|
||||||
|
);
|
||||||
|
|
||||||
|
const sendNextMove = (gameId: number, move: string): Promise<BotMoveResponse> =>
|
||||||
|
axios
|
||||||
|
.post(chesshMovePath(gameId), {
|
||||||
|
attempted_move: move.toLowerCase(),
|
||||||
|
} as BotMoveAttempt, {
|
||||||
|
headers: {
|
||||||
|
"authorization": process.env.BOT_TOKEN!
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then((r): BotMoveResponse => {
|
||||||
|
const body = r.data as BotMoveResponse;
|
||||||
|
if (r.status === 200) {
|
||||||
|
return body;
|
||||||
|
}
|
||||||
|
throw new Error(
|
||||||
|
'Move request unsuccessful, got back from cheSSH: ' +
|
||||||
|
JSON.stringify(body),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post<BotMoveRequest, string>('/move', async (req, res) => {
|
||||||
|
const updateMessages: BotMoveRequest[] = Array.isArray(req.body)
|
||||||
|
? req.body
|
||||||
|
: [req.body];
|
||||||
|
|
||||||
|
for (const update of updateMessages) {
|
||||||
|
if (update.bot_turn) {
|
||||||
|
const move = aiMove(update.fen, 3);
|
||||||
|
|
||||||
|
const moveFrom = Object.keys(move)[0];
|
||||||
|
const moveTo = move[moveFrom];
|
||||||
|
|
||||||
|
await sendNextMove(update.game_id, moveFrom + moveTo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(200).send('OK');
|
||||||
|
});
|
||||||
|
|
||||||
|
export default router;
|
24
src/app.ts
Normal file
24
src/app.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import express from 'express';
|
||||||
|
import morgan from 'morgan';
|
||||||
|
import helmet from 'helmet';
|
||||||
|
import cors from 'cors';
|
||||||
|
|
||||||
|
import * as middlewares from './middlewares';
|
||||||
|
import api from './api';
|
||||||
|
|
||||||
|
import * as dotenv from 'dotenv';
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use(morgan('dev'));
|
||||||
|
app.use(helmet());
|
||||||
|
app.use(cors());
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
app.use('/api', api);
|
||||||
|
|
||||||
|
app.use(middlewares.notFound);
|
||||||
|
app.use(middlewares.errorHandler);
|
||||||
|
|
||||||
|
export default app;
|
8
src/index.ts
Normal file
8
src/index.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
import app from './app';
|
||||||
|
|
||||||
|
const port = process.env.PORT || 4200;
|
||||||
|
app.listen(port, () => {
|
||||||
|
/* eslint-disable no-console */
|
||||||
|
console.log(`Listening: http://localhost:${port}`);
|
||||||
|
/* eslint-enable no-console */
|
||||||
|
});
|
17
src/middlewares.ts
Normal file
17
src/middlewares.ts
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
import { NextFunction, Request, Response } from 'express';
|
||||||
|
|
||||||
|
export function notFound(req: Request, res: Response, next: NextFunction) {
|
||||||
|
res.status(404);
|
||||||
|
const error = new Error(`🔍 - Not Found - ${req.originalUrl}`);
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
export function errorHandler(err: Error, req: Request, res: Response<{message: string, stack?: string | any[]}>, next: NextFunction) {
|
||||||
|
const statusCode = res.statusCode !== 200 ? res.statusCode : 500;
|
||||||
|
res.status(statusCode);
|
||||||
|
res.json({
|
||||||
|
message: err.message,
|
||||||
|
stack: process.env.NODE_ENV === 'production' ? '🥞' : err.stack,
|
||||||
|
});
|
||||||
|
}
|
18
tsconfig.json
Normal file
18
tsconfig.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "dist",
|
||||||
|
"sourceMap": true,
|
||||||
|
"target": "esnext",
|
||||||
|
"module": "commonjs",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"noImplicitAny": false,
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"./*.js",
|
||||||
|
"api/**/*.ts",
|
||||||
|
"src/**/*.ts"
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user