rendered paste body#!/bin/bash
############################################################################
# Generador + anàlisis de partides de apocalypse-eda
############################################################################
#
# executar amb paràmetre "help" per a informació relacionada amb el seu ús.
# Requereix el programa estadístic R (r-project) per a funcionar correctament.
#
############################################################################
# Copyright Pau Espin Pedrol 2010
############################################################################
#CONFIGURACIÓ:
#Ordre de terminal amb que executem el binari del APOnow:
VIEWER="./APOnow"
#Llista de jugadors:
J1="aurik"
J2="Battle"
J3="pespinold"
J4="Rocketeer"
#Fitxer on es guarda el que passa cada torn de cada partida
#LOG_FILE="/tmp/eda-output"
LOG_FILE="/dev/null"
#--------NO TOCAR A PARTIR D'AQUI------------------------------
JS="$J1 $J2 $J3 $J4"
RESULTS_FILE="/tmp/generador-eda"
NUM_PARTIDES_FILE="/tmp/num_partides"
SCORE_FILE_PREFIX="/tmp/score"
WINS_FILE_PREFIX="/tmp/wins"
R_RESULT_FILE_PREFIX="/tmp/R_result"
PARTIDES_PREFIX="/tmp/partida"
#if [ ! -f "$LOG_FILE" ]; then
# touch "$LOG_FILE"
#fi
CALL_VIEWER="$VIEWER $JS --nogui"
function show_help() {
echo "Forma d'ús:"
echo "$0 CMD"
echo ""
echo "On CMD pot ser:"
echo "help Mostra aquest text d'ajuda"
echo "clean Neteja informació temporal de partides anteriors. (S'esborren automàticament en apagar l'ordinador)"
echo "results Fa i mostra per pantalla els càlculs de les partides emmagatzemades"
echo "NUM On NUM és un nombre natural. El programa generarà NUM partides entre els jugadors
especificats a la configuració, i un cop generades, farà els anàlisis i calculs
pertinents tot mostrant-los per pantalla"
echo ""
echo ""
echo "NOTA: recorda que potser hauràs de modificar la configuració al mateix fitxer de l'script!"
}
function getline() {
sed -n "${1}p" "${2}"
}
function clean_stuff() {
rm -f "$LOG_FILE"
rm -f "$RESULTS_FILE"
rm -f "$R_SCRIPT_FILE"
rm -f "$NUM_PARTIDES_FILE"
rm -f ${PARTIDES_PREFIX}_*.apo
rm -f ${WINS_FILE_PREFIX}_${J1}_1 ${WINS_FILE_PREFIX}_${J2}_2 ${WINS_FILE_PREFIX}_${J3}_3 ${WINS_FILE_PREFIX}_${J4}_4
rm -f ${R_RESULT_FILE_PREFIX}_${J1}_1 ${R_RESULT_FILE_PREFIX}_${J2}_2 ${R_RESULT_FILE_PREFIX}_${J3}_3 ${R_RESULT_FILE_PREFIX}_${J4}_4
rm -f ${SCORE_FILE_PREFIX}_${J1}_1 ${SCORE_FILE_PREFIX}_${J2}_2 ${SCORE_FILE_PREFIX}_${J3}_3 ${SCORE_FILE_PREFIX}_${J4}_4
}
function calc_stats() {
R --quiet -e "v = read.table(\"$1\")\$V1; median(v); mean(v); sd(v)" | grep -v ">" | tr " " "\n" | grep -v "\[1\]" | tr "." "," > $2
}
function play() {
echo "-------------------------------------------" >> "$LOG_FILE"
echo "-------- `date` ------- $JS" >> "$LOG_FILE"
$CALL_VIEWER --out=${PARTIDES_PREFIX}_${1}.apo 2>> $LOG_FILE > $RESULTS_FILE
echo "-------------------------------------------" >> "$LOG_FILE"
J1_POINTS=`getline 1 "$RESULTS_FILE"`
J2_POINTS=`getline 2 "$RESULTS_FILE"`
J3_POINTS=`getline 3 "$RESULTS_FILE"`
J4_POINTS=`getline 4 "$RESULTS_FILE"`
echo "${J1_POINTS}" >> ${SCORE_FILE_PREFIX}_${J1}_1
echo "${J2_POINTS}" >> ${SCORE_FILE_PREFIX}_${J2}_2
echo "${J3_POINTS}" >> ${SCORE_FILE_PREFIX}_${J3}_3
echo "${J4_POINTS}" >> ${SCORE_FILE_PREFIX}_${J4}_4
if [ ${J1_POINTS} -gt ${J2_POINTS} ] && [ ${J1_POINTS} -gt ${J3_POINTS} ] && [ ${J1_POINTS} -gt ${J4_POINTS} ]; then
increment_wins ${J1}_1; echo " guanya $J1 (${J1_POINTS} punts)"
elif [ ${J2_POINTS} -gt ${J1_POINTS} ] && [ ${J2_POINTS} -gt ${J3_POINTS} ] && [ ${J2_POINTS} -gt ${J4_POINTS} ]; then
increment_wins ${J2}_2; echo " guanya $J2 (${J2_POINTS} punts)"
elif [ ${J3_POINTS} -gt ${J1_POINTS} ] && [ ${J3_POINTS} -gt ${J2_POINTS} ] && [ ${J3_POINTS} -gt ${J4_POINTS} ]; then
increment_wins ${J3}_3; echo " guanya $J3 (${J3_POINTS} punts)"
elif [ ${J4_POINTS} -gt ${J1_POINTS} ] && [ ${J4_POINTS} -gt ${J2_POINTS} ] && [ ${J4_POINTS} -gt ${J3_POINTS} ]; then
increment_wins ${J4}_4; echo " guanya $J4 (${J4_POINTS} punts)"
fi
}
function get_median() {
getline 1 "${R_RESULT_FILE_PREFIX}_${1}"
}
function get_mean() {
getline 2 "${R_RESULT_FILE_PREFIX}_${1}"
}
function get_sd() {
getline 3 "${R_RESULT_FILE_PREFIX}_${1}"
}
function get_num_partides_guanyades() {
cat "${WINS_FILE_PREFIX}_${1}"
}
function get_num_partides() {
cat "${NUM_PARTIDES_FILE}"
}
function get_perc_partides_guanyades() {
guany=`get_num_partides_guanyades "$1"`
total=`get_num_partides`
#echo `expr $guany / $total`
echo `awk "BEGIN{print ${guany} / ${total}}"`
}
function increment_wins() {
wins=`get_num_partides_guanyades "$1"`
echo `expr $wins + 1` > ${WINS_FILE_PREFIX}_${1}
#echo "incrementem ${WINS_FILE_PREFIX}_${1} en 1..."
}
function print_line() {
printf "%-20s %-20s %-20s %-20s %-20s\n" "$@"
}
function show_results() {
calc_stats ${SCORE_FILE_PREFIX}_${J1}_1 ${R_RESULT_FILE_PREFIX}_${J1}_1
calc_stats ${SCORE_FILE_PREFIX}_${J2}_2 ${R_RESULT_FILE_PREFIX}_${J2}_2
calc_stats ${SCORE_FILE_PREFIX}_${J3}_3 ${R_RESULT_FILE_PREFIX}_${J3}_3
calc_stats ${SCORE_FILE_PREFIX}_${J4}_4 ${R_RESULT_FILE_PREFIX}_${J4}_4
print_line "*" ${J1} ${J2} ${J3} ${J4}
print_line "mediana:" `get_median ${J1}_1` `get_median ${J2}_2` `get_median ${J3}_3` `get_median ${J4}_4`
print_line "mitjana:" `get_mean ${J1}_1` `get_mean ${J2}_2` `get_mean ${J3}_3` `get_mean ${J4}_4`
print_line "desviacio:" `get_sd ${J1}_1` `get_sd ${J2}_2` `get_sd ${J3}_3` `get_sd ${J4}_4`
print_line "n.guanyades:" `get_num_partides_guanyades ${J1}_1` `get_num_partides_guanyades ${J2}_2` `get_num_partides_guanyades ${J3}_3` `get_num_partides_guanyades ${J4}_4`
print_line "p.guanyades:" `get_perc_partides_guanyades ${J1}_1` `get_perc_partides_guanyades ${J2}_2` `get_perc_partides_guanyades ${J3}_3` `get_perc_partides_guanyades ${J4}_4`
}
if [ "$1" == "clean" ]; then
clean_stuff
elif [ "$1" == "results" ]; then
show_results
elif [ "$1" == "help" ] || [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
show_help
else
if [ ! -f ${NUM_PARTIDES_FILE} ]; then echo 0 > ${NUM_PARTIDES_FILE}; fi
if [ ! -f ${WINS_FILE_PREFIX}_${J1}_1 ]; then echo 0 > ${WINS_FILE_PREFIX}_${J1}_1; fi
if [ ! -f ${WINS_FILE_PREFIX}_${J2}_2 ]; then echo 0 > ${WINS_FILE_PREFIX}_${J2}_2; fi
if [ ! -f ${WINS_FILE_PREFIX}_${J3}_3 ]; then echo 0 > ${WINS_FILE_PREFIX}_${J3}_3; fi
if [ ! -f ${WINS_FILE_PREFIX}_${J4}_4 ]; then echo 0 > ${WINS_FILE_PREFIX}_${J4}_4; fi
if [ -z $1 ]; then
max=10
else
max=$1
fi
i=`get_num_partides`
max=`expr $max + $i`
echo "Executant partides amb la ordre:"
echo "$CALL_VIEWER 2>> $LOG_FILE >> $RESULTS_FILE"
echo ""
while [ $i -lt $max ]; do
echo -n "partida $i: "
play $i
i=`expr $i + 1`
echo $i > ${NUM_PARTIDES_FILE}
done
echo ""
show_results
fi