Torn: Custom Race Setup

Customizable race setup for Torn City

目前为 2024-10-29 提交的版本。查看 最新版本

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