diff --git a/packages/mcp/bin/scoreboard b/packages/mcp/bin/scoreboard index 58c1996..29411ad 100755 --- a/packages/mcp/bin/scoreboard +++ b/packages/mcp/bin/scoreboard @@ -8,152 +8,151 @@ ## 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) + 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 + t = A[i]; A[i] = A[j]; A[j] = t } function escape(s) { - gsub("&", "&", s) - gsub("<", "<", s) - gsub(">", ">", s) - return s + gsub("&", "&", s) + gsub("<", "<", s) + gsub(">", ">", s) + return s } function head() { - print "" - print "Project 2 Scoreboard" - print "" - print "" - print "" - print "" - print "
" + print ".name {position: absolute; right: 10px;}" + print "#scores p {margin: 0; padding: 0; border: thin solid #222; opacity: 0.92;}" + print "#scores p:hover {border: thin solid yellow;}" + print "" + print "" + print "
" } function foot() { - print "
" - print " " - print "" + print "
" + print " " + print "" } BEGIN { - base = ENVIRON["CTF_BASE"] - if (! base) { - base = "/var/lib/ctf" - } + base = ENVIRON["CTF_BASE"] + if (! base) { + base = "/var/lib/ctf" + } - head() + head() } # MAINLOOP { - time = $1 - hash = $2 - cat = $3 - points = int($4) + time = $1 + hash = $2 + cat = $3 + points = int($4) - # Build a list of team names - if (! (hash in team_names)) { - fn = sprintf("%s/state/teams/names/%s", base, hash) - getline team_names[hash] < fn - close(fn) - } + # Build a list of team names + if (! (hash in team_names)) { + fn = sprintf("%s/state/teams/names/%s", base, hash) + getline team_names[hash] < fn + close(fn) + } - # Total points possible so far in this category - if (! ((cat, points) in cat_pointval)) { - cat_total[cat] += points - cat_pointval[cat, points] = 1 - } + # Enumerate categories + if (! (cat in seen_cats)) { + seen_cats[cat] = 1 + categories[ncats++] = cat + } - # 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 + # Points this team has in this category + cat_points[hash, cat] += points + + # Token-based categories can make cat_pointval irrelevant + if (cat_points[hash, cat] > cat_total[cat]) { + cat_total[cat] = cat_points[hash, cat] + } } END { - # 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] - } - } + # 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 scores - qsort(scores, 0, nteams-1) + # Sort scores + qsort(scores, 0, nteams-1) - print "

" - for (ncat = 0; ncat < ncats; ncat += 1) { - printf("%s\n", ncat, categories[ncat]); - } - print "

" + print "

" + for (ncat = 0; ncat < ncats; ncat += 1) { + printf("%s\n", ncat, categories[ncat]); + } + print "

" - for (i = nteams-1; i >= 0; i -= 1) { - score = scores[i]; - if (score == scores[i-1]) continue; # Skip duplicates + for (i = nteams-1; i >= 0; i -= 1) { + score = scores[i]; + if (score == scores[i-1]) continue; # Skip duplicates - for (hash in team_names) { - if (total_score[hash] != score) { - continue; - } + for (hash in team_names) { + if (total_score[hash] != score) { + continue; + } - name = escape(team_names[hash]) - print "

" - printf("%s\n", name) + name = escape(team_names[hash]) + print "

" + printf("%s\n", name) - for (ncat = 0; ncat < ncats; ncat += 1) { - cat = categories[ncat]; - points = cat_points[hash, cat]; + for (ncat = 0; ncat < ncats; ncat += 1) { + cat = categories[ncat]; + points = cat_points[hash, 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 "

" - } - } + 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 "

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