moth/packages/tanks/service/tanksd/tanksd

127 lines
2.8 KiB
Bash
Executable File

#! /bin/sh
PACKAGES=$CTF_BASE/packages
STATE=$CTF_BASE/state
WWW=$CTF_BASE/www
TSTATE=$STATE/tanks
TPLAYERS=$TSTATE/players
TLOG=$TSTATE/winners.log
TWWW=$TSTATE/www
[ "$1" = "--once" ] && once=1
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 $TWWW -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 $packages/tanks/html/nav.html.inc
cat <<EOF
</body>
</html>
EOF
}
while true; do
find $STATE/teams/names -type f | while read dn; do
hash=${dn##*/}
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 $TSTATE/next-round ]; then
next=$(cat $TSTATE/next-round)
else
next=0
fi
expr $next + 1 > $TSTATE/next-round
fn=$(printf "%s/round-%04d.html" $TWWW $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
$PACKAGES/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
awk -f $PACKAGES/tanks/bin/rank.awk $rfn >>$fn
cat $PACKAGES/tanks/html/nav.html.inc >>$fn
cat <<EOF >>$fn
</body>
</html>
EOF
awk -f $PACKAGES/tanks/bin/winner.awk $rfn | while read winner; do
hash=$(basename $winner)
echo "Round $next winner: $hash" >> $log
nwinners=$(wc -l $log)
$PACKAGES/mcp/bin/pointscli $hash tanks 1 "tanks round $next"
done
ln -sf $fn $TWWW/current.html
summary > $TWWW/summary.html.$$
mv -f $TWWW/summary.html.$$ $TWWW/summary.html
rm -f $tfn $rfn
[ -n "$once" ] && break
sleep 60
done