Torn: Custom Race Setup

Customizable race setup for Torn City

  1. // ==UserScript==
  2. // @name Torn: Custom Race Setup
  3. // @namespace TornCustomRace
  4. // @description Customizable race setup for Torn City
  5. // @version 0.3.2
  6. // @license MIT
  7. // @author Robodashy
  8. // @match https://www.torn.com/loader.php?sid=racing*
  9. // @grant none
  10. // @update https://update.greasyfork.org/scripts/514608/Torn%3A%20Custom%20Race%20Setup.user.js
  11. // ==/UserScript==
  12.  
  13. /*
  14. ---- User Configuration Section ----
  15. Adjust the following variables to customize the race setup
  16. */
  17.  
  18. const carID = 'your_car_id_here'; // Car ID (e.g., found by hovering over "remove from enlisted" in My Cars, looks like '&carID=649295')
  19. const raceTitle = 'Custom Race'; // Title of the custom race, also displayed on the button
  20. const startHourUTC = 14; // Race start time in UTC (24-hour format). E.g., 14 for 14:00 UTC
  21. const laps = 100; // Number of laps for the race
  22. const trackID = 10; // Track ID (e.g., 10 for Docks)
  23. const minDrivers = 2; // Minimum and maximum drivers allowed for the race
  24. const maxDrivers = 100;
  25. const minClass = 5; // Class and car restrictions for the race
  26. const carsTypeAllowed = 1; // More details will be available in future updates
  27. const carsAllowed = 5;
  28. const betAmount = 0; // Amount to bet on the race (0 for no bet)
  29.  
  30. /* ---- End of User Configuration Section ---- */
  31.  
  32. function addButton() {
  33. function checkAndAddButton() {
  34. const targetContainer = document.querySelector('#racingAdditionalContainer .btn-wrap.silver.c-pointer');
  35. const buttonExists = document.querySelector('#customRaceButton');
  36.  
  37. if (targetContainer && !buttonExists) {
  38. // Create the button element
  39. const button = document.createElement('button');
  40. button.id = 'customRaceButton';
  41. button.className = 'btn btn-action-tab torn-btn btn-dark-bg';
  42. button.textContent = raceTitle;
  43.  
  44. // Add a result display span for feedback
  45. const resultSpan = document.createElement('span');
  46. resultSpan.id = 'customRaceResult';
  47. resultSpan.style.fontSize = '12px';
  48. resultSpan.style.fontWeight = '100';
  49.  
  50. // Append the button and span to the target container
  51. targetContainer.appendChild(button);
  52. targetContainer.appendChild(resultSpan);
  53.  
  54. // Attach click event listener to the button
  55. button.addEventListener('click', () => {
  56. resultSpan.textContent = '';
  57.  
  58. // Set the race time to the user-defined hour in UTC
  59. const now = new Date();
  60. const raceTimeUTC = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), startHourUTC, 0, 0));
  61. const unixTimecode = Math.floor(raceTimeUTC.getTime() / 1000);
  62.  
  63. // Construct the URL for the custom race
  64. const url = `https://torn.com/loader.php?sid=racing&tab=customrace&action=getInRace&step=getInRace` +
  65. `&id=&carID=${carID}&createRace=true&title=${encodeURIComponent(raceTitle)}` +
  66. `&minDrivers=${minDrivers}&maxDrivers=${maxDrivers}&trackID=${trackID}&laps=${laps}` +
  67. `&minClass=${minClass}&carsTypeAllowed=${carsTypeAllowed}&carsAllowed=${carsAllowed}` +
  68. `&betAmount=${betAmount}&waitTime=${unixTimecode}&rfcv=${getRFC()}`;
  69.  
  70. // Redirect to the constructed URL
  71. window.location = url;
  72. console.log(`${raceTitle} button clicked`);
  73. });
  74. }
  75. }
  76.  
  77. // Use MutationObserver to check when the target container is added to the DOM
  78. const observer = new MutationObserver(checkAndAddButton);
  79. observer.observe(document.body, { childList: true, subtree: true });
  80.  
  81. // Run an initial check in case the element is already loaded
  82. checkAndAddButton();
  83. }
  84.  
  85. // Ensure the function runs after the page is fully loaded
  86. window.addEventListener('load', addButton);