1- import React , { useCallback , useMemo , useState } from "react"
1+ import React , { useCallback , useEffect , useMemo , useState } from "react"
22import {
33 Grid ,
44 CardMedia ,
@@ -17,9 +17,9 @@ import { Player, PlayerProfile } from "model/Player"
1717import Leaderboard from "components/Leaderboard/Leaderboard"
1818import { Suit } from "model/Suit"
1919import { FormatName } from "utils/FormattingUtils"
20- import { CardName } from "model/Cards"
2120import { useAppSelector } from "caches/hooks"
22- import { getIsRoundPlaying , getRound } from "caches/GameSlice"
21+ import { getGame , getIsRoundPlaying , getRound } from "caches/GameSlice"
22+ import { Event } from "model/Events"
2323
2424interface PlayerRowI {
2525 player : Player
@@ -153,7 +153,11 @@ const DealerChip: React.FC<{
153153
154154const PlayerCard : React . FC < PlayerRowI > = ( { player, profile, className } ) => {
155155 const theme = useTheme ( )
156- const round = useAppSelector ( getRound )
156+ const game = useAppSelector ( getGame )
157+ const [ revisionActioned , setRevisionActioned ] = useState ( - 1 )
158+ const [ playedCard , setPlayedCard ] = useState < PlayedCard | undefined > (
159+ undefined ,
160+ )
157161 const [ modalLeaderboard , setModalLeaderboard ] = useState ( false )
158162
159163 const toggleLeaderboardModal = useCallback (
@@ -162,22 +166,53 @@ const PlayerCard: React.FC<PlayerRowI> = ({ player, profile, className }) => {
162166 )
163167
164168 const isGoer : boolean = useMemo (
165- ( ) => ! ! round && round . goerId === player . id ,
166- [ round , player ] ,
169+ ( ) => ! ! game . round && game . round . goerId === player . id ,
170+ [ game . round , player ] ,
167171 )
168172
169- const playedCard = useMemo < PlayedCard | undefined > ( ( ) => {
170- if ( round ) {
171- return round . currentHand . playedCards ?. find (
172- c => c . playerId === player . id ,
173- )
173+ const delayResetCard = useCallback ( ( ) => {
174+ // The in 3 seconds set it back to undefined
175+ setTimeout ( ( ) => {
176+ setPlayedCard ( undefined )
177+ } , 3000 )
178+ } , [ ] )
179+
180+ useEffect ( ( ) => {
181+ if ( game . round && game . revision > revisionActioned ) {
182+ if ( game . event === Event . HandEnd ) {
183+ // Set card from the last hand
184+ setPlayedCard (
185+ game . round . completedHands
186+ . toReversed ( ) [ 0 ]
187+ ?. playedCards ?. find ( c => c . playerId === player . id ) ,
188+ )
189+ delayResetCard ( )
190+ } else if ( game . event === Event . RoundEnd ) {
191+ // Set card from the previous round
192+ if ( game . previousRound ) {
193+ setPlayedCard (
194+ game . previousRound . completedHands
195+ . toReversed ( ) [ 0 ]
196+ ?. playedCards ?. find ( c => c . playerId === player . id ) ,
197+ )
198+ }
199+ delayResetCard ( )
200+ } else {
201+ setPlayedCard (
202+ game . round . currentHand . playedCards ?. find (
203+ c => c . playerId === player . id ,
204+ ) ,
205+ )
206+ }
207+ setRevisionActioned ( game . revision )
174208 }
175- return { card : CardName . EMPTY , playerId : player . id }
176- } , [ round , player ] )
209+ } , [ game , player , delayResetCard , revisionActioned ] )
177210
178211 const isCurrentPlayer : boolean = useMemo (
179- ( ) => ! ! round && round . currentHand . currentPlayerId === player . id ,
180- [ round , player ] ,
212+ ( ) =>
213+ ! ! game . round &&
214+ game . round . currentHand . currentPlayerId === player . id ,
215+ [ game . round , player ] ,
181216 )
182217
183218 const scoreClassName = useMemo ( ( ) => {
@@ -248,7 +283,7 @@ const PlayerCard: React.FC<PlayerRowI> = ({ player, profile, className }) => {
248283 < BlankCard name = { profile . name } />
249284 < DealerChip
250285 player = { player }
251- dealerId = { round ?. dealerId }
286+ dealerId = { game . round ?. dealerId }
252287 />
253288 < CallChip call = { player . call } isGoer = { isGoer } />
254289 < SuitChip isGoer = { isGoer } />
0 commit comments