Skip to content

Instantly share code, notes, and snippets.

@chrahunt
Created February 20, 2015 02:20
Show Gist options
  • Save chrahunt/cb42ad5c4132b5d38bf7 to your computer and use it in GitHub Desktop.
Save chrahunt/cb42ad5c4132b5d38bf7 to your computer and use it in GitHub Desktop.
Lap Time Tracker for /r/tagproracing
// ==UserScript==
// @name TagPro Lap Time Tracker
// @namespace http://www.reddit.com/user/NewCompte
// @description Track lap times
// @include http://maptest.newcompte.fr:*
// @include http://maptest2.newcompte.fr:*
// @license WTFPL
// @author NewCompte, snaps
// @version 0.9.1
// ==/UserScript==
function myScript()
{
var playerNamesIds = false;
var $lapSetter = $("<input />", {
type: "number",
val: 3,
width: 64
});
$("#sound").append($lapSetter);
var $button = $("<button />");
$button.text("Track lap times");
$button.click(initTimeTracker);
$("#sound").append($button);
function initTimeTracker () {
var numberOfLaps = $lapSetter.val();
tagpro.group.socket.emit("chat", "Userscript intialized. " + numberOfLaps + " laps.");
tagpro.socket.on("p", function (data) {
var time = data.t;
data = data.u || data;
for (var i = 0, l = data.length; i != l; i++) {
var d = data[i];
var id = d.id;
if (d["s-captures"] && d["s-captures"] <= numberOfLaps) {
var lap = d["s-captures"];
if (!laps[lap]) {
previousLap++;
currentPosition = 0;
laps[lap] = [];
}
laps[lap].push({id: id, time: time});
}
if (d.name) {
playerNames[id] = d.name + (playerNamesIds ? " (" + id + ")" : "");
}
if (!playerNames[id]) {
playerNames[id] = ((tagpro.players[id] && tagpro.players[id].name) || "" ) + (playerNamesIds ? " (" + id + ")" : "");
}
if (d.mongoId) {
playerMongoIds[id] = d.mongoId;
}
if (!playerMongoIds[id]) {
playerMongoIds[id] = (tagpro.players[id] && tagpro.players[id].mongoId) || "";
}
}
});
var currentPosition = 0;
var previousLap = 0;
var laps = {};
var lapNames = {};
var playerNames = {};
var playerMongoIds = {};
var lapTime = 0;
setInterval( function () {
if (!(laps[previousLap] && laps[previousLap][currentPosition]))
return;
var message = "";
var talkAbout = laps[previousLap][currentPosition];
if (!currentPosition) {
lapTime = talkAbout.time;
var id = talkAbout.id,
playerName = playerNames[id];
message = lapNames[previousLap] + " 1. " + playerName;
}
else {
var id = talkAbout.id,
playerName = playerNames[id],
time = Math.round((talkAbout.time - lapTime) * 1000 / 60),
seconds = Math.floor(time/1000),
decisecond = Math.floor(time/100) % 10,
centisecond = Math.floor(time/10) % 10,
milisecond = time % 10;
message = (currentPosition + 1) + ". " + playerName + " at +" + seconds + "." + decisecond + "" + centisecond;
}
// Add message for when the lap number moves off screen
if (previousLap == numberOfLaps) {
message += " (totally done!)";
} else {
message += " (that was lap " + previousLap + ")";
}
currentPosition++;
tagpro.group.socket.emit("chat", message);
}, 1000);
function saveAsCSV () {
function getTime(id, lap) {
return (searchObject[lap + "," + id] || "").toString();
}
var searchObject = {};
var idSearchObject = {};
var idArray = [];
for (var lap in laps) {
laps[lap].forEach ( function (perf) {
searchObject[lap + "," + perf.id] = perf.time;
idSearchObject[perf.id] = true;
});
}
for (var id in idSearchObject) {
idArray.push(id);
}
var csv = "mongoId,name";
for (var lap = 1; lap <= numberOfLaps; lap++) {
csv += ',"Lap ' + lap + '"';
}
idArray.forEach( function (id) {
csv += "\n" + playerMongoIds[id] + "," + playerNames[id];
for (var lap = 1; lap <= numberOfLaps; lap++) {
csv += "," + getTime(id, lap);
}
});
var file = new Blob([csv], {type: "data:text/csv;charset=utf-8"});
var a = document.createElement('a');
a.download = 'tagproracing-' + Date.now() + "-" + ($("div#mapInfo").text().slice(5).replace(" ", "").replace(" ", "").replace(" ", "").replace(" ", "").replace(" ", "").replace("-", "").replace("-", "").replace("-", "")) + '.csv';
a.href = (window.URL || window.webkitURL).createObjectURL(file);
var event = document.createEvent('MouseEvents');
event.initEvent('click', true, false);
// trigger download
a.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(a.href);
}
for (var lap = 1; lap < numberOfLaps; lap++) {
lapNames[lap] = "Only " + (numberOfLaps - lap) + " lap(s) remaining.";
}
lapNames[numberOfLaps] = "Final Results !";
var hasCSVBeenDownloadedYet = false;
tagpro.socket.on('end', function() {
if (! hasCSVBeenDownloadedYet) {
hasCSVBeenDownloadedYet = true;
saveAsCSV();
}
});
window.addEventListener('beforeunload', function( event ) {
if (! hasCSVBeenDownloadedYet) {
hasCSVBeenDownloadedYet = true;
saveAsCSV();
}
});
$button
.off()
.text('Lap times are tracked.')
.css('cursor', 'default');
}
}
var source = "(" + myScript + ")()";
var thescript = document.createElement('script');
thescript.setAttribute("type", "application/javascript");
thescript.textContent = source;
document.body.appendChild(thescript);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment