Tank-Randomizer

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

目前為 2025-01-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name        Tank-Randomizer
// @author      kamarov
// @description Bring an element of surprise to your tank customization experience with the Tank Randomizer,
// @version     3.0.1
// @namespace   https://github.com/kamarov-therussiantank
// @license     GPL-3.0
// @match       https://*.tanktrouble.com/*
// @desc        Randomizes your tank in just one click of a button.
// @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;
  height: 20px;
  width: 100px;
}
`);

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

    function randomizeTurretPaint() {
        randomizeTurretColor();
    }

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

    function randomizeBasePaint() {
        randomizeBaseColor();
    }

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

    function randomizeTreadsPaint() {
        randomizeTreadsColor();
    }

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

    function randomizeTurretAccessory() {
        randomizeTurretA();
    }

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

    function randomizeBarrelAccessory() {
        randomizeBarrelA();
    }

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

    function randomizeBackAccessory() {
        randomizeBackA();
    }

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

    function randomizeFrontAccessory() {
        randomizeFrontA();
    }

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

    function randomizeAllPartsAccessories() {
        randomizeFrontAccessory();
        randomizeBackAccessory();
        randomizeTurretAccessory();
        randomizeBarrelAccessory();
    }
  
    function randomizeAllPartsPaints() {
        randomizeTurretPaint();
        randomizeBasePaint();
        randomizeTreadsPaint();
    }

    Backend.getInstance().getGarageContent(
        function(result) {
            var boxes = result['boxes'];
            for (var box in boxes) {
                var accessories = boxes[box]['accessories'];
                var sprays = boxes[box]['sprayCans'];
                for (var accessory in accessories) {
                    var thing = accessories[accessory];
                    if (thing['type'] == 'front') {
                        front.push(thing['value']);
                    }
                    if (thing['type'] == 'back') {
                        back.push(thing['value']);
                    }
                    if (thing['type'] == 'tread') {
                        front.push(thing['value']);
                    }
                    if (thing['type'] == 'barrel') {
                        barrel.push(thing['value']);
                    }
                    if (thing['type'] == 'turret') {
                        turret.push(thing['value']);
                    }
                }
                for (var spray in sprays) {
                    var thing = sprays[spray]['colour'];
                    if (thing['type']) {
                        colours.push(thing['rawValue']);
                    }
                }
            }
            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>
            <hr>
            <div class="header" style="color: #e7c811; text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;">Randomize</div>
        </div>
    `);
    var content = $('<div></div>');
    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>');

    accessoriesButton.on('mouseup', () => randomizeAllPartsAccessories());
    paintsButton.on('mouseup', () => randomizeAllPartsPaints());

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

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