Remove sim
Some checks failed
Build and deploy / deploy (push) Failing after 2m4s

This commit is contained in:
2024-10-12 01:51:43 +01:00
parent 8ccac834bb
commit c072add871

View File

@@ -1,149 +0,0 @@
"use client";
import { shuffle } from "radash";
import { FormEvent, useState, useRef, useCallback, useEffect } from "react";
export default function Sim(): React.JSX.Element {
const [outputs, setOutputs] = useState<
{ runs: number; tickets: number; totalWin: number }[]
>([]);
const workerRef = useRef<Worker>();
useEffect(() => {
workerRef.current = new Worker(
new URL("../workers/sim.ts", import.meta.url)
);
workerRef.current.onmessage = (event) =>
console.log(`WebWorker Response => ${JSON.stringify(event.data)}`);
return () => {
workerRef.current?.terminate();
};
}, []);
function runCalc(opts: {
prizes: number[];
fee: number;
ticketsAlreadySold: number;
ticketsTotal: number;
}): void {
const totalPrizePool = opts.prizes.reduce((total, prize) => {
return total + prize;
}, 0);
const ticketsToBuy = 1;
const ret =
(totalPrizePool - opts.fee * ticketsToBuy) / ticketsAlreadySold +
ticketsToBuy;
const worth = ret > opts.fee;
return worth;
}
const runSim = (
runs: number,
tickets: number
): Promise<{ runs: number; tickets: number; totalWin: number }> => {
return new Promise((resolve) => {
const allWinnings = [];
for (let i = 0; i < runs; i++) {
let winnings = 0;
const soldTickets = shuffle(
Array.from({ length: tickets }).map((_, index) => index)
);
const prizes = shuffle([500, 350, 150]);
while (prizes.length > 0) {
const drawnTicket = soldTickets.pop();
if (drawnTicket) {
const thisWin = prizes.pop() ?? 0;
if (drawnTicket < tickets) {
winnings += thisWin;
}
}
}
allWinnings.push(winnings);
}
const totalWin = allWinnings.reduce((total, win) => {
return total + win;
}, 0);
resolve({
runs,
tickets,
totalWin,
});
});
};
function onSubmit(event: FormEvent<HTMLFormElement>): void {
event.preventDefault();
const formData = new FormData(event.currentTarget);
const opts = {
prizes: formData.get("prizes"),
prizes: formData.get("fee"),
prizes: formData.get("ticketsAlreadySold"),
prizes: formData.get("ticketsTotal"),
};
const wrapper = async (): Promise<void> => {
if ((runs ?? 0) > 0 && (tickets ?? 0) > 0) {
workerRef.current?.postMessage(opts);
//const output = await runSim(runs, tickets);
//setOutputs([...outputs, output]);
}
};
void wrapper();
}
return (
<>
<form className="dark:text-white" onSubmit={onSubmit}>
<label>Prizes</label>
<br />
<input className="text-black" type="text" name="prizes" />
<br />
<label>Fee</label>
<br />
<input className="text-black" type="text" name="fee" />
<br />
<label>Tickets sold</label>
<br />
<input className="text-black" type="text" name="ticketsAlreadySold" />
<br />
<label>Tickets total</label>
<br />
<input className="text-black" type="text" name="ticketsTotal" />
<br />
<button className="p-2 dark:bg-dracula-bg-lighter rounded-sm" type="submit">
Run
</button>
<br />
</form>
<br />
{outputs.length ? (
<table className={"dark:text-white"}>
<thead>
<tr>
<th>Runs</th>
<th>Tickets</th>
<th>Winnings</th>
</tr>
</thead>
<tbody>
{outputs.map((output, index) => {
return (
<tr key={index}>
<td>{output.runs}</td>
<td>{output.tickets}</td>
<td>{output.totalWin}</td>
</tr>
);
})}
</tbody>
</table>
) : null}
</>
);
}