Tell us what’s happening:
I can’t output what’s asked,
even though (I think) I’ve already done it.
Your code so far
##################SHELL SCRIPT:##################
#!/bin/bash
PSQL="psql --username=freecodecamp --dbname=number_guess -t --no-align -c"
MAIN_MENU(){
echo "Enter your username:"
read USERNAME
#if valid input
if [[ $USERNAME =~ ^[a-zA-Z]{0,22}$ ]]
then
# get user_id
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
# if user_id does not exits (user_id is empty)
if [[ -z $USER_ID ]]
then
#print welcome to new user
echo "Welcome, $USERNAME! It looks like this is your first time here."
# this is the guessing game
SECRET_NUMBER=$(echo $(( $RANDOM % 1000 + 1 )))
echo "Guess the secret number between 1 and 1000:"
read GUESS
# add new user to database
INSERT_NEW_USER=$($PSQL "INSERT INTO users (username) VALUES ('$USERNAME')")
USER_ID=$($PSQL "SELECT user_id FROM users WHERE username='$USERNAME'")
# insert new game
INSERT_NEW_GAME=$($PSQL "INSERT INTO games (user_id) VALUES ($USER_ID)")
GAME_ID=$($PSQL "SELECT MAX(game_id) FROM games;")
COUNT=1
# if not a number
if [[ $GUESS =~ ^[0-9]+$ ]]
then
GUESS_NUMBER $SECRET_NUMBER $GUESS $COUNT $USER_ID $GAME_ID
else
echo That is not an integer, guess again:
fi
# if user exists
else
# get number of games played
GAMES_PLAYED=$($PSQL "SELECT MAX(games_played) FROM games INNER JOIN users USING (user_id) WHERE user_id=$USER_ID")
# get best game of the user
BEST_GAME=$($PSQL "SELECT MIN(gd.number_of_guesses) FROM game_description gd INNER JOIN games g USING (game_id) INNER JOIN users u USING (user_id) WHERE u.user_id=$USER_ID")
# print welcome to user
echo -e "\nWelcome back, $USERNAME! You have played $GAMES_PLAYED games, and your best game took $BEST_GAME guesses."
# this is the guessing game
SECRET_NUMBER=$(echo $(( $RANDOM % 1000 + 1 )))
#echo $SECRET_NUMBER
echo -e "\nGuess the secret number between 1 and 1000:"
read GUESS
# insert new game
INSERT_NEW_GAME=$($PSQL "INSERT INTO games (user_id) VALUES ($USER_ID)")
GAME_ID=$($PSQL "SELECT MAX(game_id) FROM games;")
COUNT=1
# if not a number
if [[ $GUESS =~ ^[0-9]+$ ]]
then
GUESS_NUMBER $SECRET_NUMBER $GUESS $COUNT $USER_ID $GAME_ID
else
echo That is not an integer, guess again:
fi
fi
# not a valid input
else
echo "Please enter a valid username. Just 22 characters."
fi
}
GUESS_NUMBER(){
SECRET_NUMBER=$1
GUESS=$2
COUNT=$3
USER_ID=$4
GAME_ID=$5
while [[ $GUESS != $SECRET_NUMBER ]]
do
#echo "Guess #$COUNT"
if [[ $GUESS =~ ^[0-9]+$ ]]
then
if [[ $GUESS > $SECRET_NUMBER ]]
then
echo "It's lower than that, guess again:"
read GUESS
elif [[ $GUESS < $SECRET_NUMBER ]]
then
echo "It's higher than that, guess again:"
read GUESS
fi
COUNT=$(($COUNT+1))
else
echo "That is not an integer, guess again:"
read GUESS
fi
done
echo "You guessed it in $COUNT tries. The secret number was $SECRET_NUMBER. Nice job!"
#get amount of games played
GAMES_PLAYED=$($PSQL "SELECT MAX(games_played) FROM games WHERE user_id=$USER_ID")
# if no games played
if [[ -z $GAMES_PLAYED ]]
then
# insert first game
INSERT_GAMES_PLAYED=$($PSQL "UPDATE games SET games_played=1 WHERE game_id=$GAME_ID AND user_id=$USER_ID")
else
# insert the new game
GAMES_PLAYED=$(($GAMES_PLAYED+1))
INSERT_GAMES_PLAYED=$($PSQL "UPDATE games SET games_played=$GAMES_PLAYED WHERE user_id=$USER_ID AND game_id=$GAME_ID")
fi
# insert number of guesses
INSERT_NUMBER_OF_GUESSES=$($PSQL "INSERT INTO game_description (game_id, number_of_guesses) VALUES ($GAME_ID, $COUNT)")
}
MAIN_MENU
**#########SQL CODE / MY DATABASE#########**
--
-- 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: game_description; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.game_description (
game_id integer,
number_of_guesses integer
);
ALTER TABLE public.game_description OWNER TO freecodecamp;
--
-- Name: games; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.games (
game_id integer NOT NULL,
user_id integer,
games_played integer
);
ALTER TABLE public.games OWNER TO freecodecamp;
--
-- Name: games_game_id_seq; Type: SEQUENCE; Schema: public; Owner: freecodecamp
--
CREATE SEQUENCE public.games_game_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.games_game_id_seq OWNER TO freecodecamp;
--
-- Name: games_game_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: freecodecamp
--
ALTER SEQUENCE public.games_game_id_seq OWNED BY public.games.game_id;
--
-- Name: users; Type: TABLE; Schema: public; Owner: freecodecamp
--
CREATE TABLE public.users (
user_id integer NOT NULL,
username character varying(22),
game_id 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: games game_id; Type: DEFAULT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.games ALTER COLUMN game_id SET DEFAULT nextval('public.games_game_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: game_description; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
--
-- Data for Name: games; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
--
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
--
--
-- Name: games_game_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.games_game_id_seq', 1, false);
--
-- Name: users_user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: freecodecamp
--
SELECT pg_catalog.setval('public.users_user_id_seq', 1, false);
--
-- Name: games games_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.games
ADD CONSTRAINT games_pkey PRIMARY KEY (game_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: game_description game_description_game_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.game_description
ADD CONSTRAINT game_description_game_id_fkey FOREIGN KEY (game_id) REFERENCES public.games(game_id);
--
-- Name: games games_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.games
ADD CONSTRAINT games_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.users(user_id);
--
-- Name: users users_game_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: freecodecamp
--
ALTER TABLE ONLY public.users
ADD CONSTRAINT users_game_id_fkey FOREIGN KEY (game_id) REFERENCES public.games(game_id);
--
-- PostgreSQL database dump complete
--
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36 OPR/94.0.0.0
Challenge: Number Guessing Game - Build a Number Guessing Game
Link to the challenge: