From c072add8710d55b080ddf9fcb0b86c0d88217e02 Mon Sep 17 00:00:00 2001 From: Joe Monk Date: Sat, 12 Oct 2024 01:51:43 +0100 Subject: [PATCH] Remove sim --- src/components/sim.tsx | 149 ----------------------------------------- 1 file changed, 149 deletions(-) delete mode 100644 src/components/sim.tsx diff --git a/src/components/sim.tsx b/src/components/sim.tsx deleted file mode 100644 index 34dd943..0000000 --- a/src/components/sim.tsx +++ /dev/null @@ -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(); - - 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): 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 => { - if ((runs ?? 0) > 0 && (tickets ?? 0) > 0) { - workerRef.current?.postMessage(opts); - //const output = await runSim(runs, tickets); - //setOutputs([...outputs, output]); - } - }; - void wrapper(); - } - - return ( - <> -
- -
- -
- -
- -
- -
- -
- -
- -
- -
-
-
- {outputs.length ? ( - - - - - - - - - - {outputs.map((output, index) => { - return ( - - - - - - ); - })} - -
RunsTicketsWinnings
{output.runs}{output.tickets}{output.totalWin}
- ) : null} - - ); -}