Solution
This commit is contained in:
parent
41d02b67b3
commit
612c9c8289
46
number_guess.sh
Normal file
46
number_guess.sh
Normal file
@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
|
||||
PSQL="psql -X --username=freecodecamp --dbname=number_guess --no-align --tuples-only -c"
|
||||
RANDOM_NUMBER=$(($RANDOM % 1000 + 1))
|
||||
echo "Enter your username:"
|
||||
read USERNAME
|
||||
|
||||
USER_QUERY=$($PSQL "SELECT username, total_games_played, best_score FROM users WHERE username='$USERNAME'")
|
||||
|
||||
if [[ -z $USER_QUERY ]]
|
||||
then
|
||||
echo -e "Welcome, $USERNAME! It looks like this is your first time here."
|
||||
TOTAL=0
|
||||
BEST=9999999
|
||||
ADD_USER=$($PSQL "INSERT INTO users(username, total_games_played) VALUES('$USERNAME', $TOTAL)")
|
||||
else
|
||||
IFS="|" read USERNAME TOTAL BEST <<< $USER_QUERY
|
||||
echo -e "Welcome back, $USERNAME! You have played $TOTAL games, and your best game took $BEST guesses."
|
||||
fi
|
||||
|
||||
echo "Guess the secret number between 1 and 1000:"
|
||||
NO_OF_GUESSES=0
|
||||
while [[ true ]]
|
||||
do
|
||||
read GUESS
|
||||
if [[ $GUESS =~ ^[0-9]+$ ]]
|
||||
then
|
||||
if [[ $GUESS > $RANDOM_NUMBER ]]
|
||||
then
|
||||
((NO_OF_GUESSES++))
|
||||
echo -e "It's lower than that, guess again:"
|
||||
elif [[ $GUESS < $RANDOM_NUMBER ]]
|
||||
then
|
||||
((NO_OF_GUESSES++))
|
||||
echo -e "It's higher than that, guess again:"
|
||||
else
|
||||
((NO_OF_GUESSES++))
|
||||
echo -e "You guessed it in $NO_OF_GUESSES tries. The secret number was $RANDOM_NUMBER. Nice job!"
|
||||
break
|
||||
fi
|
||||
else
|
||||
echo -e "That is not an integer, guess again:"
|
||||
fi
|
||||
done
|
||||
|
||||
UPDATE_USER_STATS=$($PSQL "UPDATE users SET total_games_played=$(($TOTAL + 1)), best_score=$(($BEST < $NO_OF_GUESSES ? $BEST: $NO_OF_GUESSES)) WHERE username='$USERNAME'")
|
86
number_guess.sql
Normal file
86
number_guess.sql
Normal file
@ -0,0 +1,86 @@
|
||||
--
|
||||
-- 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 (
|
||||
username character varying(100) NOT NULL,
|
||||
total_games_played integer,
|
||||
best_score integer
|
||||
);
|
||||
|
||||
|
||||
ALTER TABLE public.users OWNER TO freecodecamp;
|
||||
|
||||
--
|
||||
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: freecodecamp
|
||||
--
|
||||
|
||||
INSERT INTO public.users VALUES ('dfsd', 1, 0);
|
||||
INSERT INTO public.users VALUES ('user_1657093016203', 2, 235);
|
||||
INSERT INTO public.users VALUES ('user_1657093016204', 5, 243);
|
||||
INSERT INTO public.users VALUES ('xyz', 11, 513);
|
||||
INSERT INTO public.users VALUES ('user_1657093329259', 2, 186);
|
||||
INSERT INTO public.users VALUES ('user_1657093329260', 5, 279);
|
||||
INSERT INTO public.users VALUES ('user_1657093399112', 2, 468);
|
||||
INSERT INTO public.users VALUES ('user_1657093399113', 5, 236);
|
||||
INSERT INTO public.users VALUES ('user_1657093473136', 2, 807);
|
||||
INSERT INTO public.users VALUES ('user_1657093473137', 5, 176);
|
||||
|
||||
|
||||
--
|
||||
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: freecodecamp
|
||||
--
|
||||
|
||||
ALTER TABLE ONLY public.users
|
||||
ADD CONSTRAINT users_pkey PRIMARY KEY (username);
|
||||
|
||||
|
||||
--
|
||||
-- PostgreSQL database dump complete
|
||||
--
|
||||
|
Loading…
Reference in New Issue
Block a user