#! /bin/sh
if [ "$#" -gt 0 ]; then
tanks="$@"
else
echo "Usage: $0 tank1 tank2 [...]"
exit 1
fi
TANKS_GAME=${TANKS_GAME:-./forftanks}
NAV_HTML_INC=${NAV_HTML_INC:-./nav.html.inc} export NAV_HTML_INC
rank () {
awk 'BEGIN {
FS = "\t";
}
function esc(s) {
gsub(/&/, "&", s);
gsub(/, "<", s);
gsub(/>/, ">", 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] = "Unnamed";
}
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 "
";
print "Name | Score | Cause of Death | Last Error |
";
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("");
printf("# " name[me] " | ");
printf("" score[me] " | ");
printf("" reason[me] " | ");
printf("" lasterr[me] " | ");
printf("
\n");
}
}
}
print "
";
}
' "$@"
}
summary () {
awk 'function esc(s) {
gsub(/&/, "&", s);
gsub(/, "<", s);
gsub(/>/, ">", s);
return s;
}
BEGIN {
ngames = 20;
print "";
print "";
print " ";
print " Dirtbags Tanks";
print " ";
print " ";
print " ";
print " Dirtbags Tanks
";
print " New here? Read the introduction.
";
print " Rankings
";
print " Over the last 20 games only.
";
print " ";
for (i = 1; i < ARGC; i += 1) {
id = ARGV[i];
if (1 == getline < (id "/name")) {
names[id] = esc($0);
} else {
names[id] = "Unnamed";
}
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("- # %s (%d points)
\n", color[id], names[id], points[id]);
}
}
}
print "
";
print " Rounds
";
print " ";
getline rounds < "next-round";
for (i = rounds - 1; i >= 0; i -= 1) {
printf("- %04d
\n", i, i);
}
print "
";
while (getline < ENVIRON["NAV_HTML_INC"]) {
print;
}
print " ";
print "";
}
' "$@"
}
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 <$fn
Tanks Round $next
Tanks Round $next
0 fps
EOF
rank $rfn >>$fn
rm -f $rfn
cat $NAV_HTML_INC >>$fn
cat <>$fn
EOF
summary $tanks > summary.html.$$
mv summary.html.$$ summary.html
echo "done."