Skip to content

Commit

Permalink
Fixed raise request (#69)
Browse files Browse the repository at this point in the history
Raise request didn't include player's already bet amount.
  • Loading branch information
sormys authored Jun 7, 2023
1 parent d72f6ff commit d834c1f
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/utils/commonRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,21 +375,20 @@ export async function playerCanBetAmount(
amount: string,
client: PoolClient
): Promise<boolean> {
const smallBlindValue = await getSmallBlindValue(gameId, client)
const playerSize = (await getPlayersInGame(gameId, client)).length
const smallBlind = await getSmallBlindToken(gameId, playerSize, client)
const smallBlindState = await getPlayerState(smallBlind, client)
const bigBlind = await getBigBlindToken(gameId, playerSize, client)
const bigBlindState = await getPlayerState(bigBlind, client)

if (playerToken === smallBlind && smallBlindState == null) {
amount = (+amount - +smallBlindValue).toString()
} else if (playerToken === bigBlind && bigBlindState == null) {
amount = (+amount - +smallBlindValue * 2).toString()
}
const bet = await getPlayerBet(playerToken, client)
const funds = await getPlayerFunds(playerToken, client)

return +amount <= +funds + +bet
}

export async function getPlayerBet(playerToken: string, client: PoolClient) {
const query = 'SELECT bet FROM Players WHERE token=$1'
return (await client.query(query, [playerToken])).rows[0].bet
}

const query = 'SELECT 1 FROM Players WHERE token=$1 AND funds>=$2'
return (await client.query(query, [playerToken, amount])).rowCount !== 0
export async function getPlayerFunds(playerToken: string, client: PoolClient) {
const query = 'SELECT funds FROM Players WHERE token=$1'
return (await client.query(query, [playerToken])).rows[0].funds
}

export async function isAmountRaise(
Expand Down

0 comments on commit d834c1f

Please sign in to comment.