diff --git a/index.js b/index.js index d2fbdc3..466b3c8 100644 --- a/index.js +++ b/index.js @@ -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 { diff --git a/public/pages/scoreboardView.html b/public/pages/scoreboardView.html index 152bdf0..4fa4d7f 100644 --- a/public/pages/scoreboardView.html +++ b/public/pages/scoreboardView.html @@ -6,8 +6,8 @@ Scores ! - - keyboard_double_arrow_right - +

scorebaord

+ + \ No newline at end of file diff --git a/public/scripts/app.js b/public/scripts/app.js index fb0dc87..0395332 100644 --- a/public/scripts/app.js +++ b/public/scripts/app.js @@ -68,6 +68,7 @@ socket.on("played move", (isHit, isWin) => { drawEnnemyGrid(); }); + socket.on('opponent left', () => { const modal = document.getElementById('opponentLeftModal'); modal.style.display = 'block'; diff --git a/public/scripts/scoreboard.js b/public/scripts/scoreboard.js new file mode 100644 index 0000000..89a1b7c --- /dev/null +++ b/public/scripts/scoreboard.js @@ -0,0 +1 @@ +console.log('Scoreboard in progress') \ No newline at end of file diff --git a/tools/db_auto_insert_data.sh b/tools/db_auto_insert_data.sh new file mode 100755 index 0000000..61a95e1 --- /dev/null +++ b/tools/db_auto_insert_data.sh @@ -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." diff --git a/tools/db_docker_init.sh b/tools/db_docker_init.sh index 9271db3..349a206 100755 --- a/tools/db_docker_init.sh +++ b/tools/db_docker_init.sh @@ -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' \