From b53dd16054521d0de9cff8567a9a0256b7414d6d Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sat, 18 Nov 2023 23:39:56 +0100 Subject: [PATCH] Created the Calculate.jsx game state --- .../pages/Game/states/Calculate/Calculate.jsx | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 client/src/pages/Game/states/Calculate/Calculate.jsx diff --git a/client/src/pages/Game/states/Calculate/Calculate.jsx b/client/src/pages/Game/states/Calculate/Calculate.jsx new file mode 100644 index 0000000..35d5370 --- /dev/null +++ b/client/src/pages/Game/states/Calculate/Calculate.jsx @@ -0,0 +1,103 @@ +import "./styles.sass"; +import {GroupContext} from "@/common/contexts/GroupContext.jsx"; +import {useContext, useEffect, useState} from "react"; +import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; +import {faShoppingCart} from "@fortawesome/free-solid-svg-icons"; + +const localeOptions = { + style: "decimal", + minimumFractionDigits: 2, + maximumFractionDigits: 2, + signDisplay: "never" +} + +export const Calculate = ({setState}) => { + const {round, getGroupById, updateCapital, endRound} = useContext(GroupContext); + + const [animatedGroups, setAnimatedGroups] = useState([]); + + const [nachfrage, setNachfrage] = useState(0); + + const animateNext = () => { + const current = round.shift(); + if (!current) return; + + let sold = current.amount < nachfrage ? current.amount : nachfrage; + setNachfrage(nachfrage => nachfrage - sold); + + let profit = sold * current.price - (current.amount * 1000 + 4000); + + setAnimatedGroups(old => [...old, {...current, profit, name: getGroupById(current.id).name}]); + + console.log(current.id, getGroupById(current.id).capital, profit); + + updateCapital(current.id, getGroupById(current.id).capital + profit); + } + + + useEffect(() => { + if (round.length === 0) { + endRound(); + setTimeout(() => setState("waiting"), 8000); + return; + } + + const timeout = setTimeout(() => { + animateNext(); + }, 3000); + + return () => clearTimeout(timeout); + }, [animatedGroups]); + + useEffect(() => { + let avg = 0; + round.forEach(r => avg += r.amount); + avg /= round.length; + + setNachfrage(avg < 1800 ? 50 : avg > 2200 ? 30 : 40); + setAnimatedGroups([]); + }, []); + + return ( +
+
+ {animatedGroups.length === 0 &&

Die Kuchen werden verkauft...

} + {animatedGroups.map((r, i) => { + return ( +
+
+

Unternehmen

+

{r.name}

+
+ +
+

Absatzpreis

+

{r.price?.toLocaleString("de-DE", localeOptions)} €

+
+ +
+

Menge

+

{r.amount} Kisten

+
+ +
+

Gewinn/Verlust

+ {r.profit > 0 &&

+ {r.profit?.toLocaleString("de-DE", localeOptions)} €

} + {r.profit < 0 &&

- {r.profit?.toLocaleString("de-DE", localeOptions)} €

} + {r.profit === 0 &&

0 €

} +
+
+ ) + })} +
+
+ +
+

Nachfrage

+

{nachfrage}

+
+
+ +
+ ); +} \ No newline at end of file