show detail only after gamble
This commit is contained in:
@ -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 }) => ({
|
||||
|
||||
78
src/index.ts
78
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)
|
||||
});
|
||||
|
||||
16
src/templates/error.ejs
Normal file
16
src/templates/error.ejs
Normal file
@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
<link rel="stylesheet" href="/bootstrap.min.css" />
|
||||
<script src="/bootstrap.bundle.min.js"></script>
|
||||
<title>Index</title>
|
||||
</head>
|
||||
|
||||
<body data-bs-theme="dark">
|
||||
<a href="/">index</a>
|
||||
Error: <%= error %>
|
||||
</body>
|
||||
</html>
|
||||
@ -15,11 +15,10 @@
|
||||
<div class="row p-5">
|
||||
<div class="list-group col-3">
|
||||
<% people.forEach((person) => {%>
|
||||
<a href='/person/<%= person.personId %>'
|
||||
<a href='/gamble/<%= person.personId %>'
|
||||
class="list-group-item list-group-item-action">
|
||||
<div class="me-auto">
|
||||
<span class="fw-bold"><%= person.name %></span>
|
||||
<%= person.pointsTotal %>
|
||||
</div>
|
||||
</a>
|
||||
<% }); %>
|
||||
|
||||
@ -12,7 +12,6 @@
|
||||
|
||||
<body data-bs-theme="dark">
|
||||
<a href="/">Index</a>
|
||||
<a href="/gamble/<%= person.personId %>">gamble</a>
|
||||
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
|
||||
Reference in New Issue
Block a user