This commit is contained in:
2024-09-20 14:31:19 +02:00
parent 466981457d
commit bc39f6b6d6
10 changed files with 129 additions and 65 deletions

View File

@ -30,13 +30,17 @@ app.get("/", async (req, res) => {
});
});
function getNextGambleTime(time: Date): Date {
return new Date(time.getTime() + 3 * 60 * 60 * 1000);
}
function isGambleAvailable(time: Date | null): boolean {
if (time == null) {
return true;
}
// 3 hour delay before showing
if (new Date(time.getTime() + 3 * 60 * 60 * 1000) < new Date()) {
if (getNextGambleTime(time) < new Date()) {
return true;
}
@ -49,13 +53,23 @@ function isDetailAvailable(time: Date | null): boolean {
}
// 2 minute delay before hiding
if (new Date(time.getTime() + 1 * 60 * 1000) > new Date()) {
if (new Date(time.getTime() + 2 * 60 * 1000) > new Date()) {
return true;
}
return false;
}
function sendError(res: express.Response, code: number, error: string): void {
ejs.renderFile('src/templates/error.ejs', { error: error }, function (err, str) {
if (err) {
res.status(500).send(err);
}
res.status(code).send(str);
});
}
app.get("/gamble/:id", async (req, res) => {
let id = parseInt(req.params.id);
if (id == undefined) {
@ -77,13 +91,14 @@ app.get("/gamble/:id", async (req, res) => {
}
if (!isGambleAvailable(person.gambleTime)) {
ejs.renderFile('src/templates/error.ejs', { error: "Gamble is currently not available" }, function (err, str) {
if (err) {
res.status(500).send(err);
}
res.send(str);
});
if (person.gambleTime == null) {
sendError(res, 423, "Gamble není dostupný");
} else {
sendError(res, 423, "Gamble bude dostupný až ve " + getNextGambleTime(person.gambleTime).toLocaleString('cs', {
hour: "2-digit",
minute: "2-digit",
}));
}
return;
}
@ -115,17 +130,17 @@ app.post("/gamble/:id", upload.none(), async (req, res) => {
.groupBy(personTable.personId))[0];
if (!person) {
res.status(404).send("Invalid person id");
sendError(res, 404, "Osoba nenalezena");
return;
}
if (!isGambleAvailable(person.gambleTime)) {
res.status(423).send("Gamble currently locked");
sendError(res, 423, "Nelze zadat další gamble");
return;
}
if (person.secret != req.body['secret']) {
res.sendStatus(403);
sendError(res, 423, "Nesprávé heslo");
return;
}
@ -150,7 +165,7 @@ app.post("/gamble/:id", upload.none(), async (req, res) => {
app.get("/person/:id", async (req, res) => {
let id = parseInt(req.params.id);
if (id == undefined) {
res.status(404).send("Invalid person id");
sendError(res, 404, "Osoba nenalezena");
return;
}
@ -165,18 +180,12 @@ app.get("/person/:id", async (req, res) => {
.groupBy(personTable.personId))[0];
if (!person) {
res.status(404).send("Invalid person id");
sendError(res, 404, "Osoba nenalezena");
return;
}
if (!isDetailAvailable(person.gambleTime)) {
ejs.renderFile('src/templates/error.ejs', { error: "Detail not available" }, function (err, str) {
if (err) {
res.status(500).send(err);
}
res.send(str);
});
sendError(res, 404, "Detail bude dostupný až po dalším gamblu");
}
let points = await db.query.pointsTable.findMany({