I cant seem to get this to pass the test :8, :13 no matter what i try is there something I’m missing ?
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# random number generator
SECRET_NUMBER=$((1 + $RANDOM % 1000))
echo $SECRET_NUMBER
echo "Enter your username:"
read PLAYER_NAME
USER_NAME=$($PSQL "SELECT user_name FROM users WHERE user_name = '$PLAYER_NAME'" | xargs)
if [[ -z $USER_NAME ]]
then
echo "Welcome, $PLAYER_NAME! It looks like this is your first time here."
# add user_name
$PSQL "INSERT INTO users(user_name) VALUES('$PLAYER_NAME');" > /dev/null
else
# if user_name exist get previous games
GAMES_PLAYED=$($PSQL "SELECT COUNT(*) FROM games g INNER JOIN users u ON g.user_id = u.user_id WHERE u.user_name = '$PLAYER_NAME'")
BEST_GAME=$($PSQL "SELECT MIN(guess) FROM games g INNER JOIN users u ON g.user_id = u.user_id WHERE u.user_name = '$PLAYER_NAME'")
if [ "$GAMES_PLAYED" -eq 1 ]; then
game_text="game"
else
game_text="games"
fi
if [ "$BEST_GAME" -eq 1 ]; then
guess_text="guess"
else
guess_text="guesses"
fi
# Returning player welcome!!!!!!!!
echo -e "\nWelcome back, $PLAYER_NAME! You have played 1 $game_text, and your best game took $BEST_GAME $guess_text.\n"
fi
# set count variable
GUESS_COUNT=0
# users guess function
guess_number() {
echo "Guess the secret number between 1 and 1000:"
read PLAYERS_GUESS
((GUESS_COUNT++))
if [[ ! $PLAYERS_GUESS =~ ^[0-9]+$ ]]
then
echo "That is not an integer, guess again:"
guess_number
# if users guess is lower
elif
[[ $PLAYERS_GUESS -gt $SECRET_NUMBER ]]
then
echo "It's lower than that, guess again:"
guess_number
# if users guess is higher
elif
[[ $PLAYERS_GUESS -lt $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
guess_number
else
# if users guess is correct
#update_games
USER_ID=$($PSQL "SELECT user_id FROM users WHERE user_name = '$PLAYER_NAME'")
$PSQL "INSERT INTO games(guess, user_id) VALUES($GUESS_COUNT, $USER_ID)" > /dev/null
# final message
echo -e "\nYou guessed it in $GUESS_COUNT tries. The secret number was $(( $GUESS_COUNT - 1 )). Nice job!\n"
fi
}
guess_number
exit 0