Skip to content

Commit

Permalink
Merge pull request #184 from daithihearn/cards-enum
Browse files Browse the repository at this point in the history
refactor: using enums for cards
  • Loading branch information
daithihearn authored Sep 29, 2023
2 parents e69bca0 + 63547b9 commit 61b34fe
Show file tree
Hide file tree
Showing 24 changed files with 291 additions and 224 deletions.
12 changes: 12 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** @type {import("jest").Config} */
const { defaults: tsjPreset } = require("ts-jest/presets")

module.exports = {
...tsjPreset,
testTimeout: 10000,
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
testMatch: ["**/*.(spec|test).(ts|tsx|js|jsx)"],
maxWorkers: 2,
workerIdleMemoryLimit: "1GB",
reporters: ["default", "jest-junit"],
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frontend",
"version": "7.3.3",
"version": "7.4.0",
"description": "React frontend for the Cards 110",
"author": "Daithi Hearn",
"license": "MIT",
Expand Down
File renamed without changes
File renamed without changes
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"short_name": "Cards 110",
"name": "Cards 110",
"version": "7.3.3",
"version": "7.4.0",
"icons": [
{
"src": "./assets/favicon.png",
Expand Down
10 changes: 5 additions & 5 deletions src/caches/MyCardsSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit"
import { BLANK_CARD, SelectableCard } from "model/Cards"
import { CardName, EMPTY, SelectableCard } from "model/Cards"
import { processOrderedCardsAfterGameUpdate } from "utils/GameUtils"
import { RootState } from "./caches"

Expand All @@ -15,7 +15,7 @@ export const myCardsSlice = createSlice({
name: "myCards",
initialState: initialState,
reducers: {
updateMyCards: (state, action: PayloadAction<string[]>) => {
updateMyCards: (state, action: PayloadAction<CardName[]>) => {
return {
cards: processOrderedCardsAfterGameUpdate(
state.cards,
Expand All @@ -30,7 +30,7 @@ export const myCardsSlice = createSlice({
},
removeCard: (state, action: PayloadAction<string>) => {
const idx = state.cards.findIndex(c => c.name === action.payload)
if (idx > 0) state.cards[idx] = { ...BLANK_CARD, selected: false }
if (idx > 0) state.cards[idx] = { ...EMPTY, selected: false }
},
selectCard: (state, action: PayloadAction<SelectableCard>) => {
state.cards.forEach(c => {
Expand All @@ -54,7 +54,7 @@ export const myCardsSlice = createSlice({
}),
selectAll: state => {
state.cards.forEach(c => {
if (c.name !== BLANK_CARD.name) c.selected = true
if (c.name !== EMPTY.name) c.selected = true
})
},
clearSelectedCards: state => {
Expand Down Expand Up @@ -82,7 +82,7 @@ export const {
export const getMyCards = (state: RootState) => state.myCards.cards

export const getMyCardsWithoutBlanks = createSelector(getMyCards, cards =>
cards.filter(c => c.name !== BLANK_CARD.name),
cards.filter(c => c.name !== EMPTY.name),
)

export const getSelectedCards = createSelector(getMyCards, cards =>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Game/Actions/Calling.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Calling = () => {
)

const buttonsEnabled = useMemo(
() => round && round.currentHand && cards.length > 0 && isMyGo,
() => round?.currentHand && cards.length > 0 && isMyGo,
[round, cards, isMyGo],
)

Expand Down
10 changes: 4 additions & 6 deletions src/components/Game/Actions/PlayCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import GameService from "services/GameService"
import { useAppDispatch, useAppSelector } from "caches/hooks"
import { getMyCardsWithoutBlanks, getSelectedCards } from "caches/MyCardsSlice"
import { getGameId, getIsMyGo, getRound } from "caches/GameSlice"
import { BLANK_CARD } from "model/Cards"
import parseError from "utils/ErrorUtils"
import { RoundStatus } from "model/Round"
import {
Expand All @@ -19,6 +18,7 @@ import {
} from "@mui/material"
import { getCardToPlay, updateCardToPlay } from "caches/PlayCardSlice"
import { bestCardLead, getBestCard, getWorstCard } from "utils/GameUtils"
import { CardName } from "model/Cards"

type AutoPlayState = "off" | "best" | "worst"

Expand Down Expand Up @@ -74,7 +74,7 @@ const PlayCard = () => {
round &&
round.status === RoundStatus.PLAYING &&
round.completedHands.length +
myCards.filter(c => c.name !== BLANK_CARD.name).length ===
myCards.filter(c => c.name !== CardName.EMPTY).length ===
5,

[isMyGo, round, myCards],
Expand Down Expand Up @@ -112,12 +112,10 @@ const PlayCard = () => {
if (cardToPlay) playCard(cardToPlay)
else if (autoPlay === "worst" || bestCardLead(round)) {
const worstCard = getWorstCard(myCards, round.suit)
if (worstCard)
playCard(worstCard.name)
if (worstCard) playCard(worstCard.name)
} else if (autoPlay === "best") {
const bestCard = getBestCard(myCards, round.suit)
if (bestCard)
playCard(bestCard.name)
if (bestCard) playCard(bestCard.name)
}
}
}, [playCard, autoPlay, round, isMyGo, myCards, cardToPlay])
Expand Down
6 changes: 3 additions & 3 deletions src/components/Game/Actions/SelectSuit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useSnackbar } from "notistack"
import { getMyCardsWithoutBlanks, getSelectedCards } from "caches/MyCardsSlice"
import { removeAllFromHand } from "utils/GameUtils"
import ThrowCardsWarningModal from "./ThrowCardsWarningModal"
import { SelectableCard } from "model/Cards"
import { CardName, SelectableCard } from "model/Cards"
import parseError from "utils/ErrorUtils"
import { Button } from "@mui/material"

Expand Down Expand Up @@ -81,8 +81,8 @@ const SelectSuit = () => {

for (const element of deletingCards) {
if (
element.name === "JOKER" ||
element.name === "ACE_HEARTS" ||
element.name === CardName.JOKER ||
element.name === CardName.ACE_HEARTS ||
element.suit === suit
) {
return true
Expand Down
28 changes: 12 additions & 16 deletions src/components/Game/MyCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Droppable,
DropResult,
} from "react-beautiful-dnd"
import { BLANK_CARD, SelectableCard } from "model/Cards"
import { EMPTY, SelectableCard } from "model/Cards"
import { RoundStatus } from "model/Round"
import {
getGameId,
Expand All @@ -32,11 +32,11 @@ import { CardContent, CardMedia, useTheme } from "@mui/material"
import { pickBestCards } from "utils/GameUtils"

const EMPTY_HAND = [
{ ...BLANK_CARD, selected: false },
{ ...BLANK_CARD, selected: false },
{ ...BLANK_CARD, selected: false },
{ ...BLANK_CARD, selected: false },
{ ...BLANK_CARD, selected: false },
{ ...EMPTY, selected: false },
{ ...EMPTY, selected: false },
{ ...EMPTY, selected: false },
{ ...EMPTY, selected: false },
{ ...EMPTY, selected: false },
]

const usePrevious = (value: any) => {
Expand Down Expand Up @@ -94,7 +94,7 @@ const MyCards: React.FC = () => {
card: SelectableCard,
event: React.MouseEvent<HTMLImageElement, MouseEvent>,
) => {
if (!cardsSelectable || card.name === BLANK_CARD.name) {
if (!cardsSelectable || card.name === EMPTY.name) {
return
}

Expand Down Expand Up @@ -153,7 +153,7 @@ const MyCards: React.FC = () => {
(card: SelectableCard) => {
let classes = "thumbnail-size"

if (cardsSelectable && card.name !== BLANK_CARD.name) {
if (cardsSelectable && card.name !== EMPTY.name) {
if (card.name === autoPlayCard) {
classes += " auto-played-" + theme.palette.mode
} else if (!card.selected) {
Expand All @@ -179,17 +179,15 @@ const MyCards: React.FC = () => {
ref={provided.innerRef}>
{myCards.slice(0, 5).map((card, index) => {
const draggableId = `${card.name}${
card.name === BLANK_CARD.name
? index
: ""
card.name === EMPTY.name ? index : ""
}`
return (
<Draggable
key={draggableId}
draggableId={draggableId}
index={index}
isDragDisabled={
card.name === BLANK_CARD.name
card.name === EMPTY.name
}>
{provided => (
<div
Expand Down Expand Up @@ -236,17 +234,15 @@ const MyCards: React.FC = () => {
: EMPTY_HAND
).map((card, index) => {
const draggableId = `${card.name}${
card.name === BLANK_CARD.name
? index
: ""
card.name === EMPTY.name ? index : ""
}`
return (
<Draggable
key={draggableId}
draggableId={draggableId}
index={index}
isDragDisabled={
card.name === BLANK_CARD.name
card.name === EMPTY.name
}>
{provided => (
<div
Expand Down
7 changes: 3 additions & 4 deletions src/components/Game/PlayerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ import {
Stack,
Box,
useTheme,
CardHeader,
} from "@mui/material"
import { styled } from "@mui/system"
import { getRound } from "caches/GameSlice"
import { useAppSelector } from "caches/hooks"
import { getPlayerProfiles } from "caches/PlayerProfilesSlice"
import { BLANK_CARD } from "model/Cards"
import { PlayedCard } from "model/Game"
import { Player } from "model/Player"
import Leaderboard from "components/Leaderboard/Leaderboard"
import { Suit } from "model/Suit"
import { RoundStatus } from "model/Round"
import { FormatName } from "utils/FormattingUtils"
import { CardName } from "model/Cards"

interface PlayerRowI {
player: Player
Expand Down Expand Up @@ -67,7 +66,7 @@ const SuitChip: React.FC<{ isGoer: boolean }> = ({ isGoer }) => {
const round = useAppSelector(getRound)

const suitChip = useMemo(() => {
if (!round || !round.suit) return undefined
if (!round?.suit) return undefined
switch (round.suit) {
case Suit.CLUBS:
return ChipType.Clubs
Expand Down Expand Up @@ -181,7 +180,7 @@ const PlayerCard: React.FC<PlayerRowI> = ({ player, className }) => {
c => c.playerId === player.id,
)
}
return { card: BLANK_CARD.name, playerId: player.id }
return { card: CardName.EMPTY, playerId: player.id }
}, [round, player])

const isCurrentPlayer: boolean = useMemo(
Expand Down
2 changes: 1 addition & 1 deletion src/components/Game/PlayersAndCards.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo } from "react"
import { Grid, CardContent, Container } from "@mui/material"
import { Grid, CardContent } from "@mui/material"
import { getGamePlayers } from "caches/GameSlice"
import { useAppSelector } from "caches/hooks"
import { Player } from "model/Player"
Expand Down
3 changes: 2 additions & 1 deletion src/components/Game/WebsocketManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import callAudioFile from "assets/sounds/call.ogg"
import passAudioFile from "assets/sounds/pass.ogg"
import AutoActionManager from "./AutoActionManager"
import { Round } from "model/Round"
import { CardName } from "model/Cards"

const shuffleAudio = new Audio(shuffleAudioFile)
const playCardAudio = new Audio(playCardAudioFile)
Expand Down Expand Up @@ -95,7 +96,7 @@ const WebsocketHandler = () => {
[playerProfiles],
)

const reloadCards = (cards: string[], clearSelected = false) => {
const reloadCards = (cards: CardName[], clearSelected = false) => {
if (clearSelected) {
dispatch(clearSelectedCards())
dispatch(clearAutoPlay())
Expand Down
2 changes: 1 addition & 1 deletion src/components/GameStats/PlayerSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from "react"
import { useEffect, useMemo, useState } from "react"
import {
IconButton,
ListItemIcon,
Expand Down
12 changes: 1 addition & 11 deletions src/components/Leaderboard/DoublesLeaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@ import { getGamePlayers, getIsGameActive, getRound } from "caches/GameSlice"
import { getPlayerProfiles } from "caches/PlayerProfilesSlice"
import { compareScore, compareTeamIds } from "utils/PlayerUtils"
import { Player } from "model/Player"
import {
List,
ListItem,
ListItemIcon,
Avatar,
ListItemText,
ListItemSecondaryAction,
Box,
Grid,
Typography,
} from "@mui/material"
import { Box, Grid, Typography } from "@mui/material"

interface LeaderBoardPlayer {
cardsBought?: number
Expand Down
10 changes: 3 additions & 7 deletions src/components/Leaderboard/SinglesLeaderboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { useAppSelector } from "caches/hooks"
import { getPlayerProfiles } from "caches/PlayerProfilesSlice"
import { Player } from "model/Player"
import { Box, Grid, Typography } from "@mui/material"
import { CardName } from "model/Cards"

interface LeaderboardItem {
previousCard?: string
previousCard?: CardName
score: number
rings: number
winner: boolean
Expand Down Expand Up @@ -59,12 +60,7 @@ const SinglesLeaderboard = () => {
return leaderboardData.sort((a, b) => b.score - a.score)
}, [playerProfiles, game, getProfile, previousHand])

if (
!game ||
!game.status ||
!playerProfiles ||
playerProfiles.length === 0
) {
if (!game?.status || !playerProfiles || playerProfiles.length === 0) {
return null
}

Expand Down
Loading

0 comments on commit 61b34fe

Please sign in to comment.