Created
January 26, 2019 10:24
-
-
Save jesterswilde/23cfc0802161e5f6f5d5066a0dce666c to your computer and use it in GitHub Desktop.
Poker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const listenForPlayerAction = (game: Game): Promise<ActionValue> => { | |
return new Promise((res, rej) => { | |
const { sockets, currentPlayerIndex } = game; | |
const currentSocket = sockets[currentPlayerIndex]; | |
currentSocket.once('action', async (action: ActionTypes, value: number) => { | |
if (!checkIfValidAction(game, action, value)) { | |
rej("Invalid Action"); | |
} else { | |
res({ action, value }); | |
} | |
}) | |
}) | |
} | |
const checkIfValidAction = (game: Game, action: ActionTypes, value: number): boolean => { | |
const { bets, currentPlayerIndex, maxBet, chips } = game; | |
switch (action) { | |
case ActionTypes.CHECK: | |
if (maxBet === 0) { | |
return true; | |
} | |
return false; | |
case ActionTypes.BET: | |
if (chips[currentPlayerIndex] >= value && value > game.maxBet) { | |
return true; | |
} | |
return false; | |
case ActionTypes.CALL: | |
return true; | |
case ActionTypes.FOLD: | |
return true; | |
} | |
return false; | |
} | |
export const handleAction = (game: Game, action: ActionTypes, value: number) => { | |
switch (action) { | |
case ActionTypes.CHECK: | |
handleCheck(game); | |
break; | |
case ActionTypes.BET: | |
handleBet(game, value); | |
break; | |
case ActionTypes.FOLD: | |
handleFold(game); | |
break; | |
case ActionTypes.CALL: | |
handleCall(game); | |
break; | |
} | |
} | |
export const getNextPlayerStillInHand = (game: Game) => { | |
const { currentPlayerIndex, numPlayers, folded } = game; | |
for (let i = 1; i < game.numPlayers; i++) { | |
const nextIndex = (i + currentPlayerIndex) % numPlayers; | |
if (folded[nextIndex] === false) { | |
return nextIndex; | |
} | |
} | |
return -1; | |
} | |
const handleCheck = (game: Game) => { | |
const nextIndex = getNextPlayerStillInHand(game); | |
const { dealerIndex } = game; | |
if (dealerIndex === nextIndex) { | |
game.phaseOver = true; | |
} | |
} | |
const handleBet = (game: Game, betAmount: number) => { | |
const { bets, currentPlayerIndex: player, chips } = game; | |
const betDiff = betAmount - bets[player]; | |
chips[player] -= betDiff; | |
bets[player] = game.maxBet = betAmount; | |
game.pot += betDiff; | |
game.recentBetIndex = player; | |
} | |
const handleFold = (game: Game) => { | |
const { folded, currentPlayerIndex: player } = game; | |
folded[player] = true; | |
//if there is only one player who hasn't folded, the hand is over. | |
if (folded.filter((f) => f === false).length === 1) { | |
game.phaseOver = game.handOver = true; | |
game.winnerIndex = folded.indexOf(false); | |
} | |
} | |
const handleCall = (game: Game) => { | |
const { bets, chips, currentPlayerIndex: player, maxBet, recentBetIndex } = game; | |
const callAmount = Math.min(chips[player], maxBet); | |
bets[player] = callAmount; | |
const nextIndex = getNextPlayerStillInHand(game); | |
if (nextIndex === recentBetIndex) { | |
game.phaseOver = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment