diff options
| author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-05-29 14:48:50 -0700 |
|---|---|---|
| committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-05-29 14:53:08 -0700 |
| commit | 548498d37424f2ac579770962b627aeba61925e8 (patch) | |
| tree | 1d02f147b82dd8f9a48296f01a2ae2e84d71958c /src | |
| download | chessh-bot-548498d37424f2ac579770962b627aeba61925e8.tar.gz chessh-bot-548498d37424f2ac579770962b627aeba61925e8.zip | |
added bot
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/index.ts | 58 | ||||
| -rw-r--r-- | src/app.ts | 24 | ||||
| -rw-r--r-- | src/index.ts | 8 | ||||
| -rw-r--r-- | src/interfaces/Chessh.ts | 19 | ||||
| -rw-r--r-- | src/interfaces/ChesshRequest.ts | 19 | ||||
| -rw-r--r-- | src/interfaces/ErrorResponse.ts | 6 | ||||
| -rw-r--r-- | src/middlewares.ts | 19 |
7 files changed, 153 insertions, 0 deletions
diff --git a/src/api/index.ts b/src/api/index.ts new file mode 100644 index 0000000..b201aba --- /dev/null +++ b/src/api/index.ts @@ -0,0 +1,58 @@ +import express from 'express'; +import { + BotMoveRequest, + BotMoveAttempt, + BotMoveResponse, +} from '../interfaces/Chessh'; +import axios from 'axios'; +import { aiMove } from 'js-chess-engine'; + +const router = express.Router(); + +const chesshMovePath = (gameId: number) => + (process.env.CHESSH_MOVE_PATH as unknown as string).replace( + ':gameId', + gameId.toString(), + ); + +const sendNextMove = (gameId: number, move: string): Promise<BotMoveResponse> => + axios + .post(chesshMovePath(gameId), { + token: process.env.BOT_TOKEN as unknown as string, + attempted_move: move.toLowerCase(), + } as BotMoveAttempt) + .then((r): BotMoveResponse => { + const body = r.data as BotMoveResponse; + if (r.status === 200) { + console.log(body); + 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) { + console.log(update); + const move = aiMove(update.fen); + + const moveFrom = Object.keys(move)[0]; + const moveTo = move[moveFrom]; + try { + await sendNextMove(update.game_id, moveFrom + moveTo); + } catch (e) { + console.log(e); + } + } + } + res.status(200).send('OK'); +}); + +export default router; diff --git a/src/app.ts b/src/app.ts new file mode 100644 index 0000000..7fc395f --- /dev/null +++ b/src/app.ts @@ -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; diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..6265d20 --- /dev/null +++ b/src/index.ts @@ -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 */ +}); diff --git a/src/interfaces/Chessh.ts b/src/interfaces/Chessh.ts new file mode 100644 index 0000000..898fccb --- /dev/null +++ b/src/interfaces/Chessh.ts @@ -0,0 +1,19 @@ +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 { + token: string; + attempted_move: string; +} + +export interface BotMoveResponse { + message: string; +} diff --git a/src/interfaces/ChesshRequest.ts b/src/interfaces/ChesshRequest.ts new file mode 100644 index 0000000..72340ba --- /dev/null +++ b/src/interfaces/ChesshRequest.ts @@ -0,0 +1,19 @@ +export interface BotMoveRequest { + bot_id: number; + bot_name: string; + game_id: number; + fen: string; + turn: string; + bot_turn: boolean; + status: string; +} + +export interface BotMoveAttempt { + token: string; + attempted_move: string; +} + +export interface BotMoveResponse { + success: boolean; + message: string; +} diff --git a/src/interfaces/ErrorResponse.ts b/src/interfaces/ErrorResponse.ts new file mode 100644 index 0000000..1513e10 --- /dev/null +++ b/src/interfaces/ErrorResponse.ts @@ -0,0 +1,6 @@ +import MessageResponse from './MessageResponse'; + +export default interface ErrorResponse extends MessageResponse { + stack?: string; + message: string; +} diff --git a/src/middlewares.ts b/src/middlewares.ts new file mode 100644 index 0000000..249ff67 --- /dev/null +++ b/src/middlewares.ts @@ -0,0 +1,19 @@ +import { NextFunction, Request, Response } from 'express'; + +import ErrorResponse from './interfaces/ErrorResponse'; + +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<ErrorResponse>, 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, + }); +} |
