Advertisement
Dotsarecool

Speedrun.com Previous Run Display (Tampermonkey Script)

Aug 5th, 2017
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Speedrun.com Previous Run Display
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.0
  5. // @description  Displays a runner's PB (if one exists) on the verify run page
  6. // @author       Dotsarecool
  7. // @match        http://www.speedrun.com/run/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12.     'use strict';
  13.     var url = window.location.href;
  14.     var runId = url.substring(url.indexOf("run/")+4);
  15.     var runObj = callSrcApi("runs/"+runId);
  16.    
  17.     //var values = runObj.data.values;
  18.     var categoryId = runObj.data.category;
  19.     var gameId = runObj.data.game;
  20.     var playerId = runObj.data.players[0].id;
  21.     var runTime = runObj.data.times.primary_t;
  22.    
  23.     var playerPBs = callSrcApi("users/"+playerId+"/personal-bests?game="+gameId);
  24.     var matchIdx = -1;
  25.     var i = 0;
  26.     while (i < playerPBs.data.length) {
  27.         if (playerPBs.data[i].run.category === categoryId) {
  28.             matchIdx = i;
  29.             break;
  30.         }
  31.         i++;
  32.     }
  33.    
  34.     var str = "";
  35.     if (playerPBs.data.length < 1) {
  36.         str = "This is this player's first submitted run of the game.";
  37.     } else if (matchIdx < 0) {
  38.         str = "This is this player's first submitted run of this category.";
  39.     } else {
  40.         var pbPlace = playerPBs.data[matchIdx].place;
  41.         var pbTime = playerPBs.data[matchIdx].run.times.primary_t;
  42.        
  43.         if (pbTime === runTime) {
  44.             str = "This is the player's personal best.";
  45.         } else if (pbTime < runTime) {
  46.             str = "This run is obsoleted by the player's personal best of " + prettyTime(pbTime) + " (" + prettyPlace(pbPlace) + ") - difference of " + prettyTime(runTime-pbTime) + ".";
  47.         } else {
  48.             str = "Current PB: " + prettyTime(pbTime) + " (" + prettyPlace(pbPlace) + ") - improvement of " + prettyTime(pbTime-runTime) + ".";
  49.         }
  50.     }
  51.    
  52.     $("#main > .maincontent > .panel > .panel-heading").prepend(str + "<br />");
  53. })();
  54.  
  55. function callSrcApi(args) {
  56.     var result = {};
  57.     $.ajax({
  58.         url: "http://www.speedrun.com/api/v1/" + args,
  59.         datatype: "json",
  60.         async: false,
  61.         success: function(x) {
  62.             result = x;
  63.         }
  64.     });
  65.     return result;
  66. }
  67.  
  68. function prettyTime(time) {
  69.     var sec = parseInt(time);
  70.     var h = parseInt(sec/3600);
  71.     var m = parseInt(sec/60) % 60;
  72.     var s = sec % 60;
  73.     var d = parseInt(1000*(time-sec));
  74.    
  75.     var str = "";
  76.     if (time >= 3600) {
  77.         str = h+"h "+m+"m "+s+"s";
  78.     } else {
  79.         str = m+"m "+s+"s";
  80.     }
  81.     if (d > 0) {
  82.         str += " " + d + "ms";
  83.     }
  84.     return str;
  85. }
  86.  
  87. function prettyPlace(place) {
  88.     var lastD = place % 10;
  89.     if ((place > 10 && place < 14) || lastD === 0 || lastD > 3) {
  90.         return place+"th place";
  91.     } else if (lastD === 1) {
  92.         return place+"st place";
  93.     } else if (lastD === 2) {
  94.         return place+"nd place";
  95.     } else {
  96.         return place+"rd place";
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement