2010-09-16 14:39:50 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2010-09-17 03:56:34 +02:00
|
|
|
NGAMES=${NGAMES:-1}
|
2010-09-17 07:20:43 +02:00
|
|
|
NROOMS=${NROOMS:-1}
|
2010-09-22 14:45:40 +02:00
|
|
|
HOST=${HOST:-localhost}
|
|
|
|
PORT=${PORT:-10997}
|
2010-10-19 05:18:24 +02:00
|
|
|
TIMEOUT=${TIMEOUT:-$((NGAMES*60+500))}
|
2010-10-14 14:59:45 +02:00
|
|
|
DICTS=${DICTS:-dict.xwd}
|
|
|
|
SAVE_GOOD=${SAVE_GOOD:-YES}
|
|
|
|
|
|
|
|
declare -a DICTS_ARR
|
|
|
|
for DICT in $DICTS; do
|
|
|
|
DICTS_ARR[${#DICTS_ARR[*]}]=$DICT
|
|
|
|
done
|
2010-09-22 14:45:40 +02:00
|
|
|
|
2010-10-12 14:17:32 +02:00
|
|
|
NAMES=(UNUSED Brynn Ariela Kati Eric)
|
2010-09-17 03:56:34 +02:00
|
|
|
|
2010-09-18 16:35:02 +02:00
|
|
|
LOGDIR=$(basename $0)_logs
|
2010-09-23 15:29:55 +02:00
|
|
|
[ -d $LOGDIR ] && mv $LOGDIR /tmp/${LOGDIR}_$$
|
2010-09-18 16:35:02 +02:00
|
|
|
mkdir -p $LOGDIR
|
2010-10-15 07:17:34 +02:00
|
|
|
|
|
|
|
if [ "$SAVE_GOOD" = YES ]; then
|
|
|
|
DONEDIR=$LOGDIR/done
|
|
|
|
mkdir -p $DONEDIR
|
|
|
|
fi
|
2010-09-18 16:35:02 +02:00
|
|
|
|
2010-09-17 03:56:34 +02:00
|
|
|
USE_GTK=${USE_GTK:-FALSE}
|
|
|
|
|
2010-10-05 07:16:58 +02:00
|
|
|
declare -A PIDS
|
|
|
|
declare -A CMDS
|
|
|
|
declare -A FILES
|
|
|
|
declare -A LOGS
|
|
|
|
|
2010-09-17 03:56:34 +02:00
|
|
|
if [ $USE_GTK = FALSE ]; then
|
|
|
|
PLAT_PARMS="-u -0"
|
|
|
|
fi
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo "usage: [env=val *] $0" 1>&2
|
|
|
|
echo " current env variables and their values: " 1>&2
|
2010-10-14 14:59:45 +02:00
|
|
|
for VAR in NGAMES NROOMS USE_GTK TIMEOUT HOST PORT DICTS SAVE_GOOD; do
|
2010-09-17 03:56:34 +02:00
|
|
|
echo "$VAR:" $(eval "echo \$${VAR}") 1>&2
|
|
|
|
done
|
|
|
|
exit 0
|
|
|
|
}
|
|
|
|
|
2010-09-23 15:29:55 +02:00
|
|
|
connName() {
|
|
|
|
LOG=$1
|
|
|
|
grep 'got_connect_cmd: connName' $LOG | \
|
|
|
|
sed 's,^.*connName: \"\(.*\)\"$,\1,' | \
|
|
|
|
sort -u
|
|
|
|
}
|
|
|
|
|
2010-10-05 07:16:58 +02:00
|
|
|
while [ -n "$1" ]; do
|
|
|
|
case $1 in
|
|
|
|
*) usage
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
2010-09-20 14:33:58 +02:00
|
|
|
|
2010-10-13 04:17:42 +02:00
|
|
|
declare -A CHECKED_ROOMS
|
2010-10-12 14:17:32 +02:00
|
|
|
check_room() {
|
|
|
|
ROOM=$1
|
2010-10-13 04:17:42 +02:00
|
|
|
if [ -z "${CHECKED_ROOMS[$ROOM]}" ]; then
|
|
|
|
NUM=$(echo "SELECT COUNT(*) FROM games WHERE ntotal!=sum_array(nperdevice) AND room='$ROOM'" |
|
|
|
|
psql -q -t xwgames)
|
|
|
|
NUM=$((NUM+0))
|
|
|
|
if [ "$NUM" -gt 0 ]; then
|
|
|
|
echo "$ROOM in the DB has unconsummated games. Remove them."
|
|
|
|
exit 0
|
|
|
|
else
|
|
|
|
CHECKED_ROOMS[$ROOM]=1
|
|
|
|
fi
|
2010-10-12 14:17:32 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2010-10-05 07:16:58 +02:00
|
|
|
build_cmds() {
|
|
|
|
COUNTER=0
|
|
|
|
for GAME in $(seq 1 $NGAMES); do
|
|
|
|
ROOM=ROOM_$((GAME % NROOMS))
|
2010-10-12 14:17:32 +02:00
|
|
|
check_room $ROOM
|
2010-10-05 07:16:58 +02:00
|
|
|
NDEVS=$(($RANDOM%3+2))
|
2010-10-14 14:59:45 +02:00
|
|
|
DICT=${DICTS_ARR[$((GAME%${#DICTS_ARR[*]}))]}
|
2010-09-16 14:39:50 +02:00
|
|
|
|
2010-10-05 07:16:58 +02:00
|
|
|
unset OTHERS
|
|
|
|
for II in $(seq 2 $NDEVS); do
|
|
|
|
OTHERS="-N $OTHERS"
|
2010-09-20 14:33:58 +02:00
|
|
|
done
|
2010-09-16 14:39:50 +02:00
|
|
|
|
2010-10-05 07:16:58 +02:00
|
|
|
for DEV in $(seq $NDEVS); do
|
|
|
|
FILE="${LOGDIR}/GAME_${GAME}_${DEV}.xwg"
|
|
|
|
LOG=${LOGDIR}/${GAME}_${DEV}_LOG.txt
|
2010-10-06 05:58:29 +02:00
|
|
|
touch $LOG # so greps won't show errors
|
2010-10-05 07:16:58 +02:00
|
|
|
CMD="./obj_linux_memdbg/xwords -C $ROOM -r ${NAMES[$DEV]} $OTHERS"
|
2010-10-12 14:17:32 +02:00
|
|
|
CMD="$CMD -d $DICT -p $PORT -a $HOST -f $FILE -z 1:3 $PLAT_PARMS"
|
2010-10-05 07:16:58 +02:00
|
|
|
CMDS[$COUNTER]=$CMD
|
|
|
|
FILES[$COUNTER]=$FILE
|
|
|
|
LOGS[$COUNTER]=$LOG
|
|
|
|
PIDS[$COUNTER]=0
|
|
|
|
COUNTER=$((COUNTER+1))
|
|
|
|
done
|
2010-09-16 14:39:50 +02:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2010-10-05 07:16:58 +02:00
|
|
|
launch() {
|
|
|
|
LOG=${LOGS[$1]}
|
|
|
|
CMD="${CMDS[$1]}"
|
|
|
|
exec $CMD >/dev/null 2>>$LOG
|
|
|
|
}
|
|
|
|
|
|
|
|
close_device() {
|
|
|
|
ID=$1
|
2010-10-12 14:17:32 +02:00
|
|
|
MVTO=$2
|
2010-10-05 07:16:58 +02:00
|
|
|
if [ ${PIDS[$ID]} -ne 0 ]; then
|
2010-10-12 14:17:32 +02:00
|
|
|
kill ${PIDS[$ID]} 2>/dev/null
|
|
|
|
wait ${PIDS[$ID]}
|
2010-10-05 07:16:58 +02:00
|
|
|
fi
|
|
|
|
unset PIDS[$ID]
|
|
|
|
unset CMDS[$ID]
|
2010-10-15 07:17:34 +02:00
|
|
|
if [ -n "$MVTO" ]; then
|
2010-10-14 14:59:45 +02:00
|
|
|
[ -f ${FILES[$ID]} ] && mv ${FILES[$ID]} $MVTO
|
|
|
|
mv ${LOGS[$ID]} $MVTO
|
|
|
|
else
|
|
|
|
rm -f ${FILES[$ID]}
|
|
|
|
rm -f ${LOGS[$ID]}
|
|
|
|
fi
|
2010-10-05 07:16:58 +02:00
|
|
|
unset FILES[$ID]
|
|
|
|
unset LOGS[$ID]
|
|
|
|
}
|
2010-09-16 14:39:50 +02:00
|
|
|
|
2010-10-05 07:16:58 +02:00
|
|
|
check_game() {
|
|
|
|
KEY=$1
|
|
|
|
LOG=${LOGS[$KEY]}
|
|
|
|
CONNNAME="$(connName $LOG)"
|
|
|
|
unset OTHERS
|
|
|
|
if [ -n "$CONNNAME" ]; then
|
2010-10-06 05:58:29 +02:00
|
|
|
if grep -q '\[unused tiles\]' $LOG; then
|
2010-10-05 07:16:58 +02:00
|
|
|
ALL_DONE=TRUE
|
|
|
|
for INDX in ${!LOGS[*]}; do
|
|
|
|
[ $INDX -eq $KEY ] && continue
|
|
|
|
ALOG=${LOGS[$INDX]}
|
|
|
|
CONNNAME2="$(connName $ALOG)"
|
|
|
|
if [ "$CONNNAME2" = "$CONNNAME" ]; then
|
2010-10-06 05:58:29 +02:00
|
|
|
if ! grep -q '\[unused tiles\]' $ALOG; then
|
2010-10-05 07:16:58 +02:00
|
|
|
unset OTHERS
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
OTHERS="$OTHERS $INDX"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -n "$OTHERS" ]; then
|
2010-10-12 14:17:32 +02:00
|
|
|
echo -n "Closing $CONNNAME: "
|
2010-10-05 07:16:58 +02:00
|
|
|
for ID in $OTHERS $KEY; do
|
2010-10-12 14:17:32 +02:00
|
|
|
echo -n "${LOGS[$ID]}, "
|
|
|
|
close_device $ID $DONEDIR
|
2010-10-05 07:16:58 +02:00
|
|
|
done
|
2010-10-12 14:17:32 +02:00
|
|
|
date
|
2010-10-05 07:16:58 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
run_cmds() {
|
2010-10-06 05:58:29 +02:00
|
|
|
ENDTIME=$(($(date +%s) + TIMEOUT))
|
2010-10-05 07:16:58 +02:00
|
|
|
while :; do
|
|
|
|
COUNT=${#CMDS[*]}
|
|
|
|
[ 0 -ge $COUNT ] && break
|
2010-10-06 05:58:29 +02:00
|
|
|
[ $(date +%s) -ge $ENDTIME ] && break
|
2010-10-05 07:16:58 +02:00
|
|
|
INDX=$(($RANDOM%COUNT))
|
|
|
|
KEYS=( ${!CMDS[*]} )
|
|
|
|
KEY=${KEYS[$INDX]}
|
|
|
|
if [ 0 -eq ${PIDS[$KEY]} ]; then
|
|
|
|
launch $KEY &
|
|
|
|
PIDS[$KEY]=$!
|
|
|
|
else
|
|
|
|
sleep 2 # make sure it's had some time
|
|
|
|
kill ${PIDS[$KEY]}
|
|
|
|
PIDS[$KEY]=0
|
|
|
|
check_game $KEY
|
|
|
|
fi
|
2010-09-16 14:39:50 +02:00
|
|
|
done
|
2010-10-06 05:58:29 +02:00
|
|
|
|
|
|
|
# kill any remaining games
|
2010-10-12 14:17:32 +02:00
|
|
|
if [ $COUNT -gt 0 ]; then
|
|
|
|
mkdir -p ${LOGDIR}/not_done
|
|
|
|
echo "processing unfinished games...."
|
|
|
|
for KEY in ${!CMDS[*]}; do
|
|
|
|
close_device $KEY ${LOGDIR}/not_done
|
|
|
|
done
|
|
|
|
fi
|
2010-09-16 14:39:50 +02:00
|
|
|
}
|
|
|
|
|
2010-10-05 07:16:58 +02:00
|
|
|
print_stats() {
|
|
|
|
:
|
|
|
|
}
|
2010-09-17 03:56:34 +02:00
|
|
|
|
2010-10-13 04:17:42 +02:00
|
|
|
echo "*********$0 starting: $(date)**************"
|
|
|
|
|
2010-10-05 07:16:58 +02:00
|
|
|
build_cmds
|
|
|
|
run_cmds
|
|
|
|
print_stats
|
2010-09-17 03:56:34 +02:00
|
|
|
|
|
|
|
wait
|
2010-10-13 04:17:42 +02:00
|
|
|
echo "*********$0 finished: $(date)**************"
|