Tank-Randomizer

Bring an element of surprise to your tank customization experience with the Tank Randomizer!

目前為 2024-01-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Tank-Randomizer
// @author      Kamarov
// @description Bring an element of surprise to your tank customization experience with the Tank Randomizer!
// @version     0.0.19
// @namespace   https://github.com/kamarov-therussiantank
// @license     GPL-3.0
// @match       https://*.tanktrouble.com/*
// @desc        Randomize your tank in style!
// @run-at      document-end
// @grant       GM_addStyle
// @require     https://update.greasyfork.org/scripts/482092/1297984/TankTrouble%20Development%20Library.js
// @noframes
// ==/UserScript==

GM_addStyle(`
.randomize-button {
  margin-bottom: 10px;
}
`);

whenContentInitialized().then(() => {
    var id = Users.getAllPlayerIds()[0];
    var turret = [];
    var back = [];
    var barrel = [];
    var front = [];
    var colours = [];
    var baseColor = '';

    function randomize() {
        randomizePaint();
        randomizeTankcessories();
    }

    function randomizeTankcessories() {
        randomizeTankcessoriesA();
    }

    function randomizeTankcessoriesA() {
        Backend.getInstance().setAccessory(
            function (result) {
                Users.updateUser(id, true, false);
            },
            null,
            null,
            id,
            'back',
            back[Math.floor(Math.random() * back.length)],
            Caches.getPlayerDetailsCache()
        );

        Backend.getInstance().setAccessory(
            function (result) {
                Users.updateUser(id, true, false);
            },
            null,
            null,
            id,
            'turret',
            turret[Math.floor(Math.random() * turret.length)],
            Caches.getPlayerDetailsCache()
        );

        Backend.getInstance().setAccessory(
            function (result) {
                Users.updateUser(id, true, false);
            },
            null,
            null,
            id,
            'front',
            front[Math.floor(Math.random() * front.length)],
            Caches.getPlayerDetailsCache()
        );

        Backend.getInstance().setAccessory(
            function (result) {
                Users.updateUser(id, true, false);
            },
            null,
            null,
            id,
            'tread',
            Math.floor(Math.random() * 25) + 1,
            Caches.getPlayerDetailsCache()
        );

        Backend.getInstance().setAccessory(
            function (result) {
                Users.updateUser(id, true, false);
            },
            null,
            null,
            id,
            'barrel',
            barrel[Math.floor(Math.random() * barrel.length)],
            Caches.getPlayerDetailsCache()
        );
    }

    function randomizePaint() {
        randomizePaintC();
    }

    function randomizePaintC() {
        var selectedBaseColor = getRandomColorFromGarage();
        var selectedTurretColor = getRandomColorFromGarage();
        var selectedTreadColor = getRandomColorFromGarage();

        function setColorForPart(part, color) {
            Backend.getInstance().setColour(
                function (result) {
                    Users.updateUser(id, true, false);
                },
                function (result) { },
                function (result) { },
                id,
                part,
                color,
                Caches.getPlayerDetailsCache()
            );
        }

        setColorForPart('base', selectedBaseColor);
        setColorForPart('turret', selectedTurretColor);
        setColorForPart('tread', selectedTreadColor);
    }

    Backend.getInstance().getGarageContent(
        function (result) {
            boxes = result['boxes'];
            for (box in boxes) {
                accessories = boxes[box]['accessories'];
                sprays = boxes[box]['sprayCans'];
                for (accessory in accessories) {
                    thing = accessories[accessory];
                    if (thing['type'] == 'front') {
                        front.push(thing['value']);
                    }
                    if (thing['type'] == 'back') {
                        back.push(thing['value']);
                    }
                    if (thing['type'] == 'barrel') {
                        barrel.push(thing['value']);
                    }
                    if (thing['type'] == 'turret') {
                        turret.push(thing['value']);
                    }
                }
                for (spray in sprays) {
                    thing = sprays[spray]['colour'];
                    if (thing['type']) {
                        colours.push(thing['rawValue']);
                    }
                }
            }

            // Randomly select the base color
            baseColor = colours[Math.floor(Math.random() * colours.length)];
        },
        function (res) { },
        function (res) { },
        id,
        Caches.getGarageContentCache()
    );

    var snippet = $(`
        <div id="randomizerSnippet" class="snippet">
            <div class="header">Tank Randomizer</div>
            Inject a dash of unpredictability into your tank's appearance
            <hr>
            <div class="header" style="color: #e7c811;">Randomize</div>
        </div>
    `);
    var content = $('<div></div>');
    var allPartsButton = $('<button class="randomize-button button" type="button" tabindex="-1">All Parts</button>');
    var accessoriesButton = $('<button class="randomize-button button" type="button" tabindex="-1">Accessories</button>');
    var paintsButton = $('<button class="randomize-button button" type="button" tabindex="-1">Paints</button>');

    allPartsButton.on('mouseup', () => randomize());
    accessoriesButton.on('mouseup', () => randomizeTankcessoriesA());
    paintsButton.on('mouseup', () => randomizePaintC());

    content.append([allPartsButton, accessoriesButton, paintsButton]);
    snippet.append(content);
    $('#secondaryContent').append(snippet);

    function getRandomColorFromGarage() {
        return colours[Math.floor(Math.random() * colours.length)];
    }
});