Skip to content

Commit d3bd65e

Browse files
committed
Refactor
Moving functions to GameUtils
1 parent 60270a0 commit d3bd65e

File tree

2 files changed

+53
-55
lines changed

2 files changed

+53
-55
lines changed

src/components/Game/AutoActionManager.tsx

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { useEffect } from "react"
22
import GameService from "../../services/GameService"
33

4-
import { CARDS } from "../../model/Cards"
5-
64
import { useAppDispatch, useAppSelector } from "../../caches/hooks"
75
import {
86
getCanBuyCards,
@@ -12,60 +10,9 @@ import {
1210
getIsMyGo,
1311
getRound,
1412
} from "../../caches/GameSlice"
15-
import { Round, RoundStatus } from "../../model/Round"
16-
import { Suit } from "../../model/Suit"
13+
import { RoundStatus } from "../../model/Round"
1714
import { getAutoPlayCard } from "../../caches/AutoPlaySlice"
18-
19-
const bestCardLead = (round: Round) => {
20-
let trumpCards = CARDS.filter(
21-
c => c.suit === round.suit || c.suit === Suit.WILD,
22-
)
23-
24-
// Remove played trump cards
25-
round.completedHands.forEach(hand => {
26-
hand.playedCards.forEach(p => {
27-
const card = CARDS.find(c => (c.name = p.card))
28-
if (
29-
(card && card.suit === round.suit) ||
30-
p.card === "JOKER" ||
31-
p.card === "ACE_HEARTS"
32-
)
33-
trumpCards = trumpCards.filter(c => p.card !== c.name)
34-
})
35-
})
36-
37-
// Sort Descending
38-
trumpCards.sort((a, b) => b.value - a.value)
39-
40-
return round.currentHand.leadOut === trumpCards[0].name
41-
}
42-
43-
const getWorstCard = (cards: string[], suit: Suit) => {
44-
console.info(`AutoAction -> followWorst`)
45-
const myCardsRich = CARDS.filter(card => cards.some(c => c === card.name))
46-
const myTrumpCards = myCardsRich.filter(
47-
card => card.suit === suit || card.suit === Suit.WILD,
48-
)
49-
50-
if (myTrumpCards.length > 0) {
51-
// Sort ascending by value
52-
myTrumpCards.sort((a, b) => a.value - b.value)
53-
return myTrumpCards[0]
54-
} else {
55-
// Sort ascending by cold value
56-
myCardsRich.sort((a, b) => a.coldValue - b.coldValue)
57-
58-
// if we can't find a cold card that is clearly the worst card then do nothing
59-
if (
60-
myCardsRich.length > 1 &&
61-
myCardsRich[0].coldValue === myCardsRich[1].coldValue
62-
) {
63-
return
64-
}
65-
66-
return myCardsRich[0]
67-
}
68-
}
15+
import { bestCardLead, getWorstCard } from "../../utils/GameUtils"
6916

7017
const AutoActionManager = () => {
7118
const dispatch = useAppDispatch()

src/utils/GameUtils.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CARDS, BLANK_CARD, Card, SelectableCard } from "../model/Cards"
2+
import { Round } from "../model/Round"
23
import { Suit } from "../model/Suit"
34

45
export const compareCards = (
@@ -123,3 +124,53 @@ export const removeCard = (cardToRemove: Card, orginalHand: Card[]) => {
123124

124125
return newHand
125126
}
127+
128+
export const bestCardLead = (round: Round) => {
129+
let trumpCards = CARDS.filter(
130+
c => c.suit === round.suit || c.suit === Suit.WILD,
131+
)
132+
133+
// Remove played trump cards
134+
round.completedHands.forEach(hand => {
135+
hand.playedCards.forEach(p => {
136+
const card = CARDS.find(c => (c.name = p.card))
137+
if (
138+
(card && card.suit === round.suit) ||
139+
p.card === "JOKER" ||
140+
p.card === "ACE_HEARTS"
141+
)
142+
trumpCards = trumpCards.filter(c => p.card !== c.name)
143+
})
144+
})
145+
146+
// Sort Descending
147+
trumpCards.sort((a, b) => b.value - a.value)
148+
149+
return round.currentHand.leadOut === trumpCards[0].name
150+
}
151+
152+
export const getWorstCard = (cards: string[], suit: Suit) => {
153+
const myCardsRich = CARDS.filter(card => cards.some(c => c === card.name))
154+
const myTrumpCards = myCardsRich.filter(
155+
card => card.suit === suit || card.suit === Suit.WILD,
156+
)
157+
158+
if (myTrumpCards.length > 0) {
159+
// Sort ascending by value
160+
myTrumpCards.sort((a, b) => a.value - b.value)
161+
return myTrumpCards[0]
162+
} else {
163+
// Sort ascending by cold value
164+
myCardsRich.sort((a, b) => a.coldValue - b.coldValue)
165+
166+
// if we can't find a cold card that is clearly the worst card then do nothing
167+
if (
168+
myCardsRich.length > 1 &&
169+
myCardsRich[0].coldValue === myCardsRich[1].coldValue
170+
) {
171+
return
172+
}
173+
174+
return myCardsRich[0]
175+
}
176+
}

0 commit comments

Comments
 (0)