moth/packages/tanks/service/tanksd/tanksd

124 lines
2.7 KiB
Plaintext
Raw Normal View History

2010-10-26 13:02:26 -06:00
#! /bin/sh
d=/var/lib/ctf/tanks
p=$d/players
w=/var/www/tanks
2011-11-05 14:27:56 -06:00
log=$d/winners.log
2010-10-26 13:02:26 -06:00
summary () {
cat <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Tanks</title>
<link rel="stylesheet" href="style.css" type="text/css">
2010-10-26 13:02:26 -06:00
</head>
<body>
<h1>Tanks</h1>
<p>New here? Start with the <a href="intro.html">introduction</a>.</p>
2010-10-26 13:02:26 -06:00
<p>New round every minute.</p>
<h2>Round results</h2>
<ul>
EOF
printed=0
find $w -name "round-*.html" | sort -r | while read fn; do
b=$(basename $fn)
2010-10-26 13:02:26 -06:00
if [ $printed -lt 20 ]; then
echo "<li><a href=\"$b\">$b</a></li>"
else
rm -f $fn
fi
printed=$(expr $printed + 1)
done
cat <<EOF
</ul>
EOF
cat /opt/tanks/html/nav.html.inc
2010-10-26 13:02:26 -06:00
cat <<EOF
</body>
</html>
EOF
}
while true; do
2011-11-05 14:27:56 -06:00
for dn in /var/lib/ctf/teams/names/*; do
hash=${dn##*/}
install -o ctf -d $p/$hash
2010-10-26 13:02:26 -06:00
done
# Has anyone submitted a program yet?
if [ $(find $p -name program | wc -l) = 0 ]; then
sleep 15
continue
fi
2010-10-26 13:02:26 -06:00
# Round number?
if [ -f $d/next-round ]; then
next=$(cat $d/next-round)
else
next=0
fi
expr $next + 1 > $d/next-round
fn=$(printf "%s/round-%04d.html" $w $next)
2010-10-26 13:02:26 -06:00
rfn=$(printf "/tmp/tanks-results-%04d.txt" $next)
tfn=$(printf "/tmp/tanks-token-%04d.txt" $next)
# Run a game
echo "Running round $next"
cat <<EOF >$fn
<!DOCTYPE html>
<html>
<head>
<title>Tanks Round $next</title>
<script type="application/javascript" src="tanks.js"></script>
<link rel="stylesheet" href="style.css" type="text/css">
2010-10-26 13:02:26 -06:00
<script type="application/javascript">
function go() {
start("battlefield",
// Start JSON data
EOF
/opt/tanks/bin/forftanks $p/* >>$fn 3>$rfn
2010-10-26 13:02:26 -06:00
cat <<EOF >>$fn
// end JSON data
);
}
window.onload = go;
</script>
</head>
<body>
<h1>Tanks Round $next</h1>
<div id="game_box"><canvas id="battlefield"></canvas></div>
<p><span id="fps">0</span> fps</p>
EOF
/opt/tanks/bin/rank.awk $rfn >>$fn
cat /opt/tanks/html/nav.html.inc >>$fn
2010-10-26 13:02:26 -06:00
cat <<EOF >>$fn
</body>
</html>
EOF
winner.awk $rfn | while read winner; do
hash=$(basename $winner)
2011-11-05 14:27:56 -06:00
echo "Round $next winner: $hash" >> $log
nwinners=$(wc -l $log)
# Read a token
k=$(sed -n ${nwinners}p /opt/tanks/tokens.txt)
2010-10-26 13:02:26 -06:00
# XXX: pull this out into another daemon
# XXX: this puts the token in /proc/self/cmdline
wget -q -s "http://10.0.0.2/claim.cgi?t=$hash&k=$k"
2010-10-26 13:02:26 -06:00
done
2011-03-14 10:52:25 -06:00
ln -sf $fn $w/current.html
summary > $w/summary.html.$$
mv -f $w/summary.html.$$ $w/summary.html
rm -f $tfn $rfn
sleep 60
2010-10-26 13:02:26 -06:00
done