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
|
|
|
|
|
2014-04-28 14:45:40 +04:00
|
|
|
|
# REPO="https://raw.githubusercontent.com/fat0troll/lorchess/master"
|
|
|
|
|
REPO="`dirname $0`"
|
2014-04-20 21:42:30 +04:00
|
|
|
|
TOURNAMENT="2014/1-tabiyas"
|
2013-09-21 03:20:19 +04:00
|
|
|
|
|
2014-04-20 14:13:14 +04:00
|
|
|
|
# Version information
|
2014-04-23 01:33:55 +04:00
|
|
|
|
VERSION="0.3"
|
2014-04-20 14:13:14 +04:00
|
|
|
|
|
2014-04-27 19:34:14 +04:00
|
|
|
|
argv0="${0##*/}"
|
2014-04-20 14:13:14 +04:00
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
2014-04-28 14:45:40 +04:00
|
|
|
|
Inner variable REPO points to the root of repository; its default
|
|
|
|
|
value of \`dirname \$0\` can be replaced by a URL link, absolute or
|
|
|
|
|
relative path. Inner variable TOURNAMENT specifies the sub-directory
|
|
|
|
|
of tournament.
|
2014-04-20 14:13:14 +04:00
|
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
-a List accomplished games too
|
2014-04-20 20:08:12 +04:00
|
|
|
|
-p PLAYER Show game if PLAYER is a part of a player's name
|
2014-04-20 14:13:14 +04:00
|
|
|
|
-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}"
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
show_tour_sequence() {
|
2014-04-30 01:31:28 +04:00
|
|
|
|
local tour_info
|
2013-09-23 14:31:09 +04:00
|
|
|
|
|
2014-04-30 01:31:28 +04:00
|
|
|
|
# Reduce REPO to a canonical form
|
|
|
|
|
convert_repo_url
|
|
|
|
|
|
|
|
|
|
if [[ -n "$@" ]]; then
|
|
|
|
|
for num in $@; do
|
|
|
|
|
fetch_tour_info "$num" && output_tour_info
|
|
|
|
|
done
|
2014-03-27 11:52:44 +04:00
|
|
|
|
else
|
2014-04-30 01:31:28 +04:00
|
|
|
|
# If the tour numbers were not passed as arguments, exit once
|
|
|
|
|
# the first non-existing 'tour_info' is reached
|
|
|
|
|
num=1
|
|
|
|
|
while fetch_tour_info "$num"; do
|
|
|
|
|
output_tour_info
|
|
|
|
|
num=$((num+1))
|
|
|
|
|
done
|
2014-03-27 11:52:44 +04:00
|
|
|
|
fi
|
2014-04-30 01:31:28 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetch_tour_info() {
|
|
|
|
|
# Change tour numbers: '1' -> '01', '2' -> '02', and so on
|
|
|
|
|
local tour=$(printf "%02g" "$num")
|
|
|
|
|
|
|
|
|
|
# *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'
|
|
|
|
|
local info_url="${REPO}/${TOURNAMENT}/tours/${tour}/tour_info"
|
2014-04-30 01:53:02 +04:00
|
|
|
|
tour_info="$(curl -q --fail --location --silent --write-out '\n' $info_url)"
|
2014-04-30 01:31:28 +04:00
|
|
|
|
return `[[ -n "$tour_info" ]]`
|
|
|
|
|
}
|
2013-09-21 03:20:19 +04:00
|
|
|
|
|
2014-04-30 01:31:28 +04:00
|
|
|
|
output_tour_info() {
|
|
|
|
|
local tour_num date_beg date_end
|
|
|
|
|
local white black result game_date game_url
|
|
|
|
|
# Lines with game info
|
|
|
|
|
local output_lines=""
|
|
|
|
|
|
|
|
|
|
while read line; do
|
|
|
|
|
get_tour_num "$line" && continue
|
|
|
|
|
get_tour_dates "$line" && continue
|
|
|
|
|
|
|
|
|
|
# Keep game info if needed, and store it in variables 'white',
|
|
|
|
|
# 'black', 'result', and 'game_date'
|
|
|
|
|
if keep_game_info "$line"; then
|
|
|
|
|
# Fix result
|
|
|
|
|
case "$result" in
|
|
|
|
|
"1:0") result="1-0" ;;
|
|
|
|
|
"0:1") result="0-1" ;;
|
|
|
|
|
"0.5:0.5") result="1/2" ;;
|
|
|
|
|
esac
|
|
|
|
|
|
|
|
|
|
# Store the link to game in 'game_url'
|
|
|
|
|
game_url=""
|
|
|
|
|
if $SHOW_LINK; then
|
|
|
|
|
[[ -n "$result" ]] && store_game_url
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
output_lines+="${white} ${black} ${result} ${game_url}\n"
|
2014-03-27 11:52:44 +04:00
|
|
|
|
fi
|
2014-04-30 01:31:28 +04:00
|
|
|
|
done <<< "$tour_info"
|
|
|
|
|
|
|
|
|
|
[[ -n "$output_lines" ]] && eval "info_output_$FORMAT \"${output_lines}\""
|
2014-03-27 11:52:44 +04:00
|
|
|
|
}
|
2013-09-21 03:20:19 +04:00
|
|
|
|
|
2014-04-20 21:42:30 +04:00
|
|
|
|
convert_repo_url() {
|
|
|
|
|
if [[ ! "$REPO" =~ ^(https?|file):// ]]; then
|
|
|
|
|
if [[ ! "$REPO" =~ ^/ ]]; then
|
|
|
|
|
# REPO is a relative path
|
|
|
|
|
base_dir="$(dirname $0)"
|
|
|
|
|
REPO="$(cd ${base_dir}/${REPO}; pwd)"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
REPO="file://${REPO}"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-20 20:08:12 +04:00
|
|
|
|
get_tour_num() {
|
|
|
|
|
if [[ "$1" =~ "Тур №"([0-9]+) ]]; then
|
|
|
|
|
tour_num="${BASH_REMATCH[1]}"
|
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-04-20 20:08:12 +04:00
|
|
|
|
get_tour_dates() {
|
2014-04-27 19:34:14 +04:00
|
|
|
|
local date_re="[0-9?]{2}\.[0-9?]{2}\.[0-9?]{4}"
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-04-20 23:20:46 +04:00
|
|
|
|
if [[ "$1" =~ "Время проведения:"\ *($date_re)\ *[-—]\ *($date_re) ]]; then
|
2014-04-20 20:08:12 +04:00
|
|
|
|
date_beg="${BASH_REMATCH[1]}" date_end="${BASH_REMATCH[2]}"
|
|
|
|
|
# Use short dates
|
2014-04-28 14:45:40 +04:00
|
|
|
|
date_beg="${date_beg::5}" date_end="${date_end::5}"
|
2014-04-20 20:08:12 +04:00
|
|
|
|
return 0
|
|
|
|
|
else
|
|
|
|
|
return 1
|
|
|
|
|
fi
|
2013-10-20 13:28:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-20 20:08:12 +04:00
|
|
|
|
keep_game_info() {
|
2014-04-27 19:34:14 +04:00
|
|
|
|
local date_re="([0-9?]{2})\.([0-9?]{2})\.([0-9?]{4})"
|
|
|
|
|
local res_re="(0|1|0\.5):(0|1|0\.5)"
|
2014-03-27 11:52:44 +04:00
|
|
|
|
local line="$1" keep=1
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-04-20 23:20:46 +04:00
|
|
|
|
if [[ "$line" =~ ($date_re)\ *[-—]\ *([^\ ]+)\ *($res_re)?\ *([^\ ]+) ]]; then
|
2014-04-20 20:08:12 +04:00
|
|
|
|
white="${BASH_REMATCH[5]}"
|
|
|
|
|
black="${BASH_REMATCH[9]}"
|
|
|
|
|
result="${BASH_REMATCH[6]}"
|
|
|
|
|
game_date="${BASH_REMATCH[4]}-${BASH_REMATCH[3]}-${BASH_REMATCH[2]}"
|
2014-03-27 11:52:44 +04:00
|
|
|
|
|
2014-04-20 20:08:12 +04:00
|
|
|
|
# If the player was passed as an argument, check if it is his game or not
|
|
|
|
|
if [[ -z "$PLAYER" || "$white" =~ "$PLAYER" || "$black" =~ "$PLAYER" ]]; then
|
|
|
|
|
if $SHOW_ALL; then
|
2014-03-27 11:52:44 +04:00
|
|
|
|
keep=0
|
2014-04-20 20:08:12 +04:00
|
|
|
|
else
|
|
|
|
|
# Keep line if the game is not finished
|
|
|
|
|
[[ -z "$result" ]] && keep=0
|
2014-03-27 11:52:44 +04:00
|
|
|
|
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 22:54:45 +04:00
|
|
|
|
store_game_url() {
|
2014-04-30 01:31:28 +04:00
|
|
|
|
local tour=$(printf "%02g" "$num")
|
2014-03-27 22:54:45 +04:00
|
|
|
|
local game_dir="${game_date}-${white}-vs-${black}"
|
2014-04-30 01:31:28 +04:00
|
|
|
|
local pgn_url="${REPO}/${TOURNAMENT}/tours/${tour}/${game_dir}/1.pgn"
|
2014-03-27 22:54:45 +04:00
|
|
|
|
|
|
|
|
|
while read line; do
|
2014-04-20 20:08:12 +04:00
|
|
|
|
# Search for a URL inside PGN file
|
|
|
|
|
[[ "$line" =~ "[Site \""([a-z]+:[^\"]+)"\"]" ]]
|
|
|
|
|
game_url="${BASH_REMATCH[1]}"
|
|
|
|
|
[[ -n "$game_url" ]] && break
|
2014-04-30 01:53:02 +04:00
|
|
|
|
done <<< "$(curl -q --fail --location --silent $pgn_url)"
|
2014-03-27 22:54:45 +04:00
|
|
|
|
}
|
|
|
|
|
|
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
|
2014-04-23 01:33:13 +04:00
|
|
|
|
# PLAYER is a part of 'game_url' or 'result'
|
2014-04-24 15:00:26 +04:00
|
|
|
|
[[ "$name" =~ ^http:// || "$name" =~ ^(1-0|0-1|1/2)$ ]] && continue
|
2014-04-23 01:33:13 +04:00
|
|
|
|
|
2014-04-27 19:34:14 +04:00
|
|
|
|
line=$(sed -E "s/(${name})/$(tput setaf 1)\1$(tput sgr0)/g" <<< "$line")
|
2014-03-27 11:52:44 +04:00
|
|
|
|
done
|
|
|
|
|
fi
|
2013-10-20 13:28:50 +04:00
|
|
|
|
|
2014-03-27 11:52:44 +04:00
|
|
|
|
# Highlight result
|
2014-04-27 19:34:14 +04:00
|
|
|
|
line=$(sed -E "s/ (1-0|0-1|1\/2)( |$)/ $(tput setaf 6)\1$(tput sgr0)\2/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
|
|
|
|
|
2014-04-20 22:11:22 +04:00
|
|
|
|
echo " [*] [user]${white}[/user] - [user]${black}[/user] ${result}"
|
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
|
|
|
|
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-04-20 14:42:19 +04:00
|
|
|
|
checkargs() {
|
|
|
|
|
if [[ "$OPTARG" =~ ^-[apflhv]$ ]]; then
|
2014-04-27 19:34:14 +04:00
|
|
|
|
echo "Option -${opt}: argument not found" 1>&2
|
2014-04-20 14:42:19 +04:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
if [[ "$opt" == f && ! "$OPTARG" =~ ^(term|html|lor)$ ]]; then
|
2014-04-27 19:34:14 +04:00
|
|
|
|
echo "Incorrect FORMAT specified" 1>&2
|
2014-04-20 14:42:19 +04:00
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
}
|
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
|
2014-03-27 11:52:44 +04:00
|
|
|
|
|
2014-04-20 14:42:19 +04:00
|
|
|
|
while getopts "ap:f:lhv" opt; do
|
|
|
|
|
case "$opt" in
|
|
|
|
|
a) SHOW_ALL=true
|
|
|
|
|
;;
|
|
|
|
|
p) checkargs
|
|
|
|
|
PLAYER="$OPTARG"
|
|
|
|
|
;;
|
|
|
|
|
f) checkargs
|
|
|
|
|
FORMAT="$OPTARG"
|
|
|
|
|
;;
|
|
|
|
|
l) SHOW_LINK=true
|
|
|
|
|
;;
|
|
|
|
|
h) usage && exit 0
|
|
|
|
|
;;
|
|
|
|
|
v) version && exit 0
|
|
|
|
|
;;
|
2014-03-27 11:52:44 +04:00
|
|
|
|
esac
|
2013-09-21 03:20:19 +04:00
|
|
|
|
done
|
2013-09-26 00:11:13 +04:00
|
|
|
|
|
2014-04-20 14:42:19 +04:00
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
|
show_tour_sequence "$@"
|
|
|
|
|
|
2013-09-26 00:11:13 +04:00
|
|
|
|
exit 0
|