aboutsummaryrefslogtreecommitdiff
path: root/src/api/index.ts
blob: 39f5b40a383ce8acb5459d9b850aef3a9512ed9e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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 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) {
        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;