1
Fork 0

Change function 'game_get_players' and its nested functions.

master
vonavi 2014-11-28 16:11:24 +03:00
parent 068b32de98
commit 52cb416ad8
1 changed files with 60 additions and 68 deletions

128
game-add
View File

@ -49,9 +49,7 @@ function version {
} }
function game_setup { function game_setup {
[[ -z $REPO_DIR ]] && REPO_DIR=`dirname "$0"` : ${REPO_DIR:=`dirname "$0"`}
# Convert REPO_DIR to an absolute path
[[ ! $REPO_DIR =~ ^/ ]] && REPO_DIR=$(cd ${REPO_DIR}; pwd)
# If no tournament given, set it to the last one # If no tournament given, set it to the last one
if [[ -z $TOURNAMENT ]]; then if [[ -z $TOURNAMENT ]]; then
@ -64,9 +62,14 @@ function game_setup {
# Configuration file for players # Configuration file for players
ply_ini=${REPO_DIR}/${TOURNAMENT}/players.ini ply_ini=${REPO_DIR}/${TOURNAMENT}/players.ini
[[ -f $ply_ini ]] || die "File ${ply_ini} not found."
# Pairs of players on the tour
tour_info=${REPO_DIR}/${TOURNAMENT}/tours/${TOUR}/tour_info
[[ -f $tour_info ]] || die "File ${tour_info} not found."
} }
function game_mktemp { function game_tmp_pgns {
TMP_PGN_FILES=
# Don't sort just one game # Don't sort just one game
[[ $# == 1 ]] && SORT_GAMES=false [[ $# == 1 ]] && SORT_GAMES=false
@ -88,33 +91,49 @@ function game_mktemp {
# Store PGN file in a temporal location # Store PGN file in a temporal location
local tmp_pgn=$(mktemp -t ${pgn_template}.XXXXXX) local tmp_pgn=$(mktemp -t ${pgn_template}.XXXXXX)
TMP_PGN_FILES+=" $tmp_pgn" TMP_PGN_FILES+=" $tmp_pgn"
trap "rm $TMP_INI_FILES $TMP_PGN_FILES" EXIT trap "rm $TMP_PGN_FILES" EXIT
wget -q -U "$FIREFOX_UA" -O $tmp_pgn "$game_url" \ wget -q -U "$FIREFOX_UA" -O $tmp_pgn "$game_url" \
|| die "Unreachable game PGN ${game_url}" || die "Unreachable game PGN ${game_url}"
done done
$SORT_GAMES && TMP_PGN_FILES=$(echo "$TMP_PGN_FILES" | xargs -n1 | sort | xargs) $SORT_GAMES && TMP_PGN_FILES=$(xargs -n1 <<< "$TMP_PGN_FILES" | sort | xargs)
} }
function game_get_players { function game_get_players {
# Extract players on Lichess # Make an associative array from Lichess nicks to players' names
local wt_lichess=$(sed -En "s/\[White \"([^\"]*)\"\]/\1/p" $tmp_pgn) declare -A NAMES=()
local bk_lichess=$(sed -En "s/\[Black \"([^\"]*)\"\]/\1/p" $tmp_pgn) game_assoc_names
# Get names of white and black players local ply_names=
local counter=1 players=() for pgn in $TMP_PGN_FILES; do
game_parse_config # Extract players on Lichess
while eval "config_section_player${counter}" 2>/dev/null; do local wt_lichess=$(sed -En "s/\[White \"([^\"]*)\"\]/\1/p" $pgn)
players+=("$name") local bk_lichess=$(sed -En "s/\[Black \"([^\"]*)\"\]/\1/p" $pgn)
[[ $lichess == $wt_lichess ]] && white=$name
[[ $lichess == $bk_lichess ]] && black=$name game_add_player $wt_lichess
((counter += 1)) game_add_player $bk_lichess
done
# Select the names of two players
players=( $(echo -en "$ply_names" | sort -u) )
[[ ${#players[@]} == 2 ]] || die "Players of the games are not the same."
}
function game_assoc_names {
game_parse_config
local sections=$(grep -o "config_section_player[0-9]*" $tmp_ini)
for sect in $sections; do
eval $sect
NAMES+=( [$lichess]=$name )
done done
[[ -z $white ]] && fix_white_player
[[ -z $black ]] && fix_black_player
} }
function game_parse_config { function game_parse_config {
# Temporary files
tmp_ini=$(mktemp -t `basename $ply_ini`.XXXXXX)
TMP_INI_FILES="${tmp_ini} ${tmp_ini}.prev"
trap "rm $TMP_INI_FILES $TMP_PGN_FILES" EXIT
# Copy player INI file to the temporary location # Copy player INI file to the temporary location
# NOTE: an empty line is added to the file beginning in order to # NOTE: an empty line is added to the file beginning in order to
# match the only first occurrence for non-GNU sed # match the only first occurrence for non-GNU sed
@ -133,39 +152,22 @@ function game_parse_config {
source "$tmp_ini" source "$tmp_ini"
} }
function fix_white_player { function game_add_player {
local number local lichess_ply=$1
echo "Lichess player '${wt_lichess}' not found." local ply=${NAMES[$lichess_ply]}
choose_player while [[ ! " ${NAMES[@]} " =~ \ $ply\ ]]; do
white=${players[$number]} echo "The list of players:"
} echo "${NAMES[@]}" | tr ' ' '\n' | sed "s/^/$(tput setaf 6)*$(tput sgr0) /"
echo -n "Type the name of ${lichess_ply}> "
function fix_black_player { read ply
local number done
echo "Lichess player '${bk_lichess}' not found." ply_names+="${ply}\n"
choose_player
black=${players[$number]}
}
function choose_player {
echo "Please choose a proper name from the list below:"
for ((i=0; i<${#players[@]}; ++i)); do
echo "$((i+1)) ${players[$i]}"
done \
| column -t \
| sed -E "s/^([0-9]*)/$(tput setaf 6)\1$(tput sgr0)/" # highlight number
echo -n "Put number> "
read number
if (( $number < 1 || $number > ${#players[@]} )); then
die "Incorrect player number."
fi
((number += -1))
} }
function game_add_to_repo { function game_add_to_repo {
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 correct=false length_max=0 local correct=false length_max=0
local white=${players[0]} black=${players[1]}
# Change field separator to read a file line by line # Change field separator to read a file line by line
old_IFS=$IFS IFS=$'\n' old_IFS=$IFS IFS=$'\n'
# Check if the tour number is correct # Check if the tour number is correct
@ -185,7 +187,7 @@ function game_add_to_repo {
local answer local answer
echo -n "Approve game with wrong players' sides? (Y/n)> " echo -n "Approve game with wrong players' sides? (Y/n)> "
read answer read answer
[[ ! $answer =~ ^(Y|y|Yes|yes)$ ]] && exit 1 [[ $answer =~ ^(Y|y|Yes|yes)$ ]] || exit 1
white=$fst_ply black=$snd_ply correct=true white=$fst_ply black=$snd_ply correct=true
fi fi
@ -225,10 +227,12 @@ function game_add_to_repo {
} }
function game_git_commit { function game_git_commit {
cd $REPO_DIR (
git add "${pgn_dir}/1.pgn" "$tour_info" cd $REPO_DIR
git commit -m "Tour ${TOUR#0}: ${white} vs. ${black}." git add "${pgn_dir}/1.pgn" "$tour_info"
git push git commit -m "Tour ${TOUR#0}: ${white} vs. ${black}."
git push
)
} }
function die { function die {
@ -237,10 +241,8 @@ function die {
} }
function checkargs { function checkargs {
if [[ $opt == t ]]; then [[ $OPTARG =~ ^[0-9]+$ ]] || die "Incorrect tour number."
[[ ! $OPTARG =~ ^[0-9]+$ ]] && die "Incorrect tour number." TOUR=$(printf "%02g" $OPTARG)
TOUR=$(printf "%02g" $OPTARG)
fi
} }
SORT_GAMES=true SORT_GAMES=true
@ -259,18 +261,8 @@ shift $(($OPTIND - 1))
[[ -z $TOUR || $# == 0 ]] && usage 1 [[ -z $TOUR || $# == 0 ]] && usage 1
game_setup game_setup
tour_info=${REPO_DIR}/${TOURNAMENT}/tours/${TOUR}/tour_info (cd $REPO_DIR; git pull) # update the repository
[[ ! -f $tour_info ]] && die "File ${tour_info} not found." game_tmp_pgns $@
# Ensure that the repository is up-to-date
git pull
# Temporary files
tmp_ini=$(mktemp -t `basename $ply_ini`.XXXXXX)
TMP_INI_FILES="${tmp_ini} ${tmp_ini}.prev"
TMP_PGN_FILES=
game_mktemp $@
game_get_players game_get_players
game_add_to_repo game_add_to_repo
game_git_commit game_git_commit