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

Split functions from app.ts into files #30

Merged
merged 8 commits into from
Apr 26, 2023
Merged

Conversation

mikolaj-pirog
Copy link
Member

@mikolaj-pirog mikolaj-pirog commented Apr 24, 2023

As in title. This is done by employing a router. Remove sample Express code while at it.

src/createGame.ts Fixed Show fixed Hide fixed
src/joinGame.ts Fixed Show fixed Hide fixed
Comment on lines +27 to +98
async (req, res) => {
if (!(await verifyFCMToken(req.query.creatorToken))) {
return res.sendStatus(400)
}

const client = getClient()
client
.connect()
.then(async () => {
const createPlayerQuery =
'INSERT INTO Players(token, nickname, turn, game_id, card1, card2, funds, bet) VALUES($1, $2, $3, $4, $5, $6, $7, $8)'
const createPlayerValues = [
req.query.creatorToken,
req.query.nickname,
0,
null,
null,
null,
null,
null,
]

const createGameQuery =
'SELECT * FROM insert_with_random_key($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) '
const values = [
req.query.creatorToken,
null,
null,
null,
null,
null,
0,
req.query.startingFunds ?? startingFundsDefault,
req.query.smallBlind ?? smallBlindDefault,
req.query.creatorToken,
0,
req.query.creatorToken,
]

await client.query(createPlayerQuery, createPlayerValues)
await client.query(createGameQuery, values).then(async (result) => {
await client
.query('UPDATE Players SET game_id=$1 WHERE token=$2', [
result.rows[0].insert_with_random_key,
req.query.creatorToken,
])
.then(() => {
const newGame: newGameInfo = {
gameKey: result.rows[0].insert_with_random_key,
startingFunds: startingFundsDefault,
smallBlind: smallBlindDefault,
}
if (req.query.startingFunds !== undefined) {
newGame.startingFunds = parseInt(
req.query.startingFunds as string
)
}
if (req.query.smallBlind !== undefined) {
newGame.smallBlind = parseInt(req.query.smallBlind as string)
}
res.send(newGame)
})
})
})
.catch(async (err) => {
console.log(err.stack)
return res.sendStatus(500)
})
.finally(async () => {
await client.end()
})
}

Check failure

Code scanning / CodeQL

Missing rate limiting

This route handler performs [authorization](1), but is not rate-limited.
Comment on lines +20 to +118
async (req, res) => {
if (!(await verifyFCMToken(req.query.playerToken))) {
return res.sendStatus(400)
}

const client = getClient()

client
.connect()
.then(async () => {
const checkIfGameExistsQuery =
'SELECT game_master FROM Games g join Players p on g.game_id = p.game_id WHERE g.game_id = $1 group by g.game_id having count(p.token) < 8'
const gameCheckValues = [req.query.gameId]
const createPlayerQuery =
'INSERT INTO Players(token, nickname, turn, game_id, card1, card2, funds, bet) VALUES($1, $2, $3, $4, $5, $6, $7, $8)'
const createPlayerValues = [
req.query.playerToken,
req.query.nickname,
0,
req.query.gameId,
null,
null,
null,
null,
]
const getGameInfoQuery = 'SELECT * FROM Games WHERE game_id=$1'
const getGameInfoValues = [req.query.gameId]
const getPlayersInRoomQuery =
'SELECT nickname, token FROM Players WHERE game_id=$1'
const getPlayersInRoomValues = [req.query.gameId]
await client
.query(checkIfGameExistsQuery, gameCheckValues)
.then(async (result) => {
if (result.rowCount === 0) {
return res.sendStatus(401)
} else {
const gameInfo: gameSettings = {
smallBlind: 0,
startingFunds: 0,
players: [],
gameMasterHash: sha256(result.rows[0].game_master).toString(),
}
await client.query(createPlayerQuery, createPlayerValues)
await client
.query(getGameInfoQuery, getGameInfoValues)
.then((result) => {
gameInfo.smallBlind = parseInt(result.rows[0].small_blind)
gameInfo.startingFunds = parseInt(
result.rows[0].starting_funds
)
})
await client
.query(getPlayersInRoomQuery, getPlayersInRoomValues)
.then((result) => {
const message = {
data: {
type: 'playerJoined',
// We know that the nickname will be defined
// because we checked it with celebrate.
nickname: req.query.nickname as string,
playerHash: sha256(req.query.playerToken).toString(),
},
token: '',
}
result.rows.forEach(async (row) => {
gameInfo.players.push({
nickname: row.nickname,
playerHash: sha256(row.token).toString(),
})
if (
row.token !== req.query.playerToken &&
process.env.JEST_WORKER_ID === undefined
) {
// Again, we don't want to send messages when testing.
// Sending firebase message to all players except the one who just joined.
message.token = row.token
await getMessaging()
.send(message)
.then((response) => {
console.log('Successfully sent message:', response)
})
.catch((error) => {
console.log('Error sending message:', error)
})
}
})
})
res.send(gameInfo)
}
})
})
.catch((err) => {
console.log(err.stack)
return res.sendStatus(500)
})
.finally(async () => {
await client.end()
})
}

Check failure

Code scanning / CodeQL

Missing rate limiting

This route handler performs [authorization](1), but is not rate-limited.
@mikolaj-pirog mikolaj-pirog marked this pull request as ready for review April 25, 2023 12:06
@codecov
Copy link

codecov bot commented Apr 25, 2023

Codecov Report

Patch coverage: 82.30% and project coverage change: +2.63 🎉

Comparison is base (f4abb88) 74.63% compared to head (ce4a4a0) 77.27%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #30      +/-   ##
==========================================
+ Coverage   74.63%   77.27%   +2.63%     
==========================================
  Files           3        6       +3     
  Lines         138      154      +16     
  Branches       17       17              
==========================================
+ Hits          103      119      +16     
  Misses         35       35              
Impacted Files Coverage Δ
src/firebase.ts 53.84% <53.84%> (ø)
src/routes/joinGame.ts 81.48% <81.48%> (ø)
src/routes/createGame.ts 88.57% <88.57%> (ø)
src/app.ts 95.23% <100.00%> (+14.86%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

src/app.ts Show resolved Hide resolved
src/app.ts Show resolved Hide resolved
@Kwasow Kwasow mentioned this pull request Apr 25, 2023
src/app.ts Show resolved Hide resolved
src/routes/createGame.ts Outdated Show resolved Hide resolved
src/tests/app.test.ts Outdated Show resolved Hide resolved
@mikolaj-pirog mikolaj-pirog merged commit aa3994a into main Apr 26, 2023
@Kwasow Kwasow deleted the @aetn23/splitRequests branch April 29, 2023 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants