Script 'schedule': improve the form of loops in a tour sequence.

This commit is contained in:
vonavi 2014-04-30 00:31:28 +03:00
parent 76cf82bad6
commit 1ff814c2d3

104
schedule
View File

@ -44,65 +44,69 @@ version() {
} }
show_tour_sequence() { show_tour_sequence() {
local sequence local tour_info
# Generate the sequence of tours to search in # Reduce REPO to a canonical form
if [[ -z "$@" ]]; then convert_repo_url
sequence=$(seq -f "%02g" 1 99)
if [[ -n "$@" ]]; then
for num in $@; do
fetch_tour_info "$num" && output_tour_info
done
else else
# Change tour numbers: '1' -> '01', '2' -> '02', and so on # If the tour numbers were not passed as arguments, exit once
sequence=$(for i in "$@"; do printf "%02g " "$i"; done) # 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
}
for tour in $sequence; do fetch_tour_info() {
convert_repo_url # Change tour numbers: '1' -> '01', '2' -> '02', and so on
local base_url="${REPO}/${TOURNAMENT}" local tour=$(printf "%02g" "$num")
local info_url="${base_url}/tours/${tour}/tour_info"
# *NOTE* The incorporation of a newline at the end of # *NOTE* The incorporation of a newline at the end of 'tour_info'
# 'tour_info' (--write-out '\n') is important and allows one # (--write-out '\n') is important and allows one to read the last
# to read the last line with no trailing '\n' # line with no trailing '\n'
local tour_info="$(curl -q --fail --silent --write-out '\n' $info_url)" local info_url="${REPO}/${TOURNAMENT}/tours/${tour}/tour_info"
tour_info="$(curl -q --fail --silent --write-out '\n' $info_url)"
return `[[ -n "$tour_info" ]]`
}
# Produce output only if 'tour_info' exists output_tour_info() {
if [[ -n "$tour_info" ]]; then 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
get_tour_num "$line" && continue get_tour_num "$line" && continue
get_tour_dates "$line" && continue get_tour_dates "$line" && continue
# Keep game info if needed, and store it in variables # Keep game info if needed, and store it in variables 'white',
# 'while', 'black', 'result', and 'game_date' # 'black', 'result', and 'game_date'
if keep_game_info "$line"; then if keep_game_info "$line"; then
# Fix result # Fix result
case "$result" in case "$result" in
"1:0") result="1-0" ;; "1:0") result="1-0" ;;
"0:1") result="0-1" ;; "0:1") result="0-1" ;;
"0.5:0.5") result="1/2" ;; "0.5:0.5") result="1/2" ;;
esac 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
done <<< "$tour_info"
[[ -n "$output_lines" ]] && eval "info_output_$FORMAT \"${output_lines}\""
else
# If the tour numbers were not passed as arguments, exit
# once the first non-existing 'tour_info' is reached
[[ -z "$@" ]] && break
fi fi
done done <<< "$tour_info"
[[ -n "$output_lines" ]] && eval "info_output_$FORMAT \"${output_lines}\""
} }
convert_repo_url() { convert_repo_url() {
@ -165,9 +169,9 @@ keep_game_info() {
} }
store_game_url() { store_game_url() {
local tour=$(printf "%02g" "$tour_num") local tour=$(printf "%02g" "$num")
local game_dir="${game_date}-${white}-vs-${black}" local game_dir="${game_date}-${white}-vs-${black}"
local pgn_url="${base_url}/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