Script 'tour-parse' was re-written from scratch.
This commit is contained in:
parent
86eb9c922d
commit
a07a76171b
96
tour-parse
96
tour-parse
@ -1,50 +1,72 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
# Copyright 2014 Vladimir Ivanov <ivvl82@gmail.com>
|
||||||
|
# Distributed under the terms of the GNU General Public License v2
|
||||||
|
|
||||||
YEAR=2014
|
REPO_DIR=
|
||||||
TOURNAMENT=3-fallenleaves
|
|
||||||
SCRIPT_PATH=`dirname $0`
|
|
||||||
|
|
||||||
# Regexps to parse the result of game
|
# Specify the tournament here
|
||||||
date='([0-9]{2}\.[0-9]{2}\.[0-9]{4})'
|
TOURNAMENT=
|
||||||
white='(.+)'
|
|
||||||
black='(.+)'
|
|
||||||
result='((1|0|0\.5):(1|0|0\.5))'
|
|
||||||
|
|
||||||
# Directories to search `tour_info' in
|
function parse_setup {
|
||||||
if [[ $# != 0 ]]; then
|
[[ -z $REPO_DIR ]] && REPO_DIR=`dirname "$0"`
|
||||||
tour_dirs=()
|
# Convert REPO_DIR to an absolute path
|
||||||
for number in "$@"; do
|
[[ ! $REPO_DIR =~ ^/ ]] && REPO_DIR=$(cd ${REPO_DIR}; pwd)
|
||||||
# Change tour numbers: '1' -> '01', '2' -> '02', and so on
|
|
||||||
number=$(printf "%02g" "$number")
|
|
||||||
|
|
||||||
tour_dirs+=("$SCRIPT_PATH"/"$YEAR"/"$TOURNAMENT"/tours/"$number")
|
# If no tournament given, set it to the last one
|
||||||
done
|
if [[ -z $TOURNAMENT ]]; then
|
||||||
else
|
local year_dir=$(ls -1 -d ${REPO_DIR}/[0-9][0-9][0-9][0-9]/ | tail -1)
|
||||||
tour_dirs=("$SCRIPT_PATH"/"$YEAR"/"$TOURNAMENT"/tours/*)
|
TOURNAMENT=$(ls -1 -d ${year_dir}[0-9]-*/ | tail -1 \
|
||||||
|
| sed -E "s|${REPO_DIR}/(.*)|\1|")
|
||||||
|
# Remove the trailing slash
|
||||||
|
TOURNAMENT=${TOURNAMENT%/}
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
for dir in "${tour_dirs[@]}"; do
|
function parse_tour_info {
|
||||||
number=$(printf "%01g" ${dir:(-2)}) # remove the leading `0' if any
|
# Add a newline at the end of 'tour_info' to parse the last line
|
||||||
|
local tour_info=$(cat "${REPO_DIR}/${TOURNAMENT}/tours/${tour}/tour_info"; echo)
|
||||||
echo "- number: ${number}"
|
|
||||||
echo " games:"
|
|
||||||
|
|
||||||
# Add a newline at the end of `tour_info' to parse the last line
|
|
||||||
tour_info=$(cat "$dir/tour_info" && echo)
|
|
||||||
|
|
||||||
|
local date_re="[0-9?]{2}\.[0-9?]{2}\.[0-9?]{4}"
|
||||||
|
local res_re="(1-0|1/2|0-1)"
|
||||||
while read line; do
|
while read line; do
|
||||||
# Parsed data of game: date a[1], white player a[2], black
|
if [[ $line =~ ^($date_re)\ +([^\ ]+)\ +-\ +([^\ ]+)(\ +$res_re)? ]]; then
|
||||||
# player a[6], and the result a[3]
|
local date=${BASH_REMATCH[1]}
|
||||||
data=( $(echo "$line" | gawk --re-interval \
|
local white=${BASH_REMATCH[2]}
|
||||||
'match($0, /'${date}' [-—] '${white}' '${result}' '${black}'/, a) \
|
local black=${BASH_REMATCH[3]}
|
||||||
{ print a[1], a[2], a[6], a[3] }') )
|
local result=${BASH_REMATCH[5]}
|
||||||
|
|
||||||
if [[ ${#data[@]} != 0 ]]; then
|
[[ $date =~ \? ]] && date=
|
||||||
echo " - date: ${data[0]}"
|
# Change the representation of result
|
||||||
echo " white: ${data[1]}"
|
[[ $result == 1/2 ]] && result=1/2-1/2
|
||||||
echo " black: ${data[2]}"
|
|
||||||
echo " result: '${data[3]}'"
|
local round=$(echo "($tour - 1)/($ply_count - 1) + 1" | bc)
|
||||||
echo
|
parse_to_ini
|
||||||
|
((game_counter += 1))
|
||||||
fi
|
fi
|
||||||
done <<< "$tour_info"
|
done <<< "$tour_info"
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse_to_ini {
|
||||||
|
echo "[game${game_counter}]"
|
||||||
|
echo "round = $round"
|
||||||
|
echo "date = $date"
|
||||||
|
echo "white = $white"
|
||||||
|
echo "black = $black"
|
||||||
|
echo "result = '$result'"
|
||||||
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_setup
|
||||||
|
# Configuration file for players
|
||||||
|
ply_ini=${REPO_DIR}/${TOURNAMENT}/players.ini
|
||||||
|
|
||||||
|
ply_count=$(grep "^\[player" "$ply_ini" | wc -l)
|
||||||
|
tour_seq=$(ls -1 -d ${REPO_DIR}/${TOURNAMENT}/tours/[0-9][0-9]/ \
|
||||||
|
| sed -E "s|${REPO_DIR}/${TOURNAMENT}/tours/([0-9]{2})/|\1|")
|
||||||
|
|
||||||
|
game_counter=1
|
||||||
|
for tour in $tour_seq; do
|
||||||
|
parse_tour_info
|
||||||
done
|
done
|
||||||
|
|
||||||
|
exit 0
|
||||||
|
Loading…
Reference in New Issue
Block a user