aboutsummaryrefslogtreecommitdiffstats
path: root/backend/ts/api/getCurrentGameState.ts
blob: 896ccb46e75b06563c56cb99dbcefe64f9dc2246 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { Request, Response } from "express";
import { Data, makeQuery, API_RES, GameBoard } from "../../../helpers/helpersBack";

export default async function getCurrentGameState(req: Request, res: Response) {
    if (req.session === undefined || req.session.gid === undefined) {
        res.status(400);
        res.send(API_RES.failure);
        return;
    }

    let dbRes: { gameBoard: GameBoard, currentPlayer: number, started: number, data: Data, timeTillTurnEnd: number, winner: null | number }
        = (await makeQuery("SELECT `gameBoard`, `currentPlayer`, `started`, `data`, `timeTillTurnEnd`, `winner`"
            + " FROM `fia` WHERE `id` = ?",
            [req.session.gid]))[0];
    
    if (dbRes.started === 0) {
        res.send(JSON.stringify({ data: dbRes.data }));
    } else {
        res.send(JSON.stringify(dbRes));
    }
}