Skip to content

Commit

Permalink
Merge pull request #119 from daithihearn/bugfix/miscellaneous
Browse files Browse the repository at this point in the history
Bugfix/miscellaneous
  • Loading branch information
daithihearn authored Feb 26, 2023
2 parents 261081d + 68679b2 commit 0c51c64
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
2 changes: 0 additions & 2 deletions src/caches/GameSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ export const getIsDoublesGame = createSelector(
export const getIsMyGo = createSelector(getGame, game => game.isMyGo)
export const getIamGoer = createSelector(getGame, game => game.iamGoer)
export const getIHavePlayed = createSelector(getGame, game => {
if (game.iamGoer) return false

const myPosition = game.players.findIndex(p => p.id === game.me?.id)
const currentPlayerPosition = game.players.findIndex(
p => p.id === game.round?.currentHand.currentPlayerId,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Game/Buying.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const Buying = () => {
type="button"
onClick={toggleReadyToBuy}
color={
isMyGo || !readyToBuy ? "warning" : "secondary"
isMyGo || !readyToBuy ? "primary" : "secondary"
}>
<b>
{isMyGo || !readyToBuy
Expand Down
32 changes: 11 additions & 21 deletions src/components/Game/MyCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ import {
} from "../../caches/AutoPlaySlice"
import parseError from "../../utils/ErrorUtils"

interface DoubleClickTracker {
time: number
card: string
}

const MyCards: React.FC = () => {
const dispatch = useAppDispatch()
const gameId = useAppSelector(getGameId)
Expand All @@ -51,9 +46,6 @@ const MyCards: React.FC = () => {

const { enqueueSnackbar } = useSnackbar()

const [doubleClickTracker, updateDoubleClickTracker] =
useState<DoubleClickTracker>()

const selectedCards = useAppSelector(getSelectedCards)

const playButtonEnabled = useMemo(
Expand Down Expand Up @@ -85,7 +77,10 @@ const MyCards: React.FC = () => {
)

const handleSelectCard = useCallback(
(card: SelectableCard) => {
(
card: SelectableCard,
event: React.MouseEvent<HTMLImageElement, MouseEvent>,
) => {
if (!cardsSelectable || card.name === BLANK_CARD.name) {
return
}
Expand All @@ -95,24 +90,17 @@ const MyCards: React.FC = () => {
if (autoPlayCard === card.name) {
dispatch(clearAutoPlay())
dispatch(clearSelectedCards())
} else if (
doubleClickTracker?.card === card.name &&
Date.now() - doubleClickTracker.time < 500
) {
} else if (event.detail === 2) {
dispatch(toggleAutoPlay(card))
} else {
dispatch(toggleUniqueSelect(card))
dispatch(clearAutoPlay())
updateDoubleClickTracker({
card: card.name,
time: Date.now(),
})
}
} else {
dispatch(toggleSelect(card))
}
},
[round, myCards, autoPlayCard, doubleClickTracker],
[round, myCards, autoPlayCard],
)

const handleOnDragEnd = useCallback(
Expand Down Expand Up @@ -209,9 +197,10 @@ const MyCards: React.FC = () => {
{...provided.dragHandleProps}>
<CardImg
alt={card.name}
onClick={() =>
onClick={event =>
handleSelectCard(
card,
event,
)
}
src={`/cards/thumbnails/${card.name}.png`}
Expand Down Expand Up @@ -263,9 +252,10 @@ const MyCards: React.FC = () => {
{...provided.dragHandleProps}>
<CardImg
alt={card.name}
onClick={() =>
onClick={event =>
handleSelectCard(
card,
event,
)
}
src={`/cards/thumbnails/${card.name}.png`}
Expand Down Expand Up @@ -294,7 +284,7 @@ const MyCards: React.FC = () => {
disabled={!playButtonEnabled}
type="button"
onClick={playCard}
color="warning">
color="primary">
<b>Play Card</b>
</Button>
</ButtonGroup>
Expand Down
39 changes: 25 additions & 14 deletions src/components/Game/WebsocketManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,32 @@ const WebsocketHandler = () => {
// On round completion we need to display the last round to the user
const processRoundCompleted = async (
game: GameState,
previousRound: Round,
previousRound?: Round,
) => {
// Disable actions by setting isMyGo to false
dispatch(disableActions())

// Show the last card of the penultimate round being played
playCardSound()
const penultimateHand = previousRound.completedHands.pop()
if (!penultimateHand) throw Error("Failed to get the penultimate round")
dispatch(updatePlayedCards(penultimateHand.playedCards))
await new Promise(r => setTimeout(r, 4000))

// Next show the final round being played
playCardSound()
dispatch(updatePlayedCards(previousRound.currentHand.playedCards))
dispatch(updateMyCards([]))
await new Promise(r => setTimeout(r, 6000))
// Previous round will be undefined nobody called
if (previousRound) {
// Show the last card of the penultimate round being played
playCardSound()
const penultimateHand = previousRound.completedHands.pop()
if (!penultimateHand)
throw Error("Failed to get the penultimate round")
dispatch(updatePlayedCards(penultimateHand.playedCards))
await new Promise(r => setTimeout(r, 4000))

// Next show the final round being played
playCardSound()
dispatch(updatePlayedCards(previousRound.currentHand.playedCards))
dispatch(updateMyCards([]))
await new Promise(r => setTimeout(r, 6000))
} else {
enqueueSnackbar("Nobody called. Redealing....", {
variant: "info",
})
await new Promise(r => setTimeout(r, 2000))
}

// Finally update the game with the latest state
shuffleSound()
Expand Down Expand Up @@ -186,7 +195,9 @@ const WebsocketHandler = () => {
case "ROUND_COMPLETED":
await processRoundCompleted(
action.gameState,
action.transitionData as Round,
action.transitionData
? (action.transitionData as Round)
: undefined,
)
break
case "BUY_CARDS":
Expand Down

0 comments on commit 0c51c64

Please sign in to comment.