tanks/run-tanks

244 lines
5.2 KiB
Plaintext
Raw Normal View History

2010-07-27 13:17:44 -06:00
#! /bin/sh
if [ "$#" -gt 0 ]; then
tanks="$@"
else
echo "Usage: $0 tank1 tank2 [...]"
exit 1
fi
2010-07-27 18:08:24 -06:00
TANKS_GAME=${TANKS_GAME:-forftanks}
2010-07-27 13:17:44 -06:00
NAV_HTML_INC=${NAV_HTML_INC:-./nav.html.inc} export NAV_HTML_INC
rank () {
awk 'BEGIN {
FS = "\t";
}
function esc(s) {
gsub(/&/, "&", s);
gsub(/</, "&lt;", s);
gsub(/>/, "&gt;", s);
return s;
}
{
id = $1;
ntanks += 1;
tanks[id] = id;
if ($4 == "(nil)") {
score[id] += 1;
} else {
reason[id] = $3;
killer[id] = $4;
kills[$4] += 1;
score[$4] += 1;
}
path[id] = $2;
if ($5) {
lasterr[id] = $6 " around char " $5;
} else {
lasterr[id] = $6;
}
if (1 == getline < (path[id] "/name")) {
name[id] = esc($0);
} else {
name[id] = "<i>Unnamed</i>";
}
getline < (path[id] "/color");
if (/^#[0-9A-Fa-f]+$/) {
color[id] = $0;
} else {
color[id] = "#c0c0c0";
}
}
END {
# Fill in who killed whom
for (id in tanks) {
if (score[id] > topscore) {
winner = id;
topscore = score[id];
}
if (killer[id]) {
reason[id] = reason[id] " (" name[killer[id]] ")";
}
print score[id] >> (path[id] "/points");
}
# Dole out points
# Output the table
print "<table id=\"results\">";
print "<tr><th>Name</th><th>Score</th><th>Cause of Death</th><th>Last Error</th></tr>";
for (i = ntanks; i >= 0; i -= 1) {
for (me in tanks) {
if (score[me] == i) {
if (me == winner) {
style = "style=\"font-weight: bold; background-color: #666666\"";
} else {
style = "";
}
printf("<tr " style ">");
printf("<td><span class=\"swatch\" style=\"background-color: " color[me] "\">#</span> " name[me] "</td>");
printf("<td>" score[me] "</td>");
printf("<td>" reason[me] "</td>");
printf("<td>" lasterr[me] "</td>");
printf("</tr>\n");
}
}
}
print "</table>";
}
' "$@"
}
summary () {
awk 'function esc(s) {
gsub(/&/, "&amp;", s);
gsub(/</, "&lt;", s);
gsub(/>/, "&gt;", s);
return s;
}
BEGIN {
ngames = 20;
print "<!DOCTYPE html>";
print "<html>";
print " <head>";
print " <title>Dirtbags Tanks</title>";
print " <link rel=\"stylesheet\" href=\"dirtbags.css\" type=\"text/css\">";
print " </head>";
print " <body>";
print " <h1>Dirtbags Tanks</h1>";
print " <p>New here? Read the <a href=\"intro.html\">introduction</a>.</p>";
print " <h2>Rankings</h2>";
print " <p>Over the last 20 games only.</p>";
print " <ol>";
for (i = 1; i < ARGC; i += 1) {
id = ARGV[i];
if (1 == getline < (id "/name")) {
names[id] = esc($0);
} else {
names[id] = "<i>Unnamed</i>";
}
getline < (id "/color");
if (/^#[0-9A-Fa-f]+$/) {
color[id] = $0;
} else {
color[id] = "#c0c0c0";
}
for (j = 0; 1 == getline < (id "/points"); j += 1) {
pts[id, j % ngames] = int($0);
}
total = 0;
for (j = 0; j < ngames; j += 1) {
total += pts[id, j];
}
scores[total] = total;
points[id] = total;
}
while (1) {
# Find highest score
maxscore = -1;
for (p in scores) {
if (int(p) > maxscore) {
maxscore = int(p);
}
}
if (maxscore == -1) {
break;
}
delete scores[maxscore];
for (id in points) {
if (points[id] == maxscore) {
printf("<li><span class=\"swatch\" style=\"background-color: %s;\">#</span> %s (%d points)</li>\n", color[id], names[id], points[id]);
}
}
}
print " </ol>";
print " <h2>Rounds</h2>";
print " <ul>";
getline rounds < "next-round";
for (i = rounds - 1; i >= 0; i -= 1) {
printf("<li><a href=\"round-%04d.html\">%04d</a></li>\n", i, i);
}
print " </ul>";
while (getline < ENVIRON["NAV_HTML_INC"]) {
print;
}
print " </body>";
print "</html>";
}
' "$@"
}
if [ -f next-round ]; then
next=$(cat next-round)
else
next=0
fi
expr $next + 1 > next-round
fn=$(printf "round-%04d.html" $next)
rfn=results$$.txt
echo -n "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="dirtbags.css" type="text/css">
<script type="application/javascript">
function go() {
start("battlefield",
// Start JSON data
EOF
$TANKS_GAME $tanks >>$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
rank $rfn >>$fn
rm -f $rfn
cat $NAV_HTML_INC >>$fn
cat <<EOF >>$fn
</body>
</html>
EOF
summary $tanks > summary.html.$$
mv summary.html.$$ summary.html
echo "done."