Torn Custom Race Presets

Make it easier and faster to make custom races - Extended from Xiphias's

当前为 2019-12-12 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Torn Custom Race Presets
// @namespace    greasemonkey
// @version      0.1
// @description  Make it easier and faster to make custom races - Extended from Xiphias's
// @author       Cryosis7 [926640]
// @match        *www.torn.com/loader.php?sid=racing*
// @grant        none
// ==/UserScript==


/**
 * Modify the presets as you see fit, you can add and remove presets,
 * or remove individual fields within the preset to only use the fields you care about.
 * 
 * TEMPLATE
 * {
        name: "Appears as the button name and the public name of the race",
        maxDrivers: 6,
        trackName: "Industrial",
        numberOfLaps: 1,
        upgradesAllowed: true,
        betAmount: 0,
        waitTime: 1,
        password: "",
    },
 * 
 */
var presets = [{
        name: "Quick Industrial",
        maxDrivers: 2,
        trackName: "Industrial",
        numberOfLaps: 1,
        upgradesAllowed: true,
        betAmount: 0,
        waitTime: 1,
        password: "",
    },
    {
        name: "1hr Start - Docks",
        maxDrivers: 100,
        trackName: "Docks",
        numberOfLaps: 100,
        waitTime: 60,
        password: "",
    },
];

(function() {
    'use strict';
    scrubPresets();
    $('body').ajaxComplete(function(e, xhr, settings) {
        var createCustomRaceSection = "section=createCustomRace";
        var url = settings.url;
        if (url.indexOf(createCustomRaceSection) >= 0) {
            scrubPresets();
            drawPresetBar();
        }
    });
})();

function fillPreset(index) {
    let race = presets[index];

    if ("name" in race) $('.race-wrap div.input-wrap input').attr('value', race.name);
    if ("maxDrivers" in race) $('.drivers-max-wrap div.input-wrap input').attr('value', race.maxDrivers);
    if ("numberOfLaps" in race) $('.laps-wrap > .input-wrap > input').attr('value', race.numberOfLaps);
    if ("betAmount" in race) $('.bet-wrap > .input-wrap > input').attr('value', race.betAmount);
    if ("waitTime" in race) $('.time-wrap > .input-wrap > input').attr('value', race.waitTime);
    if ("password" in race) $('.password-wrap > .input-wrap > input').attr('value', race.password);

    if ("trackName" in race) {
        $('#select-racing-track').selectmenu();
        $('#select-racing-track-menu > li:contains(' + race.trackName + ')').mouseup();
    }
    if ("upgradesAllowed" in race) {
        $('#select-allow-upgrades').selectmenu();
        $('#select-allow-upgrades-menu > li:contains(' + race.upgradesAllowed + ')').mouseup();
    }
}

function scrubPresets() {
    presets.forEach(x => {
        if ("name" in x && x.name.length > 25) x.name = x.name.substring(0, 26);
        if ("maxDrivers" in x) x.maxDrivers = (x.maxDrivers > 100) ? 100 : (x.maxDrivers < 2) ? 2 : x.maxDrivers;
        if ("trackName" in x) x.trackName.toLowerCase().split(' ').map(x => x.charAt(0).toUpperCase() + x.substring(1)).join(' ');
        if ("numberOfLaps" in x) x.numberOfLaps = (x.numberOfLaps > 100) ? 100 : (x.numberOfLaps < 1) ? 1 : x.numberOfLaps;
        if ("upgradesAllowed" in x) x.upgradesAllowed = x.upgradesAllowed === true ? "Allow upgrades" : "Stock cars only";
        if ("betAmount" in x) x.betAmount = (x.betAmount > 10000000) ? 10000000 : (x.betAmount < 0) ? 0 : x.betAmount;
        if ("waitTime" in x) x.waitTime = (x.waitTime > 2880) ? 2880 : (x.waitTime < 1) ? 1 : x.waitTime;
        if ("password" in x && x.password.length > 25) x.password = x.password.substring(0, 26);
    })
}

function drawPresetBar() {
    let filterBar = $(`
      <div class="filter-container m-top10">
        <div class="title-gray top-round">Race Presets</div>

        <div class="cont-gray p10 bottom-round">
            ${presets.map((element, index) => `<button class="torn-btn preset-btn" style="margin:0 10px 10px 0">${("name" in element) ? element.name : "Preset " + (+index + 1)}</button>`)}
        </div>
      </div>`);

    $('#racingAdditionalContainer > .form-custom-wrap').before(filterBar);
    $('.preset-btn').each((index, element) => element.onclick = function() {fillPreset(index)});
}