calculate new points
This commit is contained in:
54
src/gambleMath.ts
Normal file
54
src/gambleMath.ts
Normal 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()));
|
||||
//
|
||||
25
src/index.ts
25
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<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) {
|
||||
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);
|
||||
|
||||
@ -10,7 +10,6 @@
|
||||
</head>
|
||||
|
||||
<body data-bs-theme="dark">
|
||||
<a href="/gamble">gamble</a>
|
||||
People
|
||||
<div class="row p-5">
|
||||
<div class="list-group col-3">
|
||||
|
||||
Reference in New Issue
Block a user