-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: sormys <szymonp1806@gmail.com> Co-authored-by: Karol Wąsowski <wasowski02@protonmail.com>
- Loading branch information
1 parent
3fce4a7
commit 8afc38d
Showing
5 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { getClient } from '../../utils/databaseConnection' | ||
import { celebrate, Joi, Segments } from 'celebrate' | ||
import { | ||
sendFirebaseMessageToEveryone, | ||
verifyFCMToken, | ||
} from '../../utils/firebase' | ||
import express, { type Router } from 'express' | ||
import { rateLimiter } from '../../utils/rateLimiter' | ||
import { | ||
isPlayerInGame, | ||
isPlayersTurn, | ||
setPlayerState, | ||
setNewCurrentPlayer, | ||
changeGameRoundIfNeeded, | ||
playerHasEnoughMoney, | ||
playerRaised, | ||
getMaxBet, | ||
} from '../../utils/commonRequest' | ||
import sha256 from 'crypto-js/sha256' | ||
import { PlayerState } from '../../utils/types' | ||
|
||
const router: Router = express.Router() | ||
|
||
router.get( | ||
'/actionCall', | ||
rateLimiter, | ||
celebrate({ | ||
[Segments.QUERY]: Joi.object().keys({ | ||
playerToken: Joi.string().required().min(1).max(250).label('playerToken'), | ||
gameId: Joi.number().required().min(0).max(999999).label('gameId'), | ||
}), | ||
}), | ||
async (req, res) => { | ||
const playerToken = req.query.playerToken as string | ||
const gameId = req.query.gameId as string | ||
if (!(await verifyFCMToken(playerToken))) { | ||
return res.sendStatus(401) | ||
} | ||
|
||
const client = getClient() | ||
|
||
client | ||
.connect() | ||
.then(async () => { | ||
if (!(await isPlayerInGame(playerToken, gameId, client))) { | ||
return res.sendStatus(400) | ||
} | ||
|
||
if (!(await isPlayersTurn(playerToken, gameId, client))) { | ||
return res.sendStatus(402) | ||
} | ||
|
||
const maxBet = await getMaxBet(gameId, client) | ||
|
||
if ( | ||
!(await playerHasEnoughMoney(gameId, playerToken, maxBet, client)) | ||
) { | ||
return res.sendStatus(403) | ||
} | ||
|
||
const newPlayer = await setNewCurrentPlayer(playerToken, gameId, client) | ||
|
||
await setPlayerState(playerToken, client, PlayerState.Called) | ||
await playerRaised(gameId, playerToken, maxBet, client) | ||
await changeGameRoundIfNeeded(gameId, newPlayer, client) | ||
|
||
const message = { | ||
data: { | ||
player: sha256(playerToken).toString(), | ||
type: PlayerState.Called, | ||
actionPayload: maxBet.toString(), | ||
}, | ||
token: '', | ||
} | ||
|
||
await sendFirebaseMessageToEveryone(message, gameId, client) | ||
res.sendStatus(200) | ||
}) | ||
.catch((err) => { | ||
console.log(err.stack) | ||
return res.sendStatus(500) | ||
}) | ||
.finally(async () => { | ||
await client.end() | ||
}) | ||
} | ||
) | ||
|
||
export default router |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { app } from '../app' | ||
import request from 'supertest' | ||
import { getClient } from '../utils/databaseConnection' | ||
import type { NewGameInfo } from '../utils/types' | ||
import { getGameIdAndStatus, getPlayersInGame } from '../utils/commonRequest' | ||
|
||
test('Call, correct arguments 1', async () => { | ||
const gameMasterToken = 'CALLTEST' | ||
const gameMasterNick = 'CALLNICK' | ||
const playerToken = 'CALLTEST2' | ||
const playerNick = 'CALLNICK2' | ||
const player2Token = 'CALLTEST3' | ||
const player2Nick = 'CALLNICK3' | ||
|
||
const client = getClient() | ||
await client.connect() | ||
const res = await request(app) | ||
.get( | ||
`/createGame?creatorToken=${gameMasterToken}&nickname=${gameMasterNick}` | ||
) | ||
.expect(200) | ||
|
||
const key = (res.body as NewGameInfo).gameId | ||
await request(app) | ||
.get( | ||
`/joinGame?playerToken=${playerToken}&nickname=${playerNick}&gameId=${key}` | ||
) | ||
.expect(200) | ||
|
||
await request(app) | ||
.get( | ||
`/joinGame?playerToken=${player2Token}&nickname=${player2Nick}&gameId=${key}` | ||
) | ||
.expect(200) | ||
|
||
await request(app) | ||
.get(`/startGame?creatorToken=${gameMasterToken}`) | ||
.expect(200) | ||
|
||
const gameId = | ||
(await getGameIdAndStatus(gameMasterToken, client)).gameId ?? '' | ||
const players = await getPlayersInGame(gameId, client) | ||
console.log(players) | ||
|
||
await request(app) | ||
.get( | ||
`/actionRaise?playerToken=${players[1].token}&gameId=${gameId}&amount=5` | ||
) | ||
.expect(402) | ||
|
||
await request(app) | ||
.get( | ||
`/actionRaise?playerToken=${players[0].token}&gameId=${gameId}&amount=300` | ||
) | ||
.expect(200) | ||
await request(app) | ||
.get(`/actionCall?playerToken=${players[1].token}&gameId=${gameId}`) | ||
.expect(200) | ||
const getRound = 'SELECT game_round FROM Games WHERE game_id=$1' | ||
|
||
expect( | ||
await ( | ||
await client.query(getRound, [gameId]) | ||
).rows[0].game_round | ||
).toEqual('1') // Player 2 can still act | ||
|
||
await request(app) | ||
.get( | ||
`/actionRaise?playerToken=${players[2].token}&gameId=${gameId}&amount=2` | ||
) | ||
.expect(404) | ||
|
||
await request(app) | ||
.get( | ||
`/actionRaise?playerToken=${players[2].token}&gameId=${gameId}&amount=2000000000` | ||
) | ||
.expect(403) | ||
|
||
await request(app) | ||
.get( | ||
`/actionRaise?playerToken=${players[2].token}&gameId=${gameId}&amount=400` | ||
) | ||
.expect(200) | ||
|
||
await request(app) | ||
.get(`/actionCall?playerToken=${players[0].token}&gameId=${gameId}`) | ||
.expect(200) | ||
|
||
await request(app) | ||
.get(`/actionFold?playerToken=${players[1].token}&gameId=${gameId}`) | ||
.expect(200) | ||
|
||
await request(app) | ||
.get(`/actionFold?playerToken=${players[2].token}&gameId=${gameId}`) | ||
.expect(402) // round ended | ||
|
||
await client.end() | ||
}, 20000) |
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