tanks/summary.awk

78 lines
1.8 KiB
Awk
Raw Normal View History

#! /usr/bin/awk -f
function esc(s) {
2014-07-28 14:32:24 -06:00
gsub(/&/, "&", s);
gsub(/</, "&lt;", s);
gsub(/>/, "&gt;", s);
return s;
}
BEGIN {
ngames = 20;
2014-07-28 18:45:53 -06:00
getline rounds < "next-round";
2014-07-28 14:32:24 -06:00
print "<!DOCTYPE html>";
print "<html>";
print " <head>";
print " <title>Dirtbags Tanks</title>";
print " <link rel=\"stylesheet\" href=\"style.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 " <p>New round every minute.</p>";
print " <h2>Rankings</h2>";
2014-07-28 18:45:53 -06:00
print " <p>Over the last " ngames" games only.</p>";
print " <ol>";
2014-07-30 05:24:44 -06:00
for (i = rounds - ngames - 1; i > 0 && i < rounds; i += 1) {
2014-07-28 18:45:53 -06:00
fn = sprintf("round-%04d.html", i)
while (getline < fn) {
if ($2 == "score") {
scores[$3] += $4
if (scores[$3] > topscore) {
topscore = scores[$3]
}
}
}
}
2014-07-28 18:45:53 -06:00
for (id in scores) {
if (1 == getline < (id "/name")) {
2014-07-28 18:45:53 -06:00
names[id] = esc($0)
} else {
2014-07-28 18:45:53 -06:00
names[id] = "<i>Unnamed</i>"
}
2014-07-28 18:45:53 -06:00
getline < (id "/color")
if (/^#[0-9A-Fa-f]+$/) {
2014-07-28 18:45:53 -06:00
color[id] = $0
} else {
2014-07-28 18:45:53 -06:00
color[id] = "#c0c0c0"
}
}
2014-07-28 18:45:53 -06:00
for (s = topscore; s >= 0; s -= 1) {
for (id in scores) {
if (scores[id] == s) {
printf("<li><span class=\"swatch\" style=\"background-color: %s;\">#</span> %s (%d points)</li>\n", color[id], names[id], scores[id]);
}
}
}
print " </ol>";
print " <h2>Rounds</h2>";
print " <ul>";
2014-07-28 14:42:49 -06:00
for (i = rounds - 1; (i >= rounds - 721) && (i > 0); i -= 1) {
printf("<li><a href=\"round-%04d.html\">%04d</a></li>\n", i, i);
}
print " </ul>";
2014-07-28 14:32:24 -06:00
while (getline < ENVIRON["NAV_HTML_INC"]) {
print;
}
2014-07-28 14:32:24 -06:00
print " </body>";
print "</html>";
}