From 7abc5931867dd1f964eb7a4ed1d351de5e9f3ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Kr=C5=A1ka?= Date: Wed, 4 Sep 2024 17:13:25 +0200 Subject: [PATCH] show detail only after gamble --- src/database/schema.ts | 3 +- src/index.ts | 78 ++++++++++++++++++++++++++++++++-------- src/templates/error.ejs | 16 +++++++++ src/templates/index.ejs | 3 +- src/templates/person.ejs | 1 - 5 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 src/templates/error.ejs diff --git a/src/database/schema.ts b/src/database/schema.ts index e48fb02..a62c322 100644 --- a/src/database/schema.ts +++ b/src/database/schema.ts @@ -1,10 +1,11 @@ import { relations } from "drizzle-orm"; -import { int, mysqlTable, varchar } from "drizzle-orm/mysql-core"; +import { datetime, int, mysqlTable, varchar } from "drizzle-orm/mysql-core"; export const personTable = mysqlTable('person', { personId: int('personId').primaryKey().autoincrement(), name: varchar('name', { length: 255 }), secret: varchar('secret', { length: 255 }), + gambleTime: datetime('gampleTime') }); export const personRelations = relations(personTable, ({ many }) => ({ diff --git a/src/index.ts b/src/index.ts index f01311f..86f3843 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,21 +9,6 @@ const app = express(); const port = 8080; const upload = multer(); -let people = [ - { - id: 1, - name: "Adam", - secret: "adam", - points: [] - }, - { - id: 2, - name: "Vojta", - secret: "vojta", - points: [] - }, -] - //app.use(express.urlencoded); app.use(express.static('www')); @@ -45,6 +30,32 @@ app.get("/", async (req, res) => { }); }); +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()) { + return true; + } + + return false; +} + +function isDetailAvailable(time: Date | null): boolean { + if (time == null) { + return false; + } + + // 2 minute delay before hiding + if (new Date(time.getTime() + 1 * 60 * 1000) > new Date()) { + return true; + } + + return false; +} + app.get("/gamble/:id", async (req, res) => { let id = parseInt(req.params.id); if (id == undefined) { @@ -61,6 +72,21 @@ app.get("/gamble/:id", async (req, res) => { return; } + if (isDetailAvailable(person.gambleTime)) { + res.redirect('/person/' + id); + } + + 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); + }); + return; + } + ejs.renderFile('src/templates/gamble.ejs', { person: person }, function (err, str) { if (err) { res.status(500).send(err); @@ -86,11 +112,22 @@ app.post("/gamble/:id", upload.none(), async (req, res) => { return; } + if (!isGambleAvailable(person.gambleTime)) { + res.status(423).send("Gamble currently locked"); + return; + } + if (person.secret != req.body['secret']) { res.sendStatus(403); return; } + await db.update(personTable) + .set({ + gambleTime: sql`NOW()` + }) + .where(eq(personTable.personId, person.personId)); + await db.insert(pointsTable).values({ personId: id, points: req.body['points'] @@ -109,6 +146,7 @@ app.get("/person/:id", async (req, res) => { let person = (await db.select({ personId: personTable.personId, name: personTable.name, + gambleTime: personTable.gambleTime, pointsTotal: sql`coalesce(sum(${pointsTable.points}), 0)` }).from(personTable) .leftJoin(pointsTable, eq(personTable.personId, pointsTable.personId)) @@ -120,6 +158,16 @@ app.get("/person/:id", async (req, res) => { 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); + }); + } + let points = await db.query.pointsTable.findMany({ where: eq(pointsTable.personId, id) }); diff --git a/src/templates/error.ejs b/src/templates/error.ejs new file mode 100644 index 0000000..4b965e7 --- /dev/null +++ b/src/templates/error.ejs @@ -0,0 +1,16 @@ + + + + + + + + + Index + + + + index + Error: <%= error %> + + diff --git a/src/templates/index.ejs b/src/templates/index.ejs index 893454e..acf7a3c 100644 --- a/src/templates/index.ejs +++ b/src/templates/index.ejs @@ -15,11 +15,10 @@
<% people.forEach((person) => {%> -
<%= person.name %> - <%= person.pointsTotal %>
<% }); %> diff --git a/src/templates/person.ejs b/src/templates/person.ejs index 5705a1b..a85ef3c 100644 --- a/src/templates/person.ejs +++ b/src/templates/person.ejs @@ -12,7 +12,6 @@ Index - gamble