gamble & news

This commit is contained in:
2024-09-24 12:58:00 +02:00
parent a4810fc7ba
commit 93b40c299c
6 changed files with 71 additions and 8 deletions

View File

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

View File

@ -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

View File

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

View File

@ -29,7 +29,7 @@
<script>
setTimeout(() => {
window.location.href='/';
}, 5*60*1000);
}, 2*60*1000);
</script>
</body>
</html>

View File

@ -24,6 +24,49 @@
</div>
<% }); %>
</div>
<div class="text-center">
<div><strong>Hlášení dne:</strong></div>
<div id="news-text" class="display-3 mb-3 fw-normal"></div>
<div id="news-countdown" class="display-6 fw-bold"></div>
</div>
</div>
<script>
let news = null;
async function downloadData() {
news = await fetch(`/api/news`)
.then(response => response.json())
console.log(news);
}
setInterval(() => {
if (news == null) {
document.getElementById('news-text').innerText = "";
document.getElementById('news-countdown').innerText = "";
return;
}
document.getElementById('news-text').innerText = news.text;
if (news.countdownTo == null) {
document.getElementById('news-countdown').innerText = "";
} else {
newsDate = new Date(news.countdownTo);
// 2*60*60*1000 = timezone offset
const timeUntil = new Date(Math.abs(newsDate.getTime() - 2*60*60*1000 - Date.now()))
document.getElementById('news-countdown').innerText = timeUntil.toLocaleString('cs', {
timeZone: "Etc/UTC",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
}
}, 1000);
setInterval(downloadData, 60000);
downloadData()
</script>
</body>
</html>

View File

@ -64,7 +64,7 @@
<script>
setTimeout(() => {
window.location.href='/';
}, 5*60*1000);
}, 2*60*1000);
</script>
</body>
</html>