moth/packages/tanks/service/tanksd/tanksd

123 lines
2.8 KiB
Bash
Executable File

#! /bin/sh
d=/var/lib/ctf/tanks
p=$d/players
w=/var/www/tanks
summary () {
cat <<EOF
<!DOCTYPE html>
<html>
<head>
<title>Tanks</title>
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body>
<h1>Tanks</h1>
<p>New here? Start with the <a href="intro.html">introduction</a>.</p>
<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)
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
cat <<EOF
</body>
</html>
EOF
}
while true; do
# Make sure all teams exist
KEY='Too much cheese.' arc4 < /var/lib/ctf/teams.txt | \
while read hash; do
install -o ctf -d $p/$hash
done
# Has anyone submitted a program yet?
if [ $(find $p -name program | wc -l) = 0 ]; then
sleep 15
continue
fi
# 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)
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">
<script type="application/javascript">
function go() {
start("battlefield",
// Start JSON data
EOF
/opt/tanks/bin/forftanks $p/* >>$fn 3>$rfn
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
cat <<EOF >>$fn
</body>
</html>
EOF
# Get a token and add it to the redemption log
nc 10.0.0.2 1 -e /opt/ctfbase/bin/tokencli tanks ./category.key 3> $tfn
k=$(cat $tfn)
winner.awk $rfn | while read winner; do
hash=$(basename $winner)
echo "Round $next winner: $hash"
# 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"
done
ln -sf $fn $w/current.html
summary > $w/summary.html.$$
mv -f $w/summary.html.$$ $w/summary.html
rm -f $tfn $rfn
sleep 60
done