2011-10-13 17:22:03 -06:00
|
|
|
#! /bin/awk -f
|
2010-09-13 17:32:51 -06:00
|
|
|
|
|
|
|
##
|
|
|
|
##
|
2010-09-23 18:23:00 -06:00
|
|
|
## I'm not super happy with how this code looks. Rest assured, though,
|
|
|
|
## the C version would look far, far worse.
|
2010-09-13 17:32:51 -06:00
|
|
|
##
|
|
|
|
##
|
|
|
|
|
|
|
|
function qsort(A, left, right, i, last) {
|
|
|
|
if (left >= right)
|
|
|
|
return
|
|
|
|
swap(A, left, left+int((right-left+1)*rand()))
|
|
|
|
last = left
|
|
|
|
for (i = left+1; i <= right; i++)
|
|
|
|
if (A[i] < A[left])
|
|
|
|
swap(A, ++last, i)
|
|
|
|
swap(A, left, last)
|
|
|
|
qsort(A, left, last-1)
|
|
|
|
qsort(A, last+1, right)
|
|
|
|
}
|
|
|
|
function swap(A, i, j, t) {
|
|
|
|
t = A[i]; A[i] = A[j]; A[j] = t
|
|
|
|
}
|
|
|
|
|
|
|
|
function escape(s) {
|
|
|
|
gsub("&", "&", s)
|
|
|
|
gsub("<", "<", s)
|
|
|
|
gsub(">", ">", s)
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2010-10-26 16:43:51 -06:00
|
|
|
function print_bar(cat, teamid, teamname, n, d) {
|
2010-09-13 17:32:51 -06:00
|
|
|
printf("<div class=\"t%s score\"" \
|
|
|
|
" style=\"height: %.2f%%;\"" \
|
|
|
|
" onmouseover=\"highlight('%s')\"" \
|
|
|
|
" onmouseout=\"restore('%s')\">\n" \
|
|
|
|
"<!-- %s --> %s: %s\n" \
|
|
|
|
"</div>",
|
2010-10-26 16:43:51 -06:00
|
|
|
teamid,
|
2010-09-13 17:32:51 -06:00
|
|
|
100 * n / d,
|
2010-10-26 16:43:51 -06:00
|
|
|
teamid,
|
|
|
|
teamid,
|
|
|
|
cat, escape(name), n)
|
2010-09-13 17:32:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function output( t, c) {
|
|
|
|
for (t in teams) {
|
|
|
|
score = 0;
|
|
|
|
for (c in points_by_cat) {
|
|
|
|
if (points_by_cat[c] > 0) {
|
|
|
|
score += points_by_cat_team[c, t] / points_by_cat[c];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (score > maxscore) {
|
|
|
|
maxscore = score
|
|
|
|
}
|
|
|
|
if (score > maxscores_by_team[t]) {
|
|
|
|
maxscores_by_team[t] = score
|
|
|
|
}
|
|
|
|
scores_by_team_time[t, lasttime] = score
|
|
|
|
}
|
|
|
|
timestamps[tslen++] = lasttime
|
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
base = ENVIRON["CTF_BASE"]
|
|
|
|
if (! base) {
|
2010-09-23 23:27:14 -06:00
|
|
|
base = "/var/lib/ctf"
|
2010-09-13 17:32:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Only display two decimal places
|
|
|
|
CONVFMT = "%.2f"
|
|
|
|
|
|
|
|
# New point at least every 2.5 minutes
|
|
|
|
interval = 150
|
|
|
|
tslen = 0
|
|
|
|
|
2010-10-22 17:09:00 -06:00
|
|
|
nteams = 0;
|
2012-06-05 23:56:29 -06:00
|
|
|
}
|
2010-09-13 17:32:51 -06:00
|
|
|
|
2012-06-05 23:56:29 -06:00
|
|
|
# MAINLOOP
|
|
|
|
{
|
|
|
|
time = $1
|
|
|
|
team = $2
|
|
|
|
cat = $3
|
|
|
|
points = int($4)
|
2010-09-13 17:32:51 -06:00
|
|
|
|
2012-06-05 23:56:29 -06:00
|
|
|
if (! start) {
|
|
|
|
start = time
|
2010-09-13 17:32:51 -06:00
|
|
|
}
|
|
|
|
|
2012-06-05 23:56:29 -06:00
|
|
|
if (time > (outtime + interval)) {
|
|
|
|
outtime = time
|
|
|
|
output()
|
|
|
|
}
|
|
|
|
lasttime = time
|
|
|
|
|
|
|
|
teams[team] = nteams++
|
|
|
|
points_by_cat[cat] += points
|
|
|
|
points_by_cat_team[cat, team] += points
|
|
|
|
}
|
|
|
|
|
|
|
|
END {
|
2010-09-13 17:32:51 -06:00
|
|
|
output()
|
|
|
|
|
|
|
|
# Get team colors and names
|
|
|
|
for (team in teams) {
|
2010-09-23 23:27:14 -06:00
|
|
|
# Busybox awk segfaults if you try to close a file that didn't
|
|
|
|
# exist. We work around it by calling cat.
|
|
|
|
cmd = sprintf("cat %s/teams/colors/%s", base, team)
|
2010-11-01 17:05:14 -06:00
|
|
|
color = "444444";
|
2010-09-23 23:27:14 -06:00
|
|
|
cmd | getline color
|
|
|
|
colors_by_team[team] = color
|
|
|
|
close(cmd)
|
|
|
|
|
|
|
|
cmd = sprintf("cat %s/teams/names/%s", base, team)
|
2010-11-01 17:05:14 -06:00
|
|
|
name = "Phantoms"
|
2010-09-23 23:27:14 -06:00
|
|
|
cmd | getline name
|
|
|
|
names_by_team[team] = name
|
|
|
|
close(cmd)
|
2010-09-13 17:32:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
# Sort categories
|
|
|
|
ncats = 0
|
|
|
|
for (cat in points_by_cat) {
|
|
|
|
cats[ncats++] = cat
|
|
|
|
}
|
|
|
|
qsort(cats, 0, ncats-1)
|
|
|
|
|
|
|
|
# Create a sorted list of scores
|
|
|
|
nteams = 0
|
|
|
|
for (team in teams) {
|
|
|
|
scores[nteams++] = scores_by_team_time[team, lasttime]
|
|
|
|
}
|
|
|
|
qsort(scores, 0, nteams-1)
|
|
|
|
|
|
|
|
|
|
|
|
# Now we can start writing the document
|
|
|
|
print "<!DOCTYPE html>"
|
2012-02-02 16:51:19 -07:00
|
|
|
print "<html class=\"scoreboard\">"
|
2010-09-13 17:32:51 -06:00
|
|
|
print " <head>"
|
|
|
|
print " <title>Scoreboard</title>"
|
|
|
|
print " <link rel=\"stylesheet\" href=\"ctf.css\" type=\"text/css\">"
|
|
|
|
print " <script type=\"application/javascript\" src=\"scoreboard.js\"></script>"
|
|
|
|
|
|
|
|
# Provide raw data for the chart
|
|
|
|
print " <script type=\"application/javascript\">"
|
|
|
|
print "function init() {"
|
|
|
|
printf(" plot(\"chart\", %d, %.2f, {\n", tslen, maxscore)
|
|
|
|
c = 0
|
|
|
|
for (team in teams) {
|
|
|
|
if (maxscores_by_team[team] / maxscore < 0.01) continue
|
2010-10-22 17:09:00 -06:00
|
|
|
printf(" \"%s\": [\"#%s\",[", teams[team], colors_by_team[team])
|
2010-09-13 17:32:51 -06:00
|
|
|
for (i = 1; i < tslen; i += 1) {
|
|
|
|
time = timestamps[i]
|
|
|
|
printf("[%d,%.2f],",
|
|
|
|
i, scores_by_team_time[team, time])
|
|
|
|
}
|
|
|
|
printf("]],\n");
|
|
|
|
}
|
|
|
|
print " });"
|
|
|
|
print " if (location.hash) {"
|
|
|
|
print " cycle();"
|
|
|
|
print " setInterval(cycle, 10000);"
|
|
|
|
print " }"
|
|
|
|
print "}"
|
|
|
|
print "window.onload = init;"
|
|
|
|
print " </script>"
|
|
|
|
|
|
|
|
# Reload every minute
|
|
|
|
print " <meta http-equiv=\"refresh\" content=\"60\">"
|
|
|
|
|
|
|
|
# Set up team colors and a few page-specific styles
|
|
|
|
print " <style type=\"text/css\">"
|
|
|
|
print " body { width: 100%; }"
|
|
|
|
print " .score { overflow: hidden; color: black; }"
|
|
|
|
for (team in teams) {
|
|
|
|
printf(" .t%s { background-color: #%s; }\n",
|
2010-10-22 17:09:00 -06:00
|
|
|
teams[team], colors_by_team[team])
|
2010-09-13 17:32:51 -06:00
|
|
|
}
|
|
|
|
print " </style>"
|
|
|
|
|
|
|
|
print " </head>"
|
|
|
|
print " <body>"
|
|
|
|
print " <h1>Scoreboard</h1>"
|
2012-06-05 23:56:29 -06:00
|
|
|
print " <p id=\"debug\"></p>"
|
|
|
|
print " <img src=\"backup.png?" NR "\" alt=\"\" style=\"display: none;\" height=\"1\" width=\"1\">"
|
2010-09-13 17:32:51 -06:00
|
|
|
print " <table id=\"scoreboard\">"
|
|
|
|
print " <tr>"
|
|
|
|
print " <th>Overall</th>"
|
|
|
|
|
|
|
|
# Print out category names
|
|
|
|
for (i = 0; i < ncats; i += 1) {
|
|
|
|
cat = cats[i]
|
|
|
|
points = points_by_cat[cat]
|
|
|
|
if (0 == points) continue
|
|
|
|
printf("<th>%s (%d)</th>\n", cat, points)
|
|
|
|
}
|
|
|
|
|
|
|
|
print " </tr>"
|
|
|
|
print " <tr>"
|
|
|
|
|
|
|
|
# Print out teams, ranked by score
|
|
|
|
print " <td>"
|
|
|
|
for (i = 0; i < nteams; i += 1) {
|
|
|
|
if (scores[i] == scores[i-1]) continue;
|
|
|
|
for (team in teams) {
|
|
|
|
if (scores[i] == scores_by_team_time[team, lasttime]) {
|
|
|
|
name = names_by_team[team]
|
2010-10-26 16:43:51 -06:00
|
|
|
print_bar("total", teams[team], name, scores[i], ncats)
|
2010-09-13 17:32:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
print " </td>"
|
|
|
|
|
|
|
|
# Print out scores within each category
|
|
|
|
for (i = 0; i < ncats; i += 1) {
|
|
|
|
cat = cats[i]
|
|
|
|
points = points_by_cat[cat]
|
|
|
|
if (0 == points) break;
|
|
|
|
|
|
|
|
print "<td>"
|
|
|
|
|
|
|
|
# Create sorted list of scores in this category
|
|
|
|
n = 0
|
|
|
|
for (team in teams) {
|
|
|
|
l[n++] = points_by_cat_team[cat, team];
|
|
|
|
}
|
|
|
|
qsort(l, 0, n-1)
|
|
|
|
|
|
|
|
# Print out teams, ranked by points
|
|
|
|
for (j = 0; j < n; j += 1) {
|
|
|
|
if (l[j] == l[j-1]) continue;
|
2011-02-12 20:25:34 -07:00
|
|
|
if (0 == l[j]) continue;
|
2010-09-13 17:32:51 -06:00
|
|
|
for (team in teams) {
|
|
|
|
points = points_by_cat_team[cat, team]
|
|
|
|
if (l[j] == points) {
|
|
|
|
name = names_by_team[team]
|
2010-10-26 16:43:51 -06:00
|
|
|
print_bar(cat, teams[team], name, points, points_by_cat[cat])
|
2010-09-13 17:32:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print "</td>"
|
|
|
|
}
|
|
|
|
print " </tr>"
|
|
|
|
|
|
|
|
print " </table>"
|
2012-02-02 16:51:19 -07:00
|
|
|
print " <canvas id=\"chart\" width=\"1024\" height=\"240\"></canvas>"
|
2010-09-13 17:32:51 -06:00
|
|
|
print " </body>"
|
|
|
|
print "</html>"
|
|
|
|
}
|