From 466981457d95cd77beb2ca011cf290b41a90744d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Kr=C5=A1ka?= Date: Sat, 14 Sep 2024 16:01:51 +0200 Subject: [PATCH] calculate new points --- src/gambleMath.ts | 54 +++++++++++++++++++++++++++++++++++++++++ src/index.ts | 25 +++++++++++++------ src/templates/index.ejs | 1 - 3 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 src/gambleMath.ts diff --git a/src/gambleMath.ts b/src/gambleMath.ts new file mode 100644 index 0000000..c642e25 --- /dev/null +++ b/src/gambleMath.ts @@ -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(); +//let points = new Map(); +//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())); +// diff --git a/src/index.ts b/src/index.ts index 86f3843..2bd051b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,8 +2,9 @@ import express from "express"; import ejs from "ejs"; import multer from "multer"; 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 calculateNewPoints from "./gambleMath"; const app = express(); const port = 8080; @@ -15,8 +16,7 @@ app.use(express.static('www')); app.get("/", async (req, res) => { const people = await db.select({ personId: personTable.personId, - name: personTable.name, - pointsTotal: sql`coalesce(sum(${pointsTable.points}), 0)` + name: personTable.name }).from(personTable) .leftJoin(pointsTable, eq(personTable.personId, pointsTable.personId)) .groupBy(personTable.personId) @@ -103,9 +103,16 @@ app.post("/gamble/:id", upload.none(), async (req, res) => { return; } - let person = await db.query.personTable.findFirst({ - where: eq(personTable.personId, id) - }) + let person = (await db.select({ + personId: personTable.personId, + name: personTable.name, + secret: personTable.secret, + gambleTime: personTable.gambleTime, + pointsTotal: sql`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) { res.status(404).send("Invalid person id"); @@ -122,6 +129,10 @@ app.post("/gamble/:id", upload.none(), async (req, res) => { return; } + let currentPoints = parseInt(person.pointsTotal); + let newTotalPoints = calculateNewPoints(currentPoints); + let pointsDiff = newTotalPoints - currentPoints; + await db.update(personTable) .set({ gambleTime: sql`NOW()` @@ -130,7 +141,7 @@ app.post("/gamble/:id", upload.none(), async (req, res) => { await db.insert(pointsTable).values({ personId: id, - points: req.body['points'] + points: pointsDiff }) res.redirect('/person/' + id); diff --git a/src/templates/index.ejs b/src/templates/index.ejs index acf7a3c..50d42fe 100644 --- a/src/templates/index.ejs +++ b/src/templates/index.ejs @@ -10,7 +10,6 @@ - gamble People