Hello, we are currently working on this project to obtain the certification in relational databases. The tasks are already finished and after testing all the requested points, we noticed that it works correctly, but for some reason, it does not allow us to finish. This is the database:
--
-- PostgreSQL database dump
--
-- Dumped from database version 12.17 (Ubuntu 12.17-1.pgdg22.04+1)
-- Dumped by pg_dump version 12.17 (Ubuntu 12.17-1.pgdg22.04+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
DROP DATABASE number_guess;
--
-- Name: number_guess; Type: DATABASE; Schema: -; Owner: freecodecamp
--
CREATE DATABASE number_guess WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'C.UTF-8' LC_CTYPE = 'C.UTF-8';
ALTER DATABASE number_guess OWNER TO freecodecamp;
\connect number_guess
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: games_guess; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.games_guess (
game_guess_id integer NOT NULL,
number_guess integer NOT NULL,
secret_number integer NOT NULL,
group_guess integer NOT NULL,
result integer NOT NULL,
user_id integer NOT NULL
);
ALTER TABLE public.games_guess OWNER TO freecodecamp;
--
-- Name: games_guess_game_guess_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--
CREATE SEQUENCE public.games_guess_game_guess_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.games_guess_game_guess_id_seq OWNER TO freecodecamp;
--
-- Name: games_guess_game_guess_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--
ALTER SEQUENCE public.games_guess_game_guess_id_seq OWNED BY public.games_guess.game_guess_id;
--
-- Name: users; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.users (
user_id integer NOT NULL,
username character varying(22) NOT NULL
);
ALTER TABLE public.users OWNER TO freecodecamp;
--
-- Name: users_user_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--
CREATE SEQUENCE public.users_user_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.users_user_id_seq OWNER TO freecodecamp;
--
-- Name: users_user_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--
ALTER SEQUENCE public.users_user_id_seq OWNED BY public.users.user_id;
--
-- Name: games_guess game_guess_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.games_guess ALTER COLUMN game_guess_id SET DEFAULT nextval('public.games_guess_game_guess_id_seq'::regclass);
--
-- Name: users user_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.users ALTER COLUMN user_id SET DEFAULT nextval('public.users_user_id_seq'::regclass);
--
-- Data for Name: games_guess; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
INSERT INTO public.games_guess VALUES (1, 1, 476, 1, 0, 1);
INSERT INTO public.games_guess VALUES (2, 476, 476, 1, 1, 1);
INSERT INTO public.games_guess VALUES (3, 2, 889, 1, 0, 2);
INSERT INTO public.games_guess VALUES (4, 100, 889, 1, 0, 2);
INSERT INTO public.games_guess VALUES (5, 1, 889, 1, 0, 2);
INSERT INTO public.games_guess VALUES (6, 4, 889, 1, 0, 2);
INSERT INTO public.games_guess VALUES (7, 889, 889, 1, 1, 2);
INSERT INTO public.games_guess VALUES (8, 1, 961, 2, 0, 1);
INSERT INTO public.games_guess VALUES (9, 6, 961, 2, 0, 1);
INSERT INTO public.games_guess VALUES (10, 4, 961, 2, 0, 1);
INSERT INTO public.games_guess VALUES (11, 961, 961, 2, 1, 1);
INSERT INTO public.games_guess VALUES (12, 1, 280, 2, 0, 2);
INSERT INTO public.games_guess VALUES (13, 2, 280, 2, 0, 2);
INSERT INTO public.games_guess VALUES (14, 280, 280, 2, 1, 2);
--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
INSERT INTO public.users VALUES (1, 'A');
INSERT INTO public.users VALUES (2, 'B');
--
-- Name: games_guess_game_guess_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.games_guess_game_guess_id_seq', 14, true);
--
-- Name: users_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.users_user_id_seq', 2, true);
--
-- Name: games_guess games_guess_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.games_guess
ADD CONSTRAINT games_guess_pkey PRIMARY KEY (game_guess_id);
--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (user_id);
--
-- Name: games_guess games_guess_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.games_guess
ADD CONSTRAINT games_guess_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(user_id);
--
-- PostgreSQL database dump complete
This is the number_guess.sh file we are using:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
secret_number=$((RANDOM % 1000 + 1))
echo "Enter your username:"
read username
INICIO()
{
USER_NAME_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$username';")
VALIDAR_USUARIO
}
VALIDAR_USUARIO()
{
if [[ -z $USER_NAME_ID ]]
then
INSERT_USER_NAME=$($PSQL "INSERT INTO users (username) VALUES('$username');")
NEW_USER_NAME_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$username';")
GROUP_GUESS=1
echo $"Welcome, $username! It looks like this is your first time here."
echo "Guess the secret number between 1 and 1000:"
VALIDAR_NUMBER_GUESS
VALIDAR_NUMBER
else
NEW_USER_NAME_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$username';")
games_played=$($PSQL "SELECT SUM(result) FROM games_guess WHERE user_id = '$NEW_USER_NAME_ID';")
best_game=$($PSQL "SELECT MIN(count) FROM (SELECT group_guess AS group, COUNT(game_guess_id) AS count, MAX(result) AS resultado FROM games_guess WHERE user_id = '$NEW_USER_NAME_ID' GROUP BY group_guess) as a WHERE resultado = 1;")
LAST_GUESS_ID=$($PSQL "SELECT MAX(game_guess_id) FROM games_guess WHERE user_id = '$NEW_USER_NAME_ID';")
LAST_RESULT=$($PSQL "SELECT result FROM games_guess WHERE game_guess_id = $LAST_GUESS_ID;")
LAST_GROUP_GUESS=$($PSQL "SELECT group_guess FROM games_guess WHERE game_guess_id = $LAST_GUESS_ID;")
echo "Welcome back, $username! You have played $games_played games, and your best game took $best_game guesses."
echo "Guess the secret number between 1 and 1000:"
VALIDAR_NUMBER_GUESS
if [[ $LAST_RESULT == 1 ]]
then
GROUP_GUESS=$((LAST_GROUP_GUESS + 1))
VALIDAR_NUMBER
else
GROUP_GUESS=$LAST_GROUP_GUESS
VALIDAR_NUMBER
fi
fi
}
VALIDAR_NUMBER()
{
if [[ $NUMBER_GUESS == $secret_number ]]
then
INSERT_GUESS=$($PSQL "INSERT INTO games_guess (number_guess, secret_number, group_guess, result, user_id) VALUES ($NUMBER_GUESS, $secret_number, $GROUP_GUESS, 1, $NEW_USER_NAME_ID);")
number_of_guesses=$($PSQL "SELECT COUNT(game_guess_id) FROM games_guess WHERE user_id = '$NEW_USER_NAME_ID' AND group_guess = $GROUP_GUESS;")
echo "You guessed it in $number_of_guesses tries. The secret number was $secret_number. Nice job!"
elif [[ $NUMBER_GUESS > $secret_number ]]
then
INSERT_GUESS=$($PSQL "INSERT INTO games_guess (number_guess, secret_number, group_guess, result, user_id) VALUES ($NUMBER_GUESS, $secret_number, $GROUP_GUESS, 0, $NEW_USER_NAME_ID);")
echo "It's higher than that, guess again:"
VALIDAR_NUMBER_GUESS
VALIDAR_NUMBER
else
INSERT_GUESS=$($PSQL "INSERT INTO games_guess (number_guess, secret_number, group_guess, result, user_id) VALUES ($NUMBER_GUESS, $secret_number, $GROUP_GUESS, 0, $NEW_USER_NAME_ID);")
echo "It's lower than that, guess again:"
VALIDAR_NUMBER_GUESS
VALIDAR_NUMBER
fi
}
VALIDAR_NUMBER_GUESS()
{
read NUMBER_GUESS
if [[ $NUMBER_GUESS =~ ^[0-9]+$ ]]
then
NUMBER_GUESS=$NUMBER_GUESS
else
echo "That is not an integer, guess again:"
VALIDAR_NUMBER_GUESS
fi
}
INICIO
When we run to validate if we have already finished the tasks, we get this message:
SUBTASKS 1.1 :8 Your script should print the correct welcome message for returning users.
First of all, we thought it was because we had called the variables with different names, so we changed them. Since the message is still appearing, we copied and pasted the messages that should appear again, since suddenly we included spaces or letters that were not there, but the same message continued to appear. We’ve been changing things for a little over a week and nothing.
Searching today, we found out about these forums and saw several regarding this same project, but we did not find one that answered the cause of the problem we have.
thanks for your help