Trying to pass this final testcase:
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
Having some problem to insert new users in db, it just doesn’t work.
I also checked that I’m incrementing the NUMBER_GUESSES variable even if the user doesn’t input a number, but if I take it off I don’t pass this test:
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
My code right now is like this:
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
echo -e "\nEnter your username:"
read INPUT_USERNAME
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$INPUT_USERNAME'")
GUESS_NUMBER() {
SECRETNUMBER=$(( RANDOM % 1000 + 1 ))
echo -e "\nGuess the secret number between 1 and 1000:"
read USER_GUESS
(( NUMBER_GUESSES++ ))
while ! [[ $USER_GUESS =~ ^[0-9]+$ ]] || [[ -z $USER_GUESS ]]
do
echo "That is not an integer, guess again:"
read USER_GUESS
done
while [[ $USER_GUESS -ne $SECRETNUMBER ]]
do
if [[ $USER_GUESS -gt $SECRETNUMBER ]]
then
echo -e "\nIt's lower than that, guess again:"
else
echo -e "\nIt's higher than that, guess again:"
fi
read USER_GUESS
(( NUMBER_GUESSES++ ))
done
echo -e "\nYou guessed it in $NUMBER_GUESSES tries. The secret number was $SECRETNUMBER. Nice job!"
exit
}
if [[ -z $USER_ID ]]
then
echo -e "\nWelcome, $INPUT_USERNAME! It looks like this is your first time here."
GUESS_NUMBER
GAMES_PLAYED=1
$PSQL "INSERT INTO users(username, games_played, best_game) VALUES('$INPUT_USERNAME', $GAMES_PLAYED, $NUMBER_GUESSES)"
else
# USER_ID=$($PSQL "SELECT user_id FROM users WHERE username = '$USERNAME'")
BEST_GAME=$($PSQL "SELECT best_game FROM users WHERE user_id = $USER_ID")
GAMES_PLAYED=$($PSQL "SELECT games_played FROM users WHERE user_id = $USER_ID")
echo -e "\nWelcome back, $INPUT_USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
GUESS_NUMBER
(( GAMES_PLAYED++ ))
if [[ $NUMBER_GUESSES -lt $BEST_GAME ]]
then
$PSQL "UPDATE users SET games_played = $GAMES_PLAYED, best_game = $NUMBER_GUESSES WHERE user_id = $USER_ID"
else
$PSQL "UPDATE users SET games_played = $GAMES_PLAYED WHERE user_id = $USER_ID"
fi
fi
And my db dump:
--
-- PostgreSQL database dump
--
-- Dumped from database version 12.9 (Ubuntu 12.9-2.pgdg20.04+1)
-- Dumped by pg_dump version 12.9 (Ubuntu 12.9-2.pgdg20.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: users; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.users (
user_id integer NOT NULL,
username character varying(22) NOT NULL,
games_played integer,
best_game integer
);
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: 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: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
--
-- Name: users_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.users_user_id_seq', 1, false);
--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (user_id);
--
-- PostgreSQL database dump complete
--