Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@sormys/choosing winner #52

Merged
merged 9 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {
"start": "tsc && node dist/index.js",
"build": "tsc",
"test": "jest src/tests --coverage --config --runInBand package.json ",
"test": "jest src/tests/ --coverage --config package.json --runInBand",
"lint": "eslint --ext '.js,.ts,.tsx' src/",
"pretty": "yarn prettier --write .",
Expand Down Expand Up @@ -41,7 +42,8 @@
"firebase-admin": "^11.5.0",
"jest": "^29.5.0",
"joi": "^17.9.1",
"pg": "^8.10.0"
"pg": "^8.10.0",
"pokersolver": "^2.1.4"
},
"jest": {
"collectCoverage": true,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/fold.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { app } from '../app'
import request from 'supertest'
import { getClient } from '../utils/databaseConnection'
import type { NewGameInfo } from '../utils/types'
import { type NewGameInfo } from '../utils/types'
import { getPlayersInGame } from '../utils/commonRequest'

test('Fold, wrong args', (done) => {
Expand Down
54 changes: 53 additions & 1 deletion src/utils/commonRequest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { type Client } from 'pg'
import { type FirebasePlayerInfo, PlayerState } from './types'
import {
type FirebasePlayerInfo,
PlayerState,
type FirebasePlayerInfoWIthCards,
} from './types'
import { Hand } from 'pokersolver'
import { convertCardName } from './randomise'

export const STARTING_FUNDS_DEFAULT = 1000
export const SMALL_BLIND_DEFAULT = 100
Expand Down Expand Up @@ -196,6 +202,52 @@ export async function getSmallBlindValue(
return (await client.query(query, [gameId])).rows[0].small_blind
}


export async function getRemainingPlayersCards(
gameId: string,
client: Client
): Promise<FirebasePlayerInfoWIthCards[]> {
const query =
'SELECT token, nickname, card1, card2 FROM Players WHERE game_id=$1 and last_action <> $2'
const values = [gameId, PlayerState.Folded]
return (await client.query(query, values)).rows
}

export async function getGameCards(gameId: string, client: Client) {
const query =
'SELECT card1, card2, card3, card4, card5 FROM games WHERE game_id=$1'
const queryResult = await client.query(query, [gameId])
const cards: string[] = []
Object.entries(queryResult.rows[0]).forEach(([key, value]) => {
cards.push(convertCardName(value as string))
})
return cards
}

export async function calculateWinner(gameId: string, client: Client) {
const playersWithCards = await getRemainingPlayersCards(gameId, client)
const gameCards = await getGameCards(gameId, client)
const playersHands: any[] = []

playersWithCards.forEach((player) => {
playersHands.push(
Hand.solve([
convertCardName(player.card1),
convertCardName(player.card2),
...gameCards,
])
)
})

const solution: any[] = Hand.winners(playersHands)
const winners: any[] = []
for (let i = 0; i < playersHands.length; i++) {
if (solution.includes(playersHands[i])) {
winners.push(playersWithCards[i].token)
}
}
return winners

export async function playerHasEnoughMoney(
gameId: string,
playerToken: string,
Expand Down
38 changes: 38 additions & 0 deletions src/utils/randomise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,41 @@ export const fullCardDeck = [
'12T',
'13T',
]

export function convertCardName(cardName: string): string {
let newName = ''
if (cardName[0] === '1') {
switch (cardName[1]) {
case '0':
newName += 'T'
break
case '1':
newName += 'J'
break
case '2':
newName += 'Q'
break
case '3':
newName += 'K'
}
} else if (cardName[1] === '1') {
// Ace
newName += 'A'
} else {
newName += cardName[1]
}
switch (cardName[2]) {
case 'K':
newName += 'h'
break
case 'O':
newName += 'd'
break
case 'T':
newName += 'c'
break
case 'P':
newName += 's'
}
return newName
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4896,6 +4896,11 @@ pm2@^5.3.0:
optionalDependencies:
pm2-sysmonit "^1.2.8"

pokersolver@^2.1.4:
version "2.1.4"
resolved "https://registry.yarnpkg.com/pokersolver/-/pokersolver-2.1.4.tgz#58934f3dad1dc597c44e80474bc82e9626790ccc"
integrity sha512-vmgZS+K8H8r1RePQykFM5YyvlKo1v3xVec8FMBjg9N6mR2Tj/n/X415w+lG67FWbrk71D/CADmKFinDgaQlAsw==

postgres-array@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/postgres-array/-/postgres-array-2.0.0.tgz#48f8fce054fbc69671999329b8834b772652d82e"
Expand Down