From 93b40c299c7dddc35e5f109320e813d237d56ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Kr=C5=A1ka?= Date: Tue, 24 Sep 2024 12:58:00 +0200 Subject: [PATCH] gamble & news --- src/database/schema.ts | 8 +++++++- src/gambleMath.ts | 4 ++-- src/index.ts | 20 ++++++++++++++++--- src/templates/gamble.ejs | 2 +- src/templates/index.ejs | 43 ++++++++++++++++++++++++++++++++++++++++ src/templates/person.ejs | 2 +- 6 files changed, 71 insertions(+), 8 deletions(-) diff --git a/src/database/schema.ts b/src/database/schema.ts index c32a542..5a5efb1 100644 --- a/src/database/schema.ts +++ b/src/database/schema.ts @@ -1,5 +1,5 @@ import { relations } from "drizzle-orm"; -import { datetime, int, mysqlTable, varchar } from "drizzle-orm/mysql-core"; +import { datetime, int, mysqlTable, text, varchar } from "drizzle-orm/mysql-core"; export const personTable = mysqlTable('person', { personId: int('personId').primaryKey().autoincrement(), @@ -24,3 +24,9 @@ export const pointsRelations = relations(pointsTable, ({ one }) => ({ references: [personTable.personId], }) })); + +export const newsTable = mysqlTable('news', { + newsId: int('newsId').primaryKey().autoincrement(), + text: text('text').notNull(), + countdownTo: datetime('countdownTo') +}); diff --git a/src/gambleMath.ts b/src/gambleMath.ts index c642e25..0a2f349 100644 --- a/src/gambleMath.ts +++ b/src/gambleMath.ts @@ -1,8 +1,8 @@ -const deltaX: number = 10; +const deltaX: number = 12; const ymax: number = 1; function f(a: number, x: number): number { - return Math.exp(-1 * Math.pow((x - a), 2) / 20); + return Math.exp(-1 * Math.pow((x - a), 2) / 30); } // helper function diff --git a/src/index.ts b/src/index.ts index 8b136ad..b3128df 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,9 +95,10 @@ app.get("/gamble/:id", async (req, res) => { if (person.gambleTime == null) { sendError(res, 423, "Gamble není dostupný"); } else { - sendError(res, 423, "Gamble bude dostupný až ve " + getNextGambleTime(person.gambleTime).toLocaleString('cs', { + sendError(res, 423, "Gamble nelze, gej nebudeš až v " + new Date(getNextGambleTime(person.gambleTime).getTime()).toLocaleString('cs', { hour: "2-digit", minute: "2-digit", + timeZone: "Europe/Prague" })); } return; @@ -136,12 +137,12 @@ app.post("/gamble/:id", upload.none(), async (req, res) => { } if (!isGambleAvailable(person.gambleTime)) { - sendError(res, 423, "Nelze zadat další gamble"); + sendError(res, 423, "Další gamble nelze"); return; } if (person.secret != req.body['secret']) { - sendError(res, 423, "Nesprávé heslo"); + sendError(res, 423, "Nesprávé heslo. Also tvoje máma je gej."); return; } @@ -187,6 +188,7 @@ app.get("/person/:id", async (req, res) => { if (!isDetailAvailable(person.gambleTime)) { sendError(res, 404, "Detail bude dostupný až po dalším gamblu"); + return; } let points = await db.query.pointsTable.findMany({ @@ -202,6 +204,18 @@ app.get("/person/:id", async (req, res) => { }); }); +app.get('/api/news', async (req, res) => { + let news = await db.query.newsTable.findFirst({ + orderBy: sql`RAND()` + }); + + if (!news) { + res.json(null); + } + + res.json(news); +}); + app.listen(port, () => { console.log(`Listening on port ${port}...`); }); diff --git a/src/templates/gamble.ejs b/src/templates/gamble.ejs index 431fd1e..6104c83 100644 --- a/src/templates/gamble.ejs +++ b/src/templates/gamble.ejs @@ -29,7 +29,7 @@ diff --git a/src/templates/index.ejs b/src/templates/index.ejs index f3578e5..734af37 100644 --- a/src/templates/index.ejs +++ b/src/templates/index.ejs @@ -24,6 +24,49 @@ <% }); %> +
+
Hlášení dne:
+
+
+
+ diff --git a/src/templates/person.ejs b/src/templates/person.ejs index a1a539e..abfac88 100644 --- a/src/templates/person.ejs +++ b/src/templates/person.ejs @@ -64,7 +64,7 @@