From 2f5175d8edc43ad192f7884a6bae8c44fbc8d592 Mon Sep 17 00:00:00 2001 From: vonavi Date: Thu, 27 Mar 2014 20:54:45 +0200 Subject: [PATCH] Tournament schedule: the possibility to link games added. --- schedule | 54 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/schedule b/schedule index 47f5407..28ce01e 100755 --- a/schedule +++ b/schedule @@ -6,7 +6,7 @@ REPO=fat0troll/lorchess TOURNAMENT=2014/1-tabiyas # Variables -date_re="[0-9?]{2}\\.[0-9?]{2}\\.[0-9?]{4}" +date_re="([0-9?]{2})\\.([0-9?]{2})\\.([0-9?]{4})" res_re="(0|1|0\\.5):(0|1|0\\.5)" show_tour_sequence() { @@ -38,7 +38,7 @@ show_tour_sequence() { } fetch_info_array() { - local url="https://raw.github.com/${REPO}/master/${TOURNAMENT}/tours/${1}/tour_info" + local url="${BASEURL}/tours/${1}/tour_info" # Store 'tour_info' in an array of lines. # *NOTE* The incorporation of a newline at the end of 'tour_info' @@ -47,7 +47,7 @@ fetch_info_array() { info_array=() while read line; do info_array+=("$line") - done <<< "$(curl --fail -q --silent --write-out '\n' $url)" + done <<< "$(curl -q --fail --silent --write-out '\n' $url)" return `[[ -n $info_array ]]` } @@ -79,7 +79,7 @@ show_tour_info() { local tour_num="${BASH_REMATCH[1]}" [[ "${info_array[3]}" =~ ($date_re)" "*[-—]" "*($date_re) ]] - local date_beg="${BASH_REMATCH[1]}" date_end="${BASH_REMATCH[2]}" + local date_beg="${BASH_REMATCH[1]}" date_end="${BASH_REMATCH[5]}" # Use short dates date_beg="${date_beg:0:5}"; date_end="${date_end:0:5}" @@ -90,21 +90,39 @@ show_tour_info() { 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 + 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 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" + # Store the link of game in variable '$url' + if $SHOW_LINK; then + [[ -n "$result" ]] && store_game_url + fi + + output_lines+="${white} ${black} ${result} ${url}\n" fi done eval "info_output_$FORMAT \"${output_lines}\"" } +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)" +} + info_output_term() { echo -n "$(tput setaf 2)${tour_num} тур " echo "$(tput setaf 6)(${date_beg} - ${date_end})$(tput sgr0)" @@ -119,7 +137,7 @@ info_output_term() { fi # Highlight result - line=$(sed "s/\(1-0\|0-1\|1\\/2\)$/\\$(tput setaf 6)\0\\$(tput sgr0)/g" <<< "$line") + line=$(sed "s/\(1-0\|0-1\|1\\/2\)/\\$(tput setaf 6)\0\\$(tput sgr0)/g" <<< "$line") echo "$line" done @@ -131,7 +149,8 @@ info_output_lor() { echo "[b]${tour_num} тур (${date_beg} - ${date_end})[/b]" echo "[list]" - echo -en "$1" | while read white black result; do + echo -en "$1" | while read white black result url; do + [[ -n "$url" ]] && result="[url=${url}]${result}[/url]" local line=" [*] [user]${white}[/user] - [user]${black}[/user] ${result}" # Highlight player's name @@ -154,13 +173,15 @@ info_output_lor() { info_output_html() { echo "
" - echo " " + echo "
" echo " " echo " " - echo -en "$1" | while read white black result; do + echo -en "$1" | while read white black result url; do + [[ -n "$url" ]] && result="${result}" + # Highlight player's name if [[ -n "$PLAYER" ]]; then [[ "$white" =~ $PLAYER ]] && white="${white}" @@ -181,8 +202,8 @@ info_output_html() { # 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 \ +opts=$(getopt --options ap:f:lh \ + --longoptions all,player:,format:,link,help \ -- "$@") # Note the quotes around '$opts': they are essential! @@ -191,6 +212,8 @@ eval set -- "$opts" SHOW_ALL=false PLAYER="" FORMAT=term +SHOW_LINK=false +BASEURL="https://raw.github.com/${REPO}/master/${TOURNAMENT}" while true; do case "$1" in @@ -203,6 +226,9 @@ while true; do -f|--format) FORMAT="$2" shift 2;; + -l|--link) + SHOW_LINK=true + shift;; -h|--help) usage break;;
" echo " ${tour_num} тур (${date_beg} - ${date_end})" echo "