aboutsummaryrefslogtreecommitdiffstats
path: root/backend/ts/api/login.ts
blob: 0c6ef75c158745fe93e6cbcdf5260217817b02d2 (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
55
56
57
58
59
60
import { Request, Response } from "express";
import { fiaTable, dbRes, Color, Colors, Helpers, makeQuery, Chequer, tColors } from "../../../helpers/helpersBack";
import startGame from "../startGame";

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

  let dbRes = (await makeQuery("SELECT * FROM `fia` WHERE `started`= 0 LIMIT 1", [])) as Array<dbRes>,
    row: fiaTable;
  if (dbRes.length === 0) {
    let color = Helpers.getRandomInt(0, 5);
    row = {} as fiaTable;

    row.data = [{
      id: req.session.uid,
      index: 0,
      color: Color[color as keyof Colors],
      name: req.body.name,
    }];
    req.session.color = Color.blue;

    await makeQuery('INSERT INTO `fia`(`data`, `started`) VALUES (?, ?)', [JSON.stringify(row.data), 0]);
    req.session.gid = (await makeQuery('SELECT `id` FROM `fia` WHERE `data`= ?', [JSON.stringify(row.data)]) as any)[0].id;
  } else {
    row = Helpers.dbResToFiaTable(dbRes[0]);
    let playersColors: Array<tColors> = [],
      colors = [...Array(4).keys()];

    for (let playerData of row.data)
      playersColors.push(playerData.color);

    colors = colors.filter(el => !playersColors.includes(el));
    let color = colors[Helpers.getRandomInt(0, colors.length)],
      index = row.data.length;

    row.data.push({
      id: req.session.uid,
      index: index,
      color: color,
      name: req.body.name,
    });

    req.session.gid = row.id;
    req.session.index = index;

    makeQuery('UPDATE `fia` SET `data`= ? WHERE `id`= ?', [JSON.stringify(row.data), row.id]);

    if (row.data.length === 4) {
      startGame(req, res);
    }
  }
  makeQuery("UPDATE `fia` SET `playersCount`=`playersCount`+1 WHERE `id`= ?", [req.session.gid]);
  res.send(JSON.stringify(row.data));
}