Fix : #3 App no longer crash on user exist
continuous-integration/drone/push Build is passing Details

main
Vincent ASTOLFI 2 months ago
parent 607a9c0eef
commit 5ab3e7a81d

@ -88,38 +88,38 @@ app.post('/register', async (req, res) => {
}
if (results.length !== 0) {
return res.status(403).send({message:'User already exist. Try another username'});
}
});
var query = 'INSERT INTO users (pseudo, hashed_password) VALUES (?, ?)';
db.query(query, [pseudo, hashedPassword], (err, results) => {
if (err) {
console.error('Error inserting user into the database:', err);
return res.status(500).send('Internal server error.');
}
} else {
var query = 'INSERT INTO users (pseudo, hashed_password) VALUES (?, ?)';
db.query(query, [pseudo, hashedPassword], (err, results) => {
if (err) {
console.error('Error inserting user into the database:', err);
return res.status(500).send('Internal server error.');
}
query = 'SELECT id FROM users WHERE pseudo = ?'
db.query(query, [pseudo], (err, results) => {
if (err) {
console.error('Error inserting user into the database:', err);
return res.status(500).send('Internal server error.');
}
if (results.length === 1) {
query = 'INSERT INTO score (playerId) VALUES (?)';
db.query(query, [results[0].id], (err, results) => {
query = 'SELECT id FROM users WHERE pseudo = ?'
db.query(query, [pseudo], (err, results) => {
if (err) {
console.error('Error inserting user into the database:', err);
return res.status(500).send('Internal server error.');
}
})
}
});
if (results.length === 1) {
query = 'INSERT INTO score (playerId) VALUES (?)';
db.query(query, [results[0].id], (err, results) => {
if (err) {
console.error('Error inserting user into the database:', err);
return res.status(500).send('Internal server error.');
}
})
}
});
const token = jwt.sign({ pseudo }, secretKey, { expiresIn: '1h' });
res.cookie('authToken', token, { httpOnly: true, secure: false });
const token = jwt.sign({ pseudo }, secretKey, { expiresIn: '1h' });
res.cookie('authToken', token, { httpOnly: true, secure: false });
res.status(201).send({message: 'User registered successfully.', redirectUrl: '/game' });
})
res.status(201).send({message: 'User registered successfully.', redirectUrl: '/game' });
})
}
});
});
app.get('/user-info', (req, res) => {
@ -423,7 +423,6 @@ io.on("connection", (socket) => {
})
}
})
socket.on("reset grid", (roomId, callback) => {
const player = rooms.find((r) => r.id === roomId).players[0]
try {

@ -6,8 +6,8 @@
<title>Scores !</title>
</head>
<body>
<span class="material-symbols-outlined">
keyboard_double_arrow_right
</span>
<p>scorebaord</p>
<script src="../scripts/scoreboard.js"></script>
</body>
</html>

@ -68,6 +68,7 @@ socket.on("played move", (isHit, isWin) => {
drawEnnemyGrid();
});
socket.on('opponent left', () => {
const modal = document.getElementById('opponentLeftModal');
modal.style.display = 'block';

@ -0,0 +1 @@
console.log('Scoreboard in progress')

@ -0,0 +1,59 @@
#!/bin/bash
DB_USER="root"
DB_PASSWORD="password"
DB_NAME="battleship"
CONTAINERID=$(docker ps | grep "mysql" | cut -d ' ' -f 1)
# Function to execute MySQL commands
function run_query() {
query=$1
docker exec -i $CONTAINERID mysql -u$DB_USER -p$DB_PASSWORD $DB_NAME -e "$query"
}
# Insert 20 fake users
echo "Inserting fake users..."
for i in {1..20}; do
pseudo="Player_$i"
password="password_$i" # This should ideally be hashed in a real scenario
hashed_password=$(echo -n $password | md5sum | awk '{print $1}') # Simple hashing for this test case
run_query "INSERT INTO users (pseudo, hashed_password) VALUES ('$pseudo', '$hashed_password');"
done
# Get user IDs for reference
user_ids=($(docker exec -i $CONTAINERID mysql -u$DB_USER -p$DB_PASSWORD -e "SELECT id FROM $DB_NAME.users" | awk 'NR > 1'))
# Insert games in the scoreboard between random users
echo "Inserting random games in the scoreboard..."
for i in {1..30}; do
# Randomly pick two players
player1=${user_ids[$RANDOM % ${#user_ids[@]}]}
player2=${user_ids[$RANDOM % ${#user_ids[@]}]}
# Make sure player1 and player2 are not the same
while [ "$player1" -eq "$player2" ]; do
player2=${user_ids[$RANDOM % ${#user_ids[@]}]}
done
# Generate random win counts for both players
player1Win=$((RANDOM % 10))
player2Win=$((RANDOM % 10))
# Insert into the scoreboard
run_query "INSERT INTO scoreboards (player1, player2, player1Win, player2Win) VALUES ($player1, $player2, $player1Win, $player2Win);"
done
# Insert total win/lose score for each user in the score table
echo "Updating user scores..."
for playerId in "${user_ids[@]}"; do
# Calculate the total wins and losses for the player based on scoreboard data
wins=$(docker exec -i $CONTAINERID mysql -u$DB_USER -p$DB_PASSWORD -e "SELECT SUM(player1Win) FROM $DB_NAME.scoreboards WHERE player1 = $playerId" | awk 'NR == 2')
losses=$(docker exec -i $CONTAINERID mysql -u$DB_USER -p$DB_PASSWORD -e "SELECT SUM(player2Win) FROM $DB_NAME.scoreboards WHERE player2 = $playerId" | awk 'NR == 2')
if [ -z "$wins" ]; then wins=0; fi
if [ -z "$losses" ]; then losses=0; fi
# Insert or update the score for the player
run_query "INSERT INTO score (playerId, wins, loses) VALUES ($playerId, $wins, $losses) ON DUPLICATE KEY UPDATE wins=$wins, loses=$losses;"
done
echo "Fake data insertion completed."

@ -2,10 +2,11 @@
# Use it to create mysql container so you don't have to make it run locally
# Run it from the root directory
docker network create app-network
docker stop mysql-maettleship
docker remove mysql-maettleship
docker run --name mysql-maettleship -d -p 3306:3306 \
docker run --name db -d -p 3306:3306 \
-e MYSQL_ROOT_PASSWORD='password' \
-e MYSQL_USER='vinz' \
-e MYSQL_PASSWORS='1234' \

Loading…
Cancel
Save