Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Speedrun.com Previous Run Display
- // @namespace http://tampermonkey.net/
- // @version 1.0
- // @description Displays a runner's PB (if one exists) on the verify run page
- // @author Dotsarecool
- // @match http://www.speedrun.com/run/*
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- var url = window.location.href;
- var runId = url.substring(url.indexOf("run/")+4);
- var runObj = callSrcApi("runs/"+runId);
- //var values = runObj.data.values;
- var categoryId = runObj.data.category;
- var gameId = runObj.data.game;
- var playerId = runObj.data.players[0].id;
- var runTime = runObj.data.times.primary_t;
- var playerPBs = callSrcApi("users/"+playerId+"/personal-bests?game="+gameId);
- var matchIdx = -1;
- var i = 0;
- while (i < playerPBs.data.length) {
- if (playerPBs.data[i].run.category === categoryId) {
- matchIdx = i;
- break;
- }
- i++;
- }
- var str = "";
- if (playerPBs.data.length < 1) {
- str = "This is this player's first submitted run of the game.";
- } else if (matchIdx < 0) {
- str = "This is this player's first submitted run of this category.";
- } else {
- var pbPlace = playerPBs.data[matchIdx].place;
- var pbTime = playerPBs.data[matchIdx].run.times.primary_t;
- if (pbTime === runTime) {
- str = "This is the player's personal best.";
- } else if (pbTime < runTime) {
- str = "This run is obsoleted by the player's personal best of " + prettyTime(pbTime) + " (" + prettyPlace(pbPlace) + ") - difference of " + prettyTime(runTime-pbTime) + ".";
- } else {
- str = "Current PB: " + prettyTime(pbTime) + " (" + prettyPlace(pbPlace) + ") - improvement of " + prettyTime(pbTime-runTime) + ".";
- }
- }
- $("#main > .maincontent > .panel > .panel-heading").prepend(str + "<br />");
- })();
- function callSrcApi(args) {
- var result = {};
- $.ajax({
- url: "http://www.speedrun.com/api/v1/" + args,
- datatype: "json",
- async: false,
- success: function(x) {
- result = x;
- }
- });
- return result;
- }
- function prettyTime(time) {
- var sec = parseInt(time);
- var h = parseInt(sec/3600);
- var m = parseInt(sec/60) % 60;
- var s = sec % 60;
- var d = parseInt(1000*(time-sec));
- var str = "";
- if (time >= 3600) {
- str = h+"h "+m+"m "+s+"s";
- } else {
- str = m+"m "+s+"s";
- }
- if (d > 0) {
- str += " " + d + "ms";
- }
- return str;
- }
- function prettyPlace(place) {
- var lastD = place % 10;
- if ((place > 10 && place < 14) || lastD === 0 || lastD > 3) {
- return place+"th place";
- } else if (lastD === 1) {
- return place+"st place";
- } else if (lastD === 2) {
- return place+"nd place";
- } else {
- return place+"rd place";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement