Created
December 20, 2020 23:07
-
-
Save flexiondotorg/78217dae9737b8f1c8ce1bb303cd151c to your computer and use it in GitHub Desktop.
Hotshot Racing Standings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# The format of the input data file is as follows: | |
# | |
# Name, Emoji Country Flag, Space seperated race finishes | |
# | |
# For example: | |
# | |
# Wimpy,π¬π§,1 2 3 3 4 | |
# popey,π¬π§,2 3 1 2 2 | |
# frenchguych,π¨π,3 1 2 1 1 | |
# bigcalm,π¬π§,8 7 6 7 0 | |
# TwoD,πΈπͺ,5 4 8 0 7 | |
# AndCatchFire,π¬π§,4 8 4 6 5 | |
DATA_IN="hotshot-racing-grand-prix.txt" | |
DATA_POINTS="/tmp/hotshot-racing-points.txt" | |
DATA_SORTED="/tmp/hotshot-racing-points-sorted.txt" | |
OBS_TEMP="/tmp/hotshot-racing-standings.txt" | |
OBS_TEXT="${HOME}/Studio/OBS/hotshot-racing-standings.txt" | |
CURRENT_MD5="" | |
while true; do | |
CHECK_MD5=$(md5sum "${DATA_IN}" | cut -f1 -d' ') | |
if [ "${CURRENT_MD5}" != "${CHECK_MD5}" ]; then | |
CURRENT_MD5="${CHECK_MD5}" | |
echo -e "Pos.\tPt.\tDriver" > "${OBS_TEMP}" | |
echo >> "${OBS_TEMP}" | |
echo -n > "${DATA_POINTS}" | |
while IFS=, read -r DRIVER FLAG POSITIONS; do | |
RACES=0 | |
POINTS=0 | |
PLACING=0 | |
PODIUM="" | |
# Calcualte points | |
# Points are based on Hotshot Racing Grand prix | |
for POSITION in ${POSITIONS}; do | |
((RACES++)) | |
# Calculate placing weights; lower is better | |
if [ ${POSITION} -eq 0 ]; then | |
((PLACING+=10)) | |
else | |
((PLACING+=${POSITION})) | |
fi | |
case ${POSITION} in | |
0) PODIUM+="β";; | |
1) ((POINTS+=12)) | |
PODIUM+="π₯";; | |
2) ((POINTS+=11)) | |
PODIUM+="π₯";; | |
3) ((POINTS+=10)) | |
PODIUM+="π₯";; | |
4) ((POINTS+=9)) | |
PODIUM+="π";; | |
5) ((POINTS+=8)) | |
PODIUM+="π";; | |
6) ((POINTS+=7)) | |
PODIUM+="π";; | |
7) ((POINTS+=6)) | |
PODIUM+="π";; | |
8) ((POINTS+=5)) | |
PODIUM+="π±";; | |
esac | |
done | |
echo -e "${PLACING}\t${POINTS}\t${DRIVER}\t"${FLAG}"\t${PODIUM}" >> "${DATA_POINTS}" | |
done < "${DATA_IN}" | |
# Sort by points then by best placings in all races. | |
sort -k2nr -k1n -k3 "${DATA_POINTS}" > "${DATA_SORTED}"> "${DATA_SORTED}" | |
# Add emoji | |
PREV_POINTS=0 | |
PREV_PLACING=0 | |
PREV_MEDAL=0 | |
while read LINE; do | |
PLACING=$(echo "${LINE}" | cut -d$'\t' -f1) | |
POINTS=$(echo "${LINE}" | cut -d$'\t' -f2) | |
DRIVER=$(echo "${LINE}" | cut -d$'\t' -f3) | |
FLAG=$(echo "${LINE}" | cut -d$'\t' -f4) | |
PODIUM=$(echo "${LINE}" | cut -d$'\t' -f5) | |
# Pad points to always be double digits. | |
if [ ${POINTS} -le 9 ]; then | |
POINTS=$(printf "%02d\n" ${POINTS}) | |
fi | |
if [ ${PREV_MEDAL} -eq 0 ]; then | |
EMOJI="π₯" | |
PREV_MEDAL=1 | |
elif [ ${PREV_MEDAL} -eq 1 ] && [ "${PREV_POINTS}" -eq "${POINTS}" ] && [ "${PREV_PLACING}" -eq "${PLACING}" ]; then | |
EMOJI="π₯" | |
PREV_MEDAL=1 | |
elif [ ${PREV_MEDAL} -eq 1 ] && [ ${PREV_POINTS} -ne ${POINTS} ]; then | |
EMOJI="π₯" | |
PREV_MEDAL=2 | |
elif [ ${PREV_MEDAL} -eq 2 ] && [ "${PREV_POINTS}" -eq "${POINTS}" ] && [ "${PREV_PLACING}" -eq "${PLACING}" ]; then | |
EMOJI="π₯" | |
PREV_MEDAL=2 | |
elif [ ${PREV_MEDAL} -eq 2 ] && [ ${PREV_POINTS} -ne ${POINTS} ]; then | |
EMOJI="π₯" | |
PREV_MEDAL=3 | |
elif [ ${PREV_MEDAL} -eq 3 ] && [ "${PREV_POINTS}" -eq "${POINTS}" ] && [ "${PREV_PLACING}" -eq "${PLACING}" ]; then | |
EMOJI="π₯" | |
PREV_MEDAL=3 | |
elif [ ${PREV_MEDAL} -ge 3 ]; then | |
EMOJI="π" | |
((PREV_MEDAL++)) | |
elif [ ${PREV_MEDAL} -eq 7 ]; then | |
EMOJI="π±" | |
fi | |
# Don't show podium finishes if only one race has taken place. | |
if [ ${RACES} -eq 1 ]; then | |
PODIUM="" | |
fi | |
if [ "${#DRIVER}" -gt 8 ]; then | |
echo -e "${EMOJI}\t${POINTS}\t${FLAG} ${DRIVER}\t${PODIUM}" >> "${OBS_TEMP}" | |
elif [ "${#DRIVER}" -gt 6 ]; then | |
echo -e "${EMOJI}\t${POINTS}\t${FLAG} ${DRIVER}\t\t${PODIUM}" >> "${OBS_TEMP}" | |
else | |
echo -e "${EMOJI}\t${POINTS}\t${FLAG} ${DRIVER}\t\t\t${PODIUM}" >> "${OBS_TEMP}" | |
fi | |
PREV_POINTS=${POINTS} | |
PREV_PLACING=${PLACING} | |
done < "${DATA_SORTED}" | |
mv "${OBS_TEMP}" "${OBS_TEXT}" | |
sync | |
clear | |
cat "${OBS_TEXT}" | cut -d$'\t' -f1-3 | |
fi | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment