import "./styles.sass"; import {GroupContext} from "@/common/contexts/GroupContext.jsx"; import {useContext, useEffect, useState} from "react"; import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"; import {faForward, faShoppingCart, faStop} 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"; import {SettingsContext} from "@/common/contexts/SettingsProvider.jsx"; import {useNavigate} from "react-router"; const localeOptions = { style: "decimal", minimumFractionDigits: 2, maximumFractionDigits: 2, signDisplay: "never" } export const Calculate = ({setState, currentRound}) => { const {round, getGroupById, updateCapital, endRound, setRoundHistory} = useContext(GroupContext); const {rounds, demandTable, costPerCake, costPerRound} = useContext(SettingsContext); const navigate = useNavigate(); const [animatedGroups, setAnimatedGroups] = useState([]); const {musicEnabled} = useContext(MusicContext); const [nachfrage, setNachfrage] = useState(0); const [showNewCapital, setShowNewCapital] = useState(false); 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 * costPerCake + costPerRound); let currentRound = {...current, profit, name: getGroupById(current.id).name, sold, newCapital: getGroupById(current.id).capital + profit}; setAnimatedGroups(old => [...old, currentRound]); updateCapital(current.id, currentRound.newCapital); } useEffect(() => { if (round.length === 0) { endRound(); return; } const timeout = setTimeout(() => { animateNext(); }, 3000); return () => clearTimeout(timeout); }, [animatedGroups]); useEffect(() => { if (animatedGroups.length === 0) return; setRoundHistory(history => [...history, animatedGroups]); const timeout = setTimeout(() => { setShowNewCapital(true); }, 3000); return () => clearTimeout(timeout); }, [round]); useEffect(() => { let avg = 0; let amount = 0; round.forEach(r => { avg += r.price * r.amount; amount += parseInt(r.amount); }); avg /= amount; let nachfrage = 0; for (let key in demandTable) { if (avg >= parseInt(key)) nachfrage = demandTable[key]; } setNachfrage(nachfrage); setAnimatedGroups([]); }, []); return (

Runde {currentRound}

{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)} €

Menge

{r.amount} Kisten

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)} €

}
) })}
{showNewCapital &&

Nachfrage

{nachfrage}

); }