From 4bea87d090f614186004510c8fe81c26414db573 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Thu, 31 Jan 2013 16:54:05 -0700 Subject: [PATCH] move p2 scoreboard into mcp --- packages/mcp/bin/scoreboard | 275 +++++++++++++----------------------- 1 file changed, 96 insertions(+), 179 deletions(-) diff --git a/packages/mcp/bin/scoreboard b/packages/mcp/bin/scoreboard index 6440c7a..a44b6f3 100755 --- a/packages/mcp/bin/scoreboard +++ b/packages/mcp/bin/scoreboard @@ -30,221 +30,138 @@ function escape(s) { return s } -function print_bar(cat, teamid, teamname, n, d) { - printf("
\n" \ - " %s: %s\n" \ - "
", - teamid, - 100 * n / d, - teamid, - teamid, - cat, escape(name), n) +function head() { + print "" + print "Project 2 Scoreboard" + print "" + print "" + print "" + print "
" } -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 +function foot() { + + + print "
" + print "
"
+    print "Project 2
" + print "

A CTF for people with limited time/patience/self-confidence.

" + print "

Plug in ethernet at this table, download puzzles, go think.

" + print "

http://10.0.0.2/

" + print "

Use the terminal to claim points when you've figured something out.

" + print "
" + print " " + print "" } BEGIN { base = ENVIRON["CTF_BASE"] + if (! base) { + base = "/var/lib/ctf" + } - # Only display two decimal places - CONVFMT = "%.2f" - - # New point at least every 2.5 minutes - interval = 150 - tslen = 0 - - nteams = 0; + head() } # MAINLOOP { time = $1 - team = $2 + hash = $2 cat = $3 points = int($4) - if (! start) { - start = time + # Build a list of team names + if (! (hash in team_names)) { + fn = sprintf("%s/teams/names/%s", base, hash) + getline team_names[hash] < fn + close(fn) } - if (time > (outtime + interval)) { - outtime = time - output() + # Total points possible so far in this category + if (! ((cat, points) in cat_pointval)) { + cat_total[cat] += points + cat_pointval[cat, points] = 1 } - lasttime = time - teams[team] = nteams++ - points_by_cat[cat] += points - points_by_cat_team[cat, team] += points + # Enumerate categories + if (! (cat in seen_cats)) { + seen_cats[cat] = 1 + categories[ncats++] = cat + } + + # Points this team has in this category + cat_points[hash, cat] += points } END { - output() - - # Get team colors and names - for (team in teams) { - # 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/state/teams/colors/%s", base, team) - color = "444444"; - cmd | getline color - colors_by_team[team] = color - close(cmd) - - cmd = sprintf("cat %s/state/teams/names/%s", base, team) - name = "Phantoms" - cmd | getline name - names_by_team[team] = name - close(cmd) + # Adjust per-category points to a per-category percentage complete + for (hash in team_names) { + for (cat in cat_total) { + cat_score[hash, cat] = cat_points[hash, cat] / cat_total[cat] + total_score[hash] += cat_score[hash, cat] + } + scores[nteams++] = total_score[hash] + if (total_score[hash] > max_score) { + max_score = total_score[hash] + } } - # 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] - } + # Sort scores qsort(scores, 0, nteams-1) - - # Now we can start writing the document - print "" - print "" - print " " - print " Scoreboard" - print " " - print " " - - # Provide raw data for the chart - print " " + print "

" - # Reload every minute - print " " + for (i = nteams-1; i >= 0; i -= 1) { + score = scores[i]; + if (score == scores[i-1]) continue; # Skip duplicates - # Set up team colors and a few page-specific styles - print " " - - print " " - print " " - print "

Scoreboard

" - print "

" - print " \"\"" - print " " - print " " - print " " - - # Print out category names - for (i = 0; i < ncats; i += 1) { - cat = cats[i] - points = points_by_cat[cat] - if (0 == points) continue - printf("\n", cat, points) - } - - print " " - print " " - - # Print out teams, ranked by score - print " " - # 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; + name = escape(team_names[hash]) + print "

" + printf("%s\n", name) - print "

" } - print " " - print "
Overall%s (%d)
" - 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] - print_bar("total", teams[team], name, scores[i], ncats) + for (hash in team_names) { + if (total_score[hash] != score) { + continue; } - } - } - print " " - - # 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) + for (ncat = 0; ncat < ncats; ncat += 1) { + cat = categories[ncat]; + points = cat_points[hash, cat]; - # Print out teams, ranked by points - for (j = 0; j < n; j += 1) { - if (l[j] == l[j-1]) continue; - if (0 == l[j]) continue; - for (team in teams) { - points = points_by_cat_team[cat, team] - if (l[j] == points) { - name = names_by_team[team] - print_bar(cat, teams[team], name, points, points_by_cat[cat]) + if (cat_points[hash, cat] > 0) { + width = cat_score[hash, cat] / max_score * 90 + printf("", cat, points, name) + printf("%d", + ncat, width, cat_points[hash, cat]) } } + print "

" } - - print "
" - print " " - print " " - print "" + foot() } +