3 # This script runs a single round of the competition
11 # Ideally swarm shouldn't crash
12 # But I'm a shit programmer
13 # So I'll leave this in
14 kill -0 $1 2>&1 >> /dev/null
15 if [ "$?" != "0" ]; then
16 echo "Swarm $pid is no longer running! $2" 1>&2
21 # Copy messages to a log file
22 if [ "$BASH_ARGV" != "_worker_" ]; then
23 # Get the round number
26 round=$(ls | grep "round" | sed -e "s:round::g" | sort -n | head --lines=1)
27 if [ "$round" == "" ]; then round=0; fi
28 round=$(( $round + 1 ))
31 exec $0 "$@" _worker_ 2>&1 | tee $results_dir/round$round/run.log
35 round=$(ls | grep "round" | sed -e "s:round::g" | sort -n | head --lines=1)
38 echo "Start at $(date)"
42 if [ "$swarm_hosts" != "" ]; then
43 swarm_pid=$(swarm --daemon -l daemon.log:100)
44 if [ "$?" != "0" ]; then
45 echo "Couldn't start swarm daemon!" 1>&2
49 for h in $swarm_hosts; do
50 if [ "$h" != "local" ]; then
51 swarm -c "#ABSORB $h#"
54 sanity_swarm $swarm_pid
55 swarm -c "#.*# cd $root_dir; mkdir -p $results_dir/round$round" -l +wrapper.log:100
61 cd $root_dir/$agent_dir
64 info_file=$(ls $f | grep -w "info")
65 if [ "$info_file" == "" ]; then
66 echo "Skipping agent $f (missing info file)"
68 count=$(( $count + 1 ))
73 echo "Found $count agents in $agent_dir/"
77 # Add all the games to swarm
80 if [ "$agents" == "" ]; then
81 echo "No agents to run round $round with" 1>&2
82 rm -rf "$results_dir/round$round"
86 echo "Start round $round"
90 get_number="echo \$name | tr ':' ' ' | awk '{print \$2}'"
92 runa="$agent_dir/$a/$(head --lines=1 $agent_dir/$a/info)"
94 if [ "$a" == "$b" ]; then continue; fi
95 for ((i=1;i<=$games_per_pair;++i)); do
96 runb="$agent_dir/$b/$(head --lines=1 $agent_dir/$b/info)"
100 l="$results_dir/round$round/$f.log"
101 err="$results_dir/round$round/$f.err"
103 echo "Game #$game: $a .vs. $b ($i of $games_per_pair)"
104 if [ "$swarm_hosts" != "" ]; then
105 sanity_swarm $swarm_pid
106 swarm -c "#OUTPUT $f.result#"
107 swarm -c "$qchess --no-graphics \"$runa\" \"$runb\" --log=$l --log=@web/current\$($get_number).log 2>$err" -l +wrapper.log:100
109 $qchess --no-graphics "$runa" "$runb" --log=$l --log=@web/current.log 1> $f.result 2> $err
110 if [ "$(wc -l $err | awk '{print $1}')" == "0" ]; then rm $err; fi
118 if [ "$swarm_hosts" != "" ]; then
119 echo "Waiting for games to finish"
120 sanity_swarm $swarm_pid
121 swarm -c "#BARRIER BLOCK#" # Wait for all games to finish
123 #Copy over log files (start before updating scores as the scp may take some time)
124 for h in "local $swarm_hosts"; do
126 if [ \"\$(hostname)\" != \"$webserver\" ]; then
127 cd $root_dir/$results_dir/round$round;
128 for i in *.log *.err; do
129 if [ \"\$(wc -l \$i | awk '{print \$1}')\" != 0 ]; then
130 scp \$i $webserver/$root_dir/$results_dir/round$round/\$i
134 sanity_swarm $swarm_pid
135 swarm -c "#$h:.* &# $cmd" # Execute once on each host
139 echo "Completed $games games with $count agents"
141 # A bash function. Yes, they do exist.
142 function update_score
144 if [ -e $agent_dir/$1/score.dat ]; then
145 score=$(tail --lines=1 $agent_dir/$1/score.dat | awk '{print $1}')
151 score=$(( $score + $3 ))
152 echo "$3 $score $2 $4 $5" >> $agent_dir/$1/score.dat
157 for f in *.result; do
158 log=round$round/$(echo $f.log | sed -e "s:.result::g")
159 white=$(echo $f | tr '_' '\t' | awk '{print $1}')
160 black=$(echo $f | tr '_' '\t' | awk '{print $2}')
161 if [ "$(cat $f)" == "white" ]; then
162 update_score $white $black $win_score WIN $log
163 update_score $black $white $loss_score LOSS $log
164 elif [ "$(cat $f)" == "black" ]; then
165 update_score $white $black $loss_score LOSS $log
166 update_score $black $white $win_score WIN $log
167 elif [ "$(cat $f)" == "DRAW" ]; then
168 update_score $white $black $draw_score DRAW $log
169 update_score $black $white $draw_score DRAW $log
171 echo "Unrecognised result \"$(cat $f)\" in $f" 1>&2
177 echo "Updated scores"
180 if [ "$swarm_hosts" != "" ]; then
181 swarm -c "#BARRIER BLOCK#"
185 echo "Finished at $(date)"