-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrueOrFalse.sh
More file actions
103 lines (83 loc) · 2.55 KB
/
trueOrFalse.sh
File metadata and controls
103 lines (83 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
mRANDOM=4096
responses=( "Perfect!" "Awesome!" "You are a genius!" "Wow!" "Wonderful!" )
score=10
count=0
date=$(date +%F)
# authenticates user
echo -e "Welcome to the True or False Game!\n"
user_data=$(curl -s http://0.0.0.0:8000/download/file.txt)
user=$( echo "$user_data" | sed 's/.*"username": *"\{0,1\}\([^,"]*\)"\{0,1\}.*/\1/')
password=$( echo "$user_data" | sed 's/.*"password": *"\{0,1\}\([^,"]*\)"\{0,1\}.*/\1/')
curl http://0.0.0.0:8000/login -u "${user,,}:$password" -sc cookie.txt &>/dev/null
# prints menu
menu() {
echo "0. Exit"
echo "1. Play a game"
echo "2. Display scores"
echo "3. Reset scores"
echo "Enter an option:"
read -r option
}
play() {
while true;
# pulls cookie and parses question and answer
do
final_score=$(("$score" * "$count"))
response=$( curl http://0.0.0.0:8000/game -sb cookie.txt)
question=$( echo "$response" | sed 's/.*"question": *"\{0,1\}\([^,"]*\)"\{0,1\}.*/\1/')
answer=$( echo "$response" | sed 's/.*"answer": *"\{0,1\}\([^,"]*\)"\{0,1\}.*/\1/')
# logic for verifying correctness
echo "$question"
echo "True or False?" && read -r answer_user
if [ "$answer" == "$answer_user" ]; then
((count++))
indx=$((RANDOM%5))
echo "${responses[$indx]}"
else
echo "Wrong answer, sorry!"
echo "$name you have $count correct answer(s)."
echo "Your score is $(( "$score" * "$count" )) points."
printf "User: %s, Score: %d, Date: %s\n" "$name" "$final_score" "$date" >> scores.txt
count=0
break;
fi
done
}
# infinite loop menu and options until '0' or exit
while true;
do
menu
case $option in
0 | 'quit')
echo "See you later!"
break;
;;
# starts the game
1)
echo "What is your name?" && read -r name
play;;
# prints player scores
2)
FILE=`find . -name 'scores.txt' -print -quit`
if [ -n "$FILE" ]; then
echo "Player scores"
cat scores.txt
else
echo "File not found or no scores in it!"
fi
;;
# Reset the scores
3)
FILE=`find . -name 'scores.txt' -print -quit`
if [ -n "$FILE" ]; then
rm . scores.txt
echo "File deleted successfully!"
else
echo "File not found or no scores in it!"
fi
;;
# all other input
*)
echo -e "Invalid option!\n";;
esac
done