-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
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
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
Codecov ReportPatch coverage:
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
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. |
As in title. This is done by employing a router. Remove sample Express code while at it.