Skip to content

Commit

Permalink
Day 8.0: Discard button, chips X mult with hand calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
imisaacwu committed Jul 2, 2024
1 parent 54fe549 commit 691d9c7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
4 changes: 3 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type CardState = {
}

type CardAction = {
type: 'init' | 'select' | 'shuffle' | 'draw' | 'sortHand-rank' | 'sortHand-suit'
type: 'init' | 'select' | 'shuffle' | 'draw' | 'discard' | 'sortHand-rank' | 'sortHand-suit'
payload?: {
element?: ReactElement
handleCardClick?: (e: React.MouseEvent, id: number) => void
Expand Down Expand Up @@ -70,6 +70,8 @@ const cardReducer = (state: CardState, action: CardAction): CardState => {
let n = action.payload?.draw
if(n == undefined) { throw new Error('draw amount not specified!') }
return {...state, hand: [...state.hand, ...state.deck.slice(-n)], deck: state.deck.slice(0, -n)}
case 'discard':
return {...state, hand: state.hand.filter(c => !state.selected.includes(c)), hidden: state.selected, selected: []}
case 'sortHand-rank':
return {...state, hand: [...state.hand.sort((a, b) =>
a.props.rank !== b.props.rank ? b.props.rank - a.props.rank : a.props.suit - b.props.suit
Expand Down
32 changes: 21 additions & 11 deletions src/components/Calculator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CardStateContext } from '../App'
import { HandType } from './Constants'

type HandLevelState = {
level: {[K in keyof typeof HandType]: number}
[K in keyof typeof HandType]: { level: number, chips: number, mult: number }
}

type HandLevelAction = {
Expand All @@ -13,17 +13,27 @@ type HandLevelAction = {
amount?: number
}

const initializeHands = () => {
const levels = {} as HandLevelState['level']
Object.keys(HandType).forEach(k => levels[k as keyof typeof HandType] = 1)
return levels;
const initialHandLevel: HandLevelState = {
'FLUSH_FIVE': {level: 1, chips: 160, mult: 16},
'FLUSH_HOUSE': {level: 1, chips: 140, mult: 14},
'FIVE': {level: 1, chips: 120, mult: 12},
'ROYAL_FLUSH': {level: 1, chips: 100, mult: 8},
'STRAIGHT_FLUSH': {level: 1, chips: 100, mult: 8},
'FOUR': {level: 1, chips: 60, mult: 7},
'FULL_HOUSE': {level: 1, chips: 40, mult: 4},
'FLUSH': {level: 1, chips: 35, mult: 4},
'STRAIGHT': {level: 1, chips: 30, mult: 4},
'THREE': {level: 1, chips: 30, mult: 3},
'TWO_PAIR': {level: 1, chips: 20, mult: 2},
'PAIR': {level: 1, chips: 10, mult: 2},
'HIGH_CARD': {level: 1, chips: 5, mult: 1},
'NONE': {level: 0, chips: 0, mult: 0}
}
const initialHandLevel: HandLevelState = {level: initializeHands()}

const handLevelReducer = (state: HandLevelState, action: HandLevelAction): HandLevelState => {
switch(action.type) {
case 'add':
return {level: {...state.level, [action.hand]: state.level[action.hand] + 1}}
return {...state, [action.hand]: {...state[action.hand], level: state[action.hand].level + 1}}
default:
return state
}
Expand All @@ -45,7 +55,7 @@ export const Calculator = () => {

if(suits[0] === 5) {
if(ranks[0] === 5) return 'FLUSH_FIVE'
if(hand === 14535931) return 'ROYAL_FLUSH'
if(hand === 0x1F00) return 'ROYAL_FLUSH'
if(straights.includes(hand)) return 'STRAIGHT_FLUSH'
return 'FLUSH'
}
Expand All @@ -67,12 +77,12 @@ export const Calculator = () => {
<div id='hand-info'>
<div id='hand-type'>
<div id='hand-name'>{HandType[hand]}</div>
<div id='hand-level'>{hand !== 'NONE' ? `lvl.${levels.level[hand]}` : ''}</div>
<div id='hand-level'>{hand !== 'NONE' ? `lvl.${levels[hand].level}` : ''}</div>
</div>
<div id='calculator'>
<div id='chips'>0</div>
<div id='chips'>{levels[hand].chips}</div>
<div id='X'>X</div>
<div id='mult'>0</div>
<div id='mult'>{levels[hand].mult}</div>
</div>
</div>
)
Expand Down
7 changes: 6 additions & 1 deletion src/components/Hand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export default function Hand(props: HandProps) {
<div id='suit' className='sort-button' onClick={() => cardState.dispatch({type: 'sortHand-suit'})}>Suit</div>
</div>
</div>
<div id='discard' className={`button ${props.selected}`}>Discard</div>
<div id='discard' className={`button ${props.selected}`} onClick={() => {
const select = cardState.state.selected
cardState.dispatch({type: 'discard'})
cardState.dispatch({type: 'draw', payload: {draw: select.length}})
cardState.dispatch({type: 'sortHand-rank'})
}}>Discard</div>
</div>
</div>
)
Expand Down

0 comments on commit 691d9c7

Please sign in to comment.