aboutsummaryrefslogtreecommitdiffstats
path: root/backend/ts/api/login.ts
blob: 5b38c6205d7c56daa9fd7ee6a6e61aa5fd7a53a4 (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
import { Request, Response } from "express";
import { makeQuery } from "../helpers";

interface Colors {
  blue: number;
  red: number;
  green: number;
  yellow: number;
  0: number;
  1: number;
  2: number;
  3: number;
}

interface fiaTable {
  id: number;
  data: Array<object>;
  started: boolean;
}

const Color: Colors = {
  blue: 0,
  red: 1,
  green: 2,
  yellow: 3,
  0: 0, 1: 1, 2: 2, 3: 3
}

export default async function apiLogin(req: Request, res: Response) {
  if (req.session === undefined) {
    res.send(/* html */`<script>alert("Something went wrong, try to reload the page")</script>`);
    return;
  }
  req.session.uid = req.session.id;
  let c = await makeQuery("SELECT * FROM `fia` WHERE `started`= false LIMIT 1", []),
    row: fiaTable;
  if (c === "") {
    let data = { id: req.session.uid, color: Color.blue };
    makeQuery("INSERT INTO `fia`(`data`, `started`) VALUES (?, ?)", [JSON.stringify(data), "0"]);
  }
  else {
    row = JSON.parse(c);
    row.data.push({ id: req.session.uid, color: Color[row.data.length as keyof Colors] });
    makeQuery("UPDATE `fia` SET `data`=? WHERE `id`=?", [JSON.stringify(row.data), row.id.toString()]);
    if(row.data.length === 4)
      makeQuery("UPDATE `fia` SET `started` = 1 WHERE `id`=?", [row.id.toString()]);
  }
  res.send("[1, 2, 3]");
}