show detail only after gamble

This commit is contained in:
2024-09-04 17:13:25 +02:00
parent 91d32f2f71
commit 7abc593186
5 changed files with 82 additions and 19 deletions

View File

@ -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)
});