I can’t pass the test :
If that username has been used before, it should print Welcome back, <username>! You have played <games_played> games, and your best game took <best_game> guesses., with <username> being a users name from the database, <games_played> being the total number of games that user has played, and <best_game> being the fewest number of guesses it took that user to win the game
This is my script:
#!/bin/bash
PSQL=“psql --username=freecodecamp --dbname=number_guess -t --no-align -c”
echo “Enter your username:”
read USERNAME
USER_ID=$($PSQL “SELECT user_id FROM users WHERE username=‘$USERNAME’;”)
if [[ -n $USER_ID ]]
then
GAMES_PLAYED=$($PSQL “SELECT COUNT(*) FROM games WHERE user_id=$USER_ID;”)
BEST_GAME=$($PSQL “SELECT MIN(guesses) FROM games WHERE user_id=$USER_ID;”)
echo “Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses.”
else
$PSQL “INSERT INTO users(username) VALUES(‘$USERNAME’);”
USER_ID=$($PSQL “SELECT user_id FROM users WHERE username=‘$USERNAME’;”)
echo “Welcome, $USERNAME! It looks like this is your first time here.”
fi
SECRET_NUMBER=$(( RANDOM % 1000 + 1 ))
NUMBER_OF_GUESSES=0
echo “Guess the secret number between 1 and 1000:”
while true
do
read GUESS
if ! [[ $GUESS =~ ^[0-9]+$ ]]
then
echo "That is not an integer, guess again:"
continue
fi
((NUMBER_OF_GUESSES++))
if [[ $GUESS -lt $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
elif [[ $GUESS -gt $SECRET_NUMBER ]]
then
echo "It's lower than that, guess again:"
else
echo "You guessed it in $NUMBER_OF_GUESSES tries. The secret number was $SECRET_NUMBER. Nice job!"
$PSQL "INSERT INTO games(user_id, guesses) VALUES($USER_ID, $NUMBER_OF_GUESSES);"
break
fi
done



