I wrote the code x times and don’t wanna reach all the tasks.
The Task:
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
and
When the secret number is guessed, your script should print You guessed it in <number_of_guesses> tries. The secret number was <secret_number>. Nice job!
and finish running
failed everytime
This is my Code:
#!/bin/bash
# PSQL-command for databaserequest
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
# function, get user info or create new user
GET_USER_INFO() {
# prompt the user to write some username
echo "Enter your username:"
read USERNAME
# Valiadation of username (max. 22 signs, no special letters)
if [[ ! $USERNAME =~ ^[a-zA-Z0-9_]{1,22}$ ]];
then
echo "Invalid username. Please use only letters, numbers, or underscores, and keep it under 22 characters."
exit 1
fi
# Get user info from database
USER_INFO=$($PSQL "SELECT user_id, games_played, best_game FROM users WHERE username='$USERNAME'")
if [[ -z $USER_INFO ]]; then
echo "Welcome, $USERNAME! It looks like this is your first time here."
INSERT_USER_RESULT=$($PSQL "INSERT INTO users(username, best_game) VALUES('$USERNAME', 9999)")
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
GAMES_PLAYED=0
BEST_GAME=9999
else
# Correct parsing of the output (using of IFS)
IFS='|' read -r USER_ID GAMES_PLAYED BEST_GAME <<< "$USER_INFO"
USER_ID=$(echo $USER_ID | xargs)
GAMES_PLAYED=$(echo $GAMES_PLAYED | xargs)
BEST_GAME=$(echo $BEST_GAME | xargs)
echo -e "Welcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
fi
}
# initialize game
INITIALIZE_GAME() {
SECRET_NUMBER=$((RANDOM % 1000 + 1))
NUMBER_OF_GUESSES=0
NUMBER_OF_GUESSES=$(echo $NUMBER_OF_GUESSES | xargs) # Trimmen von NUMBER_OF_GUESSES
echo "Guess the secret number between 1 and 1000:"
}
# Function: start the game
PLAY_GAME() {
while true; do
read GUESS
((NUMBER_OF_GUESSES++))
# Check, if is the entrie an integer
if [[ ! $GUESS =~ ^[0-9]+$ ]]; then
echo "That is not an integer, guess again:"
continue
fi
# Tipps for higher or lower number
if (( GUESS < SECRET_NUMBER )); then
echo "It's higher than that, guess again:"
elif (( GUESS > 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!"
break
fi
done
}
# Function: update the statistics
UPDATE_STATISTICS() {
if [[ -n $USER_ID ]];
then
NEW_GAMES_PLAYED=$((GAMES_PLAYED + 1))
$PSQL "UPDATE users SET games_played=$NEW_GAMES_PLAYED WHERE user_id=$USER_ID"
if [[ -z $BEST_GAME || $NUMBER_OF_GUESSES -lt $BEST_GAME ]];
then
$PSQL "UPDATE users SET best_game=$NUMBER_OF_GUESSES WHERE user_id=$USER_ID"
fi
else
echo "ERROR: USER_ID is not set. Cannot update statistics."
fi
}
# Walkthrough from the programm
MAIN() {
GET_USER_INFO
INITIALIZE_GAME
PLAY_GAME
UPDATE_STATISTICS
}
# Program start
MAIN
My database:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
--------------+--------------+----------+---------+---------+-----------------------
number_guess | freecodecamp | UTF8 | C.UTF-8 | C.UTF-8 |
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
List of relations
Schema | Name | Type | Owner
--------+--------------------------+------------+--------------
public | games | table | freecodecamp
public | games_game_id_seq | sequence | freecodecamp
public | users | table | freecodecamp
public | users_user_id_seq | sequence | freecodecamp
(4 rows)
Table "public.games"
Column | Type | Collation | Nullable | Default
-----------+---------+-----------+----------+----------------------------------------
game_id | integer | | not null | nextval('games_game_id_seq'::regclass)
user_id | integer | | |
guesses | integer | | not null |
Indexes:
"games_pkey" PRIMARY KEY, btree (game_id)
Foreign-key constraints:
"games_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(user_id)
Table "public.users"
Column | Type | Collation | Nullable | Default
------------------+----------------------------+-----------+----------+-----------------------------------
user_id | integer | | not null | nextval('users_user_id_seq'::regclass)
username | character varying(22) | | not null |
games_played | integer | | | 0
best_game | integer | | not null |
Indexes:
"users_pkey" PRIMARY KEY, btree (user_id)
"users_username_key" UNIQUE CONSTRAINT, btree (username)
Referenced by:
TABLE "games" CONSTRAINT "games_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(user_id)
Can you helping me and explain me the problem, cause i didn’t find any solution for this problem and it’s the only blockade to get the Relational database certificate.