Change function 'game_get_players' and its nested functions.
This commit is contained in:
parent
068b32de98
commit
52cb416ad8
128
game-add
128
game-add
@ -49,9 +49,7 @@ function version {
|
||||
}
|
||||
|
||||
function game_setup {
|
||||
[[ -z $REPO_DIR ]] && REPO_DIR=`dirname "$0"`
|
||||
# Convert REPO_DIR to an absolute path
|
||||
[[ ! $REPO_DIR =~ ^/ ]] && REPO_DIR=$(cd ${REPO_DIR}; pwd)
|
||||
: ${REPO_DIR:=`dirname "$0"`}
|
||||
|
||||
# If no tournament given, set it to the last one
|
||||
if [[ -z $TOURNAMENT ]]; then
|
||||
@ -64,9 +62,14 @@ function game_setup {
|
||||
|
||||
# Configuration file for players
|
||||
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
|
||||
[[ $# == 1 ]] && SORT_GAMES=false
|
||||
|
||||
@ -88,33 +91,49 @@ function game_mktemp {
|
||||
# Store PGN file in a temporal location
|
||||
local tmp_pgn=$(mktemp -t ${pgn_template}.XXXXXX)
|
||||
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" \
|
||||
|| die "Unreachable game PGN ${game_url}"
|
||||
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 {
|
||||
# Extract players on Lichess
|
||||
local wt_lichess=$(sed -En "s/\[White \"([^\"]*)\"\]/\1/p" $tmp_pgn)
|
||||
local bk_lichess=$(sed -En "s/\[Black \"([^\"]*)\"\]/\1/p" $tmp_pgn)
|
||||
# Make an associative array from Lichess nicks to players' names
|
||||
declare -A NAMES=()
|
||||
game_assoc_names
|
||||
|
||||
# Get names of white and black players
|
||||
local counter=1 players=()
|
||||
game_parse_config
|
||||
while eval "config_section_player${counter}" 2>/dev/null; do
|
||||
players+=("$name")
|
||||
[[ $lichess == $wt_lichess ]] && white=$name
|
||||
[[ $lichess == $bk_lichess ]] && black=$name
|
||||
((counter += 1))
|
||||
local ply_names=
|
||||
for pgn in $TMP_PGN_FILES; do
|
||||
# Extract players on Lichess
|
||||
local wt_lichess=$(sed -En "s/\[White \"([^\"]*)\"\]/\1/p" $pgn)
|
||||
local bk_lichess=$(sed -En "s/\[Black \"([^\"]*)\"\]/\1/p" $pgn)
|
||||
|
||||
game_add_player $wt_lichess
|
||||
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
|
||||
[[ -z $white ]] && fix_white_player
|
||||
[[ -z $black ]] && fix_black_player
|
||||
}
|
||||
|
||||
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
|
||||
# NOTE: an empty line is added to the file beginning in order to
|
||||
# match the only first occurrence for non-GNU sed
|
||||
@ -133,39 +152,22 @@ function game_parse_config {
|
||||
source "$tmp_ini"
|
||||
}
|
||||
|
||||
function fix_white_player {
|
||||
local number
|
||||
echo "Lichess player '${wt_lichess}' not found."
|
||||
choose_player
|
||||
white=${players[$number]}
|
||||
}
|
||||
|
||||
function fix_black_player {
|
||||
local number
|
||||
echo "Lichess player '${bk_lichess}' not found."
|
||||
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_player {
|
||||
local lichess_ply=$1
|
||||
local ply=${NAMES[$lichess_ply]}
|
||||
while [[ ! " ${NAMES[@]} " =~ \ $ply\ ]]; do
|
||||
echo "The list of players:"
|
||||
echo "${NAMES[@]}" | tr ' ' '\n' | sed "s/^/$(tput setaf 6)*$(tput sgr0) /"
|
||||
echo -n "Type the name of ${lichess_ply}> "
|
||||
read ply
|
||||
done
|
||||
ply_names+="${ply}\n"
|
||||
}
|
||||
|
||||
function game_add_to_repo {
|
||||
local date_re="[0-9?]{2}\.[0-9?]{2}\.[0-9?]{4}"
|
||||
local correct=false length_max=0
|
||||
local white=${players[0]} black=${players[1]}
|
||||
# Change field separator to read a file line by line
|
||||
old_IFS=$IFS IFS=$'\n'
|
||||
# Check if the tour number is correct
|
||||
@ -185,7 +187,7 @@ function game_add_to_repo {
|
||||
local answer
|
||||
echo -n "Approve game with wrong players' sides? (Y/n)> "
|
||||
read answer
|
||||
[[ ! $answer =~ ^(Y|y|Yes|yes)$ ]] && exit 1
|
||||
[[ $answer =~ ^(Y|y|Yes|yes)$ ]] || exit 1
|
||||
white=$fst_ply black=$snd_ply correct=true
|
||||
fi
|
||||
|
||||
@ -225,10 +227,12 @@ function game_add_to_repo {
|
||||
}
|
||||
|
||||
function game_git_commit {
|
||||
cd $REPO_DIR
|
||||
git add "${pgn_dir}/1.pgn" "$tour_info"
|
||||
git commit -m "Tour ${TOUR#0}: ${white} vs. ${black}."
|
||||
git push
|
||||
(
|
||||
cd $REPO_DIR
|
||||
git add "${pgn_dir}/1.pgn" "$tour_info"
|
||||
git commit -m "Tour ${TOUR#0}: ${white} vs. ${black}."
|
||||
git push
|
||||
)
|
||||
}
|
||||
|
||||
function die {
|
||||
@ -237,10 +241,8 @@ function die {
|
||||
}
|
||||
|
||||
function checkargs {
|
||||
if [[ $opt == t ]]; then
|
||||
[[ ! $OPTARG =~ ^[0-9]+$ ]] && die "Incorrect tour number."
|
||||
TOUR=$(printf "%02g" $OPTARG)
|
||||
fi
|
||||
[[ $OPTARG =~ ^[0-9]+$ ]] || die "Incorrect tour number."
|
||||
TOUR=$(printf "%02g" $OPTARG)
|
||||
}
|
||||
|
||||
SORT_GAMES=true
|
||||
@ -259,18 +261,8 @@ shift $(($OPTIND - 1))
|
||||
[[ -z $TOUR || $# == 0 ]] && usage 1
|
||||
|
||||
game_setup
|
||||
tour_info=${REPO_DIR}/${TOURNAMENT}/tours/${TOUR}/tour_info
|
||||
[[ ! -f $tour_info ]] && die "File ${tour_info} not found."
|
||||
|
||||
# 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 $@
|
||||
|
||||
(cd $REPO_DIR; git pull) # update the repository
|
||||
game_tmp_pgns $@
|
||||
game_get_players
|
||||
game_add_to_repo
|
||||
game_git_commit
|
||||
|
Loading…
Reference in New Issue
Block a user