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

General code cleanup #43

Merged
merged 6 commits into from
May 13, 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
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
"preset": "ts-jest"
},
"lint-staged": {
"*.(js|ts|tsx)": "yarn lint",
"src/*": "prettier --write"
"*.(js|ts|tsx)": [
"yarn pretty",
"yarn lint"
]
}
}
3 changes: 2 additions & 1 deletion src/routes/createGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ router.get(
client
.connect()
.then(async () => {
// Define queries
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 = [
Expand All @@ -47,7 +48,6 @@ router.get(
null,
null,
]

const createGameQuery =
'SELECT * FROM insert_with_random_key($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) '
const values = [
Expand All @@ -65,6 +65,7 @@ router.get(
null,
]

// Create game
await client.query(createPlayerQuery, createPlayerValues)
await client.query(createGameQuery, values).then(async (result) => {
await client
Expand Down
100 changes: 52 additions & 48 deletions src/routes/joinGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ router.get(
client
.connect()
.then(async () => {
// Define queries
const checkIfGameExistsQuery = `SELECT game_master FROM Games g
join Players p on g.game_id = p.game_id
WHERE g.game_id = $1 and g.game_round = 0
Expand All @@ -53,6 +54,7 @@ router.get(
'SELECT nickname, token FROM Players WHERE game_id=$1'
const getPlayersInRoomValues = [req.query.gameId]

// Check if player isn't already in the game
const playerNotInGameResult = await client.query(
checkIfPlayerNotInGameQuery,
[req.query.playerToken]
Expand All @@ -61,56 +63,58 @@ router.get(
return res.sendStatus(400)
}

// Check if game exists
const checkIfGameExistResult = await client.query(
checkIfGameExistsQuery,
gameCheckValues
)

if (checkIfGameExistResult.rowCount === 0) {
return res.sendStatus(401)
}

// Prepare info for new player
const gameInfo: GameSettings = {
smallBlind: 0,
startingFunds: 0,
players: [],
gameMasterHash: sha256(
checkIfGameExistResult.rows[0].game_master
).toString(),
}
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(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(),
})
// Sending firebase message to all players except the one
// who just joined.
if (row.token !== req.query.playerToken) {
message.token = row.token
await sendFirebaseMessage(message)
}
})
})
await client.query(createPlayerQuery, createPlayerValues)
res.send(gameInfo)
}
.query(getGameInfoQuery, getGameInfoValues)
.then((result) => {
gameInfo.smallBlind = parseInt(result.rows[0].small_blind)
gameInfo.startingFunds = parseInt(result.rows[0].starting_funds)
})

// Notify players about new player
const playersInRoomResult = await client.query(
getPlayersInRoomQuery,
getPlayersInRoomValues
)

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: '',
}
playersInRoomResult.rows.forEach(async (row) => {
gameInfo.players.push({
nickname: row.nickname,
playerHash: sha256(row.token).toString(),
})
message.token = row.token
await sendFirebaseMessage(message)
})
await client.query(createPlayerQuery, createPlayerValues)
res.send(gameInfo)
})
.catch((err) => {
console.log(err.stack)
Expand Down
5 changes: 4 additions & 1 deletion src/routes/startGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ router.get(
const updatePlayerStateQuery =
'UPDATE Players SET turn=$1, card1=$2, card2=$3, funds=$4 WHERE token=$5'

// Check if the player is a master of not started game
// Check if the player is a master of a non-started game
const getGameIdResult = await client.query(getGameIdQuery, [
req.query.creatorToken,
])
Expand All @@ -52,6 +52,7 @@ router.get(
const gameId = getGameIdResult.rows[0].game_id
const startingFunds = getGameIdResult.rows[0].starting_funds

// Get players
const playersResult = await client.query(getPlayersQuery, [gameId])
const playersCount = playersResult.rowCount
if (playersCount < 2) {
Expand All @@ -67,6 +68,7 @@ router.get(
})
}

// Deal out cards
shuffleArray(playersInGame)
const cardDeck = shuffleArray(fullCardDeck.slice())

Expand Down Expand Up @@ -106,6 +108,7 @@ router.get(
])
}

// Notify players about the game state
const message = {
data: {
type: 'startGame',
Expand Down