/* Configuration */
var QUESTION_ID = 60328; // Obtain this from the url
// It will be like http://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 167084; // This should be the user ID of the challenge author.
var SECONDSINDAY = 86400;
var SAFECUTOFFDAYS = 7;
var SORTBYTIME = true;
var SUBTRACTCRACKEDPOINTS = true;
var EXPIREDTIME = 1446336000;
/* App */
var answers = [],
answers_hash, answer_ids, answer_page = 1,
more_answers = true,
comment_page;
function answersUrl(index) {
return "//api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}
function commentUrl(index, answers) {
return "//api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}
function getAnswers() {
jQuery.ajax({
url: answersUrl(answer_page++),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function(data) {
answers.push.apply(answers, data.items);
answers_hash = [];
answer_ids = [];
data.items.forEach(function(a) {
a.comments = [];
var id = +a.share_link.match(/\d+/);
answer_ids.push(id);
answers_hash[id] = a;
});
if (!data.has_more) more_answers = false;
comment_page = 1;
getComments();
}
});
}
function getComments() {
jQuery.ajax({
url: commentUrl(comment_page++, answer_ids),
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function(data) {
data.items.forEach(function(c) {
answers_hash[c.post_id].comments.push(c);
});
if (data.has_more) getComments();
else if (more_answers) getAnswers();
else process();
}
});
}
getAnswers();
var SAFE_REG = /<h\d>.*?[sS][aA][fF][eE].*<\/\h\d>/;
var POINTS_REG = /(?:<=|≤|<=)\s?(?:<\/?strong>)?\s?(\d+)/
var POINTS_REG_ALT = /<h\d>.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;
var CRACKED_HEADER_REG = /<h\d>.*[Cc][Rr][Aa][Cc][Kk][Ee][Dd].*<\/h\d>/;
var CRACKED_COMMENT_REG = /(.*[Cc][Rr][Aa][Cc][Kk][Ee][Dd].*<a href=.*)|(.*<a href=.*[Cc][Rr][Aa][Cc][Kk][Ee][Dd].*)/
var OVERRIDE_REG = /^Override\s*header:\s*/i;
var LANGUAGE_REG = /<h\d>\s*(.+?),.*<\/h\d>/;
var LANGUAGE_REG_ALT = /<h\d>\s*(<a href=.+<\/a>).*<\/h\d>/
var LANGUAGE_REG_ALT_2 = /<h\d>\s*(.+?)\s.*<\/h\d>/;
var LANGUAGE_REG_ALT_3 = /<h\d>(.+?)<\/h\d>/;
function getAuthorName(a) {
return a.owner.display_name;
}
function process() {
var valid = [];
var open = [];
answers.forEach(function(a) {
var body = a.body;
var cracked = false;
a.comments.forEach(function(c) {
var was_safe = (c.creation_date + (SECONDSINDAY * SAFECUTOFFDAYS) > a.creation_date);
if (CRACKED_COMMENT_REG.test(c.body) && !was_safe)
cracked = true;
});
if (CRACKED_HEADER_REG.test(body)) cracked = true;
// if (SUBTRACTCRACKEDPOINTS||!cracked) {
var createDate = a.creation_date;
var currentDate = Date.now() / 1000;
var timeToSafe = (createDate + (SECONDSINDAY * SAFECUTOFFDAYS) - currentDate) / SECONDSINDAY;
var SafeTimeStr = (timeToSafe > 2) ? (Math.floor(timeToSafe) + " Days") :
(timeToSafe > 1) ? ("1 Day") :
(timeToSafe > (2 / 24)) ? (Math.floor(timeToSafe * 24) + " Hours") :
(timeToSafe > (1 / 24)) ? ("1 Hour") :
"<1 Hour";
var expired = createDate > (EXPIREDTIME);
var safe = timeToSafe < 0;
var points = body.match(POINTS_REG);
if (!points) points = body.match(POINTS_REG_ALT);
safe = safe && !cracked
isOpen = !(cracked || safe);
if (points) {
var length = parseInt(points[1]);
var safepoints = 0;
if (length <= 4) safepoints = 32;
else if (length <= 8) safepoints = 16;
else if (length <= 16) safepoints = 8;
else if (length <= 32) safepoints = 4;
else if (length <= 64) safepoints = 2;
else if (length <= 128) safepoints = 1;
valid.push({
user: getAuthorName(a),
numberOfSubmissions: (safe && !expired) ? 1 : 0,
points: (safe && !expired) ? safepoints : 0,
open: (isOpen && !expired) ? 1 : 0,
cracked: (cracked && !expired) ? 1 : 0,
expired: (expired) ? 1 : 0
});
}
if ((isOpen || expired) && points) {
var language = body.match(LANGUAGE_REG);
if (!language) language = body.match(LANGUAGE_REG_ALT);
if (!language) language = body.match(LANGUAGE_REG_ALT_2);
if (!language) language = body.match(LANGUAGE_REG_ALT_3);
open.push({
user: getAuthorName(a),
length: points ? points[1] : "???",
language: language ? language[1] : "???",
link: a.share_link,
timeToSafe: timeToSafe,
timeStr: (expired) ? "Challenge closed" : SafeTimeStr
});
}
// }
});
if (SORTBYTIME) {
open.sort(function(a, b) {
return a.timeToSafe - b.timeToSafe;
});
} else {
open.sort(function(a, b) {
var r1 = parseInt(a.length);
var r2 = parseInt(b.length);
if (r1 && r2) return r1 - r2;
else if (r1) return r2;
else if (r2) return r1;
else return 0;
});
}
var pointTotals = [];
valid.forEach(function(a) {
var index = -1;
var author = a.user;
pointTotals.forEach(function(p) {
if (p.user == author) index = pointTotals.indexOf(p);
});
if (index == -1) pointTotals.push(a);
else {
pointTotals[index].points += a.points;
pointTotals[index].numberOfSubmissions += a.numberOfSubmissions;
pointTotals[index].cracked += a.cracked;
pointTotals[index].expired += a.expired;
pointTotals[index].open += a.open;
if (SUBTRACTCRACKEDPOINTS && a.cracked && pointTotals[index].cracked > 1) pointTotals[index].points -= .5;
}
});
pointTotals.forEach(function(a) {
a.points += (a.numberOfSubmissions) ? ((a.numberOfSubmissions) * 3) : 0;
});
pointTotals.sort(function(a, b) {
if (a.points != b.points)
return b.points - a.points;
else if (a.numberOfSubmissions != b.numberOfSubmissions)
return b.numberOfSubmissions - a.numberOfSubmissions;
else if (a.open != b.open)
return b.open - a.open;
else if (a.cracked != b.cracked)
return a.cracked - b.cracked;
else return 0;
});
pointTotals.forEach(function(a) {
var answer = jQuery("#answer-template").html();
answer = answer
.replace("{{NAME}}", a.user)
.replace("{{SAFE}}", a.numberOfSubmissions)
.replace("{{OPEN}}", a.open)
.replace("{{CLOSED}}", a.expired)
.replace("{{CRACKED}}", a.cracked)
.replace("{{POINTS}}", a.points);
answer = jQuery(answer);
jQuery("#answers").append(answer);
});
open.forEach(function(a) {
var answer = jQuery("#open-template").html();
answer = answer
.replace("{{NAME}}", a.user)
.replace("{{LENGTH}}", a.length)
.replace("{{LANGUAGE}}", a.language)
.replace("{{TIME}}", a.timeStr)
.replace("{{LINK}}", a.link);
answer = jQuery(answer);
jQuery("#opensubs").append(answer);
});
}
body {
text-align: left !important
}
#answer-list {
padding: 10px;
width: 350px;
float: left;
}
#open-list {
padding: 10px;
width: 470px;
float: left;
}
table thead {
font-weight: bold;
vertical-align: top;
}
table td {
padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<div id="answer-list">
<h2>Leaderboard</h2>
<table class="answer-list">
<thead>
<tr>
<td>Author</td>
<td>Safe</td>
<td>Open</td>
<td>Cracked</td>
<td>Late Entry</td>
<td>Score</td>
</tr>
</thead>
<tbody id="answers">
</tbody>
</table>
</div>
<div id="open-list">
<h2>Open submissions</h2>
<table class="open-list">
<thead>
<tr>
<td>Author</td>
<td>Length</td>
<td>Language</td>
<td>Time Remaining</td>
<td>Link (open in new tab)</td>
</tr>
</thead>
<tbody id="opensubs">
</tbody>
</table>
</div>
<table style="display: none">
<tbody id="answer-template">
<tr>
<td>{{NAME}}</td>
<td>{{SAFE}}</td>
<td>{{OPEN}}</td>
<td>{{CRACKED}}</td>
<td>{{CLOSED}}</td>
<td>{{POINTS}}</td>
</tr>
</tbody>
</table>
<table style="display: none">
<tbody id="open-template">
<tr>
<td>{{NAME}}</td>
<td>{{LENGTH}}</td>
<td>{{LANGUAGE}}</td>
<td>{{TIME}}</td>
<td><a target="_parent" href="{{LINK}}">Link</a>
</td>
</tr>
</tbody>
</table>