aboutsummaryrefslogtreecommitdiffstats
path: root/backend/ts/startGame.ts
blob: 4188d29d529bc9434f8e4717cd779c4523a600fc (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
import { } from "../../helpers/helpers";
import { makeQuery, Color, GameBoard, Chequer, Coordinates, Data } from "../../helpers/helpersBack";
import { Request, Response } from "express";

export default async function startGame(req: Request, res: Response) {
    if (req.session === undefined) {
        res.status(500);
        res.send("");
        return;
    }

    makeQuery("UPDATE `fia` SET `started` = 1 WHERE `id`= ?", [req.session.gid]);
    let dbRes: {gameBoard: GameBoard, data: Data}
        = (await makeQuery("SELECT `gameBoard`, `playersCount` FROM `fia` WHERE `id` = ?", [req.session.gid]))[0];

    for (let player of dbRes.data) {
        let chequer: Chequer = { color: player.color, x: 1, y: 1 },
            start: Coordinates;

        switch (player.color) {
            case Color.red: start = { x: 1, y: 1 }; break;
            case Color.blue: start = { x: 8, y: 1 }; break;
            case Color.yellow: start = { x: 1, y: 8 }; break;
            default /* Color.green */: start = { x: 8, y: 8 }; break;
            // case Color.green: start = { x: 8, y: 8 }; break;
        }

        for (let i = 0; i < 2; i++) {
            for (let j = 0; j < 2; j++) {
                //@ts-ignore - 8 + 1 <= 11 which is max of Coordinates
                [chequer.x, chequer.y] = [start.x + i, start.y + j];
            }
        }

        dbRes.gameBoard.push(chequer);
    }

    await makeQuery("UPDATE `fia` SET `gameBoard` = ? WHERE `id`= ?", [dbRes.gameBoard, req.session.gid]);

    res.send("");
}