From 30c4301d25a4f4d27b08e205bcb940be9eb93a2a Mon Sep 17 00:00:00 2001 From: Michael Urich <80188367+mturich@users.noreply.github.com> Date: Mon, 20 Jun 2022 21:21:28 +0200 Subject: [PATCH 1/5] added 2. button to buy 10 x Dim --- src/App.css | 64 +++++++++++++++++++---------------- src/App.tsx | 84 +++++++++++++++++++++++++++++++++++++++------- src/App2.tsx | 33 ------------------ src/App3.tsx | 42 ----------------------- src/Dimension.tsx | 34 +++++++++++++------ src/Dimentions.css | 0 src/main.tsx | 2 +- 7 files changed, 130 insertions(+), 129 deletions(-) delete mode 100644 src/App2.tsx delete mode 100644 src/App3.tsx create mode 100644 src/Dimentions.css diff --git a/src/App.css b/src/App.css index 8da3fde..f2abbdf 100644 --- a/src/App.css +++ b/src/App.css @@ -1,42 +1,48 @@ -.App { - text-align: center; +* { + /* border: 1px solid black; */ } -.App-logo { - height: 40vmin; - pointer-events: none; +.App { + margin: 1rem; +} +.heading { + display: flex; + padding: 2rem; + margin: auto; + width: 100%; } -@media (prefers-reduced-motion: no-preference) { - .App-logo { - animation: App-logo-spin infinite 20s linear; - } +.highlight { + padding: 0.5rem 2rem; + font-size: 1.3rem; + border: 1px black solid; + background-color: beige; } -.App-header { - background-color: #282c34; - min-height: 100vh; - display: flex; - flex-direction: column; - align-items: center; - justify-content: center; - font-size: calc(10px + 2vmin); - color: white; +.gridContainer { + display: grid; + grid-template-columns: repeat(4, 1fr); + gap: 10px; + grid-auto-rows: minmax(40px, auto); } -.App-link { - color: #61dafb; +.centered { + display: flex; + justify-content: center; + margin: auto; + align-items: center; + text-align: center; } -@keyframes App-logo-spin { - from { - transform: rotate(0deg); - } - to { - transform: rotate(360deg); - } +.btn { + font-size: calc(10px + 1vmin); + border: 1px black solid; + border-radius: 10px; + background-color: rgb(240, 238, 238); } -button { - font-size: calc(10px + 2vmin); +.btn:disabled{ + background-color: rgb(255, 254, 254); + border: 1px rgb(240, 238, 238) solid; + color: rgb(180, 177, 177); } diff --git a/src/App.tsx b/src/App.tsx index fcabae8..bd939a7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,29 +1,87 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, useRef } from 'react'; import './App.css'; +import Dimension from './Dimension'; + + +type DimProps = { + balance: number; + dimCount: number; + setDimCount: (fn: (dim: number) => void) => void; + setBalance: (fn: ((balance: number) => void) | number) => void; + price: number; + +}; function App() { - const [count, setCount] = useState(0); + const timerExpiredCallback = useRef(() => {}); + const [balance, setBalance] = useState(100); + const [clockSpeed, setClockSpeed] = useState(2000) const [firstDimCount, setFirstDimCount] = useState(0); - const [timer, setTime] = useState(0); + const [secondDimCount, setSecondDimCount] = useState(0); + const [thirdDimCount, setThirdDimCount] = useState(0); + // if the component updates, replace the timeout callback with one that references the new + // values of `count` and `firstDimCount` + + timerExpiredCallback.current = () => { + setBalance(prevBalance => prevBalance + firstDimCount); + setFirstDimCount(prevFirstDim => prevFirstDim + secondDimCount / 10); + setSecondDimCount(prevSecondDim => prevSecondDim + thirdDimCount / 100); + }; useEffect(() => { + // console.log(firstDimCount, secondDimCount, thirdDimCount); + }); + + useEffect(() => { + let timeID = -1; const startTimer = () => { - const myTimer = setTimeout(() => { - console.log(count, firstDimCount); - setCount(count => count + firstDimCount); + timeID = setTimeout(() => { + timerExpiredCallback.current(); startTimer(); - }, 1000); + }, 2000); }; - - startTimer(); - return () => {}; + startTimer(); + + // if we ever unmount / destroy this component instance, clear the timeout + return () => clearTimeout(timeID); }, []); return (
-
You have {count} antimatters.
-
You have {firstDimCount} First Dimension x2.0
- +
+

+ You have {balance % 1 === 0 ? balance.toFixed(0) : balance.toFixed(1)} antimatters. +

+
+ + + First Dim + + + + Second Dim + + + {secondDimCount > 0 && ( + + Third Dim + + )}
); } diff --git a/src/App2.tsx b/src/App2.tsx deleted file mode 100644 index b6775b2..0000000 --- a/src/App2.tsx +++ /dev/null @@ -1,33 +0,0 @@ -import { useState, useEffect } from 'react'; -import './App.css'; - -function App() { - const [count, setCount] = useState(0); - const [firstDimCount, setFirstDimCount] = useState(0); - const [timer, setTime] = useState(0); - const [running, setRunning] = useState(false) - - useEffect(() => { - if (running) { - const myTimer = setInterval(() => { - console.log(count, firstDimCount); - setCount(count => count + firstDimCount); - }, 500); - - return () => clearInterval(myTimer); - } - - },[running]); - - return ( -
-
You have {count} antimatters.
-
You have {firstDimCount} First Dimension x2.0
- -

- -
- ); -} - -export default App; diff --git a/src/App3.tsx b/src/App3.tsx deleted file mode 100644 index 179e2ef..0000000 --- a/src/App3.tsx +++ /dev/null @@ -1,42 +0,0 @@ -import { useState, useEffect, useRef } from 'react'; -import './App.css'; -import Dimension from './Dimension'; - -function App() { - const timerExpiredCallback = useRef(() => {}); - const [balance, setBalance] = useState(100); - const [firstDimCount, setFirstDimCount] = useState(0); - const [secondDimCount, setSecondDimCount] = useState(0); - // if the component updates, replace the timeout callback with one that references the new - // values of `count` and `firstDimCount` - - timerExpiredCallback.current = () => { - setBalance(balance + firstDimCount + 10*secondDimCount); - }; - - useEffect(() => { - let timeID = -1; - const startTimer = () => { - timeID = setTimeout(() => { - timerExpiredCallback.current(); - startTimer(); - }, 1000); - }; - startTimer(); - - // if we ever unmount / destroy this component instance, clear the timeout - return () => clearTimeout(timeID); - }, []); - - return ( -
-
You have {balance} antimatters.
- - First Dim: 10 - - Second Dim: 100 -
- ); -} - -export default App; diff --git a/src/Dimension.tsx b/src/Dimension.tsx index 69e8f26..63835e6 100644 --- a/src/Dimension.tsx +++ b/src/Dimension.tsx @@ -1,31 +1,43 @@ import { useState, useEffect } from 'react'; +import './Dimentions.css'; type DimProps = { balance: number; dimCount: number; setDimCount: (fn: (dim: number) => void) => void; setBalance: (fn: ((balance: number) => void) | number) => void; - factor: number; + price: number; children: string; }; export default function Dimension(props: DimProps) { - const { balance, dimCount, setDimCount, setBalance, factor } = props; + const { balance, dimCount, setDimCount, setBalance, price } = props; - const handleFirstDimBuy = () => { - if (balance >= factor) { - setDimCount(dimCount => dimCount + 1); - setBalance(balance => balance - factor); + const handleDimBuy = (quantity:number) => { + if (balance >= price) { + setDimCount(dimCount => dimCount + quantity); + setBalance(balance => balance - price*quantity); } }; return ( <>
-
- You have {dimCount} {props.children.split(" ")[0]} {factor / 10}x +
+

+ {`You have ${dimCount%1===0 ? dimCount.toFixed(0): dimCount.toFixed(1)} ${props.children.split(' ')[0]} + ${price / 10}x`} +

+

+ {`You have ${dimCount%1===0 ? dimCount.toFixed(0): dimCount.toFixed(1)} ${props.children.split(' ')[0]} + ${price / 10}x`} +

+ +
- +
); diff --git a/src/Dimentions.css b/src/Dimentions.css new file mode 100644 index 0000000..e69de29 diff --git a/src/main.tsx b/src/main.tsx index 9a9546e..4a1b150 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,6 +1,6 @@ import React from 'react' import ReactDOM from 'react-dom/client' -import App from './App3' +import App from './App' import './index.css' ReactDOM.createRoot(document.getElementById('root')!).render( From c38af15739176717d9dc4479736139463c163b5e Mon Sep 17 00:00:00 2001 From: Michael Urich <80188367+mturich@users.noreply.github.com> Date: Mon, 20 Jun 2022 22:37:11 +0200 Subject: [PATCH 2/5] tickspeed buy and cost increase implemented, factor bump prep --- src/App.css | 24 +++++++++++++++++++-- src/App.tsx | 55 +++++++++++++++++++++++++++++++++-------------- src/Dimension.tsx | 26 ++++++++++++++-------- 3 files changed, 78 insertions(+), 27 deletions(-) diff --git a/src/App.css b/src/App.css index f2abbdf..5b28302 100644 --- a/src/App.css +++ b/src/App.css @@ -19,13 +19,20 @@ background-color: beige; } -.gridContainer { +.gridContainer4Cols { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; grid-auto-rows: minmax(40px, auto); } +.gridContainer3Rows { + display: grid; + grid-template-rows: repeat(3, 1fr); + gap: 4px; + grid-auto-rows: minmax(40px, auto); +} + .centered { display: flex; justify-content: center; @@ -36,10 +43,23 @@ .btn { font-size: calc(10px + 1vmin); + padding: 0.2em 1em 0.2em 1em; + margin: 0 0.5rem; border: 1px black solid; - border-radius: 10px; + border-radius: 6px; background-color: rgb(240, 238, 238); } +.btn:hover { + background-image: linear-gradient(to right, #29323c, #485563, #2b5876, #4e4376); + box-shadow: 0 4px 15px 0 rgba(45, 54, 65, 0.75); +} +.btn:disabled{ + background: initial; + box-shadow: initial; +} +.sm{ + font-size: calc(10px + 0.5vmin); +} .btn:disabled{ background-color: rgb(255, 254, 254); diff --git a/src/App.tsx b/src/App.tsx index bd939a7..37873f5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,23 +2,25 @@ import { useState, useEffect, useRef } from 'react'; import './App.css'; import Dimension from './Dimension'; - type DimProps = { balance: number; dimCount: number; setDimCount: (fn: (dim: number) => void) => void; setBalance: (fn: ((balance: number) => void) | number) => void; price: number; - }; function App() { const timerExpiredCallback = useRef(() => {}); - const [balance, setBalance] = useState(100); - const [clockSpeed, setClockSpeed] = useState(2000) + const clockSpeedRef = useRef(2000); + const [tickspeedPrice, setTickspeedPrice] = useState(10); + const [balance, setBalance] = useState(200); const [firstDimCount, setFirstDimCount] = useState(0); + const [fistDimPrice, setFirstDimPrice] = useState(10); const [secondDimCount, setSecondDimCount] = useState(0); + const [secondDimPrice, setSecondDimPrice] = useState(100) const [thirdDimCount, setThirdDimCount] = useState(0); + const [thirdDimPrice, setThirdDimPrice] = useState(1000) // if the component updates, replace the timeout callback with one that references the new // values of `count` and `firstDimCount` @@ -28,17 +30,13 @@ function App() { setSecondDimCount(prevSecondDim => prevSecondDim + thirdDimCount / 100); }; - useEffect(() => { - // console.log(firstDimCount, secondDimCount, thirdDimCount); - }); - useEffect(() => { let timeID = -1; const startTimer = () => { timeID = setTimeout(() => { timerExpiredCallback.current(); startTimer(); - }, 2000); + }, clockSpeedRef.current); }; startTimer(); @@ -46,6 +44,12 @@ function App() { return () => clearTimeout(timeID); }, []); + const handleTickBtnClick = () => { + clockSpeedRef.current = clockSpeedRef.current * (1 - 0.11); + setBalance(prevPrice => prevPrice - tickspeedPrice); + setTickspeedPrice(prevPrice => prevPrice * 10); + }; + return (
@@ -53,14 +57,31 @@ function App() { You have {balance % 1 === 0 ? balance.toFixed(0) : balance.toFixed(1)} antimatters.

- + +
+

{`The current clockspeed is ${clockSpeedRef.current} ms. Reduce the tickspeed by 11%.`}

+
+ + +
+
+ - First Dim + price={fistDimPrice} + setPrice={setFirstDimPrice} + > + {`1.Dim Cost: ${fistDimPrice}`} - Second Dim + price={secondDimPrice} + setPrice={setSecondDimPrice}> + {`2.Dim Cost: ${secondDimPrice}`} {secondDimCount > 0 && ( @@ -78,8 +100,9 @@ function App() { dimCount={thirdDimCount} setDimCount={setThirdDimCount} setBalance={setBalance} - price={1000}> - Third Dim + price={thirdDimPrice} + setPrice={setThirdDimPrice}> + {`2.Dim Cost: ${thirdDimPrice}`} )}
diff --git a/src/Dimension.tsx b/src/Dimension.tsx index 63835e6..abbe8bf 100644 --- a/src/Dimension.tsx +++ b/src/Dimension.tsx @@ -10,30 +10,38 @@ type DimProps = { children: string; }; export default function Dimension(props: DimProps) { - const { balance, dimCount, setDimCount, setBalance, price } = props; + const { balance, dimCount, setDimCount, setBalance, price, setPrice } = props; - const handleDimBuy = (quantity:number) => { + const handleDimBuy = (quantity: number) => { if (balance >= price) { setDimCount(dimCount => dimCount + quantity); - setBalance(balance => balance - price*quantity); + setBalance(balance => balance - price * quantity); } + if ((dimCount + 1) % 10 === 0 && dimCount > 1) setPrice(prevPrice => prevPrice * 10); }; return ( <>
-
+

- {`You have ${dimCount%1===0 ? dimCount.toFixed(0): dimCount.toFixed(1)} ${props.children.split(' ')[0]} + {`You have ${dimCount % 1 === 0 ? dimCount.toFixed(0) : dimCount.toFixed(1)} ${ + props.children.split(' ')[0] + } ${price / 10}x`}

- {`You have ${dimCount%1===0 ? dimCount.toFixed(0): dimCount.toFixed(1)} ${props.children.split(' ')[0]} - ${price / 10}x`} + {` ${dimCount % 1 === 0 ? dimCount.toFixed(0) : dimCount.toFixed(1)} + (${dimCount.toFixed(0) % 10}) + + `}

- -
From ad46de52de59630ff9e6ba403020874ff346a155 Mon Sep 17 00:00:00 2001 From: Michael Urich <80188367+mturich@users.noreply.github.com> Date: Tue, 21 Jun 2022 22:43:31 +0200 Subject: [PATCH 3/5] localdata save prep --- src/App.tsx | 114 ++++++++++++++++++++++++++++++++++------------ src/Dimension.tsx | 42 ++++++++++------- 2 files changed, 110 insertions(+), 46 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 37873f5..2538bfa 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -3,37 +3,79 @@ import './App.css'; import Dimension from './Dimension'; type DimProps = { - balance: number; + antimatter: number; dimCount: number; setDimCount: (fn: (dim: number) => void) => void; - setBalance: (fn: ((balance: number) => void) | number) => void; + setAntimatter: (fn: ((antimatter: number) => void) | number) => void; price: number; }; function App() { const timerExpiredCallback = useRef(() => {}); const clockSpeedRef = useRef(2000); + const timerId = useRef(-1); + const toSaveId = useRef(-1); const [tickspeedPrice, setTickspeedPrice] = useState(10); - const [balance, setBalance] = useState(200); + const [antimatter, setAntimatter] = useState(200); + + const [firstDimFactor, setFirstDimFactor] = useState(1.1); const [firstDimCount, setFirstDimCount] = useState(0); - const [fistDimPrice, setFirstDimPrice] = useState(10); + const [firstDimPrice, setFirstDimPrice] = useState(10); + const [firstDimFactorCount, setFirstDimFactorCount] = useState(0); + + const [secondDimFactor, setSecondDimFactor] = useState(1.1); const [secondDimCount, setSecondDimCount] = useState(0); - const [secondDimPrice, setSecondDimPrice] = useState(100) + const [secondDimPrice, setSecondDimPrice] = useState(100); + const [secondDimFactorCount, setSecondDimFactorCount] = useState(0); + + const [thirdDimFactor, setThirdDimFactor] = useState(1.1); const [thirdDimCount, setThirdDimCount] = useState(0); - const [thirdDimPrice, setThirdDimPrice] = useState(1000) + const [thirdDimPrice, setThirdDimPrice] = useState(1000); + const [thirdDimFactorCount, setThirdDimFactorCount] = useState(0); // if the component updates, replace the timeout callback with one that references the new // values of `count` and `firstDimCount` + useEffect(() => { + const startSave = () => { + timerId.current = setTimeout(() => { + const toSave = { + timerId, + clockSpeedRef, + tickspeedPrice, + antimatter, + firstDimCount, + firstDimFactor, + firstDimPrice, + firstDimFactorCount, + secondDimCount, + secondDimFactor, + secondDimPrice, + secondDimFactorCount, + thirdDimCount, + thirdDimFactor, + thirdDimPrice, + thirdDimFactorCount, + }; + console.log(toSave); + //localStorage.setItem("data", JSON.stringify(toSave)); + startSave(); + }, 5 * 60 * 1000); + }; + startSave(); + + // if we ever unmount / destroy this component instance, clear the timeout + return () => clearTimeout(timerId.current); + }, []); + timerExpiredCallback.current = () => { - setBalance(prevBalance => prevBalance + firstDimCount); - setFirstDimCount(prevFirstDim => prevFirstDim + secondDimCount / 10); - setSecondDimCount(prevSecondDim => prevSecondDim + thirdDimCount / 100); + setAntimatter(prevantimatter => prevantimatter + firstDimCount * firstDimFactor); + setFirstDimCount(prevFirstDim => prevFirstDim + (secondDimCount / 10) * secondDimFactor); + setSecondDimCount(prevSecondDim => prevSecondDim + (thirdDimCount / 100) * thirdDimFactor); }; useEffect(() => { - let timeID = -1; const startTimer = () => { - timeID = setTimeout(() => { + timerId.current = setTimeout(() => { timerExpiredCallback.current(); startTimer(); }, clockSpeedRef.current); @@ -41,12 +83,12 @@ function App() { startTimer(); // if we ever unmount / destroy this component instance, clear the timeout - return () => clearTimeout(timeID); + return () => clearTimeout(timerId.current); }, []); const handleTickBtnClick = () => { clockSpeedRef.current = clockSpeedRef.current * (1 - 0.11); - setBalance(prevPrice => prevPrice - tickspeedPrice); + setAntimatter(prevPrice => prevPrice - tickspeedPrice); setTickspeedPrice(prevPrice => prevPrice * 10); }; @@ -54,7 +96,8 @@ function App() {

- You have {balance % 1 === 0 ? balance.toFixed(0) : balance.toFixed(1)} antimatters. + You have {antimatter % 1 === 0 ? antimatter.toFixed(0) : antimatter.toFixed(1)}{' '} + antimatters.

@@ -64,45 +107,56 @@ function App() { -
- {`1.Dim Cost: ${fistDimPrice}`} + factor={firstDimFactor} + setFactor={setFirstDimFactor} + dimFactorCount={firstDimFactorCount} + setDimFactorCount={setFirstDimFactorCount}> + {`First Dimension Cost: ${firstDimPrice}`} - {`2.Dim Cost: ${secondDimPrice}`} + setPrice={setSecondDimPrice} + factor={secondDimFactor} + setFactor={setSecondDimFactor} + dimFactorCount={secondDimFactorCount} + setDimFactorCount={setSecondDimFactorCount}> + {`Second Dimension Cost: ${secondDimPrice}`} {secondDimCount > 0 && ( - {`2.Dim Cost: ${thirdDimPrice}`} + setPrice={setThirdDimPrice} + factor={thirdDimFactor} + setFactor={setThirdDimFactor} + dimFactorCount={thirdDimFactorCount} + setDimFactorCount={setThirdDimFactorCount}> + {`Third Dimension Cost: ${thirdDimPrice}`} )}
diff --git a/src/Dimension.tsx b/src/Dimension.tsx index abbe8bf..647b638 100644 --- a/src/Dimension.tsx +++ b/src/Dimension.tsx @@ -1,48 +1,58 @@ -import { useState, useEffect } from 'react'; +import { useState, useEffect, Children } from 'react'; import './Dimentions.css'; type DimProps = { - balance: number; + antimatter: number; + setAntimatter: (fn: ((antimatter: number) => void) | number) => void; dimCount: number; setDimCount: (fn: (dim: number) => void) => void; - setBalance: (fn: ((balance: number) => void) | number) => void; price: number; + setPrice: (fn: (dim: number) => void) => void; + factor: number; + setFactor: (fn: (dim: number) => void) => void; + dimFactorCount: number; + setDimFactorCount: (fn: ((antimatter: number) => void) | number) => void; children: string; }; export default function Dimension(props: DimProps) { - const { balance, dimCount, setDimCount, setBalance, price, setPrice } = props; + const { antimatter, dimCount, setDimCount, setAntimatter, price, setPrice, factor, setFactor, dimFactorCount, setDimFactorCount } = props; + const handleDimBuy = (quantity: number) => { - if (balance >= price) { + if (antimatter >= price) { setDimCount(dimCount => dimCount + quantity); - setBalance(balance => balance - price * quantity); + setAntimatter(prevValue => prevValue - price * quantity); + setDimFactorCount(prevCount => prevCount +1) + } + if ((dimCount + 1) % 10 === 0 && dimCount > 1 || quantity===10) { + setPrice(prevPrice => prevPrice * 10); + setFactor(prevFactor => prevFactor * 2) + setDimFactorCount(0) } - if ((dimCount + 1) % 10 === 0 && dimCount > 1) setPrice(prevPrice => prevPrice * 10); }; + + return ( <>

- {`You have ${dimCount % 1 === 0 ? dimCount.toFixed(0) : dimCount.toFixed(1)} ${ - props.children.split(' ')[0] - } - ${price / 10}x`} + {`${ props.children.split(" ").slice(0,2).join(" ")} x${factor}`}

{` ${dimCount % 1 === 0 ? dimCount.toFixed(0) : dimCount.toFixed(1)} - (${dimCount.toFixed(0) % 10}) + (${dimFactorCount}) `}

-
From 2ceaec5a8fce4ac4d2087183a7fa2b76901377dc Mon Sep 17 00:00:00 2001 From: Michael Urich <80188367+mturich@users.noreply.github.com> Date: Thu, 23 Jun 2022 22:14:09 +0200 Subject: [PATCH 4/5] dimension shift and galaxy added, data is save to local but not loaded correcty --- src/App.css | 5 ++ src/App.tsx | 176 ++++++++++++++++++++++++++++++++++++++------------ src/backup.ts | 19 ++++++ 3 files changed, 158 insertions(+), 42 deletions(-) create mode 100644 src/backup.ts diff --git a/src/App.css b/src/App.css index 5b28302..43d9fa5 100644 --- a/src/App.css +++ b/src/App.css @@ -41,6 +41,11 @@ text-align: center; } +.cols-2{ + grid-template-columns: repeat(3, 1fr); + margin: auto 5rem; +} + .btn { font-size: calc(10px + 1vmin); padding: 0.2em 1em 0.2em 1em; diff --git a/src/App.tsx b/src/App.tsx index 2538bfa..cb74e73 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -11,12 +11,22 @@ type DimProps = { }; function App() { + // tried to load the object data with the first opening of the page and then assign it to all the state but this was not sucessful. Loading ervery prop this way is very slow. CUrrently I do not know how to get the load data + + // the 2. thing I did not know how to do was the Buy max button. A while loop crashed the browser :( maybe it works recursive + const dataRef = useRef(() => JSON.parse(localStorage.getItem('data')) ?? {}); + const timerExpiredCallback = useRef(() => {}); const clockSpeedRef = useRef(2000); - const timerId = useRef(-1); - const toSaveId = useRef(-1); + const timerIdRef = useRef(-1); + const idRef = useRef(-1); + + // const dataRef = useRef({}); const [tickspeedPrice, setTickspeedPrice] = useState(10); - const [antimatter, setAntimatter] = useState(200); + const [antimatter, setAntimatter] = useState(1000); + const [resetGameCounter, setResetGameCounter] = useState(2); + const [galaxyCounter, setGalaxyCounter] = useState(0); + const [highesDim, setHighesDim] = useState(0); const [firstDimFactor, setFirstDimFactor] = useState(1.1); const [firstDimCount, setFirstDimCount] = useState(0); @@ -35,47 +45,15 @@ function App() { // if the component updates, replace the timeout callback with one that references the new // values of `count` and `firstDimCount` - useEffect(() => { - const startSave = () => { - timerId.current = setTimeout(() => { - const toSave = { - timerId, - clockSpeedRef, - tickspeedPrice, - antimatter, - firstDimCount, - firstDimFactor, - firstDimPrice, - firstDimFactorCount, - secondDimCount, - secondDimFactor, - secondDimPrice, - secondDimFactorCount, - thirdDimCount, - thirdDimFactor, - thirdDimPrice, - thirdDimFactorCount, - }; - console.log(toSave); - //localStorage.setItem("data", JSON.stringify(toSave)); - startSave(); - }, 5 * 60 * 1000); - }; - startSave(); - - // if we ever unmount / destroy this component instance, clear the timeout - return () => clearTimeout(timerId.current); - }, []); - timerExpiredCallback.current = () => { - setAntimatter(prevantimatter => prevantimatter + firstDimCount * firstDimFactor); + setAntimatter(prevAntimatter => prevAntimatter + firstDimCount * firstDimFactor); setFirstDimCount(prevFirstDim => prevFirstDim + (secondDimCount / 10) * secondDimFactor); setSecondDimCount(prevSecondDim => prevSecondDim + (thirdDimCount / 100) * thirdDimFactor); }; useEffect(() => { const startTimer = () => { - timerId.current = setTimeout(() => { + timerIdRef.current = setTimeout(() => { timerExpiredCallback.current(); startTimer(); }, clockSpeedRef.current); @@ -83,7 +61,7 @@ function App() { startTimer(); // if we ever unmount / destroy this component instance, clear the timeout - return () => clearTimeout(timerId.current); + return () => clearTimeout(timerIdRef.current); }, []); const handleTickBtnClick = () => { @@ -92,6 +70,102 @@ function App() { setTickspeedPrice(prevPrice => prevPrice * 10); }; + //------------------------------------------- + + // FROM HERE + // creates constantly an uptodate object which is then saved later + useEffect(() => { + dataRef.current = { + // ...dataRef.current, + antimatter, + tickspeedPrice, + firstDimCount, + firstDimFactor, + firstDimPrice, + firstDimFactorCount, + secondDimCount, + secondDimFactor, + secondDimPrice, + secondDimFactorCount, + thirdDimCount, + thirdDimFactor, + thirdDimPrice, + thirdDimFactorCount, + resetGameCounter, + highesDim, + timerIdRef, + idRef, + clockSpeedRef, + }; + //console.log('dataRef object update cycle: ', dataRef.current ?? ''); + }); + + // saves the created object every minuit to localStorage + useEffect(() => { + const startSave = () => { + timerIdRef.current = setTimeout(() => { + //localStorage.removeItem('dataRef'); + localStorage.setItem('data', JSON.stringify(dataRef.current)); + console.log('data save cycle: ', dataRef.current); + startSave(); + }, 60000); + }; + startSave(); + + // if we ever unmount / destroy this component instance, clear the timeout + return () => clearTimeout(timerIdRef.current); + }, []); + + // this prints the saved object from local storage. It is just for dev. No final purpose + useEffect(() => { + const printStorage = () => { + idRef.current = setTimeout(() => { + let savedDataRef = JSON.parse(localStorage.getItem('data') ?? ''); + console.log('dataRef from storage', savedDataRef); + printStorage(); + }, 60000); + }; + printStorage(); + + return () => clearTimeout(idRef.current); + }, []); + + /* resets the game to unlock new dimension */ + const handleResetGameClick = () => { + clockSpeedRef.current = 2000; + timerIdRef.current = -1; + idRef.current = -1; + + setTickspeedPrice(10); + setAntimatter(1000); + + setFirstDimFactor(1.1); + setFirstDimCount(0); + setFirstDimPrice(10); + setFirstDimFactorCount(0); + + setSecondDimFactor(1.1); + setSecondDimCount(0); + setSecondDimPrice(100); + setSecondDimFactorCount(0); + + setThirdDimFactor(1.1); + setThirdDimCount(0); + setThirdDimPrice(1000); + setThirdDimFactorCount(0); + + // increase ResetCounter by one + setResetGameCounter(prev => prev + 1); + }; + /* saves the current DimCount of the highes Dim */ + useEffect(() => { + /* higher dims have to be added when created */ + if (thirdDimCount !== 0) setHighesDim(thirdDimCount); + else setHighesDim(secondDimCount); + }, [secondDimCount, thirdDimCount]); + + //--------------------------------------------------- + return (
@@ -105,12 +179,12 @@ function App() {

{`The current clockspeed is ${clockSpeedRef.current} ms. Reduce the tickspeed by 11%.`}

-
@@ -143,8 +217,8 @@ function App() { setDimFactorCount={setSecondDimFactorCount}> {`Second Dimension Cost: ${secondDimPrice}`} - - {secondDimCount > 0 && ( + {/* the 3. dimension must be unlocked */} + {resetGameCounter > 2 && ( {`Third Dimension Cost: ${thirdDimPrice}`} + + /* Here have to come all the other dims */ )} +
+
+
+

{`Dimension Shift (${resetGameCounter}) `}

+

{`requires 20 ${resetGameCounter}. Dimension `}

+ +
+
+

{`Antimatter Galaxies (${galaxyCounter})`}

+

{`requires 80 8. Dimension `}

+ +
); } diff --git a/src/backup.ts b/src/backup.ts new file mode 100644 index 0000000..e481ec0 --- /dev/null +++ b/src/backup.ts @@ -0,0 +1,19 @@ +const data = { + timerId, + toSaveId, + clockSpeedRef, + tickspeedPrice, + antimatter, + firstDimCount, + firstDimFactor, + firstDimPrice, + firstDimFactorCount, + secondDimCount, + secondDimFactor, + secondDimPrice, + secondDimFactorCount, + thirdDimCount, + thirdDimFactor, + thirdDimPrice, + thirdDimFactorCount, +}; From d4490e12498503a77124b881e26aaf1fa3ed2953 Mon Sep 17 00:00:00 2001 From: Michael Urich <80188367+mturich@users.noreply.github.com> Date: Fri, 24 Jun 2022 07:45:39 +0200 Subject: [PATCH 5/5] buy max button works --- src/App.tsx | 50 +++++++++++++++++++++++++++++++++++++++++--------- src/backup.ts | 4 ++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index cb74e73..1a6f099 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -11,15 +11,16 @@ type DimProps = { }; function App() { - // tried to load the object data with the first opening of the page and then assign it to all the state but this was not sucessful. Loading ervery prop this way is very slow. CUrrently I do not know how to get the load data + // tried to load the object data with the first opening of the page and then assign it to all the state but this was not sucessful. Loading ervery prop this way is very slow. CUrrently I do not know how to get the load data - // the 2. thing I did not know how to do was the Buy max button. A while loop crashed the browser :( maybe it works recursive + // the 2. thing I did not know how to do was the Buy max button. A while loop crashed the browser :( UPDATE: now it works with a recursion. are there better ways const dataRef = useRef(() => JSON.parse(localStorage.getItem('data')) ?? {}); - + const canIstillBuyRef = useRef(0); const timerExpiredCallback = useRef(() => {}); const clockSpeedRef = useRef(2000); const timerIdRef = useRef(-1); const idRef = useRef(-1); + const timeIdRef = useRef(-1); // const dataRef = useRef({}); const [tickspeedPrice, setTickspeedPrice] = useState(10); @@ -64,15 +65,41 @@ function App() { return () => clearTimeout(timerIdRef.current); }, []); + //------------------------------------------- + // FROM HERE + const handleTickBtnClick = () => { clockSpeedRef.current = clockSpeedRef.current * (1 - 0.11); setAntimatter(prevPrice => prevPrice - tickspeedPrice); setTickspeedPrice(prevPrice => prevPrice * 10); }; - //------------------------------------------- - - // FROM HERE + useEffect(() => { + canIstillBuyRef.current = antimatter - tickspeedPrice; + }, [tickspeedPrice]); + + /* ref is needed to get the current state */ + const canIstillBuy = () => { + if (canIstillBuyRef.current > 0) return true; + else return false; + }; + + /* It works but I do not know if there are better ways */ + const handleBuyMaxClick = () => { + const repeatMax = () => { + timeIdRef.current = setTimeout(() => { + if (canIstillBuy()) { + console.log(canIstillBuy(), canIstillBuyRef.current, tickspeedPrice); + handleTickBtnClick(); + repeatMax(); + } + }, 20); + + return () => clearTimeout(timeIdRef.current); + }; + repeatMax(); + }; + // creates constantly an uptodate object which is then saved later useEffect(() => { dataRef.current = { @@ -116,7 +143,7 @@ function App() { return () => clearTimeout(timerIdRef.current); }, []); - // this prints the saved object from local storage. It is just for dev. No final purpose + // this prints the saved object from local storage. It is just for dev. No final purpose useEffect(() => { const printStorage = () => { idRef.current = setTimeout(() => { @@ -176,7 +203,9 @@ function App() {
-

{`The current clockspeed is ${clockSpeedRef.current} ms. Reduce the tickspeed by 11%.`}

+

{`The current clockspeed is ${clockSpeedRef.current.toFixed( + 0 + )} ms. Reduce the tickspeed by 11%.`}

-
diff --git a/src/backup.ts b/src/backup.ts index e481ec0..7d26cd3 100644 --- a/src/backup.ts +++ b/src/backup.ts @@ -17,3 +17,7 @@ const data = { thirdDimPrice, thirdDimFactorCount, }; +const canIstillBuy = () => { + if (canIstillBuyRef.current > 0) true + else false +} \ No newline at end of file