|
| 1 | +import { useEffect, useState } from 'react' |
| 2 | +import '@/styles/game/index.css' |
| 3 | +import type { Item, OwnedItems } from '@/types' |
| 4 | +import { Grid2 as Grid, Card, CardContent, CardHeader } from '@mui/material' |
| 5 | +import { items } from '@/constants/items' |
| 6 | +import { Score, Gitcoin } from '@/components/game/core' |
| 7 | +import { Skills } from '@/components/game/skills' |
| 8 | +import { Store } from '@/components/game/store' |
| 9 | + |
| 10 | +export function Game() { |
| 11 | + const [lines, setLines] = useState(0) |
| 12 | + const [linesPerMillisecond, setLinesPerMillisecond] = useState(0) |
| 13 | + |
| 14 | + const [ownedItems, setOwnedItems] = useState<OwnedItems>({}) |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const interval = setInterval(() => { |
| 18 | + setLines(prev => prev + linesPerMillisecond) |
| 19 | + }, 100) |
| 20 | + return () => clearInterval(interval) |
| 21 | + }, [linesPerMillisecond]) |
| 22 | + |
| 23 | + useEffect(() => { |
| 24 | + let count = 0 |
| 25 | + |
| 26 | + Object.keys(ownedItems).forEach((name) => { |
| 27 | + const item = items.find(element => element.name === name) |
| 28 | + |
| 29 | + if (item != null) { |
| 30 | + count += item.linesPerMillisecond * ownedItems[name] |
| 31 | + } |
| 32 | + }) |
| 33 | + |
| 34 | + setLinesPerMillisecond(count) |
| 35 | + }, [ownedItems]) |
| 36 | + |
| 37 | + const handleClick = () => { |
| 38 | + setLines(lines + 1) |
| 39 | + } |
| 40 | + |
| 41 | + const handleBuy = (item: Item) => { |
| 42 | + setLines(lines - item.price) |
| 43 | + setOwnedItems({ |
| 44 | + ...ownedItems, |
| 45 | + [item.name]: (ownedItems[item.name] || 0) + 1, |
| 46 | + }) |
| 47 | + } |
| 48 | + |
| 49 | + return ( |
| 50 | + <> |
| 51 | + <Grid size={3}> |
| 52 | + <Card component="section" className="card"> |
| 53 | + <CardContent className="content"> |
| 54 | + <Score |
| 55 | + lines={Math.ceil(lines)} |
| 56 | + linesPerSecond={Math.ceil(linesPerMillisecond * 10)} |
| 57 | + /> |
| 58 | + <Gitcoin onClick={handleClick} /> |
| 59 | + </CardContent> |
| 60 | + </Card> |
| 61 | + </Grid> |
| 62 | + <Grid size="grow"> |
| 63 | + <Card component="section" className="card"> |
| 64 | + <CardHeader title="Skills" /> |
| 65 | + <CardContent sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}> |
| 66 | + <Skills skills={ownedItems} /> |
| 67 | + </CardContent> |
| 68 | + </Card> |
| 69 | + </Grid> |
| 70 | + <Grid size="grow"> |
| 71 | + <Card component="section" className="card"> |
| 72 | + <CardHeader title="Store" /> |
| 73 | + <CardContent> |
| 74 | + <Store lines={lines} onBuy={handleBuy} /> |
| 75 | + </CardContent> |
| 76 | + </Card> |
| 77 | + </Grid> |
| 78 | + </> |
| 79 | + ) |
| 80 | +} |
0 commit comments