Skip to content

Commit 7bb9ada

Browse files
committed
feat: show last card in round and hands
1 parent c2f25e8 commit 7bb9ada

21 files changed

+555
-1275
lines changed

src/components/Game/PlayerCard.tsx

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useCallback, useMemo, useState } from "react"
1+
import React, { useCallback, useEffect, useMemo, useState } from "react"
22
import {
33
Grid,
44
CardMedia,
@@ -17,9 +17,9 @@ import { Player, PlayerProfile } from "model/Player"
1717
import Leaderboard from "components/Leaderboard/Leaderboard"
1818
import { Suit } from "model/Suit"
1919
import { FormatName } from "utils/FormattingUtils"
20-
import { CardName } from "model/Cards"
2120
import { 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

2424
interface PlayerRowI {
2525
player: Player
@@ -153,7 +153,11 @@ const DealerChip: React.FC<{
153153

154154
const 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} />

src/model/Game.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface GameStateResponse {
3838
cards: CardName[]
3939
status: GameStatus
4040
round?: Round
41+
previousRound?: Round
4142
maxCall?: number
4243
me?: Player
4344
players: Player[]

src/test/data/buy-cards/after-buy-cards.json

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/test/data/buy-cards/before-buy-cards.json

Lines changed: 0 additions & 65 deletions
This file was deleted.

src/test/data/call/after-call.json

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/test/data/call/before-call.json

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)