-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctions.sh
More file actions
460 lines (419 loc) · 14.5 KB
/
functions.sh
File metadata and controls
460 lines (419 loc) · 14.5 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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#SOME ADDED AND CHANGE IN CLI learn_cli.sh in CLASSIC
shopt -s expand_aliases
source ~/.bashrc
function pause_music(){
kill -SIGTSTP $1
}
function start_quiz_music(){
if [[ "$MUTE" == "0" ]]; then
MUSIC_PID=$(ps -ef|grep "$SOUNDPLAYER_MUSIC"|grep Music|grep -v quiz|awk '{print $2}'|head -n 1)
if [[ "$MUSIC_PID" != "" ]]; then
pause_music $MUSIC_PID
fi
$SOUNDPLAYER_MUSIC_QUIZ /home/umen/.GameScript/Sounds/default/Music/quiz_1.mp3 &>/dev/null &
fi
}
function stop_quiz_music(){
if [[ "$MUTE" == "0" ]]; then
MUSIC_QUIZ_PID=$(ps -ef|grep "$SOUNDPLAYER_MUSIC_QUIZ"|grep quiz|awk '{print $2}'|head -n 1)
if [[ "$MUSIC_QUIZ_PID" != "" ]]; then
kill $MUSIC_QUIZ_PID
fi
fi
}
function download_all_sounds(){
cd $AUDIO_LOCAL || exit
i=4
rm to_dl.wget 2> /dev/null
echo "Downloading Audio..."
while [ $i -le $LINES ]; do
echo "$AUDIO_DL/$i.mp3" >> to_dl.wget
i=`expr $i + 1`
done
cat to_dl.wget | xargs -n 1 -P 4 wget -q &
}
function download_all_videos(){
cd $VIDEO_LOCAL || exit
i=4
rm to_dl.wget 2> /dev/null
echo "Downloading Videos..."
while [ $i -le $LINES ]; do
echo "$VIDEO_DL/$i.mp3.mp4" >> to_dl.wget
i=`expr $i + 1`
done
cat to_dl.wget | xargs -n 1 -P 4 wget -q &
}
function prepare_audio(){
AUDIO_LOCAL="$HOME/.GameScript/Audio/$LANGUAGE/classic/$CHAPTER_NAME/$SPEAKER/c$CHAPTER_NUMBER"
mkdir -p $AUDIO_LOCAL 2> /dev/null
AUDIO_DL="https://raw.githubusercontent.com/justUmen/GameScript/master/$LANGUAGE/classic/$CHAPTER_NAME/Audio/$SPEAKER/c$CHAPTER_NUMBER"
AUDIOCMP=1
if [[ "$MUTE" != 1 ]] && [[ "$VOICE" != "0" ]];then
if [ ! -f "$AUDIO_LOCAL/4.mp3" ]; then
wget -q --spider http://google.com
if [ $? -eq 0 ]; then
download_all_sounds
else
echo "Cannot download audio, no internet ?"
fi
fi
fi
}
function prepare_video(){
VIDEO_LOCAL="$HOME/.GameScript/Video/$LANGUAGE/classic/$CHAPTER_NAME/$SPEAKER/c$CHAPTER_NUMBER"
mkdir -p $VIDEO_LOCAL 2> /dev/null
VIDEO_DL="https://raw.githubusercontent.com/justUmen/GameScript/master/$LANGUAGE/classic/$CHAPTER_NAME/Video/$SPEAKER/c$CHAPTER_NUMBER"
VIDEOCMP=1
if [ ! -f "$VIDEO_LOCAL/4.mp3.mp4" ]; then
wget -q --spider http://google.com
if [ $? -eq 0 ]; then
download_all_videos
else
echo "Cannot download videos, no internet ?"
fi
fi
}
function encode_b64(){
echo -n "$2$1$3" | base64
}
function press_key(){
echo -en "\e[0;33m...\e[0m"
read -s -n1 key < /dev/tty
if [ "$key" == 'e' ]; then
exit
fi
}
#TODO ???
function new_sound(){
VOICE_PID=$(ps -f|grep "$SOUNDPLAYER"|grep -v grep|grep -v MUSIC|awk '{print $2}'|head -n 1)
if [[ "$VOICE_PID" != "" ]]; then
kill $VOICE_PID
fi
$SOUNDPLAYER "$AUDIO_LOCAL/$restore.mp3" &> /dev/null &
}
function new_video(){
VIDEO_PID=$(ps -ef|grep "mpv --really-quiet --input-ipc-server=/tmp/southpark"|grep -v grep|awk '{print $2}'|head -n 1)
if [[ "$VIDEO_PID" == "" ]]; then
rm /tmp/southpark
mpv --really-quiet --input-ipc-server=/tmp/southpark --no-config --include=~/.GameScript/mpv_config --loop=no $VIDEO_LOCAL/$restore.mp3.mp4 &
sleep 2
VIDEO_PID=$(ps -ef|grep "mpv --really-quiet --input-ipc-server=/tmp/southpark"|grep -v grep|awk '{print $2}'|head -n 1)
while [[ "$VIDEO_PID" == "" ]]; do
VIDEO_PID=$(ps -ef|grep "mpv --really-quiet --input-ipc-server=/tmp/southpark"|grep -v grep|awk '{print $2}'|head -n 1)
sleep 1
done
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
else
echo "{ \"command\": [\"loadfile\", \"$VIDEO_LOCAL/$restore.mp3.mp4\", \"replace\"] }" | socat - /tmp/southpark &> /dev/null
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
fi
}
function talk(){
if [[ $VIDEO == 0 ]]; then
if [[ $MUTE == 0 ]]; then
new_sound
fi
else
new_video
fi
echo -e "($restore)\e[0;32m $1\e[0m - $2"
#??? test delay avoid past read input
read -s -t 1 -n 10000 discard
#~ sleep 1
#~ while read -r -t 0; do read -r; done
#~ sleep 0.5
#~ read -s -e -t 0.1 #flush stdin ?
press_key
}
function talk_not_press_key(){
if [[ $VIDEO == 0 ]]; then
if [[ $MUTE == 0 ]]; then
new_sound
fi
else
new_video
fi
echo -e "($restore)\e[0;32m $1\e[0m - $2"
}
function talk_not_press_key_ANSWER(){
echo -en "($restore)\e[0;32m $1\e[0m - "
echo -ne "\\e[4;37m"
echo -nE "$2"
echo -e "\\e[0m"
}
function how_to_leave_chapter(){
echo ""
case $LANGUAGE in
fr) echo "Pour fermer ce chapitre, appuyez sur la touche 'e' quand vous voyez ces ... " ;;
en) echo "To close this chapter, press the key 'e' when you see these '...'" ;;
esac
echo ""
}
function answer_quiz(){
#$7 = bash, from enter_chapter
# echo " ---> $7 <--- "
echo -e " \\e[0;100m 1) \\e[0m $1"
echo -e " \\e[0;100m 2) \\e[0m $2"
echo -e " \\e[0;100m e) \\e[0m $3"
key="9"
while [ "$key" != "1" ] && [ "$key" != "2" ] && [ "$key" != "e" ]; do
echo -en " \\e[97;45m # \\e[0m"
read key < /dev/tty
done
# echo ""
#~ echo -e "\\e[0;100m 0) \\e[0m Télécharger audio en avance"
case $key in
0) if [[ $VIDEO == 0 ]]; then download_all_sounds; else download_all_videos; fi ;;
1) case $LANGUAGE in
fr) ANSWER_QUIZ_TEXT="$HOME/.GameScript/restore_$7$8 existe, continuer ou recommencer le cours du début ?"
TEXT_CONTINUE="Continuer"
TEXT_RESTART="Recommencer"
TEXT_BACK="Retour"
;;
en) ANSWER_QUIZ_TEXT="$HOME/.GameScript/restore_$7$8 already exists, continue or start lecture from the beginning ?"
TEXT_CONTINUE="Continue"
TEXT_RESTART="Restart"
TEXT_BACK="Back"
;;
esac
if [ -f "$HOME/.GameScript/restore_$7$8" ];then
echo "$ANSWER_QUIZ_TEXT"
echo -e " \\e[0;100m 1) \\e[0m $TEXT_CONTINUE"
echo -e " \\e[0;100m 2) \\e[0m $TEXT_RESTART"
echo -e " \\e[0;100m e) \\e[0m $TEXT_BACK"
choice="x"
while [ "$choice" != "1" ] && [ "$choice" != "2" ] && [ "$choice" != "e" ]; do
echo -en " \\e[97;45m # \\e[0m"
read choice < /dev/tty
done
case $choice in
1) cd `cat "$HOME/.GameScript/restore_pwd_$CHAPTER_NAME$CHAPTER_NUMBER"`
how_to_leave_chapter
start_lecture `cat "$HOME/.GameScript/restore_$CHAPTER_NAME$CHAPTER_NUMBER"`
start_quiz
;;
2) clean
how_to_leave_chapter
start_lecture 1
start_quiz
;;
e) exit ;;
esac
#~ done
fi
start_lecture 1
start_quiz
;;
2) start_quiz ;;
e) exit ;;
esac
#~ done
}
QUIZ_NUMBER=1
function answer_text_fr(){
echo ""
echo "($QUIZ_NUMBER) > $1"
#~ echo -en "\\e[97;45m # \\e[0m"
read -e -r -p $'\e[97;45m # \e[0m' USER_CODE < /dev/tty
if [ ! "$USER_CODE" == "$2" ]; then
case $LANGUAGE in
fr) talk_not_press_key justumen "\\e[4;37mDésolé, réponse fausse ou trop longue. Je vous conseille de suivre / refaire le cours.\nSi vous pensez maitriser le contenu du cours, il y a surement un piège, relisez donc attentivement la question. :-)\nSi vous vous sentez vraiment bloqué, demandez de l'aide sur notre chat : https://rocket.bjornulf.org ou notre discord : https://discord.gg/25eRgvD\\e[0m"
press_key
stop_quiz_music
#enter_chapter $CHAPTER_NAME $CHAPTER_NUMBER
exit ;;
en) talk_not_press_key justumen "\\e[4;37mSorry answer wrong or too long. I recommend you to do / re-do the lecture :-)\nIf you think you already understand the content of the lecture, this question is probably a trap, read the question again carefully. :-)\nIf you feel stuck, ask for help on our discord : https://discord.gg/Dj47Tpf\\e[0m"
#enter_chapter $CHAPTER_NAME $CHAPTER_NUMBER
press_key
stop_quiz_music
exit ;;
esac
else
talk_not_press_key justumen "Correct !"
QUIZ_NUMBER=`expr $QUIZ_NUMBER + 1`
fi
}
function answer_run(){
#~ echo -en "\\e[97;45m # \\e[0m"
read -e -r -p $'\e[97;45m # \e[0m' USER_CODE < /dev/tty
while [ ! "$USER_CODE" == "$1" ]; do
if [ ! "$USER_CODE" == "" ]; then
talk_not_press_key_ANSWER "$2" "$1"
fi
#~ echo -en "\\e[97;45m # \\e[0m"
read -e -r -p $'\e[97;45m # \e[0m' USER_CODE < /dev/tty
done
if [ ! "$1" == "" ];then
echo -e "\e[1m"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
#Quick solve for bash10 pb "alias a1='ls -a /var'" ???
#~ case $1 in
#~ a1) eval ls -a /var ;;
#~ alias) alias ;;
#~ *) eval "$1" ;;
#~ esac
eval "$1"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo -e "\\e[0m"
fi
}
function start_dots(){
echo -en "\e[0;33m...\e[0m"
}
function title(){
echo -e "\e[1m"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo "$1"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
echo -e "\\e[0m"
}
black_on_green='\e[30;48;5;82m'
black_on_lightblue='\e[30;48;5;81m'
black_on_red='\e[41m'
On_Black='\e[97;40m'
reset='\e[0m'
basic=$On_Black
code=$black_on_lightblue
codeFile=$black_on_green
codeError=$black_on_red
#UNDERLINE
#~ voc='\e[1m'
voc='\e[4;37m'
#BLUE
learn='\e[40;38;5;10m'
MUTE=0
if [ "$1" == "MUTE" ]; then
MUTE=1
fi
VIDEO=0
if [ "$1" == "VIDEO" ]; then
VIDEO=1
fi
#~ command -v mplayer &> /dev/null && SOUNDPLAYER="mplayer -af volume=10" || SOUNDPLAYER="mpg123 --scale 100000";
#SET in gamescript.sh too (need to be similar)
command -v mplayer &> /dev/null && SOUNDPLAYER="mplayer -volume 100" || SOUNDPLAYER="mpg123";
command -v mplayer &> /dev/null && SOUNDPLAYER_MUSIC="mplayer -volume 35" || SOUNDPLAYER_MUSIC="mpg123 --scale 11445"
command -v mplayer &> /dev/null && SOUNDPLAYER_MUSIC_QUIZ="mplayer -volume 50" || SOUNDPLAYER_MUSIC_QUIZ="mpg123"
#OBSOLETE ?
#~ restore=2 #first line of LIST_4GEN should be environment test (test ~/House)
restore=1
function new_game_or_restore(){
#restore, 1 new game, otherwise x in file PERSONAL_PROGRESS
restore=1
if [ -e "~/PERSONAL_PROGRESS" ]; then
restore=`cat ~/PERSONAL_PROGRESS`
else
echo -n "1" > ~/PERSONAL_PROGRESS
fi
if [ "$1" ];then
restore=`expr $1 - 1`
echo -n "$1" > ~/PERSONAL_PROGRESS
fi
}
#~ function normal_line(){
#~ restore=$1
#~ play_mp3 "$restore"
#~ print_line "$line"
#~ press_key
#~ }
function play_mp3(){
if [ -e "sounds/$1.mp3" ];then
mpg123 "sounds/$1.mp3" > /dev/null 2>&1 &
fi
}
function event(){
echo -e "\\e[0;100m $1\\e[0m"
press_key
#~ restore=$(expr $restore + 1)
}
function unlock(){
#Usage : unlock "bash" "1" "24d8" "f016"
PSEUDO=`cat "$HOME/.GameScript/username"`
PASS=`encode_b64 $PSEUDO "$3" "$4"`
echo ""
case $LANGUAGE in
fr) echo -e "Pour débloquer '$1 $2' sur rocketchat (https://rocket.bjornulf.org), ouvrez une conversation privée avec '@boti' et copiez/collez :\n\t\e[97;42mpassword$PASS\e[0m"
echo -e "Pour débloquer '$1 $2' sur discord (https://discord.gg/25eRgvD), ouvrez le channel '#mots-de-passe-boti' et copiez/collez :\n\t\e[97;42mpassword$PASS\e[0m"
command -v xclip &> /dev/null && echo "Ce mot de passe a été copié automatiquement avec 'xclip'." || echo "[ Installez 'xclip' pour copier ce mot de passe automatiquement après un questionnaire. ]"
;;
en) #echo -e "To unlock '$1 $2' on rocketchat (https://rocket.bjornulf.org), open a private conversation with '@boti' and copy/paste :\n\t\e[97;42mpassword$PASS\e[0m"
echo -e "To unlock '$1 $2' on discord (https://discord.gg/Dj47Tpf), open the channel '#passwords-boti' and copy/paste :\n\t\e[97;42mpassword$PASS\e[0m"
command -v xclip &> /dev/null && echo "This password was automaticaly copied with 'xclip'." || echo "[ Install 'xclip' to copy this password automaticaly after a quiz. ]"
;;
esac
command -v xclip &> /dev/null && echo "$PASS" | xclip -i -selection clipboard
#AUTOMATICALLY DO THIS ?
touch "$HOME/.GameScript/good_$1$2" 2> /dev/null
mkdir $HOME/.GameScript/passwords/ 2> /dev/null
echo -n "$PASS" > "$HOME/.GameScript/passwords/$1$2"
press_key
echo ""
exit
}
function enter_chapter(){
#Usage : enter_chapter bash 1 1 (first 1 is chapter, next one is for case)
echo ""
case $LANGUAGE in
fr) echo -e " \e[97;44m - $1, Chapitre $2 \e[0m"
answer_quiz "Cours" "Questionnaire" "Retour" "4" "5" "6" "$1" "$2" ;;
en) echo -e " \e[97;44m - $1, Chapter $2 \e[0m"
answer_quiz "Lecture" "Quiz" "Back" "4" "5" "6" "$1" "$2" ;;
esac
}
#VIDEO SOUTH PARK
function UNTIL_IDLE_IS_BACK(){
OUT=$(echo '{ "command": ["get_property", "filename"] }' | socat - /tmp/southpark)
#~ echo $OUT
while [ "$OUT" != '{"data":"10FPS_idle.mp4","error":"success"}' ]; do
OUT=$(echo '{ "command": ["get_property", "filename"] }' | socat - /tmp/southpark)
sleep 1
done
}
function PAUSE(){
echo '{ "command": ["set_property", "pause", true] }' | socat - /tmp/southpark &> /dev/null
}
function UNPAUSE(){
echo '{ "command": ["set_property", "pause", false] }' | socat - /tmp/southpark &> /dev/null
}
function LOOP_ON(){
echo '{ "command": ["set_property", "loop", true] }' | socat - /tmp/southpark &> /dev/null
}
function LOOP_OFF(){
echo '{ "command": ["set_property", "loop", false] }' | socat - /tmp/southpark &> /dev/null
}
function PLAYLIST_NEXT(){
echo '{ "command": ["playlist-next"] }' | socat - /tmp/southpark &> /dev/null
}
function PLAYLIST_CLEAR(){
echo '{ "command": ["playlist-clear"] }' | socat - /tmp/southpark &> /dev/null
}
function ADD_PLAYLIST(){
echo "{ \"command\": [\"loadfile\", \"$1\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
sleep 0.5
echo "{ \"command\": [\"loadfile\", \"$HOME/.GameScript/10FPS_idle.mp4\", \"append\"] }" | socat - /tmp/southpark &> /dev/null
}
function WAIT_FOR_USER(){
echo -n "LINE = "
read line
}
#WORKING TEST CODE
#~ mpv --really-quiet --input-ipc-server=/tmp/southpark --playlist mpv_playlist.txt &
#~ sleep 2
#~ LOOP_ON #play idle
#~ while true; do
#~ WAIT_FOR_USER
#~ PLAYLIST_NEXT #play next video
#~ echo "PLAY VIDEO"
#~ LOOP_OFF
#~ UNTIL_IDLE_IS_BACK
#~ echo "BACK"
#~ echo "PLAY IDLE"
#~ LOOP_ON
#~ done