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

Sending game end info through FCM #67

Merged
merged 6 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 2 additions & 12 deletions src/routes/gameplay/fold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
setPlayerState,
changeCurrentPlayer,
changeGameRoundIfNeeded,
getPlayersStillInGame,
gameEnd,
} from '../../utils/commonRequest'
import sha256 from 'crypto-js/sha256'
import { PlayerState } from '../../utils/types'
Expand Down Expand Up @@ -47,17 +47,7 @@ router.get(
await setPlayerState(playerToken, client, PlayerState.Folded)
const newPlayer = await changeCurrentPlayer(playerToken, gameId, client)
if (newPlayer === '') {
const winner = (await getPlayersStillInGame(gameId, client))[0]
const message = {
data: {
player: sha256(winner.token).toString(),
type: PlayerState.Won,
actionPayload: '',
},
token: '',
}

await sendFirebaseMessageToEveryone(message, gameId, client)
await gameEnd(gameId, client)
return res.sendStatus(201)
}

Expand Down
3 changes: 0 additions & 3 deletions src/tests/fold.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ test('Fold, correct arguments, wrong turn', async () => {
.get(`/actionFold?playerToken=${players[0].token}&gameId=${gameId}`)
.expect(200)

await request(app)
.get(`/actionFold?playerToken=${players[1].token}&gameId=${gameId}`)
.expect(201)
const getRound = 'SELECT game_round FROM Games WHERE game_id=$1'

expect(
Expand Down
56 changes: 44 additions & 12 deletions src/utils/commonRequest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { type PoolClient } from 'pg'
import {
type BasicPlayerInfo,
PlayerState,
type FirebasePlayerInfoWIthCards,
} from './types'
import { type BasicPlayerInfo, PlayerState } from './types'
import { Hand } from 'pokersolver'
import { convertCardName } from './randomise'
import { sendFirebaseMessageToEveryone } from './firebase'
import sha256 from 'crypto-js/sha256'

export const STARTING_FUNDS_DEFAULT = 1000
export const SMALL_BLIND_DEFAULT = 100
Expand Down Expand Up @@ -159,6 +156,32 @@ export async function sendNewCards(
await sendFirebaseMessageToEveryone(message, gameId, client)
}

export async function gameEnd(gameId: string, client: PoolClient) {
const getPaymentQuery =
'SELECT current_table_value FROM Games WHERE game_id=$1'
const endGamePlayersQuery = 'DELETE FROM Players WHERE game_id=$1'
const endGameGameQuery = 'DELETE FROM Games WHERE game_id=$1'
const payment: number = +(await client.query(getPaymentQuery, [gameId]))
.rows[0].current_table_value
const winners = await calculateWinner(gameId, client)
for (let i = 0; i < winners.length; i++) {
winners[i] = sha256(winners[i])
}

const message = {
data: {
type: 'gameEnd',
winner: JSON.stringify(winners),
Kwasow marked this conversation as resolved.
Show resolved Hide resolved
payment: (payment / winners.length).toString(),
Kwasow marked this conversation as resolved.
Show resolved Hide resolved
},
token: '',
}

await sendFirebaseMessageToEveryone(message, gameId, client)
await client.query(endGameGameQuery, [gameId])
await client.query(endGamePlayersQuery, [gameId])
}

export async function changeGameRoundIfNeeded(
gameId: string,
client: PoolClient
Expand All @@ -185,12 +208,16 @@ export async function changeGameRoundIfNeeded(
const startingPlayer = await chooseRoundStartingPlayer(gameId, client)

await client.query(updateGameRound, [gameId])
await client.query(resetPlayerStates, [gameId, PlayerState.Folded])
await setCurrentPlayer(gameId, startingPlayer, client)

const round: number = await getRound(gameId, client)
await sendNewCards(gameId, round, client)
// todo count cards and set winners
if (round === 6) {
await gameEnd(gameId, client)
} else {
await client.query(resetPlayerStates, [gameId, PlayerState.Folded])
await setCurrentPlayer(gameId, startingPlayer, client)

await sendNewCards(gameId, round, client)
}

return true
} else {
return false
Expand Down Expand Up @@ -295,9 +322,9 @@ export async function getSmallBlindValue(
export async function getRemainingPlayersCards(
gameId: string,
client: PoolClient
): Promise<FirebasePlayerInfoWIthCards[]> {
) {
const query =
'SELECT token, nickname, card1, card2 FROM Players WHERE game_id=$1 and last_action <> $2'
'SELECT token, nickname, card1, card2 FROM Players WHERE game_id=$1 AND (last_action <> $2 OR last_action IS NULL)'
const values = [gameId, PlayerState.Folded]
return (await client.query(query, values)).rows
}
Expand All @@ -315,6 +342,11 @@ export async function getGameCards(gameId: string, client: PoolClient) {

export async function calculateWinner(gameId: string, client: PoolClient) {
const playersWithCards = await getRemainingPlayersCards(gameId, client)
if (playersWithCards.length === 1) {
const result: any[] = []
result.push(playersWithCards[0].token)
return result
}
const gameCards = await getGameCards(gameId, client)
const playersHands: any[] = []

Expand Down