From e87356b27a59e7c8e09a2cffec5132fc7783913b Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Mon, 20 Nov 2023 21:17:52 +0100 Subject: [PATCH] Updated the Calculate.jsx --- .../pages/Game/states/Calculate/Calculate.jsx | 117 +++++++++++------- 1 file changed, 75 insertions(+), 42 deletions(-) diff --git a/client/src/pages/Game/states/Calculate/Calculate.jsx b/client/src/pages/Game/states/Calculate/Calculate.jsx index 9c29ae3..e11ef8a 100644 --- a/client/src/pages/Game/states/Calculate/Calculate.jsx +++ b/client/src/pages/Game/states/Calculate/Calculate.jsx @@ -2,11 +2,12 @@ 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"; +import {faForward, faShoppingCart} from "@fortawesome/free-solid-svg-icons"; import InterfaceSound from "@/common/sounds/interface.mp3"; import Sound from "react-sound"; import {MusicContext} from "@/common/contexts/MusicContext.jsx"; import BeginSound from "@/common/sounds/begin.mp3"; +import Button from "@/common/components/Button"; const localeOptions = { style: "decimal", @@ -23,6 +24,7 @@ export const Calculate = ({setState}) => { const {musicEnabled} = useContext(MusicContext); const [nachfrage, setNachfrage] = useState(0); + const [showNewCapital, setShowNewCapital] = useState(false); const animateNext = () => { const current = round.shift(); @@ -33,7 +35,8 @@ export const Calculate = ({setState}) => { let profit = sold * current.price - (current.amount * 1000 + 4000); - setAnimatedGroups(old => [...old, {...current, profit, name: getGroupById(current.id).name}]); + setAnimatedGroups(old => [...old, {...current, profit, name: getGroupById(current.id).name, sold, + newCapital: getGroupById(current.id).capital + profit}]); updateCapital(current.id, getGroupById(current.id).capital + profit); } @@ -41,7 +44,6 @@ export const Calculate = ({setState}) => { useEffect(() => { if (round.length === 0) { endRound(); - setTimeout(() => setState("waiting"), 8000); return; } @@ -52,10 +54,25 @@ export const Calculate = ({setState}) => { return () => clearTimeout(timeout); }, [animatedGroups]); + useEffect(() => { + if (animatedGroups.length === 0) return; + + const timeout = setTimeout(() => { + setShowNewCapital(true); + }, 3000); + + return () => clearTimeout(timeout); + }, [round]); + useEffect(() => { let avg = 0; - round.forEach(r => avg += r.price); - avg /= round.length; + let amount = 0; + round.forEach(r => { + avg += r.price * r.amount; + amount += parseInt(r.amount); + }); + + avg /= amount; setNachfrage(avg < 1800 ? 50 : avg > 2200 ? 30 : 40); setAnimatedGroups([]); @@ -64,49 +81,65 @@ export const Calculate = ({setState}) => { return (
-
- {animatedGroups.length === 0 && <> - -

Die Kuchen werden verkauft...

- } - {animatedGroups.map((r, i) => { - return ( -
- {animatedGroups.length - 1 === i && musicEnabled && - } -
-

Unternehmen

-

{r.name}

-
+
+
+ {animatedGroups.length === 0 && <> + +

Die Kuchen werden verkauft...

+ } + {animatedGroups.map((r, i) => { + return ( +
+ {animatedGroups.length - 1 === i && musicEnabled && + } +
+

Unternehmen

+

{r.name}

+
-
-

Absatzpreis

-

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

-
+
+

Absatzpreis

+

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

+
-
-

Menge

-

{r.amount} Kisten

-
+
+

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 €

} +
+

Verkauft

+

{r.sold} Kisten

+
+ + {!showNewCapital &&
+

Gewinn/Verlust

+ {r.profit > 0 &&

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

} + {r.profit < 0 &&

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

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

0 €

} +
} + + {showNewCapital &&
+

Kapital + {r.profit > 0 && + {r.profit?.toLocaleString("de-DE", localeOptions)} €} + {r.profit < 0 && - {r.profit?.toLocaleString("de-DE", localeOptions)} €} + {r.profit === 0 && (Unverändert)} +

+

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

+
}
-
- ) - })} -
-
- -
-

Nachfrage

-

{nachfrage}

+ ) + })} + {showNewCapital &&
+
+ +
+

Nachfrage

+

{nachfrage}

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