lorchess/schedule

191 lines
5.2 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
# Copyright 2014 Vladimir Ivanov <ivvl82@gmail.com>
# Distributed under the terms of the GNU General Public License v2
REPO=fat0troll/lorchess
TOURNAMENT=2014/1-tabiyas
2013-09-23 16:04:10 +04:00
# Variables
date_re="[0-9?]{2}\\.[0-9?]{2}\\.[0-9?]{4}"
res_re="(0|1|0\\.5):(0|1|0\\.5)"
2013-09-23 16:04:10 +04:00
show_tour_sequence() {
local sequence info_array
# Generate the sequence of tours to search in
if [[ -z "$@" ]]; then
sequence=$(seq -f "%02g" 1 99)
else
# Change tour numbers: '1' -> '01', '2' -> '02', and so on
sequence=$(for i in "$@"; do printf "%02g " "$i"; done)
fi
for tour in $sequence; do
# Produce output only if 'tour_info' exists
if fetch_info_array "$tour"; then
for ((i=5; i<${#info_array[@]}; ++i)); do
if keep_info_line "${info_array[$i]}"; then
show_tour_info
break
fi
done
else
# If the tour numbers were not passed as arguments, exit
# once the first non-existing 'tour_info' is reached
[[ -z "$@" ]] && break
fi
done
}
fetch_info_array() {
local url="https://raw.github.com/${REPO}/master/${TOURNAMENT}/tours/${1}/tour_info"
# Store 'tour_info' in an array of lines.
# *NOTE* The incorporation of a newline at the end of 'tour_info'
# (--write-out '\n') is important and allows one to read the last
# line with no trailing '\n'
info_array=()
while read line; do
info_array+=("$line")
done <<< "$(curl --fail -q --silent --write-out '\n' $url)"
return `[[ -n $info_array ]]`
}
keep_info_line() {
local line="$1" keep=1
if $SHOW_ALL; then
keep=0
else
# Keep line if the game is not finished
if [[ -z $(egrep "$res_re" <<< "$line") ]]; then
if [[ -z "$PLAYER" ]]; then
keep=0
# In addition, if the player was passed as an argument,
# check if it is his game or not
elif [[ -n $(grep -o "$PLAYER" <<< "$line") ]]; then
keep=0
fi
fi
fi
return $keep
}
show_tour_info() {
[[ "${info_array[0]}" =~ "Тур №"([0-9]+) ]]
local tour_num="${BASH_REMATCH[1]}"
[[ "${info_array[3]}" =~ ($date_re)" "*[-—]" "*($date_re) ]]
local date_beg="${BASH_REMATCH[1]}" date_end="${BASH_REMATCH[2]}"
# Use short dates
date_beg="${date_beg:0:5}"; date_end="${date_end:0:5}"
# Lines with game info
local output_lines=""
for ((i=5; i<${#info_array[@]}; ++i)); do
local line="${info_array[$i]}"
if keep_info_line "$line"; then
[[ "$line" =~ ($date_re)" "*[-—]" "*([^" "]+)" "*($res_re)?" "*([^" "]+) ]]
local white="${BASH_REMATCH[2]}" black="${BASH_REMATCH[6]}" result
case "${BASH_REMATCH[3]}" in
1:0) result="1-0";;
0:1) result="0-1";;
0.5:0.5) result="1/2";;
*) result="";;
esac
output_lines+="${white} ${black} ${result}\n"
fi
done
eval "info_output_$FORMAT \"${output_lines}\""
}
2013-10-20 15:30:02 +04:00
info_output_term() {
echo -n "$(tput setaf 2)${tour_num} тур "
echo "$(tput setaf 6)(${date_beg} - ${date_end})$(tput sgr0)"
echo -en "$1" | column -t | while IFS= read line; do
# Highlight player's name
if [[ -n "$PLAYER" ]]; then
players=$(grep -o "[^ ]*${PLAYER}[^ ]*" <<< "$line")
for name in $players; do
line=$(sed "s/${name}/\\$(tput setaf 1)\0\\$(tput sgr0)/g" <<< "$line")
done
fi
# Highlight result
line=$(sed "s/\(1-0\|0-1\|1\\/2\)$/\\$(tput setaf 6)\0\\$(tput sgr0)/g" <<< "$line")
echo "$line"
done
echo
}
info_output_lor() {
echo "[b]${tour_num} тур (${date_beg} - ${date_end})[/b]"
echo "[list]"
echo -en "$1" | while read white black result; do
local line=" [*] [user]${white}[/user] - [user]${black}[/user] ${result}"
# Highlight player's name
if [[ -n "$PLAYER" ]]; then
players=$(grep -o "\\[user\\][^ ]*${PLAYER}[^ ]*\\[/user\\]" <<< "$line")
# Escape symbols ']', '[', and '/'
players=$(sed 's/[][\/]/\\\0/g' <<< "$players")
for name in $players; do
line=$(sed "s/${name}/[b]\0[\\/b]/g" <<< "$line")
done
fi
echo "$line"
done
echo "[/list]"
echo
}
# Note that we use '"$@"' to let each command-line parameter expand to a
# separate word. The quotes around '$@' are essential!
# We need 'opts' as the 'eval set --' would nuke the return value of getopt
opts=$(getopt --options ap:f:h \
--longoptions all,player:,format:,help \
-- "$@")
# Note the quotes around '$opts': they are essential!
eval set -- "$opts"
SHOW_ALL=false
PLAYER=""
FORMAT=term
while true; do
case "$1" in
-a|--all)
SHOW_ALL=true
shift;;
-p|--player)
PLAYER="$2"
shift 2;;
-f|--format)
FORMAT="$2"
shift 2;;
-h|--help)
usage
break;;
--)
shift
show_tour_sequence "$@"
break;;
esac
done
2013-09-26 00:11:13 +04:00
exit 0