2013-09-21 03:20:19 +04:00
|
|
|
|
#!/usr/bin/env bash
|
2014-03-27 11:52:44 +04:00
|
|
|
|
# Copyright 2014 Vladimir Ivanov <ivvl82@gmail.com>
|
|
|
|
|
# Distributed under the terms of the GNU General Public License v2
|
2013-09-21 03:20:19 +04:00
|
|
|
|
|
2013-10-20 13:28:50 +04:00
|
|
|
|
REPO=fat0troll/lorchess
|
2014-03-27 11:52:44 +04:00
|
|
|
|
TOURNAMENT=2014/1-tabiyas
|
2013-09-21 03:20:19 +04:00
|
|
|
|
|
2014-04-20 14:13:14 +04:00
|
|
|
|
# Version information
|
|
|
|
|
VERSION="0.1"
|
|
|
|
|
|
2014-04-18 14:29:28 +04:00
|
|
|
|
# *FIXME* Withdrew players should be taken from the main config
|
|
|
|
|
WITHDREW_PLS="trex6"
|
|
|
|
|
|
2014-04-20 14:13:14 +04:00
|
|
|
|
argv0=${0##*/}
|
|
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
|
cat <<EOF
|
|
|
|
|
List games on tours of tournament
|
|
|
|
|
|
|
|
|
|
Usage: $argv0 [options] [tours]
|
|
|
|
|
Tournament tours are restricted to sequence \`tours' given in
|
|
|
|
|
Bash-style syntax, i.e., \`tours' may include explicit numbers
|
|
|
|
|
(tours=1 3), intervals (tours={1..5}), or any combination of them.
|
|
|
|
|
If no tours are specified, display each tour until an empty
|
|
|
|
|
\`tour_info' is found. By default, only unaccomplished games are
|
|
|
|
|
listed.
|
|
|
|
|
|
|
|
|
|
Inner variable REPO points to the root of repository; REPO is a URL
|
|
|
|
|
link, absolute or relative path. Inner variable TOURNAMENT specifies
|
|
|
|
|
the sub-directory of tournament.
|
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-a List accomplished games too
|
|
|
|
|
-p PLAYER Only PLAYER's games are listed; this supports globs
|
|
|
|
|
inside PLAYER for player matching
|
|
|
|
|
-f FORMAT Specify the output format; FORMAT is \`term'
|
|
|
|
|
(default), \`html', or \`lor'
|
|
|
|
|
-l Add the URL link to game (if accomplished)
|
|
|
|
|
-h Show this help output
|
|
|
|
|
-v Show version information
|
|
|
|
|
EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
version() {
|
|
|
|
|
echo "${argv0}-${VERSION}"
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-23 16:04:10 +04:00
|
|
|
|
# Variables
|
2014-03-27 22:54:45 +04:00
|
|
|
|
date_re="([0-9?]{2})\\.([0-9?]{2})\\.([0-9?]{4})"
|
2014-03-27 11:52:44 +04:00
|
|
|
|
res_re="(0|1|0\\.5):(0|1|0\\.5)"
|
2013-09-23 16:04:10 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
show_tour_sequence() {
|
|
|
|
|
local sequence info_array
|
2013-09-23 14:31:09 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
# 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
|
2013-09-21 03:20:19 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
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
|
|
|
|
|
}
|
2013-09-21 03:20:19 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
fetch_info_array() {
|
2014-03-27 22:54:45 +04:00
|
|
|
|
local url="${BASEURL}/tours/${1}/tour_info"
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
# 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")
|
2014-03-27 22:54:45 +04:00
|
|
|
|
done <<< "$(curl -q --fail --silent --write-out '\n' $url)"
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
return `[[ -n $info_array ]]`
|
2013-10-20 13:28:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
keep_info_line() {
|
|
|
|
|
local line="$1" keep=1
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
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
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
return $keep
|
|
|
|
|
}
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
show_tour_info() {
|
|
|
|
|
[[ "${info_array[0]}" =~ "Тур №"([0-9]+) ]]
|
|
|
|
|
local tour_num="${BASH_REMATCH[1]}"
|
|
|
|
|
|
|
|
|
|
[[ "${info_array[3]}" =~ ($date_re)" "*[-—]" "*($date_re) ]]
|
2014-03-27 22:54:45 +04:00
|
|
|
|
local date_beg="${BASH_REMATCH[1]}" date_end="${BASH_REMATCH[5]}"
|
2014-03-27 11:52:44 +04:00
|
|
|
|
# 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)?" "*([^" "]+) ]]
|
2014-03-27 22:54:45 +04:00
|
|
|
|
local white="${BASH_REMATCH[5]}" black="${BASH_REMATCH[9]}" result="" url=""
|
|
|
|
|
local game_date="${BASH_REMATCH[4]}-${BASH_REMATCH[3]}-${BASH_REMATCH[2]}"
|
|
|
|
|
case "${BASH_REMATCH[6]}" in
|
2014-03-27 11:52:44 +04:00
|
|
|
|
1:0) result="1-0";;
|
|
|
|
|
0:1) result="0-1";;
|
|
|
|
|
0.5:0.5) result="1/2";;
|
|
|
|
|
esac
|
|
|
|
|
|
2014-03-27 22:54:45 +04:00
|
|
|
|
# Store the link of game in variable '$url'
|
|
|
|
|
if $SHOW_LINK; then
|
2014-04-18 14:29:28 +04:00
|
|
|
|
if [[ ! "$WITHDREW_PLS" =~ (^|' '+)("${white}"|"${black}")($|' '+) ]]; then
|
|
|
|
|
[[ -n "$result" ]] && store_game_url
|
|
|
|
|
fi
|
2014-03-27 22:54:45 +04:00
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
output_lines+="${white} ${black} ${result} ${url}\n"
|
2014-03-27 11:52:44 +04:00
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
eval "info_output_$FORMAT \"${output_lines}\""
|
|
|
|
|
}
|
2013-10-20 15:30:02 +04:00
|
|
|
|
|
2014-03-27 22:54:45 +04:00
|
|
|
|
store_game_url() {
|
|
|
|
|
local tour=$(printf "%02g" "$tour_num")
|
|
|
|
|
local game_dir="${game_date}-${white}-vs-${black}"
|
|
|
|
|
local game_url="${BASEURL}/tours/${tour}/${game_dir}/1.pgn"
|
|
|
|
|
|
|
|
|
|
while read line; do
|
|
|
|
|
# Search for an URL inside PGN file
|
|
|
|
|
[[ "$line" =~ "[Site \""([a-z]+:[^"\""]+)"\"]" ]]
|
|
|
|
|
url="${BASH_REMATCH[1]}"
|
|
|
|
|
[[ -n "$url" ]] && break
|
|
|
|
|
done <<< "$(curl -q --fail --silent $game_url)"
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
info_output_term() {
|
2014-03-27 15:26:01 +04:00
|
|
|
|
echo -n "$(tput setaf 2)${tour_num} тур "
|
2014-03-27 11:52:44 +04:00
|
|
|
|
echo "$(tput setaf 6)(${date_beg} - ${date_end})$(tput sgr0)"
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 15:26:01 +04:00
|
|
|
|
echo -en "$1" | column -t | while IFS= read line; do
|
2014-03-27 11:52:44 +04:00
|
|
|
|
# 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
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
# Highlight result
|
2014-03-27 22:54:45 +04:00
|
|
|
|
line=$(sed "s/\(1-0\|0-1\|1\\/2\)/\\$(tput setaf 6)\0\\$(tput sgr0)/g" <<< "$line")
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
echo "$line"
|
|
|
|
|
done
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
echo
|
|
|
|
|
}
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 15:26:01 +04:00
|
|
|
|
info_output_lor() {
|
2014-03-27 18:21:15 +04:00
|
|
|
|
echo "[b]${tour_num} тур (${date_beg} - ${date_end})[/b]"
|
2014-03-27 15:26:01 +04:00
|
|
|
|
echo "[list]"
|
|
|
|
|
|
2014-03-27 22:54:45 +04:00
|
|
|
|
echo -en "$1" | while read white black result url; do
|
|
|
|
|
[[ -n "$url" ]] && result="[url=${url}]${result}[/url]"
|
2014-03-27 18:21:15 +04:00
|
|
|
|
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"
|
2014-03-27 15:26:01 +04:00
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
echo "[/list]"
|
|
|
|
|
echo
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 19:31:56 +04:00
|
|
|
|
info_output_html() {
|
|
|
|
|
echo "<div class=\"tour-info\">"
|
2014-03-27 23:52:49 +04:00
|
|
|
|
echo " <table class=\"table table-condensed\">"
|
2014-03-27 19:31:56 +04:00
|
|
|
|
echo " <caption>"
|
|
|
|
|
echo " <strong>${tour_num} тур (${date_beg} - ${date_end})</strong>"
|
|
|
|
|
echo " </caption>"
|
|
|
|
|
echo " <tbody>"
|
|
|
|
|
|
2014-03-27 22:54:45 +04:00
|
|
|
|
echo -en "$1" | while read white black result url; do
|
|
|
|
|
[[ -n "$url" ]] && result="<a href=\"${url}\">${result}</a>"
|
|
|
|
|
|
2014-03-27 19:31:56 +04:00
|
|
|
|
# Highlight player's name
|
|
|
|
|
if [[ -n "$PLAYER" ]]; then
|
|
|
|
|
[[ "$white" =~ $PLAYER ]] && white="<strong>${white}</strong>"
|
|
|
|
|
[[ "$black" =~ $PLAYER ]] && black="<strong>${black}</strong>"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
echo " <tr>"
|
2014-03-27 19:59:37 +04:00
|
|
|
|
echo " <td>${white} - ${black}</td><td class=\"result\">${result}</td>"
|
2014-03-27 19:31:56 +04:00
|
|
|
|
echo " </tr>"
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
echo " </tbody>"
|
|
|
|
|
echo " </table>"
|
|
|
|
|
echo "</div>"
|
|
|
|
|
echo
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
# 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
|
2014-03-27 22:54:45 +04:00
|
|
|
|
opts=$(getopt --options ap:f:lh \
|
|
|
|
|
--longoptions all,player:,format:,link,help \
|
2014-03-27 11:52:44 +04:00
|
|
|
|
-- "$@")
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
# Note the quotes around '$opts': they are essential!
|
|
|
|
|
eval set -- "$opts"
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
SHOW_ALL=false
|
|
|
|
|
PLAYER=""
|
|
|
|
|
FORMAT=term
|
2014-03-27 22:54:45 +04:00
|
|
|
|
SHOW_LINK=false
|
|
|
|
|
BASEURL="https://raw.github.com/${REPO}/master/${TOURNAMENT}"
|
2014-03-27 11:52:44 +04:00
|
|
|
|
|
|
|
|
|
while true; do
|
|
|
|
|
case "$1" in
|
|
|
|
|
-a|--all)
|
|
|
|
|
SHOW_ALL=true
|
|
|
|
|
shift;;
|
|
|
|
|
-p|--player)
|
|
|
|
|
PLAYER="$2"
|
|
|
|
|
shift 2;;
|
|
|
|
|
-f|--format)
|
|
|
|
|
FORMAT="$2"
|
|
|
|
|
shift 2;;
|
2014-03-27 22:54:45 +04:00
|
|
|
|
-l|--link)
|
|
|
|
|
SHOW_LINK=true
|
|
|
|
|
shift;;
|
2014-03-27 11:52:44 +04:00
|
|
|
|
-h|--help)
|
|
|
|
|
usage
|
|
|
|
|
break;;
|
|
|
|
|
--)
|
|
|
|
|
shift
|
|
|
|
|
show_tour_sequence "$@"
|
|
|
|
|
break;;
|
|
|
|
|
esac
|
2013-09-21 03:20:19 +04:00
|
|
|
|
done
|
2013-09-26 00:11:13 +04:00
|
|
|
|
|
|
|
|
|
exit 0
|