Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
emrergin committed Apr 15, 2024
1 parent 3617e1c commit 516a311
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 58 deletions.
56 changes: 27 additions & 29 deletions src/components/Round.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useRef, Dispatch, SetStateAction } from "react";
import Drawing from "./experimentComponents/Drawing";
import BagHolder from "./experimentComponents/BagHolder";

import { Round } from "@prisma/client";
import { Round as RoundT } from "@prisma/client";
import { DrawingT, Phase, SubTypeRound } from "@/utilities/types";
import { getDiceText } from "@/utilities/functions";
import RoundBottom from "./experimentComponents/RoundBottom";
Expand Down Expand Up @@ -35,7 +35,7 @@ function Round({
const [selectedBag, setSelectedBag] = useState<"blue" | "red">(
Math.random() < priors[0] / (priors[0] + priors[1]) ? "blue" : "red",
);
const roundData = useRef<Partial<Round>>({
const roundData = useRef<Partial<RoundT>>({
is_blue: selectedBag === "blue",
});
const [subPhase, setSubPhase] = useState<"drawing" | "input" | "result">(
Expand Down Expand Up @@ -67,15 +67,15 @@ function Round({
setSubPhase("input");
}

async function generateNewRound(lastRound: Omit<Round, "id">) {
async function generateNewRound(lastRound: Omit<RoundT, "id">) {
await fetch("./api/round", {
method: "POST",
body: JSON.stringify(lastRound),
});
}

function nextRound() {
const lastRound: Omit<Round, "id"> = {
const lastRound: Omit<RoundT, "id"> = {
...(roundData.current as SubTypeRound),
participantId,
chosen_probability: 100 - redRatio,
Expand Down Expand Up @@ -106,31 +106,29 @@ function Round({

return (
<div>
<>
<BagHolder
aBlue={aBlue}
bBlue={bBlue}
diceText={diceText}
showBalls={subPhase === "drawing"}
/>
<Drawing
numberOfDraws={arrayOfDraws[currentRound]}
numberofBlues={selectedBag === "blue" ? bBlue : aBlue}
nextFunction={(d) => endDrawing(d)}
fullView={subPhase === "drawing"}
key={currentRound}
/>
<RoundBottom
subPhase={subPhase}
redRatio={redRatio}
setRedRatio={setRedRatio}
bsr={bsr}
chosenCircle={selectedBag}
pointsForCurrentRound={pointsForCurrentRound}
setCurrentPoints={setPointsForCurrentRound}
nextSubPhase={nextSubPhase}
/>
</>
<BagHolder
aBlue={aBlue}
bBlue={bBlue}
diceText={diceText}
showBalls={subPhase === "drawing"}
/>
<Drawing
numberOfDraws={arrayOfDraws[currentRound]}
numberofBlues={selectedBag === "blue" ? bBlue : aBlue}
nextFunction={(d) => endDrawing(d)}
fullView={subPhase === "drawing"}
key={currentRound}
/>
<RoundBottom
subPhase={subPhase}
redRatio={redRatio}
setRedRatio={setRedRatio}
bsr={bsr}
chosenCircle={selectedBag}
pointsForCurrentRound={pointsForCurrentRound}
setCurrentPoints={setPointsForCurrentRound}
nextSubPhase={nextSubPhase}
/>
</div>
);
}
Expand Down
44 changes: 21 additions & 23 deletions src/components/Round2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,27 @@ function Round2({

return (
<div>
<>
<BagHolder2
aBlue={arrayOfBags[currentRound]}
showBalls={subPhase === "drawing"}
/>
<Drawing2
nextFunction={(d) => endDrawing(d)}
fullView={subPhase === "drawing"}
key={currentRound}
result={subPhase === "result"}
isBlue={currentColor === "blue"}
/>
<RoundBottom
subPhase={subPhase}
redRatio={redRatio}
setRedRatio={setRedRatio}
bsr={bsr}
chosenCircle={currentColor}
pointsForCurrentRound={pointsForCurrentRound}
setCurrentPoints={setPointsForCurrentRound}
nextSubPhase={nextSubPhase}
/>
</>
<BagHolder2
aBlue={arrayOfBags[currentRound]}
showBalls={subPhase === "drawing"}
/>
<Drawing2
nextFunction={(d) => endDrawing(d)}
fullView={subPhase === "drawing"}
key={currentRound}
result={subPhase === "result"}
isBlue={currentColor === "blue"}
/>
<RoundBottom
subPhase={subPhase}
redRatio={redRatio}
setRedRatio={setRedRatio}
bsr={bsr}
chosenCircle={currentColor}
pointsForCurrentRound={pointsForCurrentRound}
setCurrentPoints={setPointsForCurrentRound}
nextSubPhase={nextSubPhase}
/>
</div>
);
}
Expand Down
71 changes: 65 additions & 6 deletions src/utilities/functions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { RoundToDownload } from "@/pages/api/round";
import { Round } from "@prisma/client";
import { Phase, SubTypeRound } from "./types";
import { Dispatch, MutableRefObject, SetStateAction } from "react";

function arrayToCsv(data: RoundToDownload[], columnNames: string[]): string {
let data2: (string[] | RoundToDownload)[] = [columnNames, ...data];
Expand Down Expand Up @@ -65,10 +68,66 @@ export function getDateText() {
);
}

export function gaussianRandom(mean = 0, stdev = 1) {
const u = 1 - Math.random(); // Converting [0,1) to (0,1]
const v = Math.random();
const z = Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
// Transform to the desired mean and standard deviation:
return z * stdev + mean;
async function generateNewRound(lastRound: Omit<Round, "id">) {
await fetch("./api/round", {
method: "POST",
body: JSON.stringify(lastRound),
});
}

export function nextRound(
roundData: MutableRefObject<Partial<Round>>,
participantId: string,
redRatio: number,
pointsForCurrentRound: number,
currentRound: number,
pointFunction: Dispatch<SetStateAction<number>>,
numberOfRounds: number,
priors: number[],
type: "bayesian" | "guess",
time: MutableRefObject<Date>,
setCurrentColor: Dispatch<SetStateAction<"blue" | "red">>,
setSubPhase: Dispatch<SetStateAction<"input" | "result" | "drawing">>,
arrayOfNumbers: number[],
roundFunction: (r: number) => void,
setRedRatio: Dispatch<SetStateAction<number>>,
phaseFunction: (p: Phase) => void,
) {
const lastRound: Omit<Round, "id"> = {
...(roundData.current as SubTypeRound),
participantId,
chosen_probability: 100 - redRatio,
reward: pointsForCurrentRound,
round: currentRound + 1,
round_parameter: arrayOfNumbers[currentRound],
};
console.log(lastRound);
generateNewRound(lastRound);
pointFunction((p: number) => p + pointsForCurrentRound);

if (currentRound < numberOfRounds - 1) {
if (type === "bayesian") {
const newBag =
Math.random() < priors[0] / (priors[0] + priors[1]) ? "blue" : "red";
setCurrentColor(newBag);
setSubPhase("drawing");
roundData.current = {
...roundData.current,
is_blue: newBag === "blue" ? true : false,
};
} else {
const newBall =
Math.random() < arrayOfNumbers[currentRound] / 100 ? "blue" : "red";
setCurrentColor(newBall);
setSubPhase("drawing");
roundData.current = {
...roundData.current,
};
}
roundFunction(currentRound + 1);
setRedRatio(50);
time.current = new Date();
} else {
phaseFunction(Phase.Demographics);
}
}

0 comments on commit 516a311

Please sign in to comment.