Script 'schedule': smart determination of tour sequences.
This commit is contained in:
parent
9bad7c7af8
commit
dd485ccf9e
230
schedule
230
schedule
@ -2,16 +2,18 @@
|
|||||||
# Copyright 2014 Vladimir Ivanov <ivvl82@gmail.com>
|
# Copyright 2014 Vladimir Ivanov <ivvl82@gmail.com>
|
||||||
# Distributed under the terms of the GNU General Public License v2
|
# Distributed under the terms of the GNU General Public License v2
|
||||||
|
|
||||||
# REPO="https://raw.githubusercontent.com/fat0troll/lorchess/master"
|
# REPO_DIR="https://github.com/fat0troll/lorchess"
|
||||||
REPO="`dirname $0`"
|
REPO_DIR=`dirname "$0"`
|
||||||
TOURNAMENT="2014/3-fallenleaves"
|
|
||||||
|
# Specify the tournament here
|
||||||
|
TOURNAMENT=
|
||||||
|
|
||||||
# Version information
|
# Version information
|
||||||
VERSION="0.4"
|
VERSION="0.5"
|
||||||
|
|
||||||
argv0="${0##*/}"
|
argv0=${0##*/}
|
||||||
|
|
||||||
usage() {
|
function usage {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
List games on tours of tournament
|
List games on tours of tournament
|
||||||
|
|
||||||
@ -37,50 +39,84 @@ Options:
|
|||||||
-h Show this help output
|
-h Show this help output
|
||||||
-v Show version information
|
-v Show version information
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
exit "${1:-0}"
|
||||||
}
|
}
|
||||||
|
|
||||||
version() {
|
function version {
|
||||||
echo "${argv0}-${VERSION}"
|
exec echo "${argv0}-${VERSION}"
|
||||||
}
|
}
|
||||||
|
|
||||||
show_tour_sequence() {
|
function setup_repo {
|
||||||
local tour_info
|
CURL_OPTS="-q --fail --location --silent"
|
||||||
|
|
||||||
# Reduce REPO to a canonical form
|
# If no tournament given, set it to the last one
|
||||||
convert_repo_url
|
if [[ -z $TOURNAMENT ]]; then
|
||||||
|
if [[ $REPO_DIR =~ ^https://github\.com/(.*) ]]; then
|
||||||
|
REPO_NAME=${BASH_REMATCH[1]}
|
||||||
|
local year=$(curl $CURL_OPTS \
|
||||||
|
https://api.github.com/repos/${REPO_NAME}/contents \
|
||||||
|
| sed -En "/\"path\":/h;/\"type\": \"dir\"/{g;p}" \
|
||||||
|
| sed -En "s|.*\"([0-9]{4})\".*|\1|p" | tail -1)
|
||||||
|
TOURNAMENT=$(curl $CURL_OPTS \
|
||||||
|
https://api.github.com/repos/${REPO_NAME}/contents/${year} \
|
||||||
|
| sed -En "/\"path\":/h;/\"type\": \"dir\"/{g;p}" \
|
||||||
|
| sed -En "s|.*\"(${year}/[0-9]-.*)\".*|\1|p" | tail -1)
|
||||||
|
else
|
||||||
|
# Convert REPO_DIR to an absolute path
|
||||||
|
[[ $REPO_DIR =~ ^/ ]] || REPO_DIR=$(cd ${REPO_DIR}; pwd)
|
||||||
|
|
||||||
if [[ -n "$@" ]]; then
|
local year_dir=$(ls -1 -d ${REPO_DIR}/[0-9][0-9][0-9][0-9]/ | tail -1)
|
||||||
for num in $@; do
|
TOURNAMENT=$(ls -1 -d ${year_dir}[0-9]-*/ | tail -1 \
|
||||||
fetch_tour_info "$num" && output_tour_info
|
| sed -E "s|${REPO_DIR}/(.*)|\1|")
|
||||||
done
|
# Remove the trailing slash
|
||||||
else
|
TOURNAMENT=${TOURNAMENT%/}
|
||||||
# If the tour numbers were not passed as arguments, exit once
|
fi
|
||||||
# the first non-existing 'tour_info' is reached
|
|
||||||
num=1
|
|
||||||
while fetch_tour_info "$num"; do
|
|
||||||
output_tour_info
|
|
||||||
num=$((num+1))
|
|
||||||
done
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch_tour_info() {
|
function show_tour_sequence {
|
||||||
# Change tour numbers: '1' -> '01', '2' -> '02', and so on
|
local tour_seq=$@
|
||||||
local tour=$(printf "%02g" "$1")
|
|
||||||
|
|
||||||
# *NOTE* The incorporation of a newline at the end of 'tour_info'
|
# If no tour sequence given, set it to all tours
|
||||||
# (--write-out '\n') is important and allows one to read the last
|
if [[ -z $tour_seq ]]; then
|
||||||
# line with no trailing '\n'
|
if [[ $REPO_DIR =~ ^https://github\.com/(.*) ]]; then
|
||||||
local info_url="${REPO}/${TOURNAMENT}/tours/${tour}/tour_info"
|
tour_seq=$(curl $CURL_OPTS \
|
||||||
tour_info="$(curl -q --fail --location --silent --write-out '\n' $info_url)"
|
https://api.github.com/repos/${REPO_NAME}/contents/${TOURNAMENT}/tours \
|
||||||
return `[[ -n "$tour_info" ]]`
|
| sed -En "/\"path\":/h;/\"type\": \"dir\"/{g;p}" \
|
||||||
|
| sed -En "s|.*\"${TOURNAMENT}/tours/([0-9]{2})\".*|\1|p")
|
||||||
|
else
|
||||||
|
tour_seq=$(ls -1 -d ${REPO_DIR}/${TOURNAMENT}/tours/[0-9][0-9]/ \
|
||||||
|
| sed -E "s|${REPO_DIR}/${TOURNAMENT}/tours/([0-9]{2})/|\1|")
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
for tour in $tour_seq; do
|
||||||
|
# Change tour numbers: '1' -> '01', '2' -> '02', and so on
|
||||||
|
tour=$(printf "%02g" $tour)
|
||||||
|
|
||||||
|
# *NOTE* The incorporation of a newline at the end of
|
||||||
|
# 'tour_info' is important and allows one to read the last
|
||||||
|
# line with no trailing '\n'
|
||||||
|
if [[ $REPO_DIR =~ ^https://github\.com/(.*) ]]; then
|
||||||
|
local tour_info=$(curl $CURL_OPTS --write-out '\n' \
|
||||||
|
${REPO_DIR}/raw/master/${TOURNAMENT}/tours/${tour}/tour_info)
|
||||||
|
[[ -n $tour_info ]] && output_tour_info
|
||||||
|
else
|
||||||
|
local info_file=${REPO_DIR}/${TOURNAMENT}/tours/${tour}/tour_info
|
||||||
|
if [[ -f $info_file ]]; then
|
||||||
|
local tour_info=$(cat $info_file; echo)
|
||||||
|
output_tour_info
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
output_tour_info() {
|
function output_tour_info {
|
||||||
local tour_num date_beg date_end
|
local tour_num date_beg date_end
|
||||||
local white black result game_date game_url
|
local white black result game_date game_url
|
||||||
# Lines with game info
|
# Lines with game info
|
||||||
local output_lines=""
|
local output_lines=
|
||||||
|
|
||||||
while read line; do
|
while read line; do
|
||||||
# Read 'tour_num'
|
# Read 'tour_num'
|
||||||
@ -91,78 +127,57 @@ output_tour_info() {
|
|||||||
# Keep game info if needed, and store it in variables 'white',
|
# Keep game info if needed, and store it in variables 'white',
|
||||||
# 'black', 'result', and 'game_date'
|
# 'black', 'result', and 'game_date'
|
||||||
if keep_game_info "$line"; then
|
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'
|
# Store the link to game in 'game_url'
|
||||||
game_url=""
|
game_url=
|
||||||
if $SHOW_LINK; then
|
if $SHOW_LINK; then
|
||||||
[[ -n "$result" ]] && store_game_url
|
[[ -n $result ]] && store_game_url
|
||||||
fi
|
fi
|
||||||
|
|
||||||
output_lines+="${white} ${black} ${result} ${game_url}\n"
|
output_lines+="${white} ${black} ${result} ${game_url}\n"
|
||||||
fi
|
fi
|
||||||
done <<< "$tour_info"
|
done <<< "$tour_info"
|
||||||
|
|
||||||
[[ -n "$output_lines" ]] && eval "info_output_$FORMAT \"${output_lines}\""
|
[[ -n $output_lines ]] && eval "info_output_$FORMAT \"${output_lines}\""
|
||||||
}
|
}
|
||||||
|
|
||||||
convert_repo_url() {
|
function get_tour_num {
|
||||||
if [[ ! "$REPO" =~ ^(https?|file):// ]]; then
|
if [[ $1 =~ ^"Тур №"([0-9]+) ]]; then
|
||||||
if [[ ! "$REPO" =~ ^/ ]]; then
|
tour_num=${BASH_REMATCH[1]}
|
||||||
# REPO is a relative path
|
|
||||||
base_dir="$(dirname $0)"
|
|
||||||
REPO="$(cd ${base_dir}/${REPO}; pwd)"
|
|
||||||
fi
|
|
||||||
|
|
||||||
REPO="file://${REPO}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
get_tour_num() {
|
|
||||||
if [[ "$1" =~ "Тур №"([0-9]+) ]]; then
|
|
||||||
tour_num="${BASH_REMATCH[1]}"
|
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
get_tour_dates() {
|
function get_tour_dates {
|
||||||
local date_re="[0-9?]{2}\.[0-9?]{2}\.[0-9?]{4}"
|
local date_re="[0-9?]{2}\.[0-9?]{2}\.[0-9?]{4}"
|
||||||
|
|
||||||
if [[ "$1" =~ "Время проведения:"\ *($date_re)\ *[-—]\ *($date_re) ]]; then
|
if [[ $1 =~ ^"Время проведения:"\ +($date_re)\ +-\ +($date_re) ]]; then
|
||||||
date_beg="${BASH_REMATCH[1]}" date_end="${BASH_REMATCH[2]}"
|
date_beg=${BASH_REMATCH[1]} date_end=${BASH_REMATCH[2]}
|
||||||
# Use short dates
|
# Use short dates
|
||||||
date_beg="${date_beg::5}" date_end="${date_end::5}"
|
date_beg=${date_beg::5} date_end=${date_end::5}
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
keep_game_info() {
|
function keep_game_info {
|
||||||
local date_re="([0-9?]{2})\.([0-9?]{2})\.([0-9?]{4})"
|
local date_re="([0-9?]{2})\.([0-9?]{2})\.([0-9?]{4})"
|
||||||
local res_re="(0|1|0\.5):(0|1|0\.5)"
|
local res_re="(1|1/2|0)-(1|1/2|0)"
|
||||||
local line="$1" keep=1
|
local line=$1 keep=1
|
||||||
|
|
||||||
if [[ "$line" =~ ($date_re)\ *[-—]\ *([^\ ]+)\ *($res_re)?\ *([^\ ]+) ]]; then
|
if [[ $line =~ ^($date_re)\ +([^\ ]+)\ +-\ +([^\ ]+)\ *($res_re)? ]]; then
|
||||||
white="${BASH_REMATCH[5]}"
|
white=${BASH_REMATCH[5]} black=${BASH_REMATCH[6]} result=${BASH_REMATCH[7]}
|
||||||
black="${BASH_REMATCH[9]}"
|
game_date=${BASH_REMATCH[4]}-${BASH_REMATCH[3]}-${BASH_REMATCH[2]}
|
||||||
result="${BASH_REMATCH[6]}"
|
|
||||||
game_date="${BASH_REMATCH[4]}-${BASH_REMATCH[3]}-${BASH_REMATCH[2]}"
|
|
||||||
|
|
||||||
# If the player was passed as an argument, check if it is his game or not
|
# 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 [[ -z $PLAYER || $white =~ $PLAYER || $black =~ $PLAYER ]]; then
|
||||||
if $SHOW_ALL; then
|
if $SHOW_ALL; then
|
||||||
keep=0
|
keep=0
|
||||||
else
|
else
|
||||||
# Keep line if the game is not finished
|
# Keep line if the game is not finished
|
||||||
[[ -z "$result" ]] && keep=0
|
[[ -z $result ]] && keep=0
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
@ -170,37 +185,37 @@ keep_game_info() {
|
|||||||
return $keep
|
return $keep
|
||||||
}
|
}
|
||||||
|
|
||||||
store_game_url() {
|
function store_game_url {
|
||||||
local tour=$(printf "%02g" "$tour_num")
|
local tour=$(printf "%02g" "$tour_num")
|
||||||
local game_dir="${game_date}-${white}-vs-${black}"
|
local game_dir=${game_date}-${white}-vs-${black}
|
||||||
local pgn_url="${REPO}/${TOURNAMENT}/tours/${tour}/${game_dir}/1.pgn"
|
local pgn_url=${REPO}/${TOURNAMENT}/tours/${tour}/${game_dir}/1.pgn
|
||||||
|
|
||||||
while read line; do
|
while read line; do
|
||||||
# Search for a URL inside PGN file
|
# Search for a URL inside PGN file
|
||||||
[[ "$line" =~ "[Site \""([a-z]+:[^\"]+)"\"]" ]]
|
[[ $line =~ "[Site \""([a-z]+:[^\"]+)"\"]" ]]
|
||||||
game_url="${BASH_REMATCH[1]}"
|
game_url=${BASH_REMATCH[1]}
|
||||||
[[ -n "$game_url" ]] && break
|
[[ -n $game_url ]] && break
|
||||||
done <<< "$(curl -q --fail --location --silent $pgn_url)"
|
done <<< $(curl -q --fail --location --silent "$pgn_url")
|
||||||
}
|
}
|
||||||
|
|
||||||
info_output_term() {
|
function info_output_term {
|
||||||
echo -n "$(tput setaf 2)${tour_num} тур "
|
echo -n "$(tput setaf 2)${tour_num} тур "
|
||||||
echo "$(tput setaf 6)(${date_beg} - ${date_end})$(tput sgr0)"
|
echo "$(tput setaf 6)(${date_beg} - ${date_end})$(tput sgr0)"
|
||||||
|
|
||||||
echo -en "$1" | column -t | while IFS= read line; do
|
echo -en "$1" | column -t | while IFS= read line; do
|
||||||
# Highlight player's name
|
# Highlight player's name
|
||||||
if [[ -n "$PLAYER" ]]; then
|
if [[ -n $PLAYER ]]; then
|
||||||
players=$(grep -o "[^ ]*${PLAYER}[^ ]*" <<< "$line")
|
players=$(grep -o "[^ ]*${PLAYER}[^ ]*" <<< "$line")
|
||||||
for name in $players; do
|
for name in $players; do
|
||||||
# PLAYER is a part of 'game_url' or 'result'
|
# PLAYER is a part of 'game_url' or 'result'
|
||||||
[[ "$name" =~ ^http:// || "$name" =~ ^(1-0|0-1|1/2)$ ]] && continue
|
[[ $name =~ ^http:// || $name =~ ^(1-0|1/2-1/2|0-1|)$ ]] && continue
|
||||||
|
|
||||||
line=$(sed -E "s/(${name})/$(tput setaf 1)\1$(tput sgr0)/g" <<< "$line")
|
line=$(sed -E "s/(${name})/$(tput setaf 1)\1$(tput sgr0)/g" <<< "$line")
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Highlight result
|
# Highlight result
|
||||||
line=$(sed -E "s/ (1-0|0-1|1\/2)( |$)/ $(tput setaf 6)\1$(tput sgr0)\2/g" <<< "$line")
|
line=$(sed -E "s/ (1-0|1\/2-1\/2|0-1)( |$)/ $(tput setaf 6)\1$(tput sgr0)\2/g" <<< "$line")
|
||||||
|
|
||||||
echo "$line"
|
echo "$line"
|
||||||
done
|
done
|
||||||
@ -208,12 +223,12 @@ info_output_term() {
|
|||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
info_output_lor() {
|
function info_output_lor {
|
||||||
echo "[b]${tour_num} тур (${date_beg} - ${date_end})[/b]"
|
echo "[b]${tour_num} тур (${date_beg} - ${date_end})[/b]"
|
||||||
echo "[list]"
|
echo "[list]"
|
||||||
|
|
||||||
echo -en "$1" | while read white black result url; do
|
echo -en "$1" | while read white black result url; do
|
||||||
[[ -n "$url" ]] && result="[url=${url}]${result}[/url]"
|
[[ -n $url ]] && result="[url=${url}]${result}[/url]"
|
||||||
|
|
||||||
echo " [*] [user]${white}[/user] - [user]${black}[/user] ${result}"
|
echo " [*] [user]${white}[/user] - [user]${black}[/user] ${result}"
|
||||||
done
|
done
|
||||||
@ -223,7 +238,7 @@ info_output_lor() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
info_output_html() {
|
function info_output_html {
|
||||||
echo "<div class=\"tour-info\">"
|
echo "<div class=\"tour-info\">"
|
||||||
echo " <table class=\"table table-condensed\">"
|
echo " <table class=\"table table-condensed\">"
|
||||||
echo " <caption>"
|
echo " <caption>"
|
||||||
@ -232,7 +247,7 @@ info_output_html() {
|
|||||||
echo " <tbody>"
|
echo " <tbody>"
|
||||||
|
|
||||||
echo -en "$1" | while read white black result url; do
|
echo -en "$1" | while read white black result url; do
|
||||||
[[ -n "$url" ]] && result="<a href=\"${url}\">${result}</a>"
|
[[ -n $url ]] && result="<a href=\"${url}\">${result}</a>"
|
||||||
|
|
||||||
echo " <tr>"
|
echo " <tr>"
|
||||||
echo " <td>${white} - ${black}</td><td class=\"result\">${result}</td>"
|
echo " <td>${white} - ${black}</td><td class=\"result\">${result}</td>"
|
||||||
@ -245,42 +260,43 @@ info_output_html() {
|
|||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
checkargs() {
|
function die {
|
||||||
if [[ "$OPTARG" =~ ^-[apflhv]$ ]]; then
|
echo "$@" 1>&2
|
||||||
echo "Option -${opt}: argument not found" 1>&2
|
exit 1
|
||||||
exit 1
|
}
|
||||||
fi
|
|
||||||
if [[ "$opt" == f && ! "$OPTARG" =~ ^(term|html|lor)$ ]]; then
|
function checkargs {
|
||||||
echo "Incorrect FORMAT specified" 1>&2
|
[[ $OPTARG =~ ^-[apflhv]$ ]] && die "Option -${opt}: argument not found"
|
||||||
exit 1
|
[[ $opt == f && ! $OPTARG =~ ^(term|html|lor)$ ]] && die "Incorrect FORMAT specified"
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SHOW_ALL=false
|
|
||||||
PLAYER=""
|
|
||||||
FORMAT=term
|
FORMAT=term
|
||||||
|
SHOW_ALL=false
|
||||||
SHOW_LINK=false
|
SHOW_LINK=false
|
||||||
|
|
||||||
while getopts "ap:f:lhv" opt; do
|
while getopts ap:f:lhv opt; do
|
||||||
case "$opt" in
|
case $opt in
|
||||||
a) SHOW_ALL=true
|
a) SHOW_ALL=true
|
||||||
;;
|
;;
|
||||||
p) checkargs
|
p) checkargs
|
||||||
PLAYER="$OPTARG"
|
PLAYER=$OPTARG
|
||||||
;;
|
;;
|
||||||
f) checkargs
|
f) checkargs
|
||||||
FORMAT="$OPTARG"
|
FORMAT=$OPTARG
|
||||||
;;
|
;;
|
||||||
l) SHOW_LINK=true
|
l) SHOW_LINK=true
|
||||||
;;
|
;;
|
||||||
h) usage && exit 0
|
h) usage
|
||||||
;;
|
;;
|
||||||
v) version && exit 0
|
v) version
|
||||||
|
;;
|
||||||
|
*) usage 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
shift $(($OPTIND - 1))
|
shift $(($OPTIND - 1))
|
||||||
show_tour_sequence "$@"
|
setup_repo
|
||||||
|
show_tour_sequence $@
|
||||||
|
|
||||||
exit 0
|
exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user