/
decider.sh
executable file
·375 lines (327 loc) · 7.61 KB
/
decider.sh
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/bash
# Enable unofficial bash strict mode
# More info: http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
IFS=$'\n\t'
mode="${1:-alpha}"
option="${2:-alpha}"
# Config
db_name="options.db"
db_path=$(dirname "$0")
db="$db_path/$db_name"
# Default settings
default_allow_skip="Off" # Allow or disallow skipping of all options
default_option_count="3"
default_vote_threshold="0" # Vote score an option needs to have to be picked
default_compared_threshold="10" # Number of rounds an option needs to have to been picked before vote threshold is counted
### Functions
# program flow functions
function check_dependencies() {
command -v sqlite3 >/dev/null 2>&1 || {
echo >&2 "sqlite3 is required, but it's not installed. Aborting.";
exit 1;
}
}
function check_input() {
case $mode in
add)
# Add new option to DB
add_option "$option"
;;
batchadd)
# Add new options from text file
batch_add "$option"
;;
remove)
# Remove option from DB
remove_option "$option"
;;
print)
# Show DB entries
print_db
;;
start)
# Start decider
decider
;;
hiscore)
# Show best voted options
hiscore
;;
*)
# Show Main Menu
main_menu
;;
esac
}
function main_menu() {
headline "Decider Main Menu"
echo "[1] Start decider"
echo "[2] Show options"
echo "[3] Add new option"
echo "[4] Remove option"
echo "[5] Show Top 10"
echo "[6] Show Bottom 10"
echo ""
echo "[o] Change settings"
echo "[q] Quit"
read_decision
case $decision in
1)
decider
;;
2)
print_db
;;
3)
echo -e "\nEnter new option:"
read -r option
add_option "$option"
;;
4)
echo -e "\nOption to delete:"
read -r option
remove_option "$option"
;;
5)
hiscore
;;
6)
hiscore asc
;;
o)
change_settings
;;
q)
exit 0;
;;
*)
clear
;;
esac
main_menu
}
function ask_options() {
vote_threshold="${global_vote_threshold:-${default_vote_threshold}}"
compared_threshold="${global_compared_threshold:-${default_compared_threshold}}"
allow_skip="${global_allow_skip:-${default_allow_skip}}"
options=$(sql query "select option from options where vote > $vote_threshold or compared < $compared_threshold order by random() limit $1")
possible_options=$(sql query "select option from options where vote > $vote_threshold or compared < $compared_threshold")
possible_options_left=$(echo "$possible_options"|wc -w)
# Gets n Options from the DB
headline "Select best option!"
if [[ "$possible_options_left" == "1" ]]; then
echo "Option $options has won!"
hiscore
exit 0
else
echo -e "There are $possible_options_left options left to choose from.\n"
fi
local loop="1"
unset option_array
declare -a option_array
for option in $options;
do
i=$((loop++))
echo "[$i] $option"
option_array[$i]="$option"
done
if [[ "$allow_skip" == "On" ]]; then
echo -e "\n[x] Skip and downvote all"
fi
echo -e "\n[m] Main menu"
echo -e "[q] Quit"
read_decision
case $decision in
q)
exit 0;
;;
x)
write_decision "SKIP" "${option_array[*]}"
;;
m)
main_menu
;;
[1-$i])
best=${option_array[$decision]}
write_decision "$best" "${option_array[*]}"
;;
*)
echo "Press Button 1-$i to choose the best option!"
sleep 5
;;
esac
}
# Database operation functions
function create_db() {
# Open DB and if it doen't exist, create it
if [ ! -f "$db" ]; then
echo "Database not found!"
sql query "create table options (id INTEGER PRIMARY KEY,option TEXT UNIQUE,vote INTEGER,compared INTEGER);"
fi
}
function sql() {
local query="$2"
local mode="$1"
local options=""
case $mode in
list)
options="-line"
;;
query)
options=""
;;
column)
options="-column -header"
;;
esac
execute="sqlite3 ${options} ${db} \"${query}\""
eval "$execute"
}
function write_decision() {
# Add deciscion to DB
best="$1"
rest="$2"
for i in $rest; do
if [[ "$i" == "$best" ]]; then
sql query 'update options set vote = vote + 1 where option='"'$i'"''
else
sql query 'update options set vote = vote - 1 where option='"'$i'"''
fi
sql query 'update options set compared = compared + 1 where option='"'$i'"''
done
}
# Helper functions
function headline() {
local text="$1"
local text_length
text_length="$(echo \"${text}\"|wc -m)"
echo -e "\n$text"
seq -s= "$text_length"|tr -d '[:digit:]'
echo ""
}
function sanitize() {
lowercase=$(echo "$1"|tr '[:upper:]' '[:lower:]')
trimmed=$(echo "$lowercase"|tr -d '[:blank:]')
alpha=$(echo "$trimmed"|tr -cd '[:alpha:]')
capitalize="$(tr '[:lower:]' '[:upper:]' <<< \"${alpha:0:1})${alpha:1}\""
echo "${capitalize}"
}
function read_decision() {
read -r -s -n 1 decision
}
# Program modes
function decider() {
clear
option_count="${global_option_count:-${default_option_count}}"
ask_options "$option_count"
decider
}
function add_option() {
option=$(sanitize "$1")
echo "Add option $option"
query=$(sql query 'select option from options where option='"'$option'"'')
if [[ -z "$query" && ${#option} -ge 3 ]]; then
sql query 'insert into options (option,vote,compared) values ('"'$option'"',0,0);'
else
echo "Skipping, $option already in DB"
fi
}
function remove_option() {
option=$(sanitize "$1")
echo "Remove option $option"
sql query 'delete from options where option='"'$option'"''
}
function hiscore() {
orderby="${1:-desc}"
if [[ "$orderby" == "desc" ]]; then
headline "Top 10 options"
echo -e "Best voted options\n"
else
headline "Bottom 10 option"
echo -e "Worst voted options\n"
fi
sql column "select vote,option,compared from options order by vote $orderby limit 10"
}
function batch_add() {
# Add options from file
if [ ! -f "$1" ]; then
echo "File $1 not found!"
exit 1
else
echo "File is $1"
while read -r option; do
add_option "$option"
done < "$1"
fi
}
function print_db() {
headline "Database content"
sql column "select option,vote,compared from options order by option"
echo ""
entries=$(sql query "select count(option) from options")
echo "Number of options in database: $entries"
}
function change_settings() {
allow_skip="${global_allow_skip:-${default_allow_skip}}"
option_count="${global_option_count:-${default_option_count}}"
vote_threshold="${global_vote_threshold:-${default_vote_threshold}}"
compared_threshold="${global_compared_threshold:-${default_compared_threshold}}"
headline "Change settings"
echo "[1] Number of voting options: $option_count"
echo "[2] Vote threshold: $vote_threshold"
echo "[3] Compare threshold: $compared_threshold"
echo "[4] Allow skipping: $allow_skip"
echo ""
echo -e "[m] Main Menu\n"
echo -e "\nSelect setting to change:"
read_decision
case $decision in
1)
echo "Number of options to choose from?"
read_decision
if [[ "$decision" =~ ^[2-9]+$ ]]; then
global_option_count="${decision}"
else
echo "Value must be between 2 and 9!"
fi
;;
2)
echo "How many votes needs an option to be picked after it reached the compared threshold?"
read -r decision
if [[ "$decision" =~ ^-?[0-9]+$ ]]; then
global_vote_threshold="${decision}"
else
echo "Value must be a number!"
fi
;;
3)
echo "How many times must an option have been picked before the vote threshold counts?"
read -r decision
if [[ "$decision" =~ ^-?[0-9]+$ ]]; then
global_compared_threshold="${decision}"
else
echo "Value must be a number!"
fi
;;
4)
echo "Allow skipping of options? Skipping will downvote all presented options."
echo "[1] Skipping Off"
echo "[2] Skipping On"
read_decision
if [[ "$decision" = "2" ]]; then
global_allow_skip="On"
else
global_allow_skip="Off"
fi
;;
m)
main_menu
;;
esac
change_settings
}
check_dependencies
create_db
check_input
exit 0