-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor game to use context/provider
- Loading branch information
1 parent
0282873
commit e00ef7f
Showing
6 changed files
with
199 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import { | ||
PropsWithChildren, | ||
useCallback, | ||
useEffect, | ||
useMemo, | ||
useState | ||
} from 'react'; | ||
import { Card, getInitialCards } from '../card'; | ||
import { GameContext } from './gameContext'; | ||
|
||
const hasTwoMatchingCards = (cards: Card[]) => { | ||
const [one, two] = cards; | ||
return one?.value === two?.value; | ||
}; | ||
|
||
const getFaceUp = (cards: Card[]) => | ||
cards.filter((c) => c.isFaceUp && !c.isMatched); | ||
|
||
export const GameProvider = ({ children }: PropsWithChildren) => { | ||
const [start, setStart] = useState<Date | null>(null); | ||
const [end, setEnd] = useState<Date | null>(null); | ||
const [duration, setDuration] = useState('0m 0s'); | ||
const [attempts, setAttempts] = useState(0); | ||
const [cards, setCards] = useState(getInitialCards()); | ||
|
||
// computed values | ||
const faceUpCards = useMemo(() => getFaceUp(cards), [cards]); | ||
const hasFlippedTwoCards = useMemo( | ||
() => faceUpCards.length >= 2, | ||
[faceUpCards] | ||
); | ||
const hasMatchAllCards = useMemo( | ||
() => cards.find((c) => !c.isMatched) === undefined, | ||
[cards] | ||
); | ||
const counts = useMemo(() => cards.map((c) => c.count), [cards]); | ||
const score = useMemo( | ||
() => cards.filter((c) => c.isMatched).length / 2, | ||
[cards] | ||
); | ||
const hasFlippedTwoCardsWithoutMatch = useMemo(() => { | ||
return !hasFlippedTwoCards || hasTwoMatchingCards(cards) ? false : true; | ||
}, [cards, hasFlippedTwoCards]); | ||
|
||
const resetUnmatchedCards = useCallback(() => { | ||
setCards(cards.map((c) => (c.isMatched ? c : { ...c, isFaceUp: false }))); | ||
}, [cards, setCards]); | ||
|
||
// functions | ||
const getDuration = useCallback(() => { | ||
if (start === null) { | ||
return '0m 0s'; | ||
} | ||
|
||
const newEnd = end === null ? new Date() : end; | ||
|
||
const diff = newEnd.getTime() - start.getTime(); | ||
const totalSeconds = Math.floor(diff / 1000); | ||
|
||
// Calculate minutes and remaining seconds | ||
const minutes = Math.floor(totalSeconds / 60); | ||
const seconds = totalSeconds % 60; | ||
|
||
return `${minutes}m ${seconds}s`; | ||
}, [start, end]); | ||
|
||
const handleClick = useCallback( | ||
(card: Card) => { | ||
if (start === null) { | ||
setStart(new Date()); | ||
} | ||
if (card.isFaceUp || card.isMatched || hasFlippedTwoCards) { | ||
return; | ||
} | ||
setAttempts((prev) => prev + 1); | ||
|
||
setCards((oldCards) => { | ||
let updatedCards = oldCards.map((c) => | ||
c.id === card.id ? { ...c, isFaceUp: true, count: c.count + 1 } : c | ||
); | ||
|
||
if (hasTwoMatchingCards(getFaceUp(updatedCards))) { | ||
updatedCards = updatedCards.map((c) => | ||
c.isFaceUp ? { ...c, isMatched: true } : c | ||
); | ||
} | ||
return updatedCards; | ||
}); | ||
}, | ||
[hasFlippedTwoCards, start] | ||
); | ||
|
||
const reset = useCallback(() => { | ||
setStart(null); | ||
setEnd(null); | ||
setAttempts(0); | ||
setCards(getInitialCards()); | ||
}, [setStart, setEnd, setAttempts, setCards]); | ||
|
||
// side effects | ||
useEffect(() => { | ||
if (end === null && hasMatchAllCards) { | ||
setEnd(new Date()); | ||
} | ||
}, [end, hasMatchAllCards]); | ||
|
||
useEffect(() => { | ||
const interval = setInterval(() => { | ||
setDuration(getDuration()); | ||
}, 1000); | ||
|
||
return () => clearInterval(interval); | ||
}, [duration, getDuration, setDuration]); | ||
|
||
useEffect(() => { | ||
let timeout: number | undefined; | ||
|
||
if (hasFlippedTwoCardsWithoutMatch) { | ||
timeout = setTimeout(() => { | ||
resetUnmatchedCards(); | ||
}, 1000); | ||
} | ||
|
||
return () => { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
} | ||
}; | ||
}, [hasFlippedTwoCardsWithoutMatch, resetUnmatchedCards]); | ||
|
||
return ( | ||
<GameContext.Provider | ||
value={{ | ||
score, | ||
attempts, | ||
counts, | ||
hasMatchAllCards, | ||
reset, | ||
cards, | ||
handleClick, | ||
duration | ||
}} | ||
> | ||
{children} | ||
</GameContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createContext } from 'react'; | ||
import { Card } from '../card'; | ||
|
||
export const GameContext = createContext({ | ||
score: 0, | ||
attempts: 0, | ||
cards: [] as Card[], | ||
counts: [] as number[], | ||
duration: '', | ||
hasMatchAllCards: false, | ||
reset: () => {}, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
handleClick: (_card: Card) => {} | ||
}); |
Oops, something went wrong.