calculate new points

This commit is contained in:
2024-09-14 16:01:51 +02:00
parent 7abc593186
commit 466981457d
3 changed files with 72 additions and 8 deletions

54
src/gambleMath.ts Normal file
View File

@ -0,0 +1,54 @@
const deltaX: number = 10;
const ymax: number = 1;
function f(a: number, x: number): number {
return Math.exp(-1 * Math.pow((x - a), 2) / 20);
}
// helper function
function randomRange(min: number, max: number): number {
return min + Math.random() * (max - min);
}
let numberOfInterations = 0;
export default function calculateNewPoints(currentPoints: number): number {
let randomX: number;
let randomY: number;
do {
numberOfInterations++;
randomX = randomRange(currentPoints - deltaX, currentPoints + deltaX);
randomY = randomRange(0, ymax);
} while (f(currentPoints, randomX) <= randomY)
return randomX;
}
// testing functions
//let stats = new Map<number, number>();
//let points = new Map<number, number>();
//for (let i = 0; i < 1000; i++) {
// numberOfInterations = 0;
// let p = Math.round(calculateNewPoints(100));
//
// if (stats.has(numberOfInterations)) {
// let iters = stats.get(numberOfInterations) + 1;
// stats.set(numberOfInterations, iters);
// } else {
// stats.set(numberOfInterations, 1);
// }
//
//
// if (points.has(p)) {
// let po = points.get(p) + 1;
// console.log(po);
// points.set(p, po);
// } else {
// points.set(p, 1);
// }
//}
//
//console.log("iterace (počet iterací, počet výskytů):");
//console.log(new Map([...stats.entries()].sort()));
//console.log("body (počet bodů, počet výskytů):");
//console.log(new Map([...points.entries()].sort()));
//

View File

@ -2,8 +2,9 @@ import express from "express";
import ejs from "ejs"; import ejs from "ejs";
import multer from "multer"; import multer from "multer";
import { db } from "./database/db"; import { db } from "./database/db";
import { eq, sql, sum } from "drizzle-orm"; import { eq, sql } from "drizzle-orm";
import { personTable, pointsTable } from "./database/schema"; import { personTable, pointsTable } from "./database/schema";
import calculateNewPoints from "./gambleMath";
const app = express(); const app = express();
const port = 8080; const port = 8080;
@ -15,8 +16,7 @@ app.use(express.static('www'));
app.get("/", async (req, res) => { app.get("/", async (req, res) => {
const people = await db.select({ const people = await db.select({
personId: personTable.personId, personId: personTable.personId,
name: personTable.name, name: personTable.name
pointsTotal: sql`coalesce(sum(${pointsTable.points}), 0)`
}).from(personTable) }).from(personTable)
.leftJoin(pointsTable, eq(personTable.personId, pointsTable.personId)) .leftJoin(pointsTable, eq(personTable.personId, pointsTable.personId))
.groupBy(personTable.personId) .groupBy(personTable.personId)
@ -103,9 +103,16 @@ app.post("/gamble/:id", upload.none(), async (req, res) => {
return; return;
} }
let person = await db.query.personTable.findFirst({ let person = (await db.select({
where: eq(personTable.personId, id) personId: personTable.personId,
}) name: personTable.name,
secret: personTable.secret,
gambleTime: personTable.gambleTime,
pointsTotal: sql<string>`coalesce(sum(${pointsTable.points}), 0)`
}).from(personTable)
.leftJoin(pointsTable, eq(personTable.personId, pointsTable.personId))
.where(eq(personTable.personId, id))
.groupBy(personTable.personId))[0];
if (!person) { if (!person) {
res.status(404).send("Invalid person id"); res.status(404).send("Invalid person id");
@ -122,6 +129,10 @@ app.post("/gamble/:id", upload.none(), async (req, res) => {
return; return;
} }
let currentPoints = parseInt(person.pointsTotal);
let newTotalPoints = calculateNewPoints(currentPoints);
let pointsDiff = newTotalPoints - currentPoints;
await db.update(personTable) await db.update(personTable)
.set({ .set({
gambleTime: sql`NOW()` gambleTime: sql`NOW()`
@ -130,7 +141,7 @@ app.post("/gamble/:id", upload.none(), async (req, res) => {
await db.insert(pointsTable).values({ await db.insert(pointsTable).values({
personId: id, personId: id,
points: req.body['points'] points: pointsDiff
}) })
res.redirect('/person/' + id); res.redirect('/person/' + id);

View File

@ -10,7 +10,6 @@
</head> </head>
<body data-bs-theme="dark"> <body data-bs-theme="dark">
<a href="/gamble">gamble</a>
People People
<div class="row p-5"> <div class="row p-5">
<div class="list-group col-3"> <div class="list-group col-3">